-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
356 lines (278 loc) · 10.1 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
#! /usr/bin/env python3
from fabric.api import cd, env, execute, put, task, sudo, run, runs_once
from fabric.contrib.files import sed, append
RYOT_CI_SERVER = 'ci-riot-tribe.saclay.inria.fr'
GITHUB_RUNNER_NAME = 'ci-riot-tribe'
GITHUB_RUNNER_VERSION = '2.267.1'
RIOT_FORK = 'https://github.com/fjmolinas/RIOT'
env.host_string = 'ci@{server}'.format(server=RYOT_CI_SERVER)
env.use_ssh_config = True
JLINK_VERSION = 'V688a'
@task
def hello_world():
"""Check we can run a command on the server."""
run('echo Hello World ${HOSTNAME}')
@task
def setup_sudo_and_ssh_key():
"""Meta recipe that sets up ssh and sudo no passwd"""
_setup_authorized_keys()
_setup_ci_nopasswd()
def _setup_authorized_keys():
run('mkdir -p ~/.ssh')
put('template/authorized_keys', '~/.ssh')
def _setup_ci_nopasswd():
sudoers_tmp = '/tmp/ci_no_passwd'
sudoers_no_passwd = '/etc/sudoers.d/ci_no_paswd'
command = 'chown root:root {0}; mv {0} {1}'
command = command.format(sudoers_tmp, sudoers_no_passwd)
put('template/ci_no_passwd', sudoers_tmp)
# Use bash -c command for sudo
sudo('bash -c "{}"'.format(command))
@task
def disable_ssh_password_auth():
"""Disable password authentication."""
sed('/etc/ssh/sshd_config',
r'^.*PasswordAuthentication yes', 'PasswordAuthentication no',
use_sudo=True)
run('grep PasswordAuthentication /etc/ssh/sshd_config')
@task
def create_builds_directory():
"""Create builds directory"""
sudo('mkdir -p /builds')
sudo('chown ci:ci /builds')
@task
def setup_ci():
"""Avoid first install fail because of host validation"""
put('template/known_hosts', '.ssh/known_hosts')
@runs_once
@task
def update():
"""apt-get update"""
sudo('apt-get update')
@task
def dist_upgrade():
"""Upgrade packages in non interactive mode"""
sudo('apt-get dist-upgrade -yqq')
sudo('apt-get autoremove -yqq')
@task
def apt_cache_clean():
"""Clean package cache and autoremove packages."""
sudo('apt-get autoremove -yqq')
sudo('apt-get clean')
@task
def install(packages, options=''):
"""Install packages in non-interactive mode."""
execute(update)
sudo('apt-get install {0} -yqq {1}'.format(options, packages))
@task
def install_all_packages():
"""Meta recipe that installs all packages."""
install_common()
install_riot()
@task
def install_common():
"""Install common sever dependencies."""
packages = ['vim', 'tar', 'git', 'build-essential', 'screen',
'aptitude', 'openjdk-8-jre-headless', 'tmux']
packages += ['python3', 'python3-dev', 'python3-pip',
'python3-virtualenv']
@task
def install_riot():
"""Install riot specific tools."""
packages = ['make', 'docker.io', 'socat', 'picocom']
packages += ['python3-serial', 'python3-pexpect']
packages += ['python3-cryptography', 'python3-pyasn1',
'python3-ecdsa', 'python3-crypto']
packages += ['python3-cryptography', 'python3-pyasn1',
'python3-ecdsa', 'python3-crypto']
packages += ['protobuf-compiler', 'python-protobuf']
# Needed for goodfet.bsl
packages += ['python-serial']
install(' '.join(packages))
disable_dns_mask_for_docker()
run('pip3 install scapy==2.4.0 --user')
@task
def install_riot_flashers():
"""Install riot specific flashing tools."""
_install_uniflash()
_install_py_flashers()
_install_mspdebug()
_install_openocd()
_install_edbg()
_install_avrdude()
_install_jlink()
@task
def _install_jlink():
"""Install jlink."""
with cd('/opt'):
deb = 'JLink_Linux_{}_x86_64.deb'.format(JLINK_VERSION)
url = 'https://www.segger.com/downloads/jlink/{}'.format(deb)
sudo('wget --quiet --post-data \'accept_license_agreement=accepted&non_emb_ctr=confirmed&submit="Download software"\' {}'.format(url))
sudo('dpkg --install {}'.format(deb))
sudo('rm {}'.format(deb))
@task
def _install_uniflash():
"""Install uniflash."""
packages = ['libc6:i386', 'libusb-0.1-4', 'libgconf-2-4',
'libncurses5', 'libpython2.7', 'libtinfo5']
install(' '.join(packages))
with cd('/opt'):
sudo(' wget --quiet http://software-dl.ti.com/ccs/esd/uniflash/uniflash_sl.4.6.0.2176.run')
sudo('chmod +x uniflash_sl.4.6.0.2176.run')
sudo('./uniflash_sl.4.6.0.2176.run --prefix /opt/ti/uniflash --mode unattended')
sudo('rm uniflash_sl.4.6.0.2176.run')
append('/etc/environment',
'UNIFLASH_PATH=/opt/ti/uniflash',
use_sudo=True)
@task
def _install_mspdebug():
"""Install mspdebug."""
packages = ['libreadline-dev']
install(' '.join(packages))
sudo('mkdir -p /opt/mspdebug')
sudo('chown ci:ci /opt/mspdebug')
run('git clone --depth 1 https://github.com/dlbeer/mspdebug /opt/mspdebug || true')
with cd('/opt/mspdebug'):
run('make -j')
sudo('make install')
@task
def _install_edbg():
"""Install mspdebug."""
packages = ['libudev-dev']
install(' '.join(packages))
@task
def _install_openocd():
"""Install openocd."""
packages = ['make', 'pkg-config', 'libtool']
packages += ['autoconf', 'automake']
packages += ['libhidapi-hidraw0', 'libhidapi-dev']
install(' '.join(packages))
sudo('mkdir -p /opt/openocd')
sudo('chown ci:ci /opt/openocd')
run('git clone --depth 1 git://git.code.sf.net/p/openocd/code /opt/openocd || true')
with cd('/opt/openocd'):
run('git pull')
run('./bootstrap')
run('./configure --enable-stlink --enable-jlink --enable-ftdi --enable-cmsis-dap')
run('make -j')
sudo('make install')
@task
def _install_avrdude():
"""Install avrdude."""
install('avrdude')
@task
def _install_py_flashers():
"""Install pyocd and esptool."""
sudo('pip3 install pyocd esptool junit-xml')
@task
def disable_dns_mask_for_docker():
"""Disable dnsmask for NetworkManager."""
sed('/etc/NetworkManager/NetworkManager.conf',
r'^dns=dnsmasq', '#dns=dnsmasq', use_sudo=True)
sudo('systemctl restart NetworkManager.service')
@task
def configure_udev():
"""Configure connected boards udev rules"""
put('template/70-riotboards.rules', '/etc/udev/rules.d/', use_sudo=True)
put('template/70-tty.rules', '/etc/udev/rules.d/', use_sudo=True)
sudo('udevadm control --reload')
@task
def configure_default_python():
"""RIOT used python3 as default python"""
sudo("update-alternatives --install /usr/bin/python python /usr/bin/python3 10")
@task
def configure_ci_groups():
"""Add ci to main groups"""
sudo('usermod -a -G docker,plugdev,dialout ci')
@task
def configure_builds_config():
"""Copy makefiles.pre and makefiles.post"""
run('rm -rf /builds/conf')
put('template/conf', '/builds')
# This set makefiles.pre to avoid sourcing every time ssh is called
append('/etc/environment',
'RIOT_MAKEFILES_GLOBAL_PRE="/builds/conf/makefiles.pre"',
use_sudo=True)
@task
def configure_riot_flash_tool():
"""Use latest master to flash"""
run('mkdir -p /builds/boards')
run('git clone https://github.com/RIOT-OS/RIOT.git /builds/boards/RIOT || true')
run('git -C /builds/boards/RIOT fetch origin')
run('git -C /builds/boards/RIOT checkout origin/master')
put('template/boards/Makefile', '/builds/boards')
run('mkdir -p /builds/boards/bin')
@task
def setup_ci_tools():
"""Copy ci scripts"""
run('rm -rf /builds/scripts')
put('scripts/', '/builds', mirror_local_mode=True)
run('git init --bare ~/.gitcache')
# Add scripts locations to env
SCRIPTS_BASE = '/builds/scripts'
append(
'/etc/environment',
'PYTHONPATH=$PYTHONPATH:{}/'.format(SCRIPTS_BASE),
use_sudo=True
)
# Add compile_and_test_for_board to path
RIOTBASE = '/builds/boards/RIOT'
COMPILE_AND_TEST_FOR_BOARD = 'dist/tools/compile_and_test_for_board/'
append(
'/etc/environment',
'PYTHONPATH=$PYTHONPATH:{}/{}'
.format(RIOTBASE, COMPILE_AND_TEST_FOR_BOARD),
use_sudo=True
)
@task
def get_dockertargets_mk():
"""Fetch dockertargets makefile to use RIOT in docker"""
# For now since this is not in RIOT master wget dockertargets.inc.mk
# to use as makefile pre
with cd('/builds/conf'):
run('rm dockertargets.inc.mk || true')
run('wget -q https://raw.githubusercontent.com/fjmolinas/RIOT/wip_docker_targets/makefiles/docker/dockertargets.inc.mk')
@task
def setup_github_runner(token=None, runners=7):
"""Setups github runner, doesn't run unless token is provided"""
if token is None:
return
for i in range(0, runners):
directory = '/builds/actions-runner_{}'.format(i)
run('mkdir -p {}'.format(directory))
with cd(directory):
runner_url = 'https://github.com/actions/runner/releases/download/v{}/actions-runner-linux-x64-{}.tar.gz'.format(GITHUB_RUNNER_VERSION, GITHUB_RUNNER_VERSION)
run('curl -O -L {}'.format(runner_url))
run('tar xzf ./actions-runner-linux-x64-{}.tar.gz'.format(GITHUB_RUNNER_VERSION))
cmd = './config.sh'
cmd += ' --url {}'.format(RIOT_FORK)
cmd += ' --token {}'.format(token)
cmd += ' --name {}-{}'.format(GITHUB_RUNNER_NAME, i)
cmd += ' --unattended '
cmd += ' || true'
run(cmd)
sudo('./svc.sh install || true')
sudo('./svc.sh stop || true')
sudo('./svc.sh start')
@task
def setup():
"""Setup the whole server.
This can be called multiple times without problems
"""
execute(hello_world)
execute(setup_sudo_and_ssh_key)
execute(disable_ssh_password_auth)
execute(create_builds_directory)
execute(setup_ci)
execute(update)
execute(dist_upgrade)
execute(install_all_packages)
execute(configure_udev)
execute(configure_builds_config)
execute(configure_ci_groups)
execute(configure_default_python)
execute(configure_riot_flash_tool)
execute(setup_ci_tools)
execute(install_riot_flashers)
execute(get_dockertargets_mk)
execute(setup_github_runner)