Add ability to clear guestinfo keys after reading
This patch adds the ability to clear data from the guestinfo dictionary
after the data has been read by the datasource. This feature is enabled
by adding the following to the metadata:
cleanup-guestinfo:
- userdata
- vendordata
When the above snippet is added to a VM's metadata, the keys
"guestinfo.userdata", "guestinfo.userdata.encoding",
"guestinfo.vendordata", and "guestinfo.vendordata.encoding" will all be
set to the value of a single space character.
diff --git a/DataSourceVMwareGuestInfo.py b/DataSourceVMwareGuestInfo.py
index 3cf3450..a1467ea 100644
--- a/DataSourceVMwareGuestInfo.py
+++ b/DataSourceVMwareGuestInfo.py
@@ -106,7 +106,8 @@
that the get_data functions in newer versions of cloud-init do,
such as calling persist_instance_data.
"""
- if not get_data_access_method():
+ data_access_method = get_data_access_method()
+ if not data_access_method:
LOG.error("vmware-rpctool is required to fetch guestinfo value")
return False
@@ -119,6 +120,10 @@
# Get the vendor data.
self.vendordata_raw = guestinfo('vendordata')
+ # Check to see if any of the guestinfo data should be removed.
+ if data_access_method == VMWARE_RPCTOOL:
+ clear_guestinfo_keys(self.metadata['cleanup-guestinfo'])
+
if self.metadata or self.userdata_raw or self.vendordata_raw:
return True
else:
@@ -268,6 +273,61 @@
return None
+def set_guestinfo_value(key, value):
+ '''
+ Sets a guestinfo value for the specified key. Set value to an empty string
+ to clear an existing guestinfo key.
+ '''
+
+ # If value is an empty string then set it to a single space as it is not
+ # possible to set a guestinfo key to an empty string. Setting a guestinfo
+ # key to a single space is as close as it gets to clearing an existing
+ # guestinfo key.
+ if value == "":
+ value = " "
+
+ LOG.debug("Setting guestinfo key=%s to value=%s", key, value)
+
+ data_access_method = get_data_access_method()
+
+ if data_access_method == VMX_GUESTINFO:
+ return True
+
+ if data_access_method == VMWARE_RPCTOOL:
+ try:
+ util.subp(
+ [VMWARE_RPCTOOL, ("info-set guestinfo.%s %s" % (key, value))])
+ return True
+ except util.ProcessExecutionError as error:
+ util.logexc(
+ LOG, "Failed to set guestinfo key=%s to value=%s: %s", key, value, error)
+ except Exception:
+ util.logexc(
+ LOG, "Unexpected error while trying to set guestinfo key=%s to value=%s", key, value)
+
+ return None
+
+
+def clear_guestinfo_keys(keys):
+ '''
+ clear_guestinfo_keys clears guestinfo of all of the keys in the given list
+ '''
+ if not keys:
+ return
+ for key in keys:
+ clear_guestinfo_value(key)
+ clear_guestinfo_value(key + ".encoding")
+
+
+def clear_guestinfo_value(key):
+ '''
+ clear_guestinfo_value sets the specified guestinfo key to an empty string
+ '''
+ LOG.info("clearing guestinfo.%s", key)
+ if not set_guestinfo_value(key, ''):
+ LOG.error("failed to clear guestinfo.%s", key)
+
+
def guestinfo(key):
'''
guestinfo returns the guestinfo value for the provided key, decoding