-
Notifications
You must be signed in to change notification settings - Fork 7
/
cluster_manager.py
450 lines (376 loc) · 16.1 KB
/
cluster_manager.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""Code to bootstrap and update the project so a remote host can be used as a
worker to help with the automation of tasks.
This requires ssh/scp and rsync to work on all machines.
This is currently only tested on Linux machines.
"""
import json
import os
import shlex
import stat
import subprocess
import sys
from textwrap import dedent
try:
from urllib import urlopen
except ImportError:
from urllib.request import urlopen
class BootstrapError(Exception):
pass
class ClusterManager(object):
"""The cluster manager class.
This class primarily helps setup software on a remote worker machine such
that it can run any computational jobs from the automation framework.
The general directory structure of a remote worker machine is as follows::
remote_home/ # Could be ~
automan/ # Root of automation directory (configurable)
envs/ # python virtual environments for use.
my_project/ # Current directory for specific projects.
The project directories are synced from this machine to the remote worker.
The "my_project" is the root of the directory with the automation script
and this should contain the required sources that need to be executed. One
can use a list of source directories which will be copied over but it is
probably most convenient to put it all in the root of the project directory
to keep everything self-contained.
The `ClusterManager` class manages these remote workers by helping setup
the directories, bootstrapping the Python virtualenv and also keeping these
up-to-date as project directory is changed on the local machine.
The class therefore has two primary public methods,
1. `add_worker(self, host, home, nfs)` which adds a new worker machine by
bootstrapping the machine with the software and the appropriate source
directories.
2. `update()`, which keeps the directory and software up-to-date.
The class variables BOOTSTRAP and UPDATE are the content of scripts
uploaded to these machines and should be extended by users to do what they
wish.
The class creates a ``config.json`` in the current working directory that
may be edited by a user. It also creates a directory called
``.{self.root}`` which defaults to ``.automan``. The bootstrap and
update scripts are put here and may be edited by the user for any new
hosts.
One may override the `_get_python, _get_helper_scripts`, and
`_get_bootstrap_code, _get_update_code` methods to change this to use other
package managers like edm or conda. See the conda_cluster_manager for an
example.
"""
#######################################################
# These scripts are used to bootstrap the installation
# and update them.
BOOTSTRAP = dedent("""\
#!/bin/bash
set -e
if hash virtualenv 2>/dev/null; then
virtualenv -p python3 --system-site-packages envs/{project_name}
else
python3 virtualenv.py --system-site-packages envs/{project_name}
fi
source envs/{project_name}/bin/activate
pip install automan
# Run any requirements.txt from the user
cd {project_name}
if [ -f "requirements.txt" ] ; then
pip install -r requirements.txt
fi
""")
UPDATE = dedent("""\
#!/bin/bash
set -e
source envs/{project_name}/bin/activate
# Run any requirements.txt from the user
cd {project_name}
if [ -f "requirements.txt" ] ; then
pip install -r requirements.txt
fi
""")
#######################################################
def __init__(self, root='automan', sources=None,
config_fname='config.json', exclude_paths=None,
testing=False):
"""Create a cluster manager instance.
**Parameters**
root: str
The name of the root directory where all the files on the remote
will be created.
sources: list
A list of source directories to sync.
config_fname: str
The name of the config file to create.
exclude_paths: list
A list of paths to exclude while syncing. This is in a form suitable
to pass to rsync.
testing: bool
Use this while testing. This allows us to run unit tests for remotes
on the local machine.
"""
self.root = root
self.workers = []
self.sources = sources
self.scripts_dir = os.path.abspath('.' + self.root)
self.exclude_paths = exclude_paths if exclude_paths else []
self.testing = testing
# This is setup by the config and is the name of
# the project directory.
self.project_name = None
# The config file will always trump any direct settings
# unless there is no config file.
self.config_fname = config_fname
self._read_config()
if not os.path.exists(self.scripts_dir):
os.makedirs(self.scripts_dir)
# ### Private Protocol ########################################
def _bootstrap(self, host, home):
helper_scripts = self._get_helper_scripts()
base_cmd = ("cd {home}; mkdir -p {root}/envs; "
"mkdir -p {root}/{project_name}/.{root}").format(
home=home, root=self.root,
project_name=self.project_name
)
self._ssh_run_command(host, base_cmd)
abs_root = os.path.join(home, self.root)
if helper_scripts:
real_host = '' if self.testing else '{host}:'.format(host=host)
cmd = "scp {helper_scripts} {host}{root}".format(
host=real_host, root=abs_root, helper_scripts=helper_scripts
)
self._run_command(cmd)
self._update_sources(host, home)
cmd = "cd {abs_root}; ./{project_name}/.{root}/bootstrap.sh".format(
abs_root=abs_root, root=self.root, project_name=self.project_name
)
try:
self._ssh_run_command(host, cmd)
except subprocess.CalledProcessError:
msg = dedent("""
******************************************************************
Bootstrapping of remote host {host} failed.
All files have been copied to the host.
Please take a look at
{abs_root}/{project_name}/.{root}/bootstrap.sh
and try to fix it.
You should run it from within the {root} directory as:
./{project_name}/.{root}/bootstrap.sh
Once the bootstrap.sh script runs successfully, the worker can be
used without any further steps.
The default bootstrap script is in
{scripts_dir}
and can be edited by you. These will be used for any new hosts
you add.
******************************************************************
""".format(abs_root=abs_root, root=self.root, host=host,
scripts_dir=self.scripts_dir,
project_name=self.project_name)
)
print(msg)
raise BootstrapError(msg)
else:
print("Bootstrapping {host} succeeded!".format(host=host))
def _get_bootstrap_code(self):
return self.BOOTSTRAP.format(project_name=self.project_name)
def _get_python(self, host, home):
return os.path.join(
home, self.root,
'envs/{project_name}/bin/python'.format(
project_name=self.project_name
)
)
def _get_update_code(self):
return self.UPDATE.format(project_name=self.project_name)
def _get_helper_scripts(self):
"""Return a space separated string of script files that you need copied over to
the remote host.
When overriding this, you can return None or '' if you do not need any.
"""
script = os.path.join(self.scripts_dir, 'virtualenv.py')
if not os.path.exists(script):
print("Downloading latest virtualenv.py")
url = 'https://raw.githubusercontent.com/pypa/virtualenv/master/virtualenv.py'
opener = urlopen(url)
with open(script, 'wb') as f:
f.write(opener.read())
return script
def _read_config(self):
if os.path.exists(self.config_fname):
with open(self.config_fname) as f:
data = json.load(f)
self.root = data['root']
self.project_name = data['project_name']
self.sources = data['sources']
self.workers = data['workers']
else:
if self.sources is None or len(self.sources) == 0:
project_dir = os.path.abspath(os.getcwd())
self.project_name = os.path.basename(project_dir)
self.sources = [project_dir]
self.workers = [dict(host='localhost', home='', nfs=False)]
self._write_config()
self.scripts_dir = os.path.abspath('.' + self.root)
def _rebuild(self, host, home):
abs_root = os.path.join(home, self.root)
base_cmd = "cd {abs_root}; ./{project_name}/.{root}/update.sh".format(
abs_root=abs_root, root=self.root, project_name=self.project_name
)
self._ssh_run_command(host, base_cmd)
def _run_command(self, cmd, **kw):
print(cmd)
subprocess.check_call(shlex.split(cmd), **kw)
def _ssh_run_command(self, host, base_cmd):
if self.testing:
command = base_cmd
print(command)
subprocess.check_call(command, shell=True)
else:
command = "ssh {host} '{cmd}'".format(host=host, cmd=base_cmd)
self._run_command(command)
def _sync_dir(self, host, src, dest):
options = ""
kwargs = dict()
if os.path.isdir(os.path.join(src, '.git')):
exclude = 'git -C {src} ls-files --exclude-standard -oi '\
'--directory '.format(src=src)
options = '--exclude-from=-'
proc = subprocess.Popen(
shlex.split(exclude),
stdout=subprocess.PIPE
)
kwargs['stdin'] = proc.stdout
if self.exclude_paths:
for path in self.exclude_paths:
options += ' --exclude="%s"' % path
real_host = '' if self.testing else '{host}:'.format(host=host)
command = "rsync -a {options} {src} {host}{dest} ".format(
options=options, src=src, host=real_host, dest=dest
)
self._run_command(command, **kwargs)
def _update_sources(self, host, home):
for local_dir in self.sources:
remote_dir = os.path.join(home, self.root + '/')
self._sync_dir(host, local_dir, remote_dir)
scripts_dir = self.scripts_dir
bootstrap_code = self._get_bootstrap_code()
update_code = self._get_update_code()
scripts = {'bootstrap.sh': bootstrap_code,
'update.sh': update_code}
for script, code in scripts.items():
fname = os.path.join(scripts_dir, script)
if not os.path.exists(fname):
# Create the scripts if they don't exist.
with open(fname, 'w') as f:
f.write(code)
script_files = [os.path.join(scripts_dir, x) for x in scripts]
for fname in script_files:
mode = os.stat(fname).st_mode
os.chmod(fname, mode | stat.S_IXUSR | stat.S_IXGRP)
path = os.path.join(home, self.root, self.project_name,
'.' + self.root)
real_host = '' if self.testing else '{host}:'.format(host=host)
cmd = "scp {script_files} {host}{path}".format(
host=real_host, path=path, script_files=' '.join(script_files)
)
self._run_command(cmd)
def _delete_outputs(self, host, home, sim_dir):
path = os.path.join(home, self.root, self.project_name,
sim_dir)
real_host = '' if self.testing else '{host}'.format(host=host)
cmd = "rm -rf {path}".format(
path=path
)
self._ssh_run_command(real_host, cmd)
def _write_config(self):
print("Writing %s" % self.config_fname)
data = dict(
project_name=self.project_name,
root=self.root,
sources=self.sources,
workers=self.workers
)
with open(self.config_fname, 'w') as f:
json.dump(data, f, indent=2)
# ### Public Protocol ########################################
def add_worker(self, host, home, nfs):
if host == 'localhost':
self.workers.append(dict(host=host, home=home, nfs=nfs))
else:
curdir = os.path.basename(os.getcwd())
if nfs:
python = sys.executable
chdir = curdir
else:
python = self._get_python(host, home)
chdir = os.path.join(home, self.root, curdir)
self.workers.append(
dict(host=host, home=home, nfs=nfs, python=python, chdir=chdir)
)
self._write_config()
if host != 'localhost' and not nfs:
self._bootstrap(host, home)
def update(self, rebuild=True):
for worker in self.workers:
host = worker.get('host')
home = worker.get('home')
nfs = worker.get('nfs', False)
if host != 'localhost' and not nfs:
self._update_sources(host, home)
if rebuild:
self._rebuild(host, home)
def delete(self, sim_dir, remotes):
hosts = [h.get('host') for h in self.workers]
if remotes[-1] == 'all':
remotes = hosts.copy()
else:
for remote in remotes:
if remote not in hosts:
print('%s remote is not a worker' % (remote))
sys.exit(1)
for worker in self.workers:
host = worker.get('host')
home = worker.get('home')
nfs = worker.get('nfs', False)
if host in remotes:
if host != 'localhost' and not nfs:
self._delete_outputs(host, home, sim_dir)
def create_scheduler(self):
"""Return a `automan.jobs.Scheduler` from the configuration.
"""
from .jobs import Scheduler
scheduler = Scheduler(root='.')
for worker in self.workers:
host = worker.get('host')
nfs = worker.get('nfs', False)
if host == 'localhost':
scheduler.add_worker(dict(host='localhost'))
else:
python = worker.get('python')
chdir = worker.get('chdir')
config = dict(host=host, python=python, chdir=chdir, nfs=nfs)
if self.testing:
config['testing'] = True
scheduler.add_worker(config)
return scheduler
def cli(self, argv=None):
"""This is just a demonstration of how this class could be used.
"""
import argparse
parser = argparse.ArgumentParser(description='Setup remote workers.')
parser.add_argument(
'-a', '--add-node', action="store", dest="host", type=str,
default='', help="Add a new remote worker."
)
parser.add_argument(
'--home', action="store", dest="home", type=str,
default='',
help='Home directory of the remote worker (to be used with -a)'
)
parser.add_argument(
'--nfs', action="store_true", dest="nfs",
default=False,
help=('Does the remote remote worker share the filesystem '
'(to be used with -a)')
)
parser.add_argument(
'--no-rebuild', action="store_true", dest="no_rebuild",
default=False, help="Do not rebuild the sources on sync."
)
args = parser.parse_args(argv)
if len(args.host) == 0:
self.update(not args.no_rebuild)
else:
self.add_worker(args.host, args.home, args.nfs)