summaryrefslogtreecommitdiff
path: root/cloudinit/sources/helpers/vmware/imc/config_file.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2016-03-01 00:51:26 -0500
committerScott Moser <smoser@ubuntu.com>2016-03-01 00:51:26 -0500
commitf0e26abe896318819a6b568dd72e9974b53928c8 (patch)
treee2aa8a1c6260b94c2c416f81c1c54af624a1afbb /cloudinit/sources/helpers/vmware/imc/config_file.py
parent14915526ca67bbf7842028d48170015b85f87469 (diff)
parentc5d2f79a982258d86181368b25ce6bc6638ef645 (diff)
downloadvyos-cloud-init-f0e26abe896318819a6b568dd72e9974b53928c8.tar.gz
vyos-cloud-init-f0e26abe896318819a6b568dd72e9974b53928c8.zip
Add Image Customization Parser for VMware vSphere Hypervisor Support.
This is the first changeset submitted as a part of project to add cloud-init support for VMware vSphere Hypervisor. This changeset contains _only_ the changes for a simple python parser for a Image Customization Specification file pushed by VMware vSphere hypervisor into the guest VMs. In a later changeset, will be submitting another patch to actually detect the underlying VMware vSphere hypervisor and do the necessary customization.
Diffstat (limited to 'cloudinit/sources/helpers/vmware/imc/config_file.py')
-rw-r--r--cloudinit/sources/helpers/vmware/imc/config_file.py129
1 files changed, 129 insertions, 0 deletions
diff --git a/cloudinit/sources/helpers/vmware/imc/config_file.py b/cloudinit/sources/helpers/vmware/imc/config_file.py
new file mode 100644
index 00000000..bb9fb7dc
--- /dev/null
+++ b/cloudinit/sources/helpers/vmware/imc/config_file.py
@@ -0,0 +1,129 @@
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2015 Canonical Ltd.
+# Copyright (C) 2015 VMware Inc.
+#
+# Author: Sankar Tanguturi <stanguturi@vmware.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3, as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import logging
+
+try:
+ import configparser
+except ImportError:
+ import ConfigParser as configparser
+
+from .config_source import ConfigSource
+
+logger = logging.getLogger(__name__)
+
+
+class ConfigFile(ConfigSource, dict):
+ """ConfigFile module to load the content from a specified source."""
+
+ def __init__(self, filename):
+ self._loadConfigFile(filename)
+ pass
+
+ def _insertKey(self, key, val):
+ """
+ Inserts a Key Value pair.
+
+ Keyword arguments:
+ key -- The key to insert
+ val -- The value to insert for the key
+
+ """
+ key = key.strip()
+ val = val.strip()
+
+ if key.startswith('-') or '|-' in key:
+ canLog = False
+ else:
+ canLog = True
+
+ # "sensitive" settings shall not be logged
+ if canLog:
+ logger.debug("ADDED KEY-VAL :: '%s' = '%s'" % (key, val))
+ else:
+ logger.debug("ADDED KEY-VAL :: '%s' = '*****************'" % key)
+
+ self[key] = val
+
+ def _loadConfigFile(self, filename):
+ """
+ Parses properties from the specified config file.
+
+ Any previously available properties will be removed.
+ Sensitive data will not be logged in case the key starts
+ from '-'.
+
+ Keyword arguments:
+ filename - The full path to the config file.
+ """
+ logger.info('Parsing the config file %s.' % filename)
+
+ config = configparser.ConfigParser()
+ config.optionxform = str
+ config.read(filename)
+
+ self.clear()
+
+ for category in config.sections():
+ logger.debug("FOUND CATEGORY = '%s'" % category)
+
+ for (key, value) in config.items(category):
+ self._insertKey(category + '|' + key, value)
+
+ def should_keep_current_value(self, key):
+ """
+ Determines whether a value for a property must be kept.
+
+ If the propery is missing, it is treated as it should be not
+ changed by the engine.
+
+ Keyword arguments:
+ key -- The key to search for.
+ """
+ # helps to distinguish from "empty" value which is used to indicate
+ # "removal"
+ return key not in self
+
+ def should_remove_current_value(self, key):
+ """
+ Determines whether a value for the property must be removed.
+
+ If the specified key is empty, it is treated as it should be
+ removed by the engine.
+
+ Return true if the value can be removed, false otherwise.
+
+ Keyword arguments:
+ key -- The key to search for.
+ """
+ # helps to distinguish from "missing" value which is used to indicate
+ # "keeping unchanged"
+ if key in self:
+ return not bool(self[key])
+ else:
+ return False
+
+ def get_count_with_prefix(self, prefix):
+ """
+ Return the total count of keys that start with the specified prefix.
+
+ Keyword arguments:
+ prefix -- prefix of the key
+ """
+ return len([key for key in self if key.startswith(prefix)])