def clone(name, source, *varargs, **kwargs): ''' Clone a VM (qvm-clone). ''' # Return if VM already exists exists_status = Status(**_state_action('qvm.check', name, *['exists'])) if exists_status.passed(): message = "A VM with the name '{0}' already exists.".format(name) status = Status()._format(prefix='[SKIP] ', message=message) return vars(status._finalize(test_mode=__opts__['test'])) return _state_action('qvm.clone', source, name, *varargs, **kwargs)
def kill(name, *varargs, **kwargs): ''' Kill vmname (qvm-kill). ''' # Return if VM already halted (stderr will contain message if VM absent) halted_status = Status(**_state_action('qvm.state', name, *['halted'])) if halted_status.passed(): message = halted_status.stderr or "'{0}' is already halted.".format( name) status = Status()._format(prefix='[SKIP] ', message=message) return vars(status._finalize(test_mode=__opts__['test'])) return _state_action('qvm.kill', name, *varargs, **kwargs)
def absent(name, *varargs, **kwargs): ''' Make sure the named VM is absent. VM will be deleted (removed) if present (qvm-absent). ''' # Return if VM already absent missing_status = Status(**_state_action('qvm.check', name, *['missing'])) if missing_status.passed(): message = "The VM with the name '{0}' is already missing.".format(name) status = Status()._format(prefix='[SKIP] ', message=message) return vars(status._finalize(test_mode=__opts__['test'])) return _state_action('qvm.remove', name, *varargs, **kwargs)
def present(name, *varargs, **kwargs): ''' Make sure the named VM is present. VM will be created if missing (qvm-present). ''' # Return if VM already exists exists_status = Status(**_state_action('qvm.check', name, *['exists'])) if exists_status.passed(): message = "A VM with the name '{0}' already exists.".format(name) status = Status()._format(prefix='[SKIP] ', message=message) return vars(status._finalize(test_mode=__opts__['test'])) return _state_action('qvm.create', name, *varargs, **kwargs)
def halted(name, *varargs, **kwargs): ''' Return True is vmname is halted (qvm-halted). ''' varargs = list(varargs) varargs.append('halted') # Return if VM already halted (stderr will contain message if VM absent) halted_status = Status( **_state_action('qvm.state', name, *varargs, **kwargs)) if halted_status.passed() or halted_status.stderr: message = halted_status.stderr or "'{0}' is already halted.".format( name) status = Status()._format(prefix='[SKIP] ', message=message) return vars(status._finalize(test_mode=__opts__['test'])) return _state_action('qvm.state', name, *varargs, **kwargs)
def create(name, comment, result=None): ''' Used to show an alert message when a condition is met not to include a state. :param name: :param comment: :param result: ''' return dict(Status(name=name, result=result, comment=comment))
def _state_action(_action, *varargs, **kwargs): ''' ''' try: status = __salt__[_action](*varargs, **kwargs) except (SaltInvocationError, CommandExecutionError) as e: status = Status(retcode=1, result=False, comment=e.message + '\n') return vars(status)
def status(self): ''' Returns finalized merged 'data' status. ''' status = Status() status_mode = 'last' if 'last' in self.defaults.status_mode else 'all' debug_mode = True if 'debug' in self.defaults.status_mode else False # pylint: disable=W0212 return status._finalize(data=self._data, status_mode=status_mode, cli_mode=self.defaults.cli_mode, debug_mode=debug_mode, test_mode=__opts__['test'])
def run(self, cmd, test_ignore=False, post_hook=None, data=None, **options): ''' Executes cmd using salt.utils run_all function. Fake status are returned instead of executing the command if test mode is enabled. :param cmd: :param test_ignore: :param post_hook: :param data: ''' if __opts__['test'] and not test_ignore: status = Status(retcode=0, prefix='[TEST] ') else: if isinstance(cmd, list): cmd = ' '.join(pipes.quote(c) for c in cmd) status = Status(**__salt__['cmd.run_all'](cmd, runas=self.run_as_user, output_loglevel='quiet', **options)) delattr(status, 'pid') self._run_post_hook(post_hook, cmd, status, data) cmd_options = str(options) if options else '' cmd_string = '{0} {1}'.format(cmd, cmd_options) return self.save_status(status, message=cmd_string)
def _state_action(_action, *varargs, **kwargs): ''' State utility to standardize calling Qubes modules. Python State Example: .. code-block:: python from qubes_state_utils import state_action as _state_action def exists(name, *varargs, **kwargs): varargs = list(varargs) varargs.append('exists') return _state_action('qvm.check', name, *varargs, **kwargs) ''' try: status = __salt__[_action](*varargs, **kwargs) except (SaltInvocationError, CommandExecutionError) as err: status = Status(retcode=1, result=False, stderr=err.message + '\n') return vars(status)
def save_status(self, status=None, retcode=None, result=None, data=None, prefix=None, message='', error_message=''): ''' Merges data from individual status into master data dictionary which will be returned and includes all changes and comments as well as the overall status status. :param status: :param retcode: :param result: :param data: :param prefix: :param message: :param error_message: ''' # Create a default status if one does not exist if status is None: status = Status() if not status.name: status.name = self.__virtualname__ # pylint: disable=W0212 status._format(retcode=retcode, result=result, data=data, prefix=prefix, message=message, error_message=error_message) self._data.append(status) return status