-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.sh
executable file
·215 lines (171 loc) · 4.82 KB
/
init.sh
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# Constants
export USER='akurath'
export TERM='xterm-256color'
export HOME="/home/akurath"
WORKSPACE="${HOME}/workspace/"
SSH_DIR="${HOME}/.ssh/"
CODEBOX_DIR="/opt/akurath"
SERVER_SCRIPT="${CODEBOX_DIR}/bin/akurath.js"
PYTHON_ACTIVATE="/opt/virtualenv/bin/activate"
# Extra flags for the akurath server
CBFLAGS=""
## Variables provided by environment
# RSA_PRIVATE, RSA_PUBLIC
# EMAIL, NAME, USERNAME
# GIT_URL, 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"
}
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 ${GIT_URL} ${WORKSPACE}
}
function setup_hg () {
echo "Calling setup_hg ..."
# Skip if git directory exists
if [ -d "$WORKSPACE.hg" ]; then
"Skipping setup_hg because WORKSPACE is already setup ..."
return
fi
if [ ! "$HG_URL" ]; then
echo "Skipping setup_hg because no HG_URL given ..."
echo "Init empty hg repository in workspace ..."
hg init ${WORKSPACE}
return
fi
# Do cloning
hg clone ${HG_URL} ${WORKSPACE}
}
# Sets up a akurath sample if one exists
function setup_sample () {
CBFLAGS="${CBFLAGS} --sample ${CODEBOXIO_STACK}"
}
# Sets up git or mercurial
function setup_repo () {
echo "Calling setup_repo ..."
# Check if workspace directory already contains stuff
if [ -n "$(ls -A ${WORKSPACE})" ]; then
echo "Skipping setup_repo because workspace folder is not empty"
return
fi
# Check if we should setup either
# git or mercurial based on env variables provided
if [ -n "$GIT_URL" ]; then
setup_git
return
elif [ -n "$HG_URL" ];then
setup_hg
return
elif [ -n "$CODEBOXIO_STACK" ]; then
setup_sample
fi
}
function setup_perm () {
echo "Calling setup_perm ..."
chown ${USER} -R ${HOME}
chmod +x ${SSH_DIR}
chmod 600 ${SSH_DIR}*
# Ensure /tmp's permissions
sudo chmod 777 /tmp
}
function setup_appengine () {
# PHP and Python
if [ -d "/opt/google_appengine" ]; then
export PATH="/opt/google_appengine:${PATH}"
# GO
elif [ -d "/opt/go_appengine" ]; then
export PATH="/opt/go_appengine:${PATH}"
export GOROOT="/opt/go_appengine/goroot"
export GOPATH="/opt/go_appengine/gopath"
# Java
elif [ -d "/opt/java_appengine" ]; then
export PATH="/opt/java_appengine/bin:${PATH}"
fi
}
function setup_env () {
echo "Calling setup_env ..."
# Set home
export CODEBOX_USER=${USER}
export WORKSPACE_DIR=${WORKSPACE}
export WORKSPACE_ADDONS_DIR="${HOME}/.akurath-addons/"
# Set command prompt
export PS1="\[$(tput setaf 1)\]\u\[$(tput setaf 3)\] \W \[$(tput setaf 2)\]# \[$(tput sgr0)\]"
# Set App Engine related variables
setup_appengine
# 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 ${WORKSPACE} ${CBFLAGS}
}
# Do all setups
setup_workspace
setup_ssh
setup_netrc
setup_perm
setup_repo
# If git clone fails we need to rebuild dir
setup_workspace
setup_env
setup_python
start_server