diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-04-09 09:27:32 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-04-09 09:27:32 +0300 |
commit | 86b6c3eda54a7b697772db5bd568bd7cb038a596 (patch) | |
tree | e7b1b035fd841e6da65f5d553ef30518349d4e3a /contractor | |
parent | b550bc07f2c028d8eb7f03465daa299cb21828e1 (diff) | |
download | ick-contractor-86b6c3eda54a7b697772db5bd568bd7cb038a596.tar.gz |
Refactor: add Manager class
Diffstat (limited to 'contractor')
-rwxr-xr-x | contractor | 114 |
1 files changed, 59 insertions, 55 deletions
@@ -44,21 +44,65 @@ class ContractorApplication(cliapp.Application): bs = self.load_build_spec(args[0]) self.output.write('{}\n'.format(json.dumps(bs.as_dict(), indent=4))) + def cmd_build(self, args): + self.verbose('Building using spec at {}'.format(args[0])) + bs = self.load_build_spec(args[0]) + + self.cmd_worker_stop([]) + + image = os.path.expanduser(build['worker-image']) + self.cmd_worker_image([image]) + self.cmd_worker_start([]) + + pkgs = build.get('install', []) + if pkgs: + self.verbose('Installing packages: {}'.format(', '.join(pkgs))) + self.cmd_setup( + ['env', 'DEBIAN_INTERFACE=noninteractive', + 'apt-get', 'install', '-y'] + pkgs) + + workspace = os.path.expanduser(build['workspace']) + if workspace and os.path.isdir(workspace): + self.verbose('Uploading saved workspace from {}'.format(workspace)) + self.cmd_put_workspace([workspace]) + + source = os.path.expanduser(build['source']) + self.verbose('Uploading source code from {}'.format(source)) + self.on_worker('worker', ['find', '/workspace/src', '-delete']) + self.cmd_sources([source]) + + self.verbose('Running build commands') + o = self.on_worker( + 'worker', ['sh', '-euxc', build['build']], + stdout=None, stderr=None, + remote_cwd='/workspace/src') + if o is None: + self.error('Could not build on worker') + sys.exit(1) + self.output.write(o.decode('utf-8')) + + if workspace: + self.verbose('Saving workspace to {}'.format(workspace)) + if os.path.exists(workspace): + shutil.rmtree(workspace) + os.makedirs(workspace) + self.cmd_get_workspace([workspace]) + + self.verbose('Build finished OK') + def load_build_spec(self, filename): with open(filename) as f: return BuildSpec(f.read()) def cmd_manager_status(self, args): - if ssh(self.manager_target(), ['true']) != 0: + user = self.settings['manager-user'] + addr = self.settings['manager-address'] + m = Manager(user, addr) + if m.ssh(['true']) != 0: self.error('Manager VM is NOT available') sys.exit(1) self.verbose('Manager VM is available') - def manager_target(self): - user = self.settings['manager-user'] - addr = self.settings['manager-address'] - return '{}@{}'.format(user, addr) - ############################ def cmd_on_manager(self, args): @@ -132,55 +176,6 @@ class ContractorApplication(cliapp.Application): if self.settings['verbose']: self.output.write(o.decode('utf-8')) - def cmd_build(self, args): - filename = args[0] - with open(filename) as f: - build = yaml.safe_load(f) - - self.verbose('Building using spec at {}'.format(filename)) - - self.cmd_worker_stop([]) - - image = os.path.expanduser(build['worker-image']) - self.cmd_worker_image([image]) - self.cmd_worker_start([]) - - pkgs = build.get('install', []) - if pkgs: - self.verbose('Installing packages: {}'.format(', '.join(pkgs))) - self.cmd_setup( - ['env', 'DEBIAN_INTERFACE=noninteractive', - 'apt-get', 'install', '-y'] + pkgs) - - workspace = os.path.expanduser(build['workspace']) - if workspace and os.path.isdir(workspace): - self.verbose('Uploading saved workspace from {}'.format(workspace)) - self.cmd_put_workspace([workspace]) - - source = os.path.expanduser(build['source']) - self.verbose('Uploading source code from {}'.format(source)) - self.on_worker('worker', ['find', '/workspace/src', '-delete']) - self.cmd_sources([source]) - - self.verbose('Running build commands') - o = self.on_worker( - 'worker', ['sh', '-euxc', build['build']], - stdout=None, stderr=None, - remote_cwd='/workspace/src') - if o is None: - self.error('Could not build on worker') - sys.exit(1) - self.output.write(o.decode('utf-8')) - - if workspace: - self.verbose('Saving workspace to {}'.format(workspace)) - if os.path.exists(workspace): - shutil.rmtree(workspace) - os.makedirs(workspace) - self.cmd_get_workspace([workspace]) - - self.verbose('Build finished OK') - def cmd_sources(self, args): dirname = args[0] @@ -343,4 +338,13 @@ class SpecMissingKey(Exception): 'Build specification is missing required key {!r}'.format(key)) +class Manager: + + def __init__(self, user, addr): + self._target = '{}@{}'.format(user, addr) + + def ssh(self, argv): + return ssh(self._target, argv) + + ContractorApplication().run() |