-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
214 lines (173 loc) · 6.33 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
import re
from itertools import chain, repeat
from fabric import task
from patchwork import files
# list of available machines, excluding:
# - the_beast (windows machine),
# - composter (Petr's machine),
# - pickaxe (Rob's machine),
# - joiner (Bainsaw's machine),
# - jigsaw (dead machine :/).
machines = [
'gluegun',
'harvester',
'blender',
'jackhammer',
'lawnmower',
'roomba',
'strimmer',
'sander',
'grinder',
# 'projector',
'chainsaw',
'wheelbarrow',
'sledgehammer',
'penknife',
# 'scythe',
'sprinkler',
'nailgun'
]
hosts = ['{}.mrsic-flogel.swc.ucl.ac.uk'.format(m) for m in machines]
# users mapping, from SWC login to logins found on machines
swc_users = {
'adilk': [],
'antoninv': ['antoninv'],
'akhilkevich': ['andreik'],
'alexanderf': ['alexf'],
'antoninb': ['blota'],
'dulciev': ['dulciev'],
'francescag': [],
'francoisc': ['chabrol'],
'hernandom': ['hernandom'],
'ioanag': ['gasler'],
'ivanao': ['orsolici'],
'ivanv': ['ivan'],
'kellyc': ['clancy'],
'lisah': [],
'lauras': ['lauras'],
'maximer': ['rioma'],
'majas': ['majas'],
'michellel': [],
'mitraj': ['javadzam'],
'morganer': ['rothmo', 'morgane'],
'naureeng': ['naureeng'],
'nicolev': ['nicolev'],
'petrz': ['znamensk', 'petr'],
'rajam': ['raja'],
'robc': ['rob'],
'shoheif': ['shohei', 'furutach'],
'sonjah': [],
'takahirok': ['kanamori'],
'tommf': [],
}
# reverse mapping from machines logins to SWC logins
machines_users = dict(
chain.from_iterable(zip(v, repeat(k)) for k, v in swc_users.items())
)
@task(hosts=hosts)
def run(ctx, cmd):
"""run a command, by default on all analysis machines"""
ctx.run(cmd)
@task(hosts=hosts)
def list_users(ctx):
"""list users having a home directory and match them with SWC login"""
host = ctx.run('hostname', hide=True).stdout.strip()
homes = ctx.run('ls -1 /home', hide=True).stdout.strip().split('\n')
known_users = [user for user in homes if user in machines_users]
unknown_users = [user for user in homes if user not in machines_users]
print('###', host, '###')
print('SWC users:', ', '.join(known_users))
print('other users:', ', '.join(unknown_users))
@task
def add_winstor(ctx, user=None):
"""add winstor mount point for a user"""
# check user exist on the machine and in SWC
homes = ctx.run('ls -1 /home', hide=True).stdout.strip().split('\n')
if user not in homes:
host = ctx.run('hostname', hide=True).stdout.strip()
print('"{}" user does not have a home folder on {}!'
.format(user, host))
return
if user not in machines_users:
print('"{}" user is not a SWC user!'.format(user))
return
# create user mount point, if needed
mount_point = '/mnt/{user}/winstor'.format(user=user)
files.directory(ctx, mount_point, sudo=True)
# add a line to /etc/fstab, if needed
fstab_path = '/etc/fstab'
if not files.contains(ctx, fstab_path, mount_point):
mount_point_string = (
'\n'
'# winstor storage for {user}\n'
'//winstor.ad.swc.ucl.ac.uk/winstor {mount} cifs '
'username={swc},vers=3.0,noauto,users 0 0'
.format(mount=mount_point, swc=machines_users[user], user=user)
)
# files.append(ctx, fstab_path, mount_point_string, sudo=True)
ctx.sudo('sh -c \'echo "{new_lines}" >>{fstab} \''
.format(new_lines=mount_point_string, fstab=fstab_path))
print('[fstab] winstor added for {}'.format(user))
@task(hosts=hosts)
def add_winstor_all(ctx):
"""add winstor mount for all SWC users found"""
homes = ctx.run('ls -1 /home', hide=True).stdout.strip().split('\n')
users = [user for user in homes if user in machines_users]
for user in users:
add_winstor(ctx, user)
@task
def add_swc_homes(ctx, user=None):
"""add swc-homes mount point for a user"""
# check user exist on the machine and in SWC
homes = ctx.run('ls -1 /home', hide=True).stdout.strip().split('\n')
if user not in homes:
host = ctx.run('hostname', hide=True).stdout.strip()
print('"{}" user does not have a home folder on {}!'
.format(user, host))
return
if user not in machines_users:
print('"{}" user is not a SWC user!'.format(user))
return
# create user mount point, if needed
mount_point = '/mnt/{user}/swc-homes'.format(user=user)
files.directory(ctx, mount_point, sudo=True)
# add a line to /etc/fstab, if needed
fstab_path = '/etc/fstab'
if not files.contains(ctx, fstab_path, mount_point):
mount_point_string = (
'\n'
'# swc-homes storage for {user}\n'
'//swc-homes.ad.swc.ucl.ac.uk/{swc} {mount} cifs '
'username={swc},vers=3.0,noauto,users 0 0'
.format(mount=mount_point, swc=machines_users[user], user=user)
)
# files.append(ctx, fstab_path, mount_point_string, sudo=True)
ctx.sudo('sh -c \'echo "{new_lines}" >>{fstab} \''
.format(new_lines=mount_point_string, fstab=fstab_path))
print('[fstab] swc-homes added for {}'.format(user))
@task(hosts=hosts)
def add_swc_homes_all(ctx):
"""add swc-homes mount for all SWC users found"""
homes = ctx.run('ls -1 /home', hide=True).stdout.strip().split('\n')
users = [user for user in homes if user in machines_users]
for user in users:
add_swc_homes(ctx, user)
@task(hosts=hosts)
def btrfs_scrub(ctx):
"""check state of BTRFS scrub operations"""
btrfs_pattern = re.compile(' on (.+) type btrfs ')
mounts = ctx.run('mount', hide=True).stdout.strip().split('\n')
mounts = [btrfs_pattern.search(mount) for mount in mounts]
mounts = [mount.groups()[0] for mount in mounts if mount is not None]
host = ctx.run('hostname', hide=True).stdout.strip()
for mount in mounts:
print(host, mount)
ctx.sudo('btrfs scrub status {}'.format(mount))
@task(hosts=hosts)
def deploy_vpn(ctx):
"""install SonicWall's NetExtender"""
ctx.sudo('apt install -y expect')
ctx.put('vpn/NetExtender.x86_64.tgz')
ctx.run('tar zxvf NetExtender.x86_64.tgz')
ctx.put('vpn/netextender_install', 'netExtenderClient/')
ctx.sudo('sudo sh -c "cd ~/netExtenderClient/ && ./netextender_install"')