]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Reading configuration
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 20 Mar 2018 17:05:08 +0000 (18:05 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 20 Mar 2018 17:05:08 +0000 (18:05 +0100)
lib/cr_vmware_tpl/app.py
lib/cr_vmware_tpl/config.py

index f0e737249dfdfff8b54c933afb15aa28407e69e6..67dfc6797ec4994d1e9c6bb8187e9bc03582c5a0 100644 (file)
@@ -32,7 +32,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-__version__ = '0.2.2'
+__version__ = '0.2.3'
 LOG = logging.getLogger(__name__)
 
 
@@ -388,8 +388,12 @@ class CrTplApplication(PpBaseObject):
 
         """
 
-        self.perform_arg_parser()
         self.init_logging()
+        self.config.read()
+        if self.config.verbose > self.verbose:
+            self.verbose = self.config.verbose
+
+        self.perform_arg_parser()
 
         if not self.config.password:
             prompt = 'Enter password for host {h!r} and user {u!r}: '.format(
index ee56dc66263b68c0e3156889e0d1a7e8ae0db0c4..0e3b4445030dbb7b10d55326097e6db20a98f92c 100644 (file)
@@ -26,7 +26,7 @@ from .errors import FunctionNotImplementedError, PpError
 
 from .obj import PpBaseObject
 
-__version__ = '0.1.0'
+__version__ = '0.2.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -65,6 +65,8 @@ class CrTplConfiguration(PpBaseObject):
         self.template_vm = self.default_template_vm
         self.template_name = self.default_template_name
 
+        self.encoding = 'utf-8'
+
         super(CrTplConfiguration, self).__init__(
             appname=appname,
             verbose=verbose,
@@ -73,6 +75,9 @@ class CrTplConfiguration(PpBaseObject):
             initialized=False,
         )
 
+        self.config_dir = os.path.join(self.base_dir, 'etc')
+        self.config_file = os.path.join(self.config_dir, self.appname + '.ini')
+
         # Workaround, bis das Lesen der Config implementiert ist
         self.vsphere_host = 'test-vcsa01.pixelpark.net'
         self.vsphere_user = 'Administrator@vsphere.local'
@@ -102,6 +107,98 @@ class CrTplConfiguration(PpBaseObject):
 
         return res
 
+    # -------------------------------------------------------------------------
+    def read(self):
+        """Reading the configuration file."""
+
+        if self.verbose > 2:
+            LOG.debug("Searching for {!r} ...".format(self.config_file))
+        if not os.path.isfile(self.config_file):
+            LOG.warn("Config file {!r} not found.".format(self.config_file))
+            return
+
+        open_opts = {}
+        if six.PY3 and self.encoding:
+            open_opts['encoding'] = self.encoding
+            open_opts['errors'] = 'surrogateescape'
+
+        if self.verbose > 1:
+            LOG.debug("Reading {!r} ...".format(self.config_file))
+
+        config = configparser.ConfigParser()
+        try:
+            with open(self.config_file, 'r', **open_opts) as fh:
+                stream = StringIO("[default]\n" + fh.read())
+                if six.PY2:
+                    config.readfp(stream)
+                else:
+                    config.read_file(stream)
+        except ConfigParseError as e:
+            msg = "Wrong configuration in {!r} found: ".format(cfg_file)
+            msg += str(e)
+            self.handle_error(msg, "Configuration error")
+            return
+
+        self.eval_config(config)
+
+    # -------------------------------------------------------------------------
+    def eval_config(self, config):
+        """Evaluating of all found configuration options."""
+
+        for section in config.sections():
+
+            if self.verbose > 2:
+                LOG.debug("Options of section {!r} ...".format(section))
+
+            if section.lower() == 'default' or if section.lower() == 'global':
+
+                for (key, value) in config.items(section):
+
+                    if self.verbose > 2:
+                        LOG.debug("Key {k!r}, value {v!r}.".format(k=key, v=value))
+
+                    if key.lower() == 'verbose':
+                        val = int(value)
+                        if val > self.verbose:
+                            self.verbose = val
+
+
+            if section.lower() == 'vsphere':
+
+                for (key, value) in config.items(section):
+
+                    if self.verbose > 2:
+                        LOG.debug("Key {k!r}, value {v!r}.".format(k=key, v=value))
+
+                    if key.lower() == 'host':
+                        self.vsphere_host = value
+                        continue
+                    if key.lower() == 'port':
+                        self.vsphere_port = int(value)
+                        continue
+                    if key.lower() == 'user':
+                        self.vsphere_user = value
+                        continue
+                    if key.lower() == 'password':
+                        self.password = value
+                        continue
+                    if key.lower() == 'folder':
+                        self.folder = value
+
+                continue
+
+            if section.lower() == 'template':
+
+                for (key, value) in config.items(section):
+                    if self.verbose > 2:
+                        LOG.debug("Key {k!r}, value {v!r}.".format(k=key, v=value))
+
+                    if key.lower() == 'vm':
+                        self.template_vm = value
+                        continue
+                    if key.lower() == 'name':
+                        self.template_name = value
+
 
 # =============================================================================