]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Defining common Puppet command line options.
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2023 09:50:31 +0000 (10:50 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2023 09:50:31 +0000 (10:50 +0100)
lib/dpx_puppettools/app/__init__.py
lib/dpx_puppettools/app/get_forge_module.py

index 93b2640417373876b77a43b7f831e950ef03a981..ecc413f129a3e853a676c2fa01b787ce4fd76622 100644 (file)
@@ -14,6 +14,8 @@ import pipes
 import os
 import shutil
 
+from pathlib import Path
+
 from email.mime.text import MIMEText
 from email import charset
 
@@ -27,10 +29,11 @@ from fb_tools.cfg_app import FbConfigApplication
 from fb_tools.errors import FbAppError
 from fb_tools.xlate import format_list
 from fb_tools import MailAddress
+from fb_tools.argparse_actions import DirectoryOptionAction
 
 # Own modules
 from .. import __version__ as GLOBAL_VERSION
-from .. import DEFAULT_CONFIG_DIR, DEFAULT_ENVIRONMENTS_ROOT_DIR
+from .. import DEFAULT_CONFIG_DIR
 from .. import DEFAULT_TERMINAL_WIDTH, DEFAULT_TERMINAL_HEIGHT
 from .. import pp
 
@@ -45,7 +48,7 @@ LOG = logging.getLogger(__name__)
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.4.0'
+__version__ = '0.4.1'
 
 
 # =============================================================================
@@ -73,8 +76,6 @@ class BaseDPXPuppetApplication(FbConfigApplication):
 
     charset.add_charset('utf-8', charset.SHORTEST, charset.QP)
 
-    default_env_root_dir = DEFAULT_ENVIRONMENTS_ROOT_DIR
-
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=GLOBAL_VERSION, base_dir=None, quiet=False,
@@ -83,7 +84,8 @@ class BaseDPXPuppetApplication(FbConfigApplication):
             additional_stems=None, additional_cfgdirs=None, cfg_encoding=DEFAULT_ENCODING,
             config_dir=DEFAULT_CONFIG_DIR):
 
-        self._env_root_dir = self.default_env_root_dir
+        self._env_root_dir = DpxPuppetConfig.default_env_root_dir
+        self._var_dir = DpxPuppetConfig.default_var_dir
         self.environments = []
 
         super(BaseDPXPuppetApplication, self).__init__(
@@ -104,7 +106,10 @@ class BaseDPXPuppetApplication(FbConfigApplication):
     @env_root_dir.setter
     def env_root_dir(self, value):
         if value is None:
-            self._env_root_dir = None
+            if hasattr(self, 'cfg') and self.cfg:
+                self._env_root_dir = self.cfg.env_root_dir
+            else:
+                self._env_root_dir = DpxPuppetConfig.default_env_root_dir
             return
 
         val = Path(value)
@@ -115,6 +120,27 @@ class BaseDPXPuppetApplication(FbConfigApplication):
             raise ValueError(msg)
         self._env_root_dir = val
 
+    # -------------------------------------------------------------------------
+    @property
+    def var_dir(self):
+        """The root directory of all puppet environments."""
+        return self._var_dir
+
+    @var_dir.setter
+    def var_dir(self, value):
+        if value is None:
+            if hasattr(self, 'cfg') and self.cfg:
+                self._var_dir = self.cfg.var_dir
+            else:
+                self._var_dir = DpxPuppetConfig.default_var_dir
+            return
+
+        val = Path(value)
+        if not val.is_absolute():
+            msg = _("Path {!r} for the var directory must be an absolute path.").format(value)
+            raise ValueError(msg)
+        self._var_dir = val
+
     # -------------------------------------------------------------------------
     def as_dict(self, short=True):
         """
@@ -128,9 +154,106 @@ class BaseDPXPuppetApplication(FbConfigApplication):
 
         res['default_env_root_dir'] = self.default_env_root_dir
         res['env_root_dir'] = self.env_root_dir
+        res['var_dir'] = self.env_root_dir
 
         return res
 
+    # -------------------------------------------------------------------------
+    def init_arg_parser(self):
+        """
+        Public available method to initiate the argument parser.
+        """
+
+        puppet_group = self.arg_parser.add_argument_group(_('Common options for Puppet'))
+
+        help_txt = _(
+            "The directory containing the checked out r10k directories. Default: {!r}.").format(
+             DpxPuppetConfig.default_env_root_dir)
+        puppet_group.add_argument(
+            '--env-root-dir', dest='env_root_dir', metavar=_('DIRECTORY'),
+            action=DirectoryOptionAction, must_exists=True, help=help_txt,
+        )
+
+        help_txt = _(
+            "The directory containing variable data from DPX puppet tools. "
+            "Cache files for the forge information are read and written from the sub "
+            "directory {fd!r} below it. iCaching Git repositories are cloned in the "
+            "sub directory {gd!r} below it. Default: {vd!r}.").format(
+            vd=str(DpxPuppetConfig.default_var_dir), fd='forge', gd='git')
+        puppet_group.add_argument(
+            '--var-dir', dest='var_dir', metavar=_('DIRECTORY'),
+            action=DirectoryOptionAction, must_exists=True, help=help_txt,
+        )
+
+        mail_group = self.arg_parser.add_argument_group(_('Mailing options'))
+
+        mail_from = DpxPuppetConfig.default_mail_from_complete
+        mail_method = DpxPuppetConfig.default_mail_method
+        mail_server = DpxPuppetConfig.default_mail_server
+        smtp_port = DpxPuppetConfig.default_smtp_port
+
+        if self.cfg:
+            mail_from = self.cfg.mail_from
+            mail_method = self.cfg.mail_method
+            mail_server = self.cfg.mail_server
+            smtp_port = self.cfg.smtp_port
+
+        mail_group.add_argument(
+            '--from', '--mail-from',
+            metavar=_("ADDRESS"), dest="mail_from",
+            help=_(
+                "Sender mail address for mails generated by this script. "
+                "Default: {!r}").format(mail_from),
+        )
+
+        mail_group.add_argument(
+            '--recipients', '--mail-recipients',
+            metavar=_("ADDRESS"), nargs='+', dest="mail_recipients",
+            help=_("Mail addresses of all recipients for mails generated by this script.")
+        )
+
+        mail_group.add_argument(
+            '--cc', '--mail-cc',
+            metavar=_("ADDRESS"), nargs='*', dest="mail_cc",
+            help=_("Mail addresses of all CC recipients for mails generated by this script.")
+        )
+
+        mail_group.add_argument(
+            '--reply-to', '--mail-reply-to',
+            metavar=_("ADDRESS"), dest="mail_reply_to",
+            help=_("Reply mail address for mails generated by this script.")
+        )
+
+        methods = DpxPuppetConfig.valid_mail_methods
+        method_list = format_list(methods, do_repr=True)
+        mail_group.add_argument(
+            '--mail-method',
+            metavar=_("METHOD"), choices=methods, dest="mail_method",
+            help=_(
+                "Method for sending the mails generated by this script. "
+                "Valid values: {v}, default: {d!r}.").format(
+                    v=method_list, d=mail_method)
+        )
+
+        mail_group.add_argument(
+            '--mail-server',
+            metavar=_("SERVER"), dest="mail_server",
+            help=_(
+                "Mail server for submitting generated by this script if "
+                "the mail method of this script is 'smtp'. Default: {!r}.").format(mail_server)
+        )
+
+        mail_group.add_argument(
+            '--smtp-port',
+            metavar=_("PORT"), type=int, dest='smtp_port', what="SMTP",
+            action=PortOptionAction,
+            help=_(
+                "The port to use for submitting generated by this script if "
+                "the mail method of this script is 'smtp'. Default: {}.").format(smtp_port)
+        )
+
+        super(BaseDPXPuppetApplication, self).init_arg_parser()
+
     # -------------------------------------------------------------------------
     def post_init(self):
         """
@@ -153,6 +276,18 @@ class BaseDPXPuppetApplication(FbConfigApplication):
         else:
             LOG.debug(_("Don't using a logfile."))
 
+        var_dir = getattr(self.args, 'var_dir', None)
+        if var_dir:
+            self.var_dir = var_dir
+        else:
+            self.var_dir = self.cfg.var_dir
+
+        env_root_dir = getattr(self.args, 'env_root_dir', None)
+        if env_root_dir:
+            self.env_root_dir = env_root_dir
+        else:
+            self.env_root_dir = self.cfg.env_root_dir
+
         v = getattr(self.args, 'mail_method', None)
         if v:
             self.cfg.mail_method = v
@@ -254,81 +389,6 @@ class BaseDPXPuppetApplication(FbConfigApplication):
 
         self.cfg.reply_to = v
 
-    # -------------------------------------------------------------------------
-    def init_arg_parser(self):
-        """
-        Public available method to initiate the argument parser.
-        """
-
-        super(BaseDPXPuppetApplication, self).init_arg_parser()
-
-        mail_group = self.arg_parser.add_argument_group(_('Mailing options'))
-
-        mail_from = DpxPuppetConfig.default_mail_from_complete
-        mail_method = DpxPuppetConfig.default_mail_method
-        mail_server = DpxPuppetConfig.default_mail_server
-        smtp_port = DpxPuppetConfig.default_smtp_port
-
-        if self.cfg:
-            mail_from = self.cfg.mail_from
-            mail_method = self.cfg.mail_method
-            mail_server = self.cfg.mail_server
-            smtp_port = self.cfg.smtp_port
-
-        mail_group.add_argument(
-            '--from', '--mail-from',
-            metavar=_("ADDRESS"), dest="mail_from",
-            help=_(
-                "Sender mail address for mails generated by this script. "
-                "Default: {!r}").format(mail_from),
-        )
-
-        mail_group.add_argument(
-            '--recipients', '--mail-recipients',
-            metavar=_("ADDRESS"), nargs='+', dest="mail_recipients",
-            help=_("Mail addresses of all recipients for mails generated by this script.")
-        )
-
-        mail_group.add_argument(
-            '--cc', '--mail-cc',
-            metavar=_("ADDRESS"), nargs='*', dest="mail_cc",
-            help=_("Mail addresses of all CC recipients for mails generated by this script.")
-        )
-
-        mail_group.add_argument(
-            '--reply-to', '--mail-reply-to',
-            metavar=_("ADDRESS"), dest="mail_reply_to",
-            help=_("Reply mail address for mails generated by this script.")
-        )
-
-        methods = DpxPuppetConfig.valid_mail_methods
-        method_list = format_list(methods, do_repr=True)
-        mail_group.add_argument(
-            '--mail-method',
-            metavar=_("METHOD"), choices=methods, dest="mail_method",
-            help=_(
-                "Method for sending the mails generated by this script. "
-                "Valid values: {v}, default: {d!r}.").format(
-                    v=method_list, d=mail_method)
-        )
-
-        mail_group.add_argument(
-            '--mail-server',
-            metavar=_("SERVER"), dest="mail_server",
-            help=_(
-                "Mail server for submitting generated by this script if "
-                "the mail method of this script is 'smtp'. Default: {!r}.").format(mail_server)
-        )
-
-        mail_group.add_argument(
-            '--smtp-port',
-            metavar=_("PORT"), type=int, dest='smtp_port', what="SMTP",
-            action=PortOptionAction,
-            help=_(
-                "The port to use for submitting generated by this script if "
-                "the mail method of this script is 'smtp'. Default: {}.").format(smtp_port)
-        )
-
     # -------------------------------------------------------------------------
     def perform_arg_parser(self):
 
index 82dca051102566264c7844599271b405dbdadabe..3f776d4e3563fe88b8ecbf8dcdc4d583bdcf7a09 100644 (file)
@@ -14,7 +14,6 @@ import os
 from pathlib import Path
 
 # Third party modules
-from fb_tools.argparse_actions import DirectoryOptionAction
 
 # Own modules
 from .. import pp, DEFAULT_VAR_DIR
@@ -32,7 +31,7 @@ LOG = logging.getLogger(__name__)
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.6.3'
+__version__ = '0.6.4'
 
 
 # =============================================================================
@@ -108,15 +107,6 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
         write_cache_group.add_argument(
             '-W', '--no-write-cache', dest='no_write_cache', action="store_true", help=help_txt)
 
-        help_txt = _(
-            "The directory containing variable data from DPX puppet tools, Default: {vd!r}. "
-            "Cache files for the forge information are read and written from the sub "
-            "directory {sd!r} below it.").format(vd=str(DEFAULT_VAR_DIR), sd='forge')
-        forge_group.add_argument(
-            '--dir', '--var-dir', dest='var_dir', metavar=_('DIRECTORY'),
-            action=DirectoryOptionAction, must_exists=True, help=help_txt,
-        )
-
         help_txt = _("Show more details of the forge module.")
         forge_group.add_argument(
             '-D', '--details', dest="details", action="store_true", help=help_txt)
@@ -152,16 +142,11 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
             if no_write_cache:
                 self.write_cache = False
 
-        var_dir = getattr(self.args, 'var_dir', None)
-        if var_dir:
-            if self.wite_cache:
-                if not os.access(str(var_dir), os.W_OK):
-                    msg = _("The given var directory {!r} is not writeable.").format(
-                        str(var_dir))
-                    LOG.error(msg)
-                    self.exit(1)
-        else:
-            var_dir = self.cfg.var_dir
+        if self.write_cache:
+            if not os.access(str(self.var_dir), os.W_OK):
+                msg = _("The  var directory {!r} is not writeable.").format(str(self.var_dir))
+                LOG.error(msg)
+                self.exit(1)
 
         self.details = getattr(self.args, 'details', False)
 
@@ -171,7 +156,7 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
 
         self.module_info = ForgeModuleInfo(
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
-            full_name=self.module_name, var_dir=var_dir, initialized=True,)
+            full_name=self.module_name, var_dir=self.var_dir, initialized=True,)
 
         LOG.debug(_("Finished creating a {} object.").format('ForgeModuleInfo'))