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 is_running(target): _command = "qvm-check --quiet --running {} 2>/dev/null".format(target) try: lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) except lib.QsmProcessError as error: if error.returncode != constants.QVM_CHECK_IS_NOT_RUNNING: # is not exit code 1 # some other error occurred lib.print_sub( "a problem occurred when checking that {} is running".format( target), failed=True) raise error return False # is exit code 1 == domain is not running return True
def exists(target): _command = "qvm-check --quiet {} 2>/dev/null".format(target) try: lib.run(command=_command, target="dom0", user=getpass.getuser(), show_message=False) except lib.QsmProcessError as error: if error.returncode != constants.QVM_CHECK_EXISTS_NOT_FOUND: # is not exit code 2 # some other error occurred lib.print_sub( "a problem occurred when checking that {} exists".format( target), failed=True) raise error return False # is exit code 2 == domain doesn't exist return True
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))