From 716a6c0a991796816ade2816778bfde0c9fb55e1 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 23 Apr 2020 11:06:46 +0300 Subject: Change: use Ansible for configuring worker, not apt-get --- contractor | 61 ++++++++++++++++++++++++++++++++++++++++++----------------- contractor.md | 22 +++++++++++++++++---- vm.vmdb | 1 + 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/contractor b/contractor index 2a9136f..2f1f372 100755 --- a/contractor +++ b/contractor @@ -91,7 +91,6 @@ class ContractorApplication(cliapp.Application): TryUnmountWS(), MountWS(), ChownWS(), - Mkdir('/mnt/src', owner=WORKER_UID, group=WORKER_GID), ] self.exec_quietly(manager, *execs) timer.report('setup') @@ -103,6 +102,10 @@ class ContractorApplication(cliapp.Application): self.sync_to_workspace(ws, dest, '.') timer.report('upload-saved-workspace') + execs = [ + Mkdir('/mnt/src', owner=WORKER_UID, group=WORKER_GID), + ] + self.exec_quietly(manager, *execs) src = bs.source() self.sync_to_workspace(src, dest, 'src') timer.report('upload-source') @@ -116,18 +119,20 @@ class ContractorApplication(cliapp.Application): timer.report('wait-for-worker') self.exec_quietly(manager, AttachWS()) + self.verbose('attached') worker = OnWorker( dest, 'worker@{}'.format(worker_ip), verbose=self.verbose) self.exec_quietly(worker, Mkdir('/workspace'), MountWSonWorker()) - execs = [] - install = bs.install() - if install: - execs.append(AptInstall(install)) + ansible = bs.ansible() + if ansible: + self.exec_quietly(manager, Ansible(ansible, worker_ip)) - execs.append(Chdir('/workspace/src')) - execs.append(Build(bs.build())) + execs = [ + Chdir('/workspace/src'), + Build(bs.build()), + ] self.exec_verbosely(worker, *execs) timer.report('build') @@ -313,17 +318,30 @@ class TrueCmd(RemoteExecution): return ['true'] -class AptInstall(RemoteExecution): +class Ansible(RemoteExecution): - def __init__(self, packages): - self._packages = packages + def __init__(self, playbook, worker_ip): + self._playbook = playbook + self._worker_ip = worker_ip def msg(self): - return 'installing packages: ' + ' '.join(self._packages) + return 'running Ansible playbook on worker' def argv(self): - return ['sudo', 'DEBIAN_FRONTEND=noninteractive', - 'apt-get', '-y', 'install'] + self._packages + x = ['sh', '-c', ''' +cat > hosts << EOF +[all] +worker ansible_ssh_host={ip} +EOF + +cat > worker.yaml << EOF +{playbook} +EOF + +ansible-playbook -i hosts worker.yaml +'''.format(ip=self._worker_ip, playbook=yaml.safe_dump(self._playbook))] + logging.debug('Ansible: {!r}'.format(x)) + return x class Chdir(RemoteExecution): @@ -539,6 +557,15 @@ echo "$ip" '''] +class Find(RemoteExecution): + + def __init__(self, dirname): + self._dirname = dirname + + def argv(self): + return ['sudo', 'find', self._dirname, '-ls'] + + class BuildSpec: def __init__(self, yaml_text): @@ -546,14 +573,14 @@ class BuildSpec: self._image = os.path.expanduser(self._get(spec, 'worker-image')) self._source = os.path.expanduser(self._get(spec, 'source')) self._workspace = os.path.expanduser(self._get(spec, 'workspace', '')) - self._install = self._get(spec, 'install', []) + self._ansible = self._get(spec, 'ansible', []) self._build = self._get(spec, 'build') def worker_image(self): return self._image - def install(self): - return self._install + def ansible(self): + return self._ansible def source(self): return self._source @@ -567,7 +594,7 @@ class BuildSpec: def as_dict(self): return { 'worker-image': self.worker_image(), - 'install': self.install(), + 'ansible': self.ansible(), 'source': self.source(), 'workspace': self.workspace(), 'build': self.build(), diff --git a/contractor.md b/contractor.md index 42ed3c8..9a19a39 100644 --- a/contractor.md +++ b/contractor.md @@ -312,8 +312,15 @@ then the JSON output matches dump.yaml ~~~{.file #dump.yaml .yaml .numberLines} worker-image: worker.img -install: - - build-essential +ansible: + - hosts: worker + remote_user: worker + become: yes + tasks: + - apt: + name: build-essential + vars: + ansible_python_interpreter: /usr/bin/python3 source: . workspace: workspace build: | @@ -354,8 +361,15 @@ int main() ~~~{.file #hello.yaml .yaml .numberLines} worker-image: worker.img -install: - - build-essential +ansible: + - hosts: worker + remote_user: worker + become: yes + tasks: + - apt: + name: build-essential + vars: + ansible_python_interpreter: /usr/bin/python3 source: . workspace: ws build: | diff --git a/vm.vmdb b/vm.vmdb index 82b0ab6..45def5e 100644 --- a/vm.vmdb +++ b/vm.vmdb @@ -34,6 +34,7 @@ steps: - python3 - sudo - ssh + - ansible tag: / unless: rootfs_unpacked -- cgit v1.2.1