def create(name, label, options="", exists_ok=True): lib.print_header("creating vm {}".format(name)) _command = "qvm-create --quiet --label {} {} {} 2>/dev/null".format( label, options, name) if exists_ok: try: lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) except lib.QsmProcessError as error: if error.returncode != constants.QVM_CREATE_DOMAIN_ALREADY_EXISTS: # is not exit code 1 # some other error occurred raise error lib.print_sub_warning("{} already exists, using that".format(name)) return else: not_exists_or_throws(name) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{} creation finished".format(name))
def install(packages): lib.print_header("installing packages on dom0") _command = "qubes-dom0-update -y {}".format(lib.parse_packages(packages)) lib.run(command=_command, target="dom0", user="******") lib.print_sub("dom0 package installation finished")
def uninstall(target, packages): lib.print_header("removing packages from {}".format(target)) _packages = lib.parse_packages(packages) lib.run(command=remote.remove(_packages), target=target, user="******") lib.print_sub("{} package uninstallation finished".format(target))
def install(target, packages): lib.print_header("installing packages on {}".format(target)) _packages = lib.parse_packages(packages) lib.run(command=remote.install(_packages), target=target, user="******") lib.print_sub("{} package installation finished".format(target))
def start(target): lib.print_header("starting {}".format(target)) exists_or_throws(target) _command = "qvm-start --skip-if-running {}".format(target) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{} started".format(target))
def clone(source, target): lib.print_header("cloning {} into {}".format(source, target)) exists_or_throws(source) not_exists_or_throws(target) _command = "qvm-clone --quiet {} {} 2>/dev/null".format(source, target) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{} created".format(target))
def enable_services(target, services): assert type(services) is list, "services should be a list" lib.print_header("enabling services on {}...".format(target)) exists_or_throws(target) for _service in services: _command = "qvm-service --enable {} {}".format(target, _service) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{}".format(_service))
def vm_prefs(target, prefs): assert type(prefs) is dict, "prefs should be a dict" lib.print_header("setting prefs for {}".format(target)) exists_or_throws(target) for _key, _value in prefs.items(): _command = "qvm-prefs -s {} {} \'{}\'".format(target, _key, _value) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{}: {}".format(_key, _value))
def create_template(target, source_template, prefs=None, jobs=None, update=True, packages_file_path=None, shutdown=True): lib.print_header("creating template '{}' from '{}'".format( target, source_template)) assert target != source_template, "target must not be the same as source template" not_exists_or_throws(target) if not exists(source_template): install(source_template) source_template_prefs = vm.VmPrefsBuilder()\ .label("gray")\ .include_in_backups(False)\ .build() vm_prefs(source_template, source_template_prefs) else: is_template_or_throws(source_template) clone(source_template, target) target_prefs = vm.VmPrefsBuilder().label("black").build() vm_prefs(target, target_prefs) if prefs: vm_prefs(target, prefs) if update: vm.update(target) if packages_file_path: packages = read_packages_file(packages_file_path) vm.install(target, packages) if jobs: assert _is_all_funcs(jobs), \ "jobs should be a list of funcs/lambdas, that take no params: [lambda: my_func(param), ...]" for job in jobs: job() lib.print_sub("jobs complete") if shutdown: stop(target)
def stop(target, timeout=120): lib.print_header("stopping {}".format(target)) exists_or_throws(target) if is_running(target): _command = "qvm-shutdown --wait --timeout {} {}".format( timeout, target) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{} stopped".format(target)) return lib.print_sub_warning("{} already stopped".format(target))
def remove(target, shutdown_ok=False): lib.print_header("removing {}".format(target)) _command = "qvm-remove --quiet --force {}".format(target) if exists(target): # pep.. shhh if shutdown_ok: stop(target) else: is_stopped_or_throws(target) lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) lib.print_sub("{} removal finished".format(target)) return lib.print_sub_warning("{} doesn't exist, continuing...".format(target))
def init(config_dir=join(expanduser("~"), ".qsm")): config_file = join(config_dir, "qsm.conf") plugins_dir = join(config_dir, "plugins") data_dir = join(config_dir, "data") makedirs(config_dir, exist_ok=True, mode=0o750) makedirs(plugins_dir, exist_ok=True, mode=0o750) makedirs(data_dir, exist_ok=True, mode=0o750) if not isfile(config_file): lib.print_header("creating a new config file") # true for dirs, let raise if is dir with open(config_file, "w") as f: json.dump({ "data_dir": data_dir, "plugins_dir": plugins_dir }, f, indent=2, sort_keys=True) chmod(config_file, 0o640) lib.print_sub("config file created @ {}".format(config_file)) return config_file
def update(): lib.print_header("updating dom0") lib.run(command="qubes-dom0-update -y", target="dom0", user="******") lib.print_sub("dom0 update finished")
def update(target): lib.print_header("updating {}".format(target)) lib.run(command=remote.update(), target=target, user="******") lib.print_sub("{} update finished".format(target))