From 58eb29ebe4376a276a54f4fd984802a0e50fb8e3 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Fri, 15 Jul 2011 15:05:46 -0400 Subject: Updates to the XenServer agent plugin to fix file injection: -Update _agent_has_method so that it parses the features 'message' from nova-agent correctly. (it was trying to call .split on a dict). -Rip out the agent_has_method caching functionality which just plain isn't working with XenServer 5.6 SP2. -Pass the arg_dict to _agent_has_method. This fixes an issue where a subsequent call to xenstore.write_record didn't get the 'dom_id' (KeyError). -Fix a string formatting issue in inject_file in creating the b64 data. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/agent | 44 ++++++++++------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent index 68d7e7bff..b169a74bf 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent @@ -37,7 +37,7 @@ import time import XenAPIPlugin from pluginlib_nova import * -configure_logging("xenstore") +configure_logging("agent") import xenstore AGENT_TIMEOUT = 30 @@ -129,18 +129,18 @@ def inject_file(self, arg_dict): b64_path = arg_dict["b64_path"] b64_file = arg_dict["b64_contents"] request_id = arg_dict["id"] - if self._agent_has_method("file_inject"): + if _agent_has_method(self, "file_inject", arg_dict): # New version of the agent. Agent should receive a 'value' # key whose value is a dictionary containing 'b64_path' and # 'b64_file'. See old version below. arg_dict["value"] = json.dumps({"name": "file_inject", "value": {"b64_path": b64_path, "b64_file": b64_file}}) - elif self._agent_has_method("injectfile"): + elif _agent_has_method(self, "injectfile", arg_dict): # Old agent requires file path and file contents to be # combined into one base64 value. raw_path = base64.b64decode(b64_path) raw_file = base64.b64decode(b64_file) - new_b64 = base64.b64encode("%s,%s") % (raw_path, raw_file) + new_b64 = base64.b64encode("%s,%s" % (raw_path, raw_file)) arg_dict["value"] = json.dumps({"name": "injectfile", "value": new_b64}) else: @@ -174,30 +174,24 @@ def agent_update(self, arg_dict): return resp -def _agent_has_method(self, method): +def _agent_has_method(self, method, arg_dict): """Check that the agent has a particular method by checking its - features. Cache the features so we don't have to query the agent - every time we need to check. + features. """ + tmp_id = commands.getoutput("uuidgen") + dct = {} + dct.update(arg_dict) + dct["value"] = json.dumps({"name": "features", "value": ""}) + dct["path"] = "data/host/%s" % tmp_id + xenstore.write_record(self, dct) try: - self._agent_methods - except AttributeError: - self._agent_methods = [] - if not self._agent_methods: - # Haven't been defined - tmp_id = commands.getoutput("uuidgen") - dct = {} - dct["value"] = json.dumps({"name": "features", "value": ""}) - dct["path"] = "data/host/%s" % tmp_id - xenstore.write_record(self, dct) - try: - resp = _wait_for_agent(self, tmp_id, dct) - except TimeoutError, e: - raise PluginError(e) - response = json.loads(resp) - # The agent returns a comma-separated list of methods. - self._agent_methods = response.split(",") - return method in self._agent_methods + resp = _wait_for_agent(self, tmp_id, dct) + except TimeoutError, e: + raise PluginError(e) + response = json.loads(resp) + method_arr = response["message"].split(",") + # The agent returns a comma-separated list of methods in the message + return method in method_arr def _wait_for_agent(self, request_id, arg_dict): -- cgit From 3233d6cafb22305f09c1384ba30e677751cace6a Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Sat, 16 Jul 2011 23:30:47 -0400 Subject: Change _agent_has_method to _get_agent_features. Update the inject files function so that it calls _get_agent_features only once per injected file. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/agent | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent index b169a74bf..1321c820e 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent @@ -129,13 +129,14 @@ def inject_file(self, arg_dict): b64_path = arg_dict["b64_path"] b64_file = arg_dict["b64_contents"] request_id = arg_dict["id"] - if _agent_has_method(self, "file_inject", arg_dict): + agent_features = _get_agent_features(self, arg_dict) + if "file_inject" in agent_features: # New version of the agent. Agent should receive a 'value' # key whose value is a dictionary containing 'b64_path' and # 'b64_file'. See old version below. arg_dict["value"] = json.dumps({"name": "file_inject", "value": {"b64_path": b64_path, "b64_file": b64_file}}) - elif _agent_has_method(self, "injectfile", arg_dict): + elif "injectfile" in agent_features: # Old agent requires file path and file contents to be # combined into one base64 value. raw_path = base64.b64decode(b64_path) @@ -174,10 +175,8 @@ def agent_update(self, arg_dict): return resp -def _agent_has_method(self, method, arg_dict): - """Check that the agent has a particular method by checking its - features. - """ +def _get_agent_features(self, arg_dict): + """Return an array of features that an agent supports.""" tmp_id = commands.getoutput("uuidgen") dct = {} dct.update(arg_dict) @@ -189,9 +188,7 @@ def _agent_has_method(self, method, arg_dict): except TimeoutError, e: raise PluginError(e) response = json.loads(resp) - method_arr = response["message"].split(",") - # The agent returns a comma-separated list of methods in the message - return method in method_arr + return response["message"].split(",") def _wait_for_agent(self, request_id, arg_dict): -- cgit From b927674112849e7b4ebbd59c188d8f7a1eb47e2a Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 18 Jul 2011 09:38:50 -0400 Subject: Check returncode in get_agent_features. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/agent | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent index 1321c820e..41225e6f3 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent @@ -188,7 +188,10 @@ def _get_agent_features(self, arg_dict): except TimeoutError, e: raise PluginError(e) response = json.loads(resp) - return response["message"].split(",") + if response['returncode'] != '0': + return response["message"].split(",") + else: + return {} def _wait_for_agent(self, request_id, arg_dict): -- cgit From cf14d867673e944cc0c0d5fc160d2fbcfe56e98e Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 18 Jul 2011 10:29:49 -0400 Subject: returncode is an integer. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/agent | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent index 41225e6f3..8ec4fb061 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent @@ -188,7 +188,7 @@ def _get_agent_features(self, arg_dict): except TimeoutError, e: raise PluginError(e) response = json.loads(resp) - if response['returncode'] != '0': + if response['returncode'] != 0: return response["message"].split(",") else: return {} -- cgit From 813253e8bea9a8db9c1df45f9aa5e094503a015a Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 18 Jul 2011 13:18:16 -0400 Subject: Don't jsonify the inject_file response. It is already json. --- plugins/xenserver/xenapi/etc/xapi.d/plugins/agent | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent index 8ec4fb061..288ccc78a 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/agent @@ -114,7 +114,6 @@ def resetnetwork(self, arg_dict): xenstore.write_record(self, arg_dict) -@jsonify def inject_file(self, arg_dict): """Expects a file path and the contents of the file to be written. Both should be base64-encoded in order to eliminate errors as they are passed -- cgit