def register_plugin_calls(*funcs): """Wrapper around XenAPIPlugin.dispatch which handles pickle serialization. """ wrapped_dict = {} for func in funcs: wrapped_dict[func.__name__] = _handle_serialization(func) XenAPIPlugin.dispatch(wrapped_dict)
exists_args = { "dom_id": arg_dict["dom_id"], "path": "name", } dom_id_is_present = xenstore.record_exists(exists_args) if not dom_id_is_present: reboot_detected = True break # No response within the timeout period; bail out # First, delete the request record arg_dict["path"] = "data/host/%s" % request_id xenstore.delete_record(self, arg_dict) if reboot_detected: raise RebootDetectedError(_("REBOOT: dom_id %s no longer " "present") % arg_dict["dom_id"]) else: raise TimeoutError(_("TIMEOUT: No response from agent within" " %s seconds.") % timeout) if __name__ == "__main__": XenAPIPlugin.dispatch( {"version": version, "key_init": key_init, "password": password, "resetnetwork": resetnetwork, "inject_file": inject_file, "agentupdate": agent_update})
#!/usr/bin/env python # Simple XenAPI plugin import XenAPIPlugin, time def main(session, args): if args.has_key("sleep"): secs = int(args["sleep"]) time.sleep(secs) return "args were: %s" % (repr(args)) if __name__ == "__main__": XenAPIPlugin.dispatch({"main": main})
def after_extauth_enable(session, args): return rewrite_etc_pamd_ssh(session, args) def after_xapi_initialize(session, args): return rewrite_etc_pamd_ssh(session, args) def after_subject_add(session, args): return rewrite_etc_pamd_ssh(session, args) def after_subject_remove(session, args): return rewrite_etc_pamd_ssh(session, args) def after_roles_update(session, args): return rewrite_etc_pamd_ssh(session, args) def before_extauth_disable(session, args): return revert_etc_pamd_ssh(session, args) # The dispatcher if __name__ == "__main__": dispatch_tbl = { "after-extauth-enable": after_extauth_enable, "after-xapi-initialize": after_xapi_initialize, "after-subject-add": after_subject_add, "after-subject-remove": after_subject_remove, "after-roles-update": after_roles_update, "before-extauth-disable":before_extauth_disable, } XenAPIPlugin.dispatch(dispatch_tbl)
raise return file_like_object.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)) if __name__ == "__main__": XenAPIPlugin.dispatch({"get_console_log": get_console_log})
""" pipe = subprocess.PIPE proc = subprocess.Popen(cmd, shell=False, stdin=pipe, stdout=pipe, stderr=pipe, close_fds=True) (out, err) = proc.communicate(cmd_input) return proc.returncode, out, err def run_command(session, args): cmd = json.loads(args.get('cmd')) if cmd and cmd[0] not in ALLOWED_CMDS: msg = _("Dom0 execution of '%s' is not permitted") % cmd[0] raise PluginError(msg) returncode, out, err = _run_command( cmd, json.loads(args.get('cmd_input', 'null'))) if not err: err = "" if not out: out = "" # This runs in Dom0, will return to neutron-ovs-agent in compute node result = {'returncode': returncode, 'out': out, 'err': err} return json.dumps(result) if __name__ == "__main__": XenAPIPlugin.dispatch({"run_command": run_command})
"dom_id": arg_dict["dom_id"], "path": "name", } dom_id_is_present = xenstore.record_exists(exists_args) if not dom_id_is_present: reboot_detected = True break # No response within the timeout period; bail out # First, delete the request record arg_dict["path"] = "data/host/%s" % request_id xenstore.delete_record(self, arg_dict) if reboot_detected: raise RebootDetectedError("REBOOT: dom_id %s no longer " "present" % arg_dict["dom_id"]) else: raise TimeoutError("TIMEOUT: No response from agent within" " %s seconds." % timeout) if __name__ == "__main__": XenAPIPlugin.dispatch({ "version": version, "key_init": key_init, "password": password, "resetnetwork": resetnetwork, "inject_file": inject_file, "agentupdate": agent_update })
elif mode == "nbd": cmdout = util.pread2(["rbd-nbd", "--nbds_max", NBDS_MAX, "map", "%s/%s" % (CEPH_POOL_NAME, vdi_name), "--name", CEPH_USER]).rstrip('\n') util.pread2(["ln", "-s", cmdout, dev_name]) return "mapped" def _unmap(session, arg_dict): mode = arg_dict['mode'] dev_name = arg_dict['dev_name'] CEPH_POOL_NAME = arg_dict['CEPH_POOL_NAME'] CEPH_USER = arg_dict['CEPH_USER'] NBDS_MAX = arg_dict['NBDS_MAX'] if arg_dict.has_key("snap_name"): vdi_name = arg_dict["snap_name"] else: vdi_name = arg_dict['vdi_name'] if mode == "kernel": util.pread2(["rbd", "unmap", dev_name, "--name", CEPH_USER]) elif mode == "fuse": pass elif mode == "nbd": nbddev = util.pread2(["realpath", dev_name]).rstrip('\n') util.pread2(["unlink", dev_name]) util.pread2(["rbd-nbd", "unmap", nbddev, "--name", CEPH_USER]) return "unmapped" if __name__ == "__main__": XenAPIPlugin.dispatch({"map": _map, "unmap": _unmap})
output = _run_command(["ls", "/sys/bus/pci/devices/" + pci_device + "/"]) if "physfn" in output: return "type-VF" if "virtfn" in output: return "type-PF" return "type-PCI" if __name__ == "__main__": # Support both serialized and non-serialized plugin approaches _, methodname = xmlrpclib.loads(sys.argv[1]) if methodname in ['query_gc', 'get_pci_device_details', 'get_pci_type', 'network_config']: utils.register_plugin_calls(query_gc, get_pci_device_details, get_pci_type, network_config) XenAPIPlugin.dispatch( {"host_data": host_data, "set_host_enabled": set_host_enabled, "host_shutdown": host_shutdown, "host_reboot": host_reboot, "host_start": host_start, "host_join": host_join, "get_config": get_config, "set_config": set_config, "iptables_config": iptables_config, "host_uptime": host_uptime})
import XenAPIPlugin from xcpngutils import run_command, error_wrapped @error_wrapped def list_block_devices(session, args): result = run_command(["lsblk", "-P", "-b", "-o", "NAME,KNAME,PKNAME,SIZE,TYPE,RO,MOUNTPOINT"]) output_string = result["stdout"].decode("utf-8").strip() results = list() blockdevices = dict() for output in output_string.split("\n"): output_dict = dict(re.findall(r'(\S+)=(".*?"|\S+)', output)) output_dict = {key.lower(): output_dict[key].strip('"') for key in output_dict} kname = output_dict["kname"] pkname = output_dict["pkname"] if pkname != "": parent = blockdevices[pkname] if "children" not in parent: parent["children"] = list() parent["children"].append(output_dict) else: results.append(output_dict) blockdevices[kname] = output_dict return json.dumps({'blockdevices': results}) if __name__ == "__main__": XenAPIPlugin.dispatch({"list_block_devices": list_block_devices})
# child of same parent ret.append("%s/%s" % ("/".join(path), barename)) elif this_level > level: path.append(last_nm) ret.append("%s/%s" % ("/".join(path), barename)) level = this_level elif this_level < level: path = path[:this_level] ret.append("%s/%s" % ("/".join(path), barename)) level = this_level last_nm = barename return ret def _run_command(cmd): """Wrap utils.run_command to raise XenstoreError on failure """ try: return utils.run_command(cmd) except utils.SubprocessException, e: raise XenstoreError(e.cmdline, e.ret, e.err, e.out) if __name__ == "__main__": XenAPIPlugin.dispatch({ "read_record": read_record, "write_record": write_record, "list_records": list_records, "delete_record": delete_record })
def after_subject_update(session, args): """Callback after subject update""" return refresh_dynamic_pam(session, args, "after_subject_update") def after_roles_update(session, args): """Callback after roles update""" return refresh_dynamic_pam(session, args, "after_roles_update") def before_extauth_disable(session, args): """Callback before disable external auth""" return refresh_all_configurations(session, args, "before_extauth_disable", False) # The dispatcher if __name__ == "__main__": dispatch_tbl = { "after-extauth-enable": after_extauth_enable, "after-xapi-initialize": after_xapi_initialize, "after-subject-add": after_subject_add, "after-subject-update": after_subject_update, "after-subject-remove": after_subject_remove, "after-roles-update": after_roles_update, "before-extauth-disable": before_extauth_disable, } XenAPIPlugin.dispatch(dispatch_tbl)
filename = "" logging.debug("Cached kernel/ramdisk image not found") return filename def _remove_file(filepath): try: os.remove(filepath) except OSError as exc: # noqa if exc.errno != errno.ENOENT: raise def remove_kernel_ramdisk(session, args): """Removes kernel and/or ramdisk from dom0's file system.""" kernel_file = optional(args, 'kernel-file') ramdisk_file = optional(args, 'ramdisk-file') if kernel_file: _remove_file(kernel_file) if ramdisk_file: _remove_file(ramdisk_file) return "ok" if __name__ == '__main__': XenAPIPlugin.dispatch({ 'copy_vdi': copy_vdi, 'create_kernel_ramdisk': create_kernel_ramdisk, 'remove_kernel_ramdisk': remove_kernel_ramdisk })
# a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # NOTE: XenServer still only supports Python 2.4 in it's dom0 userspace # which means the Nova xenapi plugins must use only Python 2.4 features import XenAPIPlugin def get_val(session, args): config_key = args['key'] config_file = open('/etc/xapi.conf') try: for line in config_file: split = line.split('=') if (len(split) == 2) and (split[0].strip() == config_key): return split[1].strip() return "" finally: config_file.close() if __name__ == '__main__': XenAPIPlugin.dispatch({"get_val": get_val})
# returns empty string if no streaming is configured @error_wrapped def get_netdata_api_key(session, args): with OperationLocker(): try: with open("/etc/netdata/stream.conf", "r") as conf_file: content = conf_file.readlines() content = map(lambda line: line.split('#')[0].strip(), content) api_key_line = filter(lambda line: line.startswith('api key'), content) # Python 2&3 compatible code api_key_line = [x for x in api_key_line][0] api_key = api_key_line.split('=')[1].strip() return api_key except EnvironmentError as e: # if the file doesn't exist, the system is not configured for streaming, return empty string if e.errno == errno.ENOENT: return '' raise _LOGGER = configure_logging('netdata') if __name__ == "__main__": XenAPIPlugin.dispatch({ 'is_netdata_installed': is_netdata_installed, 'install_netdata': install_netdata, 'get_netdata_api_key': get_netdata_api_key })
path = [] elif this_level == level: # child of same parent ret.append("%s/%s" % ("/".join(path), barename)) elif this_level > level: path.append(last_nm) ret.append("%s/%s" % ("/".join(path), barename)) level = this_level elif this_level < level: path = path[:this_level] ret.append("%s/%s" % ("/".join(path), barename)) level = this_level last_nm = barename return ret def _run_command(cmd): """Wrap utils.run_command to raise XenstoreError on failure """ try: return utils.run_command(cmd) except utils.SubprocessException, e: # noqa raise XenstoreError(e.cmdline, e.ret, e.err, e.out) if __name__ == "__main__": XenAPIPlugin.dispatch( {"read_record": read_record, "write_record": write_record, "list_records": list_records, "delete_record": delete_record})
logging.debug("Done. Filename: %s", filename) else: filename = "" logging.debug("Cached kernel/ramdisk image not found") return filename def _remove_file(filepath): try: os.remove(filepath) except OSError, exc: # noqa if exc.errno != errno.ENOENT: raise def remove_kernel_ramdisk(session, args): """Removes kernel and/or ramdisk from dom0's file system.""" kernel_file = optional(args, 'kernel-file') ramdisk_file = optional(args, 'ramdisk-file') if kernel_file: _remove_file(kernel_file) if ramdisk_file: _remove_file(ramdisk_file) return "ok" if __name__ == '__main__': XenAPIPlugin.dispatch({'copy_vdi': copy_vdi, 'create_kernel_ramdisk': create_kernel_ramdisk, 'remove_kernel_ramdisk': remove_kernel_ramdisk})
# plugin entry point def main(session, args): xelogging.logToStderr() try: url = args['url'] except KeyError: xelogging.log("Missing argument 'url'") raise Exception('MISSING_URL') xelogging.log("Verifying repo...") succeeded = False if not test_repo(url): xelogging.log("%s is not a valid repo" % url) raise Exception('INVALID_URL') else: xelogging.log("Repo ok, preparing for upgrade.") succeeded = prepare_host_upgrade(url) if succeeded: xelogging.log("Preparation succeeded, ready for upgrade.") return "true" else: xelogging.log("There was an error in preparing the host for upgrade.") raise Exception('ERROR_PREPARING_HOST') if __name__ == '__main__': XenAPIPlugin.dispatch({"main": main, "testUrl": testUrl})
import sys echoRequest = parseRequest(args) data = echoRequest.data if echoRequest.path: writeToPath(data, echoRequest.path) if echoRequest.stdout: sys.stdout.write(data) if echoRequest.stderr: sys.stderr.write(data) if echoRequest.exitCode is not None: sys.exit(echoRequest.exitCode) return echoRequest.data def toXapiArgs(args): result = [] for k, v in args.iteritems(): if v is not None: result.append('args:%s="%s"' % (k, v)) return result if __name__ == "__main__": import XenAPIPlugin XenAPIPlugin.dispatch({ECHO_FN_NAME: echo})