From 14cd493f0bb7cfffe753b0575f1a4ac4a650a98e Mon Sep 17 00:00:00 2001 From: Jonatan Schlag Date: Tue, 24 Apr 2018 09:25:45 +0200 Subject: [PATCH] Support copying of files into all machines Signed-off-by: Jonatan Schlag --- disk.py | 39 +++++++++++++++++++++++++++++++++++++++ test.py | 24 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 disk.py diff --git a/disk.py b/disk.py new file mode 100755 index 0000000..d93328c --- /dev/null +++ b/disk.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +import guestfs + +import tempfile +import tarfile +import os + + +class disk(): + def __init__(self, disk): + self.con = guestfs.GuestFS(python_return_dict=True) + self.con.add_drive_opts(disk, format="qcow2") + + def mount(self, uuid, path): + self.con.launch() + part = self.con.findfs_uuid(uuid) + self.con.mount(part, path) + + def copy_in(self, fr, to): + tmp = tempfile.mkstemp() + tmp = tmp[1] + ".tar" + with tarfile.open(tmp, "w") as tar: + for file in fr: + tar.add(file, arcname=os.path.basename(file)) + self.con.tar_in_opts(tmp, to) + + def umount(self, path): + self.con.umount_opts(path) + + def close(self): + self.con.shutdown() + self.con.close() + +# test = disk("/var/lib/libvirt/images/alice.qcow2") +# test.mount("45598e92-3487-4a1b-961d-79aa3dd42a7d", "/") +# test.copy_in("/home/jonatan/nitsi/libguestfs-test", "/root/") +# test.umount("/") +# test.close() \ No newline at end of file diff --git a/test.py b/test.py index 317d420..2b26e68 100755 --- a/test.py +++ b/test.py @@ -14,6 +14,8 @@ import os import configparser +from disk import disk + class log(): def __init__(self, log_level): self.log_level = log_level @@ -74,6 +76,7 @@ class vm(): self.log.error("No such file: {}".format(self.image)) self.root_uid = root_uid + self.disk = disk(image) self.username = username self.password = password @@ -150,6 +153,15 @@ class vm(): def cmd(self, cmd): return self.serial_con.command(cmd) + def copy_in(self, fr, to): + try: + self.disk.mount(self.root_uid, "/") + self.disk.copy_in(fr, to) + except BaseException as e: + self.log.error(e) + finally: + self.disk.umount("/") + self.disk.close() class connection(): def __init__(self, device, username=None): @@ -531,6 +543,17 @@ class test(): self.config.read(self.settings_file) self.name = self.config["DEFAULT"]["Name"] self.description = self.config["DEFAULT"]["Description"] + self.copy_to = self.config["DEFAULT"]["Copy_to"] + self.copy_from = self.config["DEFAULT"]["Copy_from"] + self.copy_from = self.copy_from.split(",") + + tmp = [] + for file in self.copy_from: + file = file.strip() + file = os.path.normpath(self.path + "/" + file) + tmp.append(file) + + self.copy_from = tmp self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"] self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"] @@ -551,6 +574,7 @@ class test(): for name in self.virtual_environ.machine_names: self.virtual_machines[name].define() self.virtual_machines[name].create_snapshot() + self.virtual_machines[name].copy_in(self.copy_from, self.copy_to) self.virtual_machines[name].start() self.log.debug("Try to login on all machines") -- 2.39.2