From 8b8ea31dc32a1de0f7cf471a058daec6600a0516 Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 3 Mar 2022 13:23:51 -0500 Subject: [PATCH] Remove pkgbuilder help text from repomgr commands Repomgr commands are often forwarded into the package builder for execution. They are run under the user's id, and a help text is displayed as if the 'stx control enter' command had been used. That help text is confusing in this context. The mechanism to run these commands failed to distinguish 'user' from 'interactive'. This update adds that distinction. Only an interactive session should display the help text. Repomgr will use the non-interactive method, avoiding the unwanted help text. This update also adds some missing repomgr sub-commands supported by the repo_manage.py back end, e.g. search_pkg. It also fixes arguement passing so that --help functions as expected on sub-commands. NOTE: While testing, I noticed that two recently added repomgr commands, 'merge' and 'search_pkg', available within the build container, were reflected in the external wrapper. I've added them Testing stx control enter ... displays help text stx repomgr list ... shows repo list without help text stx repomgr search_pkg --help ... command is valid and help functions as expected stx repomgr merge --help command is valid and help functions as expected Story: 2008862 Task: 44683 Signed-off-by: Scott Little Change-Id: Iad96b3f93ce15c52837ddebed16b9f67899ea27c --- stx/lib/stx/k8s.py | 13 ++++++++++--- stx/lib/stx/stx_build.py | 2 +- stx/lib/stx/stx_main.py | 10 ++++++---- stx/lib/stx/stx_repomgr.py | 9 ++++++--- stx/toCOPY/builder/userenv | 3 ++- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/stx/lib/stx/k8s.py b/stx/lib/stx/k8s.py index 727d25702..263204a08 100644 --- a/stx/lib/stx/k8s.py +++ b/stx/lib/stx/k8s.py @@ -71,20 +71,27 @@ class KubeHelper: else: return False - def generatePrefixCommand(self, podname, command, enableuser): + def generatePrefixCommand(self, podname, command, enableuser, interactive=False): '''Generate the command executed in the host''' prefix_exec_cmd = self.config.kubectl() + ' exec -ti ' builder_exec_cmd = prefix_exec_cmd + podname prefix_bash_cmd = ' -- bash -l -c ' - prefix_bash_with_user_cmd = ' -- bash -l -c \'sudo -u ${MYUNAME} bash \ + prefix_bash_with_user_cmd = ' -- bash -l -c \'sudo -u ${MYUNAME} \ + BASH_ENV=/home/$MYUNAME/userenv bash --rcfile /home/$MYUNAME/userenv -c ' + prefix_bash_with_interactive_user_cmd = ' -- bash -l -i -c \'sudo -u ${MYUNAME} bash \ --rcfile /home/$MYUNAME/userenv -i -c ' builder_exec_bash_cmd = builder_exec_cmd + prefix_bash_cmd builder_exec_bash_with_user_cmd = builder_exec_cmd + \ prefix_bash_with_user_cmd + builder_exec_bash_with_interactive_user_cmd = builder_exec_cmd + \ + prefix_bash_with_interactive_user_cmd if enableuser: - cmd = builder_exec_bash_with_user_cmd + command + if interactive: + cmd = builder_exec_bash_with_interactive_user_cmd + command + else: + cmd = builder_exec_bash_with_user_cmd + command else: cmd = builder_exec_bash_cmd + command diff --git a/stx/lib/stx/stx_build.py b/stx/lib/stx/stx_build.py index b39ba7c6c..0622fda9d 100644 --- a/stx/lib/stx/stx_build.py +++ b/stx/lib/stx/stx_build.py @@ -150,7 +150,7 @@ class HandleBuildTask: '***********************************') sys.exit(1) - prefix_cmd = self.k8s.generatePrefixCommand(podname, '', 1) + prefix_cmd = self.k8s.generatePrefixCommand(podname, '', 1, 1) if args.build_task == 'image': cmd = self.buildImageCMD(args, prefix_cmd) diff --git a/stx/lib/stx/stx_main.py b/stx/lib/stx/stx_main.py index dadd31b36..ea365bdb0 100644 --- a/stx/lib/stx/stx_main.py +++ b/stx/lib/stx/stx_main.py @@ -127,12 +127,14 @@ image.\t\teg: [ prepare|layer|image|download|world|${pkgname}]') repo_subparser = subparsers.add_parser('repomgr', help='Manage source|binary \ -packages.\t\teg: [ list|download|sync|mirror|clean|remove_repo|upload_pkg|\ -delete_pkg ]') +packages.\t\teg: [ list|download|sync|merge|mirror|clean|\ +remove_repo|search_pkg|upload_pkg|delete_pkg ]') repo_subparser.add_argument('repomgr_task', - help='[ list|download|sync|mirror|clean|\ - remove_repo|upload_pkg|delete_pkg ]: \ + help='[ list|download|sync|merge|mirror|clean|\ + remove_repo|search_pkg|upload_pkg|delete_pkg ]: \ Execute the management task.\n\n') + # Pass remaining arguements into repo_manage.py for additional processing + repo_subparser.add_argument('args', nargs=argparse.REMAINDER) repo_subparser.set_defaults(handle=self.handlerepomgr.handleCommand) parser.add_argument('-d', '--debug', diff --git a/stx/lib/stx/stx_repomgr.py b/stx/lib/stx/stx_repomgr.py index d1e1cb6e6..ed09d281a 100644 --- a/stx/lib/stx/stx_repomgr.py +++ b/stx/lib/stx/stx_repomgr.py @@ -16,6 +16,7 @@ import logging from stx.k8s import KubeHelper from stx import utils # pylint: disable=E0611 import subprocess +import sys logger = logging.getLogger('STX-Repomgr') utils.set_logger(logger) @@ -38,12 +39,14 @@ class HandleRepomgrTask: logger.error('The builder container does not exist, so please \ consider to use the control module') - prefix_cmd = self.k8s.generatePrefixCommand(podname, '', 1) - cmd = prefix_cmd + '"repo_manage.py ' + args.repomgr_task + '"\'' + prefix_cmd = self.k8s.generatePrefixCommand(podname, '', 1, 0) + cmd = prefix_cmd + ' '.join(['"repo_manage.py', args.repomgr_task, ' '.join(args.args), '"\'']) logger.debug('Manage the repo with the command [%s]', cmd) try: subprocess.check_call(cmd, shell=True) except subprocess.CalledProcessError as exc: - raise Exception('Failed to manage the repo with the command [%s].\n \ + # raise Exception('Failed to manage the repo with the command [%s].\n \ + logger.error('Failed to manage the repo with the command [%s].\n \ Returncode: %s' % (cmd, exc.returncode)) + sys.exit(2) diff --git a/stx/toCOPY/builder/userenv b/stx/toCOPY/builder/userenv index ae378ea4a..f3b62a74c 100644 --- a/stx/toCOPY/builder/userenv +++ b/stx/toCOPY/builder/userenv @@ -19,6 +19,7 @@ if [ ! -d $MY_WORKSPACE ]; then mkdir -p $MY_WORKSPACE > /dev/null 2>&1 fi +if echo $- | grep -q i ; then cat <