From f46f0d43af633da4f1097c25a9cd74e3e22da315 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 6 Jan 2022 16:27:04 +0100 Subject: [PATCH] Adding Gitlab job log sections --- lib/cr_vmware_tpl/__init__.py | 29 ++++++++++++++++++++++- lib/cr_vmware_tpl/cobbler.py | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/cr_vmware_tpl/__init__.py b/lib/cr_vmware_tpl/__init__.py index 512d689..5f3e2f2 100644 --- a/lib/cr_vmware_tpl/__init__.py +++ b/lib/cr_vmware_tpl/__init__.py @@ -1,6 +1,33 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '2.2.16' +import time + +__version__ = '2.3.0' + + +# ------------------------------------------------------------------------- +def print_section_start(name, header=None, collapsed=False): + + msg = '\x1b[0Ksection_start:{ts}:{n}'.format(ts=int(time.time()), n=name) + if collapsed: + msg += '[collapsed=true]' + msg += '\r\x1b[0K' + if header and header.strip(): + msg += header.strip() + else: + msg += name + + print(msg) + + +# ------------------------------------------------------------------------- +def print_section_end(name): + + msg = '\x1b[0Ksection_end:{ts}:{n}\r\x1b[0K'.format( + ts=int(time.time()), n=name) + + print(msg) + # vim: ts=4 et list diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 35a5fce..fd28cd3 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -36,6 +36,8 @@ from fb_tools.handling_obj import CompletedProcess from fb_tools.handler import BaseHandler +from . import print_section_start, print_section_end + from .config import CrTplConfiguration from .xlate import XLATOR @@ -463,6 +465,8 @@ class Cobbler(BaseHandler): profile = self.config.cobbler_profile LOG.info(_("Ensuring profile {!r} ...").format(profile)) + print_section_start( + 'cobbler_ensure_profile', 'Ensuring profile.', collapsed=True) profile_list = self.get_profile_list() @@ -471,6 +475,8 @@ class Cobbler(BaseHandler): else: self.add_profile() + print_section_end('cobbler_ensure_profile') + # ------------------------------------------------------------------------- def change_profile(self): """Ensure correctnes of an existing profile.""" @@ -617,8 +623,11 @@ class Cobbler(BaseHandler): remote_ks = self.config.system_ks LOG.info(_("Ensuring currentness of system kickstart script {!r}.").format( str(remote_ks))) + print_section_start( + 'ensure_system_ks', 'Ensuring currentness of system kickstart script', collapsed=True) self.ensure_remote_file(local_ks, remote_ks) + print_section_end('ensure_system_ks') # ------------------------------------------------------------------------- def ensure_snippets(self): @@ -628,6 +637,7 @@ class Cobbler(BaseHandler): LOG.info(_("Ensuring currentness of snippets below {!r}.").format( str(self.config.snippets_dir))) + print_section_start('ensure_snippets', "Ensuring currentness of snippets", collapsed=True) for local_snippet in local_snippets_dir.glob('*'): remote_snippet = self.config.snippets_dir / local_snippet.name @@ -635,6 +645,8 @@ class Cobbler(BaseHandler): loc=str(local_snippet), rem=str(remote_snippet))) self.ensure_remote_file(local_snippet, remote_snippet, check_parent=False) + print_section_end('ensure_snippets') + # ------------------------------------------------------------------------- def ensure_bashrc(self): @@ -643,6 +655,8 @@ class Cobbler(BaseHandler): remote_dir = docroot / self.config.system_status LOG.info(_("Ensuring currentness of bashrc files.")) + print_section_start( + 'ensure_bashrc', 'Ensuring currentness of bashrc files.', collapsed=True) for local_rc_file in files_dir.glob('bashrc*'): remote_rc_file = remote_dir / local_rc_file.name @@ -650,6 +664,8 @@ class Cobbler(BaseHandler): loc=str(local_rc_file), rem=str(remote_rc_file))) self.ensure_remote_file(local_rc_file, remote_rc_file, check_parent=False) + print_section_end('ensure_bashrc') + # ------------------------------------------------------------------------- def ensure_vimrc(self): @@ -658,6 +674,8 @@ class Cobbler(BaseHandler): remote_dir = docroot / self.config.system_status LOG.info(_("Ensuring currentness of vimrc files.")) + print_section_start( + 'ensure_vimrc', "Ensuring currentness of vimrc files.", collapsed=True) for local_rc_file in files_dir.glob('vimrc*'): remote_rc_file = remote_dir / local_rc_file.name @@ -665,6 +683,8 @@ class Cobbler(BaseHandler): loc=str(local_rc_file), rem=str(remote_rc_file))) self.ensure_remote_file(local_rc_file, remote_rc_file, check_parent=False) + print_section_end('ensure_vimrc') + # ------------------------------------------------------------------------- def ensure_logrotate_files(self): @@ -673,6 +693,9 @@ class Cobbler(BaseHandler): remote_dir = docroot / self.config.system_status LOG.info(_("Ensuring currentness of logrotate files.")) + print_section_start( + 'ensure_logrotate_files', "Ensuring currentness of logrotate files.", + collapsed=True) for local_file in files_dir.glob('logrotate*'): remote_file = remote_dir / local_file.name @@ -680,6 +703,8 @@ class Cobbler(BaseHandler): loc=str(local_file), rem=str(remote_file))) self.ensure_remote_file(local_file, remote_file, check_parent=False) + print_section_end('ensure_logrotate_files') + # ------------------------------------------------------------------------- def ensure_create_motd(self): @@ -689,11 +714,14 @@ class Cobbler(BaseHandler): remote_script = remote_dir / local_script.name LOG.info(_("Ensuring currentness of create-motd.sh.")) + print_section_start('ensure_create_motd', "Ensuring currentness of create-motd.sh.") LOG.debug(_("Ensuring {loc!r} => {rem!r}.").format( loc=str(local_script), rem=str(remote_script))) self.ensure_remote_file(local_script, remote_script, check_parent=False) + print_section_end('ensure_create_motd') + # ------------------------------------------------------------------------- def add_system(self, name, fqdn, mac_address, comment=None): """Creating a new system.""" @@ -702,6 +730,7 @@ class Cobbler(BaseHandler): os_id = self.config.os_id LOG.info(_("Creating new system {!r} ...").format(name)) + print_section_start('add_system', "Creating new system ...", collapsed=True) if not comment: comment = "VMWare template for creating a {} system.".format(os_id) @@ -753,8 +782,10 @@ class Cobbler(BaseHandler): err = proc.stdout msg = _("Error creating a cobbler system - returncode was {rc}: {err}").format( rc=proc.returncode, err=err) + print_section_end('add_system') raise ExpectedCobblerError(msg) + print_section_end('add_system') self.sync() # ------------------------------------------------------------------------- @@ -762,6 +793,7 @@ class Cobbler(BaseHandler): """Removing the given system.""" LOG.info(_("Removing system {!r} ...").format(name)) + print_section_start('remove_system', "Creating new system ...", collapsed=True) args = ['system', 'remove'] args.append('--name') @@ -777,8 +809,10 @@ class Cobbler(BaseHandler): err = proc.stdout msg = _("Error removing the cobbler system {n!r} - returncode was {rc}: {err}").format( n=name, rc=proc.returncode, err=err) + print_section_end('remove_system') raise ExpectedCobblerError(msg) + print_section_end('remove_system') self.sync() # ------------------------------------------------------------------------- @@ -814,8 +848,11 @@ class Cobbler(BaseHandler): LOG.info(_("Ensuring currentness of authorized_keys file of root {!r}.").format( str(remote_file))) + print_section_start( + 'ensure_keys', "Ensuring authorized_keys of root.", collapsed=True) self.ensure_remote_directory(remote_dir) self.ensure_remote_file(auth_keys_file, remote_file, check_parent=False) + print_section_end('ensure_keys') # ------------------------------------------------------------------------- def ensure_repo_files(self): @@ -825,6 +862,8 @@ class Cobbler(BaseHandler): remote_dir = docroot / self.config.system_status / 'repos' LOG.info(_("Ensuring currentness of repo files below {!r}.").format(str(files_dir))) + print_section_start( + 'ensure_repo_files', "Ensuring repo files.", collapsed=True) for local_repo_dir in files_dir.glob('repos-*'): if not local_repo_dir.is_dir(): @@ -840,6 +879,7 @@ class Cobbler(BaseHandler): self.ensure_remote_file(local_repo_file, remote_file, check_parent=False) LOG.debug(_("Finished with repo files.")) + print_section_end('ensure_repo_files') # ------------------------------------------------------------------------- def ensure_postfix_files(self): @@ -849,6 +889,8 @@ class Cobbler(BaseHandler): remote_dir = docroot / self.config.system_status / 'postfix' LOG.info(_("Ensuring currentness of postfix files below {!r}.").format(str(files_dir))) + print_section_start( + 'ensure_postfix_files', "Ensuring postfix files.", collapsed=True) self.ensure_remote_directory(remote_dir) for local_file in files_dir.glob('*'): @@ -856,6 +898,7 @@ class Cobbler(BaseHandler): self.ensure_remote_file(local_file, remote_file, check_parent=False) LOG.debug(_("Finished with postfix files.")) + print_section_end('ensure_postfix_files') # ------------------------------------------------------------------------- def get_dhcp_ip(self, mac_address): -- 2.39.5