From e601ab4a1068029b2f0b79789ed506fda1332404 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Thu, 16 Dec 2010 20:59:27 -0600 Subject: XenAPI Snapshots first cut --- plugins/xenapi/etc/xapi.d/plugins/glance | 108 +++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 plugins/xenapi/etc/xapi.d/plugins/glance (limited to 'plugins') diff --git a/plugins/xenapi/etc/xapi.d/plugins/glance b/plugins/xenapi/etc/xapi.d/plugins/glance new file mode 100644 index 000000000..55d71b79e --- /dev/null +++ b/plugins/xenapi/etc/xapi.d/plugins/glance @@ -0,0 +1,108 @@ +#!/usr/bin/env python + +# Copyright (c) 2010 Citrix Systems, Inc. +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# 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. + +# +# XenAPI plugin for putting images into glance +# + +import base64 +import errno +import hmac +import os +import os.path +import sha +import subprocess +import time +import urlparse + +import XenAPIPlugin + +#FIXME(sirp): should this use pluginlib from 5.6? +from pluginlib_nova import * +configure_logging('glance') + +#FIXME(sirp): Should this just be 64K like elsewhere +CHUNK_SIZE = 4096 + +FILE_SR_PATH = '/var/run/sr-mount' + +#TODO(sirp): use get_vhd_parent code from Citrix guys +#TODO(sirp): look for Citrix guys get VDI from VM_ref (does this exist in +#pluginlib? +# Need to add glance client to plugins (how do we get this onto XenServer +# machine?) +# WHen do we cleanup unused VDI's in SR + +def put_vdis(session, args): + vdi_uuids = exists(args, 'vdi_uuids').split(',') + glance_label = exists(args, 'glance_label') + + sr_path = get_sr_path(session) + tar_cmd = ['tar', '-zc', '--directory=%s' % sr_path] + paths = [ "%s.vhd" % vdi_uuid for vdi_uuid in vdi_uuids ] + tar_cmd.extend(paths) + logging.debug("Bundling image with cmd: %s", tar_cmd) + tar_proc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE) + + test_file = "%s.tar.gz" % os.path.join('/tmp', glance_label) + logging.debug("Writing to test file %s", test_file) + bundle = tar_proc.stdout + f = open(test_file, 'w') + try: + chunk = bundle.read(CHUNK_SIZE) + while chunk: + f.write(chunk) + chunk = bundle.read(CHUNK_SIZE) + finally: + f.close() + + return "" # FIXME(sirp): return anything useful here? + + +def get_sr_path(session): + sr_ref = find_sr(session) + + if sr_ref is None: + raise Exception('Cannot find SR to read VDI from') + + sr_rec = session.xenapi.SR.get_record(sr_ref) + sr_uuid = sr_rec["uuid"] + sr_path = os.path.join(FILE_SR_PATH, sr_uuid) + return sr_path + + +#TODO(sirp): both objectstore and glance need this, should this be refactored +#into common lib +def find_sr(session): + host = get_this_host(session) + srs = session.xenapi.SR.get_all() + for sr in srs: + sr_rec = session.xenapi.SR.get_record(sr) + if not ('i18n-key' in sr_rec['other_config'] and + sr_rec['other_config']['i18n-key'] == 'local-storage'): + continue + for pbd in sr_rec['PBDs']: + pbd_rec = session.xenapi.PBD.get_record(pbd) + if pbd_rec['host'] == host: + return sr + return None + + +if __name__ == '__main__': + XenAPIPlugin.dispatch({'put_vdis': put_vdis}) -- cgit From 650a0cdbc854d37fd62348ce34a14ef91ccbabad Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Fri, 17 Dec 2010 19:17:39 -0600 Subject: Cleaned up TODOs, using flags now --- plugins/xenapi/etc/xapi.d/plugins/glance | 76 ++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 24 deletions(-) (limited to 'plugins') diff --git a/plugins/xenapi/etc/xapi.d/plugins/glance b/plugins/xenapi/etc/xapi.d/plugins/glance index 55d71b79e..13e79ff9f 100644 --- a/plugins/xenapi/etc/xapi.d/plugins/glance +++ b/plugins/xenapi/etc/xapi.d/plugins/glance @@ -24,8 +24,10 @@ import base64 import errno import hmac +import httplib import os import os.path +import pickle import sha import subprocess import time @@ -37,43 +39,69 @@ import XenAPIPlugin from pluginlib_nova import * configure_logging('glance') -#FIXME(sirp): Should this just be 64K like elsewhere -CHUNK_SIZE = 4096 - +CHUNK_SIZE = 8192 FILE_SR_PATH = '/var/run/sr-mount' -#TODO(sirp): use get_vhd_parent code from Citrix guys -#TODO(sirp): look for Citrix guys get VDI from VM_ref (does this exist in -#pluginlib? -# Need to add glance client to plugins (how do we get this onto XenServer -# machine?) -# WHen do we cleanup unused VDI's in SR - def put_vdis(session, args): - vdi_uuids = exists(args, 'vdi_uuids').split(',') - glance_label = exists(args, 'glance_label') + params = pickle.loads(exists(args, 'params')) + vdi_uuids = params["vdi_uuids"] + glance_label = params["glance_label"] + glance_host = params["glance_host"] + glance_port = params["glance_port"] + glance_storage_location = params["glance_storage_location"] sr_path = get_sr_path(session) - tar_cmd = ['tar', '-zc', '--directory=%s' % sr_path] + #FIXME(sirp): writing to a temp file until Glance supports chunked-PUTs + tmp_file = "%s.tar.gz" % os.path.join('/tmp', glance_label) + tar_cmd = ['tar', '-zcf', tmp_file, '--directory=%s' % sr_path] paths = [ "%s.vhd" % vdi_uuid for vdi_uuid in vdi_uuids ] tar_cmd.extend(paths) logging.debug("Bundling image with cmd: %s", tar_cmd) - tar_proc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE) - - test_file = "%s.tar.gz" % os.path.join('/tmp', glance_label) - logging.debug("Writing to test file %s", test_file) - bundle = tar_proc.stdout - f = open(test_file, 'w') + subprocess.call(tar_cmd) + logging.debug("Writing to test file %s", tmp_file) + put_bundle_in_glance(tmp_file, glance_label, glance_storage_location, + glance_host, glance_port) + return "" # FIXME(sirp): return anything useful here? + + +def put_bundle_in_glance(tmp_file, glance_label, glance_storage_location, + glance_host, glance_port): + + size = os.path.getsize(tmp_file) + + basename = os.path.basename(tmp_file) + location = os.path.join(glance_storage_location, basename) + + bundle = open(tmp_file, 'r') try: + headers = { + 'x-image-meta-is_public': 'True', + 'x-image-meta-name': glance_label, + 'x-image-meta-location': location, + 'x-image-meta-size': size, + 'content-length': size, + } + conn = httplib.HTTPConnection(glance_host, glance_port) + #NOTE(sirp): httplib under python2.4 won't accept a file-like object + # to request + conn.putrequest('POST', '/images') + + for header, value in headers.iteritems(): + conn.putheader(header, value) + conn.endheaders() + chunk = bundle.read(CHUNK_SIZE) while chunk: - f.write(chunk) + conn.send(chunk) chunk = bundle.read(CHUNK_SIZE) - finally: - f.close() - - return "" # FIXME(sirp): return anything useful here? + + res = conn.getresponse() + #FIXME(sirp): should this be 201 Created? + if res.status != httplib.OK: + raise Exception("Unexpected response from Glance %i" % res.status) + finally: + bundle.close() def get_sr_path(session): sr_ref = find_sr(session) -- cgit From af4d6e84c67b8f59f63ef0275778fa897dac9e95 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Wed, 22 Dec 2010 13:01:33 -0600 Subject: Getting Snapshots to work with cloudservers command-line tool --- plugins/xenapi/etc/xapi.d/plugins/glance | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'plugins') diff --git a/plugins/xenapi/etc/xapi.d/plugins/glance b/plugins/xenapi/etc/xapi.d/plugins/glance index 13e79ff9f..5e648b970 100644 --- a/plugins/xenapi/etc/xapi.d/plugins/glance +++ b/plugins/xenapi/etc/xapi.d/plugins/glance @@ -45,41 +45,37 @@ FILE_SR_PATH = '/var/run/sr-mount' def put_vdis(session, args): params = pickle.loads(exists(args, 'params')) vdi_uuids = params["vdi_uuids"] - glance_label = params["glance_label"] + image_name = params["image_name"] glance_host = params["glance_host"] glance_port = params["glance_port"] - glance_storage_location = params["glance_storage_location"] sr_path = get_sr_path(session) #FIXME(sirp): writing to a temp file until Glance supports chunked-PUTs - tmp_file = "%s.tar.gz" % os.path.join('/tmp', glance_label) + tmp_file = "%s.tar.gz" % os.path.join('/tmp', image_name) tar_cmd = ['tar', '-zcf', tmp_file, '--directory=%s' % sr_path] paths = [ "%s.vhd" % vdi_uuid for vdi_uuid in vdi_uuids ] tar_cmd.extend(paths) logging.debug("Bundling image with cmd: %s", tar_cmd) subprocess.call(tar_cmd) logging.debug("Writing to test file %s", tmp_file) - put_bundle_in_glance(tmp_file, glance_label, glance_storage_location, - glance_host, glance_port) + put_bundle_in_glance(tmp_file, image_name, glance_host, glance_port) return "" # FIXME(sirp): return anything useful here? -def put_bundle_in_glance(tmp_file, glance_label, glance_storage_location, - glance_host, glance_port): - +def put_bundle_in_glance(tmp_file, image_name, glance_host, glance_port): size = os.path.getsize(tmp_file) - basename = os.path.basename(tmp_file) - location = os.path.join(glance_storage_location, basename) bundle = open(tmp_file, 'r') try: headers = { + 'x-image-meta-store': 'file', 'x-image-meta-is_public': 'True', - 'x-image-meta-name': glance_label, - 'x-image-meta-location': location, + 'x-image-meta-type': 'raw', + 'x-image-meta-name': image_name, 'x-image-meta-size': size, 'content-length': size, + 'content-type': 'application/octet-stream', } conn = httplib.HTTPConnection(glance_host, glance_port) #NOTE(sirp): httplib under python2.4 won't accept a file-like object -- cgit From 66f8e28fb4f4a898803ac6a38974a9fa804612d0 Mon Sep 17 00:00:00 2001 From: Ed Leafe Date: Thu, 30 Dec 2010 18:12:57 -0600 Subject: completed the basic xenstore read/write/delete functionality --- .../xenapi/etc/xapi.d/plugins/xenstore.py | 158 +++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100755 plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py (limited to 'plugins') diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py new file mode 100755 index 000000000..3b9d65b85 --- /dev/null +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python + +# Copyright (c) 2010 Citrix Systems, Inc. +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# 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. + +# +# XenAPI plugin for reading/writing information to xenstore +# + +try: + import json +except ImportError: + import simplejson as json +import subprocess + +import XenAPIPlugin + +from pluginlib_nova import * +configure_logging("xenstore") + + +def read_record(self, arg_dict): + """Returns the value stored at the given path for the given dom_id. + These must be encoded as key/value pairs in arg_dict. You can + optinally include a key 'ignore_missing_path'; if this is present + and boolean True, attempting to read a non-existent path will return + the string 'None' instead of raising an exception. + """ + cmd = "xenstore-read /local/domain/%(dom_id)s/%(path)s" % arg_dict + try: + return _run_command(cmd).rstrip("\n") + except PluginError, e: + if arg_dict.get("ignore_missing_path", False): + cmd = "xenstore-exists /local/domain/%(dom_id)s/%(path)s; echo $?" % arg_dict + ret = _run_command(cmd).strip() + # If the path exists, the cmd should return "0" + if ret != "0": + # No such path, so ignore the error + return "None" + # Either we shouldn't ignore path errors, or another + # error was hit. Re-raise. + raise + +def write_record(self, arg_dict): + """Writes to xenstore at the specified path. If there is information + already stored in that location, it is overwritten. As in read_record, + the dom_id and path must be specified in the arg_dict; additionally, + you must specify a 'value' key, whose value must be a string. Typically, + you can json-ify more complex values and store the json output. + """ + cmd = "xenstore-write /local/domain/%(dom_id)s/%(path)s '%(value)s'" % arg_dict + _run_command(cmd) + return arg_dict["value"] + +def list_records(self, arg_dict): + """Returns all the stored data at or below the given path for the + given dom_id. The data is returned as a json-ified dict, with the + path as the key and the stored value as the value. If the path + doesn't exist, an empty dict is returned. + """ + cmd = "xenstore-ls /local/domain/%(dom_id)s/%(path)s" % arg_dict + cmd = cmd.rstrip("/") + try: + recs = _run_command(cmd) + except PluginError, e: + if "No such file or directory" in "%s" % e: + # Path doesn't exist. + return json.dumps({}) + raise + base_path = arg_dict["path"] + paths = _paths_from_ls(recs) + ret = {} + for path in paths: + if base_path: + arg_dict["path"] = "%s/%s" % (base_path, path) + else: + arg_dict["path"] = path + rec = read_record(self, arg_dict) + try: + val = json.loads(rec) + except ValueError: + val = rec + ret[path] = val + return json.dumps(ret) + +def delete_record(self, arg_dict): + """Just like it sounds: it removes the record for the specified + VM and the specified path from xenstore. + """ + cmd = "xenstore-rm /local/domain/%(dom_id)s/%(path)s" % arg_dict + return _run_command(cmd) + +def _paths_from_ls(recs): + """The xenstore-ls command returns a listing that isn't terribly + useful. This method cleans that up into a dict with each path + as the key, and the associated string as the value. + """ + ret = {} + last_nm = "" + level = 0 + path = [] + ret = [] + for ln in recs.splitlines(): + nm, val = ln.rstrip().split(" = ") + barename = nm.lstrip() + this_level = len(nm) - len(barename) + if this_level == 0: + ret.append(barename) + level = 0 + 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): + """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 PluginError(err) + return proc.stdout.read() + + +if __name__ == "__main__": + XenAPIPlugin.dispatch( + {"read_record": read_record, + "write_record": write_record, + "list_records": list_records, + "delete_record": delete_record}) -- cgit From b097d5a247f95fac180c3270cb1f613edfa46523 Mon Sep 17 00:00:00 2001 From: Ed Leafe Date: Fri, 31 Dec 2010 04:44:45 -0600 Subject: Corrected the sloppy import in the xenstore plugin that was copied from other plugins. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py index 3b9d65b85..e0a125170 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py @@ -29,8 +29,8 @@ import subprocess import XenAPIPlugin -from pluginlib_nova import * -configure_logging("xenstore") +import pluginlib_nova as pluginlib +pluginlib.configure_logging("xenstore") def read_record(self, arg_dict): @@ -43,7 +43,7 @@ def read_record(self, arg_dict): cmd = "xenstore-read /local/domain/%(dom_id)s/%(path)s" % arg_dict try: return _run_command(cmd).rstrip("\n") - except PluginError, e: + except pluginlib.PluginError, e: if arg_dict.get("ignore_missing_path", False): cmd = "xenstore-exists /local/domain/%(dom_id)s/%(path)s; echo $?" % arg_dict ret = _run_command(cmd).strip() @@ -76,7 +76,7 @@ def list_records(self, arg_dict): cmd = cmd.rstrip("/") try: recs = _run_command(cmd) - except PluginError, e: + except pluginlib.PluginError, e: if "No such file or directory" in "%s" % e: # Path doesn't exist. return json.dumps({}) @@ -146,7 +146,7 @@ def _run_command(cmd): proc.wait() err = proc.stderr.read() if err: - raise PluginError(err) + raise pluginlib.PluginError(err) return proc.stdout.read() -- cgit From 933531440767f0696e14a73069448d0c3f5ae24e Mon Sep 17 00:00:00 2001 From: Ed Leafe Date: Fri, 31 Dec 2010 08:53:01 -0600 Subject: Added OpenStack's copyright to the xenstore plugin. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins') diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py index e0a125170..71ed82d62 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # Copyright (c) 2010 Citrix Systems, Inc. +# Copyright 2010 OpenStack LLC. # Copyright 2010 United States Government as represented by the # Administrator of the National Aeronautics and Space Administration. # All Rights Reserved. -- cgit From 02c86d1e1146c1162a36620560eb8116ce8d47f1 Mon Sep 17 00:00:00 2001 From: Ed Leafe Date: Tue, 4 Jan 2011 15:20:10 -0600 Subject: Made the plugin output fully json-ified, so I could remove the exception handlers in vmops.py. Cleaned up some pep8 issues that weren't caught in earlier runs. --- .../xenapi/etc/xapi.d/plugins/pluginlib_nova.py | 28 +++++++++++----- .../xenapi/etc/xapi.d/plugins/xenstore.py | 39 +++++++++++++++++----- 2 files changed, 49 insertions(+), 18 deletions(-) (limited to 'plugins') diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/pluginlib_nova.py b/plugins/xenserver/xenapi/etc/xapi.d/plugins/pluginlib_nova.py index 2d323a016..8e7a829d5 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/pluginlib_nova.py +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/pluginlib_nova.py @@ -45,6 +45,7 @@ class PluginError(Exception): def __init__(self, *args): Exception.__init__(self, *args) + class ArgumentError(PluginError): """Raised when required arguments are missing, argument values are invalid, or incompatible arguments are given. @@ -67,6 +68,7 @@ def ignore_failure(func, *args, **kwargs): ARGUMENT_PATTERN = re.compile(r'^[a-zA-Z0-9_:\.\-,]+$') + def validate_exists(args, key, default=None): """Validates that a string argument to a RPC method call is given, and matches the shell-safe regex, with an optional default value in case it @@ -76,20 +78,24 @@ def validate_exists(args, key, default=None): """ if key in args: if len(args[key]) == 0: - raise ArgumentError('Argument %r value %r is too short.' % (key, args[key])) + raise ArgumentError('Argument %r value %r is too short.' % + (key, args[key])) if not ARGUMENT_PATTERN.match(args[key]): - raise ArgumentError('Argument %r value %r contains invalid characters.' % (key, args[key])) + raise ArgumentError('Argument %r value %r contains invalid ' + 'characters.' % (key, args[key])) if args[key][0] == '-': - raise ArgumentError('Argument %r value %r starts with a hyphen.' % (key, args[key])) + raise ArgumentError('Argument %r value %r starts with a hyphen.' + % (key, args[key])) return args[key] elif default is not None: return default else: raise ArgumentError('Argument %s is required.' % key) + def validate_bool(args, key, default=None): - """Validates that a string argument to a RPC method call is a boolean string, - with an optional default value in case it does not exist. + """Validates that a string argument to a RPC method call is a boolean + string, with an optional default value in case it does not exist. Returns the python boolean value. """ @@ -99,7 +105,9 @@ def validate_bool(args, key, default=None): elif value.lower() == 'false': return False else: - raise ArgumentError("Argument %s may not take value %r. Valid values are ['true', 'false']." % (key, value)) + raise ArgumentError("Argument %s may not take value %r. " + "Valid values are ['true', 'false']." % (key, value)) + def exists(args, key): """Validates that a freeform string argument to a RPC method call is given. @@ -110,6 +118,7 @@ def exists(args, key): else: raise ArgumentError('Argument %s is required.' % key) + def optional(args, key): """If the given key is in args, return the corresponding value, otherwise return None""" @@ -122,13 +131,14 @@ def get_this_host(session): def get_domain_0(session): this_host_ref = get_this_host(session) - expr = 'field "is_control_domain" = "true" and field "resident_on" = "%s"' % this_host_ref + expr = 'field "is_control_domain" = "true" and field "resident_on" = "%s"' + expr = expr % this_host_ref return session.xenapi.VM.get_all_records_where(expr).keys()[0] def create_vdi(session, sr_ref, name_label, virtual_size, read_only): vdi_ref = session.xenapi.VDI.create( - { 'name_label': name_label, + {'name_label': name_label, 'name_description': '', 'SR': sr_ref, 'virtual_size': str(virtual_size), @@ -138,7 +148,7 @@ def create_vdi(session, sr_ref, name_label, virtual_size, read_only): 'xenstore_data': {}, 'other_config': {}, 'sm_config': {}, - 'tags': [] }) + 'tags': []}) logging.debug('Created VDI %s (%s, %s, %s) on %s.', vdi_ref, name_label, virtual_size, read_only, sr_ref) return vdi_ref diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py index 71ed82d62..695bf3448 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/xenstore.py @@ -34,10 +34,17 @@ import pluginlib_nova as pluginlib pluginlib.configure_logging("xenstore") +def jsonify(fnc): + def wrapper(*args, **kwargs): + return json.dumps(fnc(*args, **kwargs)) + return wrapper + + +@jsonify def read_record(self, arg_dict): """Returns the value stored at the given path for the given dom_id. - These must be encoded as key/value pairs in arg_dict. You can - optinally include a key 'ignore_missing_path'; if this is present + These must be encoded as key/value pairs in arg_dict. You can + optinally include a key 'ignore_missing_path'; if this is present and boolean True, attempting to read a non-existent path will return the string 'None' instead of raising an exception. """ @@ -46,16 +53,21 @@ def read_record(self, arg_dict): return _run_command(cmd).rstrip("\n") except pluginlib.PluginError, e: if arg_dict.get("ignore_missing_path", False): - cmd = "xenstore-exists /local/domain/%(dom_id)s/%(path)s; echo $?" % arg_dict + cmd = "xenstore-exists /local/domain/%(dom_id)s/%(path)s; echo $?" + cmd = cmd % arg_dict ret = _run_command(cmd).strip() # If the path exists, the cmd should return "0" if ret != "0": - # No such path, so ignore the error - return "None" + # No such path, so ignore the error and return the + # string 'None', since None can't be marshalled + # over RPC. + return "None" # Either we shouldn't ignore path errors, or another # error was hit. Re-raise. raise + +@jsonify def write_record(self, arg_dict): """Writes to xenstore at the specified path. If there is information already stored in that location, it is overwritten. As in read_record, @@ -63,10 +75,13 @@ def write_record(self, arg_dict): you must specify a 'value' key, whose value must be a string. Typically, you can json-ify more complex values and store the json output. """ - cmd = "xenstore-write /local/domain/%(dom_id)s/%(path)s '%(value)s'" % arg_dict + cmd = "xenstore-write /local/domain/%(dom_id)s/%(path)s '%(value)s'" + cmd = cmd % arg_dict _run_command(cmd) return arg_dict["value"] + +@jsonify def list_records(self, arg_dict): """Returns all the stored data at or below the given path for the given dom_id. The data is returned as a json-ified dict, with the @@ -80,7 +95,8 @@ def list_records(self, arg_dict): except pluginlib.PluginError, e: if "No such file or directory" in "%s" % e: # Path doesn't exist. - return json.dumps({}) + return {} + return str(e) raise base_path = arg_dict["path"] paths = _paths_from_ls(recs) @@ -96,8 +112,10 @@ def list_records(self, arg_dict): except ValueError: val = rec ret[path] = val - return json.dumps(ret) + return ret + +@jsonify def delete_record(self, arg_dict): """Just like it sounds: it removes the record for the specified VM and the specified path from xenstore. @@ -105,6 +123,7 @@ def delete_record(self, arg_dict): cmd = "xenstore-rm /local/domain/%(dom_id)s/%(path)s" % arg_dict return _run_command(cmd) + def _paths_from_ls(recs): """The xenstore-ls command returns a listing that isn't terribly useful. This method cleans that up into a dict with each path @@ -137,13 +156,15 @@ def _paths_from_ls(recs): last_nm = barename return ret + 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 = subprocess.Popen([cmd], shell=True, stdin=pipe, stdout=pipe, + stderr=pipe, close_fds=True) proc.wait() err = proc.stderr.read() if err: -- cgit