From: Frank Brehm Date: Tue, 20 Mar 2018 15:35:59 +0000 (+0100) Subject: Adding and using lib/cr_vmware_tpl/config.py X-Git-Tag: 0.1.1~106 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=bd863863e4bfd5356a66a49cba1546ddfca12d6a;p=pixelpark%2Fcreate-vmware-tpl.git Adding and using lib/cr_vmware_tpl/config.py --- diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 7437498..40ddf5e 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -16,6 +16,7 @@ import re import traceback import textwrap import argparse +import getpass # Third party modules @@ -29,7 +30,9 @@ from .colored import ColoredFormatter, colorstr from .obj import PpBaseObject -__version__ = '0.1.3' +from .config import CrTplConfiguration + +__version__ = '0.2.1' LOG = logging.getLogger(__name__) @@ -151,6 +154,9 @@ class CrTplApplication(PpBaseObject): self._init_env() self._perform_env() + self.config = CrTplConfiguration( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + self.post_init() # ----------------------------------------------------------- @@ -385,6 +391,11 @@ class CrTplApplication(PpBaseObject): self.perform_arg_parser() self.init_logging() + if not self.config.password: + prompt = 'Enter password for host {h!r} and user {u!r}: '.format( + h=self.config.vsphere_host, u=self.config.vsphere_user) + self.config.password = getpass.getpass(prompt=prompt) + self.initialized = True # ------------------------------------------------------------------------- @@ -556,7 +567,50 @@ class CrTplApplication(PpBaseObject): """ - pass + vmware_group = self.arg_parser.add_argument_group('VMWare options') + + vmware_group.add_argument( + '-H', '--host', dest='host', + help="Remote vSphere host to connect to (Default: {!r}).".format( + CrTplConfiguration.default_vsphere_host) + ) + + vmware_group.add_argument( + '-p', '--port', dest='port', type=int, + help="Port on vSphere host to connect on (Default: {}).".format( + CrTplConfiguration.default_vsphere_port) + ) + + vmware_group.add_argument( + '-U', '--user', dest='user', + help="User name to use when connecting to vSphere host (Default: {!r}).".format( + CrTplConfiguration.default_vsphere_user) + ) + + vmware_group.add_argument( + '-P', '--password', dest='password', + help="Password to use when connecting to vSphere host.", + ) + + vmware_group.add_argument( + '-F', '--folder', dest='folder', + help="Folder in vSphere, where to create the template (Default: {!r}).".format( + CrTplConfiguration.default_template_vm) + ) + + vmware_group.add_argument( + '-M', '--vm', dest='vm', + help=( + "The temporary VM, which will be created and converted into a " + "template (Default: {!r}).").format(CrTplConfiguration.default_template_vm) + ) + + vmware_group.add_argument( + '-T', '--template', + help=( + "The name of the created template as result of this script " + "(Default: {!r}).").format(CrTplConfiguration.default_template_name) + ) # ------------------------------------------------------------------------- def _perform_arg_parser(self): diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py new file mode 100644 index 0000000..ee56dc6 --- /dev/null +++ b/lib/cr_vmware_tpl/config.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2018 by Frank Brehm, Berlin +@summary: A module for providing a configuration +""" +from __future__ import absolute_import + +# Standard module +import sys +import os +import logging + +# Third party modules +import six + +from six import StringIO +from six.moves import configparser + +from configparser import Error as ConfigParseError + +# Own modules +from .errors import FunctionNotImplementedError, PpError + +from .obj import PpBaseObject + +__version__ = '0.1.0' +LOG = logging.getLogger(__name__) + + +# ============================================================================= +class ConfigError(PpError): + """Base error class for all exceptions happened during + execution this configured application""" + + pass + + +# ============================================================================= +class CrTplConfiguration(PpBaseObject): + """ + A class for providing a configuration for the CrTplApplication class + and methods to read it from configuration files. + """ + + default_vsphere_host = 'vcs01.ppbrln.internal' + default_vsphere_port = 443 + default_vsphere_user = 'root' + default_folder = 'templates' + default_template_vm = 'template.pixelpark.com' + default_template_name = 'oracle-linux-7.4-template' + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, verbose=0, version=__version__, base_dir=None, + initialized=False): + + self.vsphere_host = self.default_vsphere_host + self.vsphere_port = self.default_vsphere_port + self.vsphere_user = self.default_vsphere_user + self.password = None + self.folder = self.default_folder + self.template_vm = self.default_template_vm + self.template_name = self.default_template_name + + super(CrTplConfiguration, self).__init__( + appname=appname, + verbose=verbose, + version=version, + base_dir=base_dir, + initialized=False, + ) + + # Workaround, bis das Lesen der Config implementiert ist + self.vsphere_host = 'test-vcsa01.pixelpark.net' + self.vsphere_user = 'Administrator@vsphere.local' + + if initialized: + self.initialized = True + + # ------------------------------------------------------------------------- + def as_dict(self, short=True): + """ + Transforms the elements of the object into a dict + + @param short: don't include local properties in resulting dict. + @type short: bool + + @return: structure as dict + @rtype: dict + """ + + res = super(CrTplConfiguration, self).as_dict(short=short) + res['default_vsphere_host'] = self.default_vsphere_host + res['default_vsphere_port'] = self.default_vsphere_port + res['default_vsphere_user'] = self.default_vsphere_user + res['default_folder'] = self.default_folder + res['default_template_vm'] = self.default_template_vm + res['default_template_name'] = self.default_template_name + + return res + + +# ============================================================================= + +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list