-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
415 lines (342 loc) · 13.2 KB
/
fabfile.py
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# vim: ai ts=4 sts=4 et sw=4 ft=python fdm=indent et
""" OBOR fabfile.py """
# Copyright (C) 2016 Jorge Costa
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import json
from time import sleep
from multiprocessing import Process as mp
from profilehooks import timecall
from jinja2 import Template
from retrying import retry
import yaml
from fabric.api import task, env, local
from fabric.operations import sudo
from fabric.contrib.project import rsync_project
from fabric.context_managers import settings, prefix
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.setrecursionlimit(30000)
# pylint: disable=wrong-import-position
from bookshelf.api_v2.logging_helpers import (log_green, log_red)
@task
@retry(stop_max_attempt_number=3, wait_fixed=10000)
def clean():
""" destroy all VMs """
log_green('running clean')
jobs = []
jobs.append(
mp(target=destroy_railtrack)
)
jobs.append(
mp(
target=local,
args=("vagrant destroy -f "
"> log/`date '+%Y%m%d%H%M%S'`.vagrant.destroy.log 2>&1",)
)
)
for job in jobs:
job.start()
exit_code = 0
for job in jobs:
job.join()
exit_code = exit_code + job.exitcode
if exit_code != 0:
raise Exception('clean failed')
log_green('running clean completed')
@task
@retry(stop_max_attempt_number=3, wait_fixed=10000)
def update(
host_dir=None,
rsync='yes',
nix_gc='yes',
nix_release='18.09',
switch='no'
): # pylint: disable=too-many-arguments
""" deploy or update OBOR on a host """
log_green('running update on {}'.format(env.host_string))
if not host_dir:
host_dir = env.host_string
local('rm -f {}/result'.format(host_dir))
yes_answers = ['yes', 'y', 'YES', 'Y', 'True', 'true']
if rsync in yes_answers:
with settings(
warn_only=True,
shell='/run/current-system/sw/bin/bash -l -c'
):
rsync_project(
remote_dir='/etc/nixos/',
local_dir=host_dir + '/',
delete=True,
extra_opts='--rsync-path="sudo rsync"',
default_opts='-chavzPq',
ssh_opts=' -o UserKnownHostsFile=/dev/null ' +
'-o StrictHostKeyChecking=no '
)
rsync_project(
remote_dir='/etc/nixos/common',
local_dir='common/',
delete=True,
extra_opts='--rsync-path="sudo rsync"',
default_opts='-chavzPq',
ssh_opts=' -o UserKnownHostsFile=/dev/null ' +
'-o StrictHostKeyChecking=no '
)
with settings(
warn_only=True,
shell='/run/current-system/sw/bin/bash -l -c'
):
sudo('rm -f /etc/nixos/result')
if nix_gc in yes_answers:
sudo('nix-collect-garbage -d >/dev/null')
sudo(
'nix-channel --add '
'https://nixos.org/channels/nixos-{} nixos'.format(nix_release)
)
sudo('nix-channel --update')
def _nixos_rebuild():
""" wrapper for nixos-rebuild """
with settings(
shell='/run/current-system/sw/bin/bash -l -c'
):
sudo('nixos-rebuild build -Q')
sudo('nixos-rebuild boot -Q')
def _nixos_switch():
""" wrapper for nixos-rebuild """
with settings(
shell='/run/current-system/sw/bin/bash -l -c'
):
sudo('nixos-rebuild switch -Q')
_nixos_rebuild()
if switch in yes_answers:
_nixos_switch()
@task
@retry(stop_max_attempt_number=6, wait_fixed=90000)
def acceptance_tests_mesos_master():
""" run acceptance tests on mesos master """
local(
"testinfra --connection=ssh --ssh-config=ssh.config "
"-v --hosts='{}' "
"-m 'dnsmasq or docker or marathon or mesos-dns or "
"mesos_master or tincd or zookeeper or dns_resolution or consul' "
"tests".format(env.host_string)
)
@task
@retry(stop_max_attempt_number=6, wait_fixed=90000)
def acceptance_tests_mesos_slave():
""" run acceptance tests on mesos slave """
local(
"testinfra --connection=ssh --ssh-config=ssh.config "
"-v --hosts='{}' "
"-m 'mesos_slave or tincd or docker or dns_resolution or "
"marathon_lb or consul' "
"tests".format(env.host_string)
)
@task
@retry(stop_max_attempt_number=3, wait_fixed=10000)
def destroy_railtrack():
""" destroys Railtrack VMs """
railtrack_env = [
"eval `ssh-agent`",
"ssh-add $PWD/Railtrack/key-pairs/*.priv",
"virtualenv $PWD/Railtrack/venv",
"export PATH=$PWD/Railtrack/venv/bin:$PATH"
]
# local() doesn't support most context managers
# so let's bake a local environment file and consume as a prefix()
with open('shell_env', 'w') as shell_env:
for line in railtrack_env:
shell_env.write(line + '\n')
local('chmod +x shell_env')
with settings(shell='/run/current-system/sw/bin/bash -l -c'):
with prefix(". ./shell_env"): # pylint: disable=not-context-manager
local("cd Railtrack && "
"pip install -r requirements.txt >"
"../log/`date '+%Y%m%d%H%M%S'`."
"pip.install.requirements.txt.log 2>&1")
local("cd Railtrack && "
"fab -f tasks/fabfile.py clean "
"> ../log/`date '+%Y%m%d%H%M%S'`.railtrack.clean.log 2>&1")
@task
@retry(stop_max_attempt_number=3, wait_fixed=10000)
def spin_up_railtrack():
""" deploys Railtrack locally """
with settings(warn_only=True):
local('git clone https://github.com/JeevesTakesOver/Railtrack.git')
local('cd Railtrack && git fetch --all --tags && git checkout v1.1.0')
railtrack_env = [
"virtualenv $PWD/Railtrack/venv",
"eval `ssh-agent`",
"ssh-add $PWD/Railtrack/key-pairs/*.priv",
"export PATH=$PWD/Railtrack/venv/bin:$PATH",
]
# local() doesn't support most context managers
# so let's bake a local environment file and consume as a prefix()
with open('shell_env', 'w') as shell_env:
for line in railtrack_env:
shell_env.write(line + '\n')
local('chmod +x shell_env')
# make sure we are able to consume these key pairs
local('chmod 700 Railtrack')
local('chmod 400 Railtrack/key-pairs/*.priv')
with settings(shell='/run/current-system/sw/bin/bash -l -c'):
with prefix(". ./shell_env"): # pylint: disable=not-context-manager
local("cd Railtrack && "
"pip install -r requirements.txt >"
"../log/`date '+%Y%m%d%H%M%S'`."
"pip.install.requirements.txt.log 2>&1")
with settings(shell='/run/current-system/sw/bin/bash -l -c'):
with prefix(". ./shell_env"): # pylint: disable=not-context-manager
local("cd Railtrack && "
"fab -f tasks/fabfile.py step_01_create_hosts"
" > ../log/`date '+%Y%m%d%H%M%S'`.railtrack.step01.log 2>&1")
@task
@retry(stop_max_attempt_number=3, wait_fixed=90000)
def provision_railtrack():
""" deploys Railtrack locally """
railtrack_env = [
"virtualenv $PWD/Railtrack/venv",
"eval `ssh-agent`",
"ssh-add $PWD/Railtrack/key-pairs/*.priv",
"export PATH=$PWD/Railtrack/venv/bin:$PATH",
]
# local() doesn't support most context managers
# so let's bake a local environment file and consume as a prefix()
with open('shell_env', 'w') as shell_env:
for line in railtrack_env:
shell_env.write(line + '\n')
local('chmod +x shell_env')
with settings(shell='/run/current-system/sw/bin/bash -l -c'):
with prefix(". ./shell_env"): # pylint: disable=not-context-manager
local("cd Railtrack && "
"pip install -r requirements.txt >"
"../log/`date '+%Y%m%d%H%M%S'`."
"pip.install.requirements.txt.log 2>&1")
local("cd Railtrack && "
"fab -f tasks/fabfile.py run_it"
"> ../log/`date '+%Y%m%d%H%M%S'`.railtrack.run_it.log 2>&1")
local("cd Railtrack && "
"fab -f tasks/fabfile.py acceptance_tests"
"> ../log/`date '+%Y%m%d%H%M%S'`."
"railtrack.acceptance_tests.log 2>&1")
@task # NOQA
def jenkins_build(
mesos_masters=[
('[email protected]', 'nixos-vagrant-configs/mesos-zk-01'),
('[email protected]', 'nixos-vagrant-configs/mesos-zk-02'),
('[email protected]', 'nixos-vagrant-configs/mesos-zk-03'),
],
mesos_slaves=[
('[email protected]', 'nixos-vagrant-configs/slave')
],
cleanup=True
):
""" runs a jenkins build """
nodes = mesos_masters + mesos_slaves
# clean previous build logs
local('rm -f log/*')
def _provision_obor(nodes=nodes):
log_green('running _provision_obor')
local('chmod 600 nixos-vagrant-configs/vagrant.priv')
count = 1
while True or count > 3:
jobs = []
for node, hostdir in nodes:
jobs.append(
mp(
target=local,
args=("fab -i nixos-vagrant-configs/vagrant.priv " +
"-H %s update:" % node +
"host_dir=%s," % hostdir +
"rsync='yes'," +
"nix_gc='no'," +
"nix_release='18.09'," +
"switch='no'" +
"> log/`date '+%Y%m%d%H%M%S'`." +
"%s.provision.log 2>&1" % node,)
)
)
for job in jobs:
job.start()
exit_code = 0
for job in jobs:
job.join()
exit_code = exit_code + job.exitcode
if exit_code == 0:
break
count = count + 1
log_green('_provision_obor completed')
def _test_obor(mesos_masters=mesos_masters, mesos_slaves=mesos_slaves):
log_green('running _test_obor')
obor_env = [
"eval `ssh-agent`",
"ssh-add $PWD/nixos-vagrant-configs/*.priv",
]
# local() doesn't support most context managers
# so let's bake a local environment file and consume as a prefix()
with open('shell_env', 'w') as shell_env:
for line in obor_env:
shell_env.write(line + '\n')
local('chmod +x shell_env')
local('chmod 600 nixos-vagrant-configs/vagrant.priv')
with settings(shell='/run/current-system/sw/bin/bash -l -c'):
with prefix(". ./shell_env"): # pylint: disable=not-context-manager
for target, _ in mesos_masters:
local(
"fab -i nixos-vagrant-configs/vagrant.priv " +
"-H {} acceptance_tests_mesos_master ".format(target) +
"> log/`date '+%Y%m%d%H%M%S'`."
"{}.test_obor.log 2>&1".format(target)
)
for target, _ in mesos_slaves:
local("fab -i nixos-vagrant-configs/vagrant.priv " +
"-H {} acceptance_tests_mesos_slave ".format(target) +
"> log/`date '+%Y%m%d%H%M%S'`."
"{}.test_obor.log 2>&1".format(target))
log_green('_test_obor completed')
def _flow1():
# spin up and provision the Cluster
local('vagrant up')
sleep(45) # allow VMs to boot up
_provision_obor()
def _flow2():
# spin up Railtrack, which is required for OBOR
spin_up_railtrack()
sleep(45) # allow VMs to boot up
provision_railtrack()
try:
p_flow1 = mp(target=_flow1)
p_flow2 = mp(target=_flow2)
p_flow1.start()
p_flow2.start()
p_flow1.join()
p_flow2.join()
# reload after initial provision
local('vagrant reload')
sleep(240) # allow the start services
# test all the things
_test_obor()
# and now destroy Railtrack and mesos VMs
if cleanup in [True, 'yes', 'y', 'Y', 'YES']:
clean()
except: # noqa: E722 pylint: disable=bare-except
log_red("jenkins_build() FAILED, aborting...")
if cleanup in [True, 'yes', 'y', 'Y', 'YES']:
clean()
sys.exit(1)
#
# ___main___
#
env.connection_attempts = 10
env.timeout = 30
env.warn_only = False
env.disable_known_hosts = True