diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-09-08 12:23:48 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-10-06 12:08:24 +0300 |
commit | 746ddbc6e17d9ab8238cc4a8e3a348ccb44878b5 (patch) | |
tree | 404e18d59a99bcc516cc7f3f07d12a168967f165 /subplot/subplot.py | |
parent | 4819295206d7e1ef2f384dd8b66259434181a3d7 (diff) | |
download | debian-ansible-746ddbc6e17d9ab8238cc4a8e3a348ccb44878b5.tar.gz |
test: add a subplot to verify the roles work
Diffstat (limited to 'subplot/subplot.py')
-rw-r--r-- | subplot/subplot.py | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/subplot/subplot.py b/subplot/subplot.py new file mode 100644 index 0000000..4434f24 --- /dev/null +++ b/subplot/subplot.py @@ -0,0 +1,120 @@ +import logging +import os +import yaml + + +def fixme(ctx, **kwargs): + assert 0 + + +def create_vm(ctx): + QemuSystem = globals()["QemuSystem"] + srcdir = globals()["srcdir"] + + name = "debian-ansible-test" + base_image = "/home/liw/tmp/debian-10-openstack-amd64.qcow2" + GiB = 1024 ** 3 + disk_size = 10 * GiB + pubkey = open(os.path.join(srcdir, "ssh", "id.pub")).read().strip() + memory = 1 * GiB + cpus = 2 + username = "debian" + + logging.info("starting a VM using qemu-system") + logging.info(f" name : {name}") + logging.info(f" image : {base_image}") + logging.info(f" disk : {disk_size}") + logging.info(f" pubkey : {pubkey}") + logging.info(f" memory : {memory}") + logging.info(f" cpus : {cpus}") + logging.info(f" username: {username}") + + qemu = QemuSystem(name, base_image, disk_size, pubkey) + qemu.set_memory(memory) + qemu.set_vcpus(cpus) + qemu.set_username(username) + qemu.start() + + logging.debug("waiting for SSH to be ready") + if qemu.wait_for_ssh(): + logging.debug("SSH is ready") + else: + logging.error("SSH did not get ready") + assert 0 + logging.info("a qemu-system VM is up and running and accessible over SSH") + ctx["qemu"] = qemu + + +def destroy_vm(ctx): + logging.debug(f"destroying qemu running") + qemu = ctx["qemu"] + qemu.stop() + + +def run_true_on_host(ctx): + qemu = ctx["qemu"] + qemu.ssh(["/bin/true"]) + + +def use_role_in_playbook(ctx, role=None): + empty_playbook = { + "hosts": "test-host", + "remote_user": "debian", # FIXME: don't hardcode this + "become": True, + "roles": [], + } + playbook = ctx.get("playbook", dict(empty_playbook)) + playbook["roles"].append(role) + ctx["playbook"] = playbook + + +def set_vars_file(ctx, filename=None): + get_file = globals()["get_file"] + data = get_file(filename) + with open("vars.yml", "wb") as f: + f.write(data) + + +def run_playbook(ctx): + runcmd = globals()["runcmd"] + exit_code_zero = globals()["exit_code_zero"] + assert_ne = globals()["assert_ne"] + srcdir = globals()["srcdir"] + + with open("hosts", "w") as f: + f.write("test-host\n") + + if not os.path.exists("vars.yml"): + with open("vars.yml", "w") as f: + yaml.safe_dump([], stream=f) + + playbook = [ctx["playbook"]] + assert_ne(playbook, None) + with open("playbook.yml", "w") as f: + yaml.safe_dump(playbook, stream=f) + + ssh_opts = [ + "-ouserknownhostsfile=/dev/null", + "-ostricthostkeychecking=accept-new", + "-i", + os.path.join(srcdir, "ssh", "id"), + "-p2222", + ] + + env = dict(os.environ) + env["ANSIBLE_SSH_ARGS"] = " ".join(ssh_opts) + env["ANSIBLE_LOG"] = "ansible.log" + env["ANSIBLE_ROLES_PATH"] = os.path.join(srcdir, "roles") + + argv = [ + "ansible-playbook", + "-i", + "hosts", + f"-e@vars.yml", + "-eansible_ssh_host=localhost", + "-eansible_ssh_port=2222", + "playbook.yml", + ] + + runcmd(ctx, argv, env=env) + exit_code_zero(ctx) |