diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-04-09 10:43:55 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-04-09 10:43:55 +0300 |
commit | 230d4a762dbb0085c16d1f9ba3bd2a56574a4ff2 (patch) | |
tree | 798a84de55016033692481c6ec7ca2a355e5a5dd /contractor | |
parent | 6e76b45f9fcf1d47aca42dd0dfad7bdc16f13b75 (diff) | |
download | ick-contractor-230d4a762dbb0085c16d1f9ba3bd2a56574a4ff2.tar.gz |
Fix: some error handling
Diffstat (limited to 'contractor')
-rwxr-xr-x | contractor | 92 |
1 files changed, 52 insertions, 40 deletions
@@ -64,24 +64,29 @@ class ContractorApplication(cliapp.Application): bs = self.load_build_spec(args[0]) m = self.manager() + # This might fail. We ignore the failure. self.verbose('stopping worker') m.stop_worker() self.verbose( 'uploading image to worker: {}'.format(bs.worker_image())) - m.upload_worker_image(bs.worker_image()) + if m.upload_worker_image(bs.worker_image()) != 0: + self.error('could not upload image to worker') + sys.exit(1) self.verbose('starting worker') - if not m.start_worker(): + w = m.start_worker() + if not w: self.error('could not start worker') + sys.exit(1) - # install = bs.install() - # if install: - # self.verbose( - # 'installing packages: {}'.format(', '.join(install))) - # else: - # self.verbose('no packages to install') - # m.install_on + install = bs.install() + if install: + self.verbose( + 'installing packages: {}'.format(', '.join(install))) + m.install_on + else: + self.verbose('no packages to install') # if pkgs: # self.cmd_setup( @@ -387,42 +392,17 @@ class Manager: self.virsh(['undefine', 'worker']) def upload_worker_image(self, filename): - rsync(filename, '{}:worker.img'.format(self._target)) - - def copy_worker_image(self): - self.ssh(['cp', 'worker.img', 'temp.img']) - - def get_cpu_count(self): - out, err, exit_code = self.ssh(['lscpu'], quiet=True) - if exit_code != 0: - logging.error('lscpu on manager failed: {!r}'.format(err)) - return None - - for line in out.decode('UTF8').splitlines(): - if line.startswith('CPU(s):'): - return int(line.split()[-1]) - - logging.error('Could not find number of CPUs on manager') - return None - - def worker_ip(self): - filename = '/var/lib/libvirt/dnsmasq/virbr0.status' - out, err, exit_code = self.ssh(['cat', filename], quiet=True) - if exit_code != 0: - logging.error('Could not read dnsmasq status file') - return None - logging.debug('virbr0.status from manager: {!r}'.format(out)) + return rsync(filename, '{}:worker.img'.format(self._target)) - status = json.loads(out) - if not status: + def start_worker(self): + if self.copy_worker_image() != 0: + logging.error('Could not copy worker image for new instance') return None - status.sort(key=lambda e: e['expiry-time']) - return status[-1]['ip-address'] - - def start_worker(self): + # We ignore failures on these, as they shouldn't matter. self.virsh(['net-autostart', 'default']) self.virsh(['net-start', 'default']) + n = self.get_cpu_count() if not isinstance(n, int): logging.error('Could not start worker due to missing CPU count') @@ -450,6 +430,38 @@ class Manager: return self.wait_for_worker() + def copy_worker_image(self): + out, err, exit_code = self.ssh(['cp', 'worker.img', 'temp.img']) + return exit_code + + def get_cpu_count(self): + out, err, exit_code = self.ssh(['lscpu'], quiet=True) + if exit_code != 0: + logging.error('lscpu on manager failed: {!r}'.format(err)) + return None + + for line in out.decode('UTF8').splitlines(): + if line.startswith('CPU(s):'): + return int(line.split()[-1]) + + logging.error('Could not find number of CPUs on manager') + return None + + def worker_ip(self): + filename = '/var/lib/libvirt/dnsmasq/virbr0.status' + out, err, exit_code = self.ssh(['cat', filename], quiet=True) + if exit_code != 0: + logging.error('Could not read dnsmasq status file') + return None + logging.debug('virbr0.status from manager: {!r}'.format(out)) + + status = json.loads(out) + if not status: + return None + + status.sort(key=lambda e: e['expiry-time']) + return status[-1]['ip-address'] + def wait_for_worker(self): # We look up the IP and try to use it. The IP might be for a # previous worker instance. |