-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbutil_fabfile.py
More file actions
191 lines (167 loc) · 6.55 KB
/
tbutil_fabfile.py
File metadata and controls
191 lines (167 loc) · 6.55 KB
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
from fabric.api import local, run, sudo
from fabric.colors import red, green, yellow
from fabric.contrib.console import confirm
from fabric.context_managers import quiet, cd, settings
from fabric.decorators import hosts, roles
from fabric.state import env, output
from fabric.contrib.files import exists
users='users.deterlab.net'
# GTL - this can be removed once we've updated fabric to new version.
env.disable_known_hosts = True
env.always_use_pty = False
output['running'] = False
class FabricException(Exception):
pass
def show_ok(msg):
print(green('[OK ]') + ': ' + msg)
def show_warn(msg):
print(yellow('[WARN ]') + ': ' + msg)
def show_err(msg, exitval=0):
print(red('[ERROR]') + ': ' + msg)
if exit:
exit(exitval)
@hosts(users)
def swapexp(pid, eid, swapin):
cmd = 'swapexp -e {},{} '.format(pid, eid)
msg = 'Swapping experiment {},{} in'.format(pid, eid)
if swapin:
cmd += 'in'
else:
cmd += 'out'
with quiet():
result = local(cmd, capture=True)
if result.failed:
show_err(msg, exit=2)
show_ok(msg)
@hosts(users)
def getnodes(pid, eid):
cmd = '/usr/testbed/bin/node_list -v -c -e {},{}'.format(pid, eid)
msg = 'getting nodenames for experiment...'
with quiet():
result = local(cmd, capture=True)
if result:
return result.split()
return None
@hosts(users)
def initenv(pid, eid, parallel=True):
nodenames = getnodes(pid, eid)
msg = 'Initializing environment for experiment {},{}'.format(pid, eid)
if not nodenames:
show_warn(msg)
if confirm('Experiment not found. Swap it in?)'):
swapexp(pid, eid, swapin=True)
else:
show_err(msg + ' Exiting.', exitval=1)
show_ok(msg)
nodes = ['{}.{}.{}'.format(n, eid, pid) for n in nodenames]
env.hosts = nodes
env.parallel = parallel
def restart_magi():
kill_magi()
start_magi()
def kill_magi():
msg = 'Killing and uninstalling Magi on {}'.format(env.host_string)
with settings(warn_only=True, abort_exception=FabricException), quiet():
try:
run('sudo killall mongos mongod magi_daemon.py magi_orchestrator.py')
run('sudo rm -rf /var/log/magi/db /space/var/log/magi/db /usr/local/lib/python*/dist-packages/magi /tmp/MAGI* /tmp/magi*')
show_ok(msg)
except FabricException:
show_err(msg)
def magi_log_to_space(old="/var/log/", new="/space/var/log/", conf="/var/log/magi/config/experiment.conf", mdir='/proj/edgect/magi/current'):
msg = 'Changing MAGI\'s default log dir of {} to {}'.format(old,new)
# XXX Hack
if '/space' in new:
if not exists('/space'):
show_err('\'/space/\' not set up.')
return
try:
run('sudo mkdir -p {}'.format(new))
except FabricException:
show_err(msg)
if exists(new):
if exists(conf):
try:
kill_magi()
start_magi(mdir=mdir)
file_find_replace(file=conf, old_text=old, new_text=new)
run('sudo cp {} /tmp/experiment-custom.conf'.format(conf))
kill_magi()
start_magi(mdir=mdir, expconf='/tmp/experiment-custom.conf')
except FabricException:
show_err(msg)
else:
show_err('Conf file {} does not exist.'.format(conf))
else:
show_err('New log directory does not exist.')
def start_magi(mdir='/proj/edgect/magi/current', expconf=None):
msg = 'Installing and starting Magi on {}'.format(env.host_string)
with settings(warn_only=True, abort_exception=FabricException), quiet():
try:
if expconf != None:
expconf_arg = ' --expconf {}'.format(expconf)
else:
expconf_arg =''
run('sudo {}/magi_bootstrap.py {} -fp {}'.format(mdir, expconf_arg, mdir))
show_ok(msg)
except FabricException:
show_err(msg)
def kill_deterdash():
if exists('/space/deterdash'):
msg = 'Killing Deterdash on {}'.format(env.host_string)
with settings(warn_only=True, abort_exception=FabricException), quiet():
try:
run('sudo pkill -f runserver.py')
run('sudo pkill -f websocketd')
show_ok(msg)
except FabricException:
show_err(msg)
def start_deterdash():
d = '/space/deterdash'
if exists(d):
msg = 'Starting deterdash on {}'.format(env.host_string)
with settings(warn_only=True, abort_exception=FabricException), quiet():
try:
run('sudo /space/deterdash/start_deterdash.sh')
show_ok(msg)
except FabricException:
show_err(msg)
def kill_click():
f = '/tmp/vrouter.click'
if exists(f):
msg = 'Killing click on {}'.format(env.host_string)
with settings(warn_only=True, abort_exception=FabricException), quiet():
try:
if exists('/tmp/ifconfig.json'):
run('sudo pkill -f dpdk')
else:
run('sudo click-uninstall')
show_ok(msg)
except FabricException:
show_err(msg)
def start_click():
f = '/tmp/vrouter.click'
if exists(f):
msg = 'Starting click on {}'.format(env.host_string)
with settings(warn_only=True, abort_exception=FabricException), quiet():
try:
if exists('/tmp/ifconfig.json'):
show_ok('Starting DPDK click on {}'.format(env.host_string))
run('sudo rm /click /tmp/click.log')
run('sudo nohup click --dpdk -c 0xffffff -n 4 -- -u /click /tmp/vrouter.click >/tmp/click.log 2>&1 < /dev/null &')
else:
show_ok('Starting kernel click on {}'.format(env.host_string))
run('sudo click-install {}'.format(f))
except FabricException:
show_err(msg)
def file_find_replace(file='/tmp/none', old_text='', new_text=''):
if exists(file):
msg = 'Replacing {} with {} on {}'.format(old_text, new_text, file)
with settings(warn_only=True, debug=True, abort_exception=FabricException):
try:
cmd="sudo sed 's/{}/{}/g' {} > /tmp/`basename {}`.new".format(old_text.replace('/', '\\/'), new_text.replace('/', '\\/'), file, file)
run(cmd)
run("sudo cp /tmp/`basename {}`.new {}".format(file,file))
show_ok(msg)
except FabricException:
show_err(msg)