-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathswarm.py
executable file
·291 lines (199 loc) · 6.82 KB
/
swarm.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
#!/usr/bin/env python
"""Usage:
swarm start
swarm allocate --count=N [--name=NAME]
swarm stop
swarm kill [ID]
swarm ping
swarm queue [all]
swarm run ID COMMAND
swarm ps
swarm hello
swarm info
swarm init
swarm join
swarm nodes [ID]
swarm layout
Arguments:
N Number workers
Options:
-h --help
Description:
Allocation:
python swarm.py allocate --count=5
create an allocation on which we run swarm (e.g. 5 nodes)
python swarm.py quque
list the allocations.YOu will see th IDs that you need to interact with the allocation
python swarm.py kill 142
kills the allocation with the id 142. You will need the ID that you qget via the queue command
python swarm.py ping
returns a hardcoded info of the nodes, should be done dynamically. TODO
"""
from __future__ import print_function
import sys
if not hasattr(sys, 'real_prefix'):
print("ERROR: please use a virtualenv")
sys.exit(1)
from docopt import docopt
from os import system
from cloudmesh_client.common.dotdict import dotdict
from pprint import pprint
from cloudmesh_client.common.util import banner
from cloudmesh_client.shell.console import Console
from cloudmesh_client.common.ConfigDict import ConfigDict
from cloudmesh_client.common.Shell import Shell
from cloudmesh_client.common.hostlist import Parameter
def _ssh(command):
args = command.split(" ")
r = Shell.ssh(*args)
return r
def nodes(user, id):
r = _ssh("india /opt/slurm/bin/squeue -u {user} -h --format=%N".format(**locals())).split("\n")
return r
def ids(user):
print (locals())
args = "india /opt/slurm/bin/squeue -u {user} -h --format=%i".format(**locals()).split(" ")
r = Shell.ssh(*args).split("\n")
return r
# return Parameter.expand(r)
def jobs(user, allusers=False):
print (locals())
if allusers:
system("ssh india /opt/slurm/bin/squeue".format(**locals()))
else:
system("ssh india /opt/slurm/bin/squeue -u {user} ".format(**locals()))
def init_docker(name):
data = {'worker': name}
system("ssh -t india ssh -t {worker} sudo docker swarm init".format(**data))
def join_docker(name, master):
data = {'worker': name,
'master': master}
system("ssh -t india ssh -t {worker} sudo docker join {master}".format(**data))
def info_docker(name):
data = {'worker': name}
system("ssh -t india ssh -t {worker} sudo docker info".format(**data))
def start_docker(name):
data = {'worker': name}
system("ssh -t india ssh -t {worker} sudo systemctl start docker".format(**data))
def stop_docker(name):
data = {'worker': name}
system("ssh -t india ssh -t {worker} sudo systemctl stop docker".format(**data))
def ps_docker(name, user):
jobs('gvonlasz')
#data = {'worker': name}
system("ssh -t india ssh -t {name} \"ps aux | grep docker | grep -v grep\"".format(**locals()))
def get_layout(id, user):
if id is None:
myjobs = ids(user)
else:
myjobs = [id]
if len(myjobs) > 1:
print(jobs, len(myjobs))
Console.error("More than one swarm cluster running, please specify ID")
n = nodes(user, myjobs[0])[0]
all_nodes = Parameter.expand(n)
if __name__ == '__main__':
arguments = docopt(__doc__)
data = dotdict(arguments)
data.user = ConfigDict(filename='cloudmesh.yaml')['cloudmesh']['profile']['user']
data.NAME = data['--name'] or 'swarm'
data.N = data['--count']
if data.username in ['TBD']:
Console.error("cloudmesh.profile.user not set for delta access. Please edit the cloudmesh.yaml file.")
pprint (data)
workers = ['d006']
manager = ['d005']
if arguments['start']:
manager, workers = get_layout(data.ID, data.user)
print (manager, workers)
for w in workers + manager:
banner("HOST: " + w)
data['worker'] = w
start_docker(w)
elif arguments['info']:
for w in workers + manager:
banner("HOST: " + w)
data['worker'] = w
info_docker(w)
elif arguments['init']:
for w in manager:
banner("HOST: " + w)
data['worker'] = w
init_docker(w)
elif arguments['join']:
for w in workers:
banner("HOST: " + w)
data['worker'] = w
join_docker(w, 'd001')
elif arguments['ps']:
for w in workers + manager:
banner("HOST: " + w)
data['worker'] = w
ps_docker(w)
elif arguments['stop']:
for w in workers + manager:
banner("HOST: " + w)
data['worker'] = w
stop_docker(w)
elif arguments['ping']:
for w in workers + manager:
banner("HOST: " + w)
data['worker'] = w
args = "india ssh {worker} hostname".format(**data).split(" ")
r = Shell.ssh(*args)
print (r)
elif arguments['hello']:
for w in workers + manager:
banner("HOST: " + w)
data['worker'] = w
args = "-t india ssh -t {worker} sudo docker run hello-world".format(**data).split(" ")
r = Shell.ssh(*args)
if "Hello from Docker." in r:
Console.ok("Docker working.")
else:
print (r)
elif arguments['allocate']:
system("ssh -t india /opt/slurm/bin/salloc --no-shell -J {NAME} -N {N} -p delta".format(**data))
system("ssh -t india /opt/slurm/bin/squeue")
elif arguments['kill']:
if data.ID is None:
myjobs = ids(data.user)
else:
myjobs = [data.ID]
for j in myjobs:
data.ID = j
system("ssh india /opt/slurm/bin/scancel {ID}".format(**data))
system("ssh india /opt/slurm/bin/squeue")
elif arguments['nodes']:
if data.ID is None:
myjobs = ids(data.user)
else:
myjobs = [data.ID]
print (type(myjobs))
print ('jjj', myjobs)
if len(myjobs) > 1:
print (jobs, len(myjobs))
Console.error("More than one swarm cluster running, please specify ID")
n = nodes(data.user, myjobs[0])[0]
print (n)
print (Parameter.expand(n))
elif arguments['queue']:
#system("ssh india /opt/slurm/bin/squeue | grep {user} ".format(**data))
if data.all:
jobs (data.user)
else:
jobs(data.user, allusers=True)
n = ids(data.user)
print (n)
elif arguments['run']:
system(" ssh india -t /opt/slurm/bin/srun --jobid {ID} \"{COMMAND}\"".format(**data))
'''
ssh india
# start daeomons on each ost
sudo service docker start
see if the node works
sudo docker pull centos
sudo docker images centos
# stop daemons
sudo service docker start
'''