forked from travis-ci/worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis-worker-install
executable file
·305 lines (259 loc) · 9.19 KB
/
travis-worker-install
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env bash
set -o errexit
main() {
export DEBIAN_FRONTEND=noninteractive
if [[ $# == 0 ]]; then
echo 'No arguments provided, installing with'
echo 'default configuration values.'
fi
: "${INSTALL_MODE:=stable}"
: "${AWS:=false}"
while [ $# -gt 0 ]; do
case "$1" in
--aws=* | --aws)
AWS="${1#*=}"
: "${AWS:=true}"
export AWS
;;
--docker[-_]apt[-_]file=*)
export DOCKER_APT_FILE="${1#*=}"
;;
--docker[-_]cmd=*)
export DOCKER_CMD="${1#*=}"
;;
--docker[-_]config[-_]file=*)
export DOCKER_CONFIG_FILE="${1#*=}"
;;
--docker[-_]image[-_]language[-_]mappings=*)
export DOCKER_IMAGE_LANGUAGE_MAPPINGS="${1#*=}"
;;
--docker[-_]image[-_]languages=*)
export DOCKER_IMAGE_LANGUAGES="${1#*=}"
;;
--stable)
export INSTALL_MODE=stable
export PACKAGECLOUD_SOURCE='travisci/worker'
;;
--edge)
export INSTALL_MODE=edge
export PACKAGECLOUD_SOURCE='travisci/worker-testing'
;;
--enterprise)
export TRAVIS_ENTERPRISE=1
;;
--docker[-_]install[-_]mode=*)
export INSTALL_MODE="${1#*=}"
;;
--packagecloud[-_]source=*)
export PACKAGECLOUD_SOURCE="${1#*=}"
;;
--skip[-_]docker[-_]populate=* | --skip[-_]docker[-_]populate)
SKIP_DOCKER_POPULATE="${1#*=}"
: "${SKIP_DOCKER_POPULATE:=true}"
export SKIP_DOCKER_POPULATE
;;
--trace)
set -o xtrace
;;
--travis[-_]enterprise[-_]build[-_]endpoint=*)
export TRAVIS_ENTERPRISE_BUILD_ENDPOINT="${1#*=}"
export TRAVIS_ENTERPRISE=1
;;
--travis[-_]enterprise[-_]config=*)
export TRAVIS_ENTERPRISE_CONFIG="${1#*=}"
export TRAVIS_ENTERPRISE=1
;;
--travis[-_]enterprise[-_]host=*)
export TRAVIS_ENTERPRISE_HOST="${1#*=}"
export TRAVIS_ENTERPRISE=1
;;
--travis[-_]enterprise[-_]security[-_]token=*)
export TRAVIS_ENTERPRISE_SECURITY_TOKEN="${1#*=}"
export TRAVIS_ENTERPRISE=1
;;
--travis[-_]queue[-_]name=*)
export TRAVIS_QUEUE_NAME="${1#*=}"
;;
--travis[-_]worker[-_]version=*)
export TRAVIS_WORKER_VERSION="${1#*=}"
;;
-h | --help | help)
__help
exit 0
;;
*)
echo "Error: Invalid argument '$1'" >&2
__help
exit 1
;;
esac
shift
done
if [[ $AWS != false ]]; then
AWS=true
fi
export AWS
if [[ ! $INSTALL_MODE =~ stable|edge ]]; then
echo "ERROR: Invalid \$INSTALL_MODE \"$INSTALL_MODE\"" >&2
echo "ERROR: Valid options are 'edge' or 'stable'" >&2
__help
exit 1
fi
__assert_trusty
__assert_root
__install_docker
if [[ ! -n $SKIP_DOCKER_POPULATE ]]; then
__pull_docker_images
fi
killall -9 travis-worker &>/dev/null || echo 'No travis-worker processes found'
stop travis-worker &>/dev/null || echo 'No travis-worker service running'
__install_travis_worker
__configure_travis_worker
__configure_grub
start travis-worker
echo 'Installation complete.'
echo 'It is recommended that this host is restarted before running jobs through it'
}
__help() {
cat <<EOF
Usage: travis-worker-install [options]
Bootstrap Ubuntu 14.04 host with docker and travis-worker installation.
OPTIONS:
--aws - assume being installed on aws ec2 (default: true) [\$AWS]
--docker-apt-file - name of APT source list for docker [\$DOCKER_APT_FILE]
--docker-cmd - docker executable name (default: docker) [\$DOCKER_CMD]
--docker-config-file - name of docker config file [\$DOCKER_CONFIG_FILE]
--docker-image-language-mappings - mappings of languages to aliases for docker images [\$DOCKER_IMAGE_LANGUAGE_MAPPINGS]
--docker-image-languages - travis-specific language docker images to pull [\$DOCKER_IMAGE_LANGUAGES]
--docker-install-mode - install docker "edge" or "stable" (default: $INSTALL_MODE) [\$INSTALL_MODE]
--edge - shortcut for --docker-install-mode=edge & --packagecloud-source=travisci/worker-testing
--enterprise - assume running as part of a Travis Enterprise installation [\$TRAVIS_ENTERPRISE]
--packagecloud-source - name of source for travis-worker installation (default: travisci/worker) [\$PACKAGECLOUD_SOURCE]
--skip-docker-populate - skip pulling docker images for jobs [\$SKIP_DOCKER_POPULATE]
--stable - shortcut for --docker-install-mode=stable & --packagecloud-source=travisci/worker
--trace - run with tracing
--travis-enterprise-build-endpoint - full hostname of travis-build API [\$TRAVIS_ENTERPRISE_BUILD_ENDPOINT]
--travis-enterprise-config - path to enterprise-specific config file [\$TRAVIS_ENTERPRISE_CONFIG]
--travis-enterprise-host - hostname of enterprise platform (default: demo.enterprise.travis-ci.com) [\$TRAVIS_ENTERPRISE_HOST]
--travis-enterprise-security-token - auth token for communicating with enterprise platform [\$TRAVIS_ENTERPRISE_SECURITY_TOKEN]
--travis-queue-name - queue from which to consume jobs [\$TRAVIS_QUEUE_NAME]
--travis-worker-version - travis-worker version to install (default: latest) [\$TRAVIS_WORKER_VERSION]
-h / --help - show this help text and exit 0
EOF
}
__assert_trusty() {
if [[ $(lsb_release -sc) != trusty ]]; then
echo 'This should only be run on Ubuntu 14.04'
exit 1
fi
}
__assert_root() {
if [[ $(whoami) != root ]]; then
echo 'This should only be run as root'
exit 1
fi
}
__install_docker() {
: "${DOCKER_APT_FILE:=/etc/apt/sources.list.d/docker.list}"
: "${DOCKER_CONFIG_FILE:=/etc/default/docker}"
if [[ ! -f $DOCKER_APT_FILE ]]; then
curl -sSL 'https://download.docker.com/linux/ubuntu/gpg' | apt-key add -
echo 'deb https://download.docker.com/linux/ubuntu trusty stable edge' >"$DOCKER_APT_FILE"
fi
apt-get update
if ! docker version &>/dev/null; then
apt-get install -y \
"linux-image-extra-$(uname -r)" \
docker-ce
fi
if [[ $AWS == true ]]; then
local docker_mount_point='--graph=/mnt/docker'
fi
if ! grep -q icc "${DOCKER_CONFIG_FILE}"; then
local docker_opts='-H tcp://0.0.0.0:4243 -H unix://var/run/docker.sock'
docker_opts="$docker_opts --storage-driver=aufs --icc=false"
docker_opts="$docker_opts $docker_mount_point"
echo "DOCKER_OPTS=\"$docker_opts\"" >>"$DOCKER_CONFIG_FILE"
if ! service docker stop; then
stop docker || echo "Failed to stop docker (exit $?)"
fi
if ! service docker start; then
stop docker || echo "Failed to start docker (exit $?)"
fi
sleep 2
fi
}
__pull_docker_images() {
: "${DOCKER_CMD:=docker}"
: "${DOCKER_IMAGE_LANGUAGES:=android erlang go haskell jvm node-js perl php python ruby}"
: "${DOCKER_IMAGE_LANGUAGE_MAPPINGS:=clojure:jvm scala:jvm groovy:jvm java:jvm elixir:erlang node_js:node-js}"
for lang in ${DOCKER_IMAGE_LANGUAGES}; do
"${DOCKER_CMD}" pull "quay.io/travisci/travis-${lang}:latest"
"${DOCKER_CMD}" tag -f "quay.io/travisci/travis-${lang}:latest" "travis:${lang}"
done
"${DOCKER_CMD}" tag -f travis:ruby travis:default
for lang_map in $DOCKER_IMAGE_LANGUAGE_MAPPINGS; do
"${DOCKER_CMD}" tag -f \
"quay.io/travisci/travis-${lang_map%%:*}:latest" \
"travis:${lang_map##*:}"
done
}
__install_travis_worker() {
: "${PACKAGECLOUD_SOURCE:=travisci/worker}"
if [[ ! -f /etc/apt/sources.list.d/travisci_worker.list ]]; then
curl -s "https://packagecloud.io/install/repositories/${PACKAGECLOUD_SOURCE}/script.deb.sh" | bash
if [[ $TRAVIS_WORKER_VERSION ]]; then
apt-get install "travis-worker=${TRAVIS_WORKER_VERSION}"
else
apt-get install travis-worker
fi
fi
}
__configure_travis_worker() {
: "${TRAVIS_ENTERPRISE_CONFIG:=/etc/default/travis-enterprise}"
: "${TRAVIS_WORKER_CONFIG:=/etc/default/travis-worker}"
if [[ $TRAVIS_QUEUE_NAME ]]; then
sed -i \
"s/export QUEUE_NAME='builds.linux'/export QUEUE_NAME=\\'$TRAVIS_QUEUE_NAME\\'/" \
"$TRAVIS_WORKER_CONFIG"
fi
if [[ ! $TRAVIS_ENTERPRISE ]]; then
return
fi
sed -i \
"/\\# export TRAVIS_WORKER_DOCKER_PRIVILEGED/s/^\\# //"
$TRAVIS_ENTERPRISE_CONFIG
if [[ $TRAVIS_ENTERPRISE_HOST ]]; then
sed -i \
"s/\\# export TRAVIS_ENTERPRISE_HOST=\"enterprise.yourhostname.corp\"/export TRAVIS_ENTERPRISE_HOST=\"$TRAVIS_ENTERPRISE_HOST\"/" \
"$TRAVIS_ENTERPRISE_CONFIG"
fi
if [[ $TRAVIS_ENTERPRISE_SECURITY_TOKEN ]]; then
sed -i \
"s/\\# export TRAVIS_ENTERPRISE_SECURITY_TOKEN=\"abcd1234\"/export TRAVIS_ENTERPRISE_SECURITY_TOKEN=\"$TRAVIS_ENTERPRISE_SECURITY_TOKEN\"/" \
"$TRAVIS_ENTERPRISE_CONFIG"
fi
if [[ $TRAVIS_ENTERPRISE_BUILD_ENDPOINT ]]; then
sed -i \
"s/export TRAVIS_ENTERPRISE_BUILD_ENDPOINT=\"__build__\"/export TRAVIS_ENTERPRISE_BUILD_ENDPOINT=\"$TRAVIS_ENTERPRISE_BUILD_ENDPOINT\"/" \
"$TRAVIS_ENTERPRISE_CONFIG"
fi
}
__configure_grub() {
local grub_cmdline_linux='cgroup_enable=memory swapaccount=1 apparmor=0'
if [[ -d /etc/default/grub.d ]]; then
echo "GRUB_CMDLINE_LINUX='${grub_cmdline_linux}'" \
>"/etc/default/grub.d/99-travis-worker-settings.cfg"
update-grub
return
fi
local grub_cfg='/etc/default/grub'
touch "${grub_cfg}"
if ! grep -q cgroup_enabled "${grub_cfg}"; then
sed -i \
"s/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"${grub_cmdline_linux}\"/" \
"${grub_cfg}"
fi
update-grub
}
main "$@"