def set_host_enabled(self, arg_dict): """Sets this host's ability to accept new instances. It will otherwise continue to operate normally. """ enabled = arg_dict.get("enabled") if enabled is None: raise pluginlib.PluginError( _("Missing 'enabled' argument to set_host_enabled")) host_uuid = arg_dict['host_uuid'] if enabled == "true": result = _run_command(["xe", "host-enable", "uuid=%s" % host_uuid]) elif enabled == "false": result = _run_command(["xe", "host-disable", "uuid=%s" % host_uuid]) else: raise pluginlib.PluginError(_("Illegal enabled status: %s") % enabled) # Should be empty string if result: raise pluginlib.PluginError(result) # Return the current enabled status cmd = ["xe", "host-param-get", "uuid=%s" % host_uuid, "param-name=enabled"] host_enabled = _run_command(cmd) if host_enabled == "true": status = "enabled" else: status = "disabled" return {"status": status}
def network_config(session, args): """network config functions""" cmd = pluginlib.exists(args, 'cmd') if not isinstance(cmd, basestring): msg = _("invalid command '%s'") % str(cmd) raise pluginlib.PluginError(msg) return if cmd not in ALLOWED_NETWORK_CMDS: msg = _("Dom0 execution of '%s' is not permitted") % cmd raise pluginlib.PluginError(msg) return cmd_args = pluginlib.exists(args, 'args') return ALLOWED_NETWORK_CMDS[cmd](cmd_args)
def _run_command(cmd, cmd_input=None): """Wrap utils.run_command to raise PluginError on failure """ try: return utils.run_command(cmd, cmd_input=cmd_input) except utils.SubprocessException, e: # noqa raise pluginlib.PluginError(e.err)
def iptables_config(session, args): # command should be either save or restore logging.debug("iptables_config:enter") logging.debug("iptables_config: args=%s", args) cmd_args = pluginlib.exists(args, 'cmd_args') logging.debug("iptables_config: cmd_args=%s", cmd_args) process_input = pluginlib.optional(args, 'process_input') logging.debug("iptables_config: process_input=%s", process_input) cmd = json.loads(cmd_args) cmd = map(str, cmd) # either execute iptable-save or iptables-restore # command must be only one of these two # process_input must be used only with iptables-restore if len(cmd) > 0 and cmd[0] in ('iptables-save', 'iptables-restore', 'ip6tables-save', 'ip6tables-restore'): result = _run_command(cmd, process_input) ret_str = json.dumps(dict(out=result, err='')) logging.debug("iptables_config:exit") return ret_str # else don't do anything and return an error else: raise pluginlib.PluginError(_("Invalid iptables command"))
def make_partition(session, dev, partition_start, partition_end): dev_path = utils.make_dev_path(dev) if partition_end != "-": raise pluginlib.PluginError("Can only create unbounded partitions") utils.run_command(['sfdisk', '-uS', dev_path], '%s,;\n' % (partition_start))
def _power_action(action, arg_dict): # Host must be disabled first host_uuid = arg_dict['host_uuid'] result = _run_command(["xe", "host-disable", "uuid=%s" % host_uuid]) if result: raise pluginlib.PluginError(result) # All running VMs must be shutdown result = _run_command(["xe", "vm-shutdown", "--multiple", "resident-on=%s" % host_uuid]) if result: raise pluginlib.PluginError(result) cmds = {"reboot": "host-reboot", "startup": "host-power-on", "shutdown": "host-shutdown"} result = _run_command(["xe", cmds[action], "uuid=%s" % host_uuid]) # Should be empty string if result: raise pluginlib.PluginError(result) return {"power_action": action}
def _run_command(cmd): """Abstracts out the basics of issuing system commands. If the command returns anything in stderr, a PluginError is raised with that information. Otherwise, a tuple of (return code, stdout data) is returned. """ pipe = subprocess.PIPE proc = subprocess.Popen(cmd, stdin=pipe, stdout=pipe, stderr=pipe, close_fds=True) ret = proc.wait() err = proc.stderr.read() if err: raise pluginlib.PluginError(err) return (ret, proc.stdout.read())
def get_console_log(session, arg_dict): try: raw_dom_id = arg_dict['dom_id'] except KeyError: raise pluginlib_nova.PluginError("Missing dom_id") try: dom_id = int(raw_dom_id) except ValueError: raise pluginlib_nova.PluginError("Invalid dom_id") logfile = open(CONSOLE_LOG_FILE_PATTERN % dom_id, 'rb') try: try: log_content = _last_bytes(logfile) except IOError, e: # noqa msg = "Error reading console: %s" % e logging.debug(msg) raise pluginlib_nova.PluginError(msg) finally: logfile.close() return base64.b64encode(zlib.compress(log_content))
def _run_command(cmd): """Abstracts out the basics of issuing system commands. If the command returns anything in stderr, a PluginError is raised with that information. Otherwise, the output from stdout is returned. """ pipe = subprocess.PIPE proc = subprocess.Popen([cmd], shell=True, stdin=pipe, stdout=pipe, stderr=pipe, close_fds=True) proc.wait() err = proc.stderr.read() if err: raise pluginlib.PluginError(err) return proc.stdout.read()
def _mkfs(fs, path, label): """Format a file or block device :param fs: Filesystem type (only 'swap', 'ext3' supported) :param path: Path to file or block device to format :param label: Volume label to use """ if fs == 'swap': args = ['mkswap'] elif fs == 'ext3': args = ['mkfs', '-t', fs] # add -F to force no interactive execute on non-block device. args.extend(['-F']) if label: args.extend(['-L', label]) else: raise pluginlib.PluginError("Partition type %s not supported" % fs) args.append(path) utils.run_command(args)
def _resume_compute(session, compute_ref, compute_uuid): """Resume compute node on slave host after pool join. This has to happen regardless of the success or failure of the join operation. """ try: # session is valid if the join operation has failed session.xenapi.VM.start(compute_ref, False, True) except XenAPI.Failure: # if session is invalid, e.g. xapi has restarted, then the pool # join has been successful, wait for xapi to become alive again for c in range(0, DEFAULT_TRIES): try: _run_command(["xe", "vm-start", "uuid=%s" % compute_uuid]) return except pluginlib.PluginError: logging.exception('Waited %d seconds for the slave to ' 'become available.' % (c * DEFAULT_SLEEP)) time.sleep(DEFAULT_SLEEP) raise pluginlib.PluginError('Unrecoverable error: the host has ' 'not come back for more than %d seconds' % (DEFAULT_SLEEP * (DEFAULT_TRIES + 1)))