fix usage of the subp module and function
diff --git a/DataSourceVMwareGuestInfo.py b/DataSourceVMwareGuestInfo.py
index 0e6e143..242c72f 100644
--- a/DataSourceVMwareGuestInfo.py
+++ b/DataSourceVMwareGuestInfo.py
@@ -35,11 +35,22 @@
from cloudinit import sources
from cloudinit import util
from cloudinit import safeyaml
-from cloudinit import version as cl_ver
from deepmerge import always_merger
import netifaces
+# in cloud init >= 20.3 subp is in its own module
+try:
+ import subp
+except ImportError:
+ subp_module = util
+else:
+ # early versions of the subp module don't have the subp function
+ if hasattr(subp, 'subp'):
+ subp_module = subp
+ else:
+ subp_module = util
+
LOG = logging.getLogger(__name__)
NOVAL = "No value found"
VMWARE_RPCTOOL = find_executable("vmware-rpctool")
@@ -51,7 +62,6 @@
WAIT_ON_NETWORK = 'wait-on-network'
WAIT_ON_NETWORK_IPV4 = 'ipv4'
WAIT_ON_NETWORK_IPV6 = 'ipv6'
-CLOUD_INIT_VERSION = cl_ver.version_string()
class NetworkConfigError(Exception):
'''
@@ -302,20 +312,6 @@
LOG.debug("No value found for key %s", key)
return None
-def get_subp_obj():
- '''
- cloud-init 20.3 onwards, subp is a separate module
- So, to keep things backward compatible this is needed
- '''
- subp_obj = None
- if CLOUD_INIT_VERSION <= '20.2':
- subp_obj = util
- else:
- subp_obj = util.subp
-
- return subp_obj
-
-
def get_guestinfo_value(key):
'''
Returns a guestinfo value for the specified key.
@@ -329,9 +325,8 @@
return handle_returned_guestinfo_val(key, os.environ.get(env_key, ""))
if data_access_method == VMWARE_RPCTOOL:
- subp_obj = get_subp_obj()
try:
- (stdout, stderr) = subp_obj.subp(
+ (stdout, stderr) = subp_module.subp(
[VMWARE_RPCTOOL, "info-get guestinfo." + key])
if stderr == NOVAL:
LOG.debug("No value found for key %s", key)
@@ -339,7 +334,7 @@
LOG.error("Failed to get guestinfo value for key %s", key)
else:
return handle_returned_guestinfo_val(key, stdout)
- except subp_obj.ProcessExecutionError as error:
+ except subp_module.ProcessExecutionError as error:
if error.stderr == NOVAL:
LOG.debug("No value found for key %s", key)
else:
@@ -373,12 +368,11 @@
return True
if data_access_method == VMWARE_RPCTOOL:
- subp_obj = get_subp_obj()
try:
- subp_obj.subp(
+ subp_module.subp(
[VMWARE_RPCTOOL, ("info-set guestinfo.%s %s" % (key, value))])
return True
- except subp_obj.ProcessExecutionError as error:
+ except subp_module.ProcessExecutionError as error:
util.logexc(
LOG, "Failed to set guestinfo key=%s to value=%s: %s", key, value, error)
except Exception: