Repository navigation
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·143 lines (107 loc) · 3.02 KB
/
Copy pathinit.sh
File metadata and controls
executable file
·143 lines (107 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# Constants
HOME="/home/codebox"
WORKSPACE="${HOME}/workspace/"
SSH_DIR="${HOME}/.ssh/"
SERVER_SCRIPT="/opt/codebox/bin/codebox.js"
PYTHON_ACTIVATE="/opt/virtualenv/bin/activate"
## Variables provided by environment
# RSA_PRIVATE, RSA_PUBLIC
# EMAIL, NAME, USERNAME
# GIT_URL, GIT_BRANCH, GIT_USER, GIT_PASSWD (some private token)
# GIT_HOST, WEBHOOK_URL
function setup_workspace () {
echo "Calling setup_workspace ..."
# Create workspace dir
mkdir -p ${WORKSPACE}
}
function setup_ssh () {
echo "Calling setup_ssh ..."
if [ ! "$RSA_PUBLIC" ] || [ ! "$RSA_PRIVATE" ]; then
echo "Skipping setup_ssh, no private and public keys to setup ..."
fi
# Ensure directory
mkdir -p ${SSH_DIR}
# Store/Update keys
echo "${RSA_PUBLIC}" | tee "${SSH_DIR}id_rsa.pub"
echo "${RSA_PRIVATE}" | tee "${SSH_DIR}id_rsa"
chmod 600 "${SSH_DIR}id_rsa.pub"
chmod 600 "${SSH_DIR}id_rsa"
ssh-keyscan -H github.com >> "${SSH_DIR}known_hosts"
ssh-keyscan -H bitbucket.org >> "${SSH_DIR}known_hosts"
}
function setup_netrc () {
echo "Calling setup_netrc ..."
# No valid things to setup
if [ ! $GIT_HOST ] || [ ! $GIT_USER ] || [ ! $GIT_PASSWD ]; then
echo "Skipping setup_netrc ..."
return
fi
local filename="${HOME}/.netrc"
# Exit if already there
if grep -i "machine ${GIT_HOST}" $filename; then
return
fi
# Git auth over http/https with token
echo "machine ${GIT_HOST}
login ${GIT_USER}
password ${GIT_PASSWD}
" >> $filename
chmod 600 $filename
}
function setup_git () {
echo "Calling setup_git ..."
# Skip if git directory exists
if [ -d "$WORKSPACE.git" ]; then
"Skipping setup_git because WORKSPACE is already setup ..."
return
fi
if [ ! "$GIT_URL" ]; then
echo "Skipping setup_git because no GIT_URL given ..."
echo "Init empty git repository in workspace ..."
git init ${WORKSPACE}
return
fi
# Do cloning
git clone -b ${GIT_BRANCH} ${GIT_URL} ${WORKSPACE}
}
function setup_perm () {
echo "Calling setup_perm ..."
chown ${USER} -R ${HOME}
chmod 0600 -R ${SSH_DIR}
}
function setup_env () {
echo "Calling setup_env ..."
# Set home
export CODEBOX_USER=${USER}
export WORKSPACE_DIR=${WORKSPACE}
export WORKSPACE_ADDONS_DIR="${HOME}/.codebox-addons/"
# Set command prompt
export PS1="\[$(tput setaf 1)\]\u\[$(tput setaf 3)\] \W \[$(tput setaf 2)\]# \[$(tput sgr0)\]"
# Unset sensitive stuff
unset RSA_PRIVATE
unset RSA_PUBLIC
unset GIT_PASSWD
}
function setup_python () {
echo "Callling setup_python ..."
if [ -f "${PYTHON_ACTIVATE}" ]; then
source "${PYTHON_ACTIVATE}"
return
fi;
echo "Skipped setup_python ..."
}
function start_server () {
echo "Calling start_server ..."
cd ${WORKSPACE}
exec ${SERVER_SCRIPT} run .
}
# Do all setups
setup_workspace
setup_ssh
setup_netrc
setup_git
setup_perm
setup_env
setup_python
start_server