From 0ed3e55da6c97ddb1dda5326550604de523aff77 Mon Sep 17 00:00:00 2001 From: Davlet Panech Date: Mon, 25 Apr 2022 16:09:38 -0400 Subject: [PATCH] debian: auto-upgrade stx.conf on pod restart This patch adds any missing keys to stx.conf while restarting pods via stx-init-env. This should minimize disruption to developers when new keys are added to the reference file, stx.conf.sample. TESTS ======================== - Remove some required keys from stx.conf, then run stx-init-env and make sure they get re-added Story: 2009897 Task: 45179 Signed-off-by: Davlet Panech Change-Id: Ifbed10e01dc2e5653b827274a556f6dad166f1d8 --- stx-init-env | 1 + stx/lib/stx/stx_configparser.py | 20 ++++++++++++++++++++ stx/lib/stx/stx_main.py | 5 ++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/stx-init-env b/stx-init-env index 89b11d09..f3147aaf 100755 --- a/stx-init-env +++ b/stx-init-env @@ -271,6 +271,7 @@ fi # Restart pods notice "Restarting pods" stx control stop || exit 1 +stx config --upgrade || exit 1 stx control start || exit 1 notice "Run 'stx control status' to check the pod startup status" diff --git a/stx/lib/stx/stx_configparser.py b/stx/lib/stx/stx_configparser.py index be9fda93..cabe156e 100644 --- a/stx/lib/stx/stx_configparser.py +++ b/stx/lib/stx/stx_configparser.py @@ -124,6 +124,23 @@ class STXConfigParser: def syncConfigFile(self): self.cf.write(open(self.configpath, "w")) + def upgradeConfigFile(self): + ref_config_path = os.path.join(os.environ['PRJDIR'], "stx.conf.sample") + ref_config = configparser.ConfigParser() + ref_config.read(ref_config_path, encoding="utf-8") + for section_name, data in ref_config.items(): + if section_name == 'DEFAULT': + ref_options = ref_config.defaults() + else: + ref_options = ref_config.options(section_name) + if not self.cf.has_section(section_name): + self.cf.add_section(section_name) + for key in ref_options: + value = ref_config.get(section_name, key, raw=True) + if not self.cf.has_option(section_name, key): + self.cf.set(section_name, key, value) + self.syncConfigFile() + class HandleConfigTask: '''Handle the task for the config sub-command''' @@ -188,5 +205,8 @@ class HandleConfigTask: elif args.show is True: self.handleShow() + elif args.upgrade: + self.stxconfig.upgradeConfigFile() + else: print(helper.help_config()) diff --git a/stx/lib/stx/stx_main.py b/stx/lib/stx/stx_main.py index e000c380..605de4db 100644 --- a/stx/lib/stx/stx_main.py +++ b/stx/lib/stx/stx_main.py @@ -69,7 +69,7 @@ task.\t\teg: [start|enter|stop|status|upgrade]') config_subparser = subparsers.add_parser('config', help='Change stx config \ -settings.\t\teg: [ --show|--get|--add|--unset|--remove-section ]') +settings.\t\teg: [ --show|--get|--add|--unset|--remove-section|--upgrade ]') config_subparser.add_argument('--show', help='Show all the content of the config\ file\n\n', action='store_true') @@ -89,6 +89,9 @@ settings.\t\teg: [ --show|--get|--add|--unset|--remove-section ]') help='Remove the section from the \ config file.\n\n', nargs=1, required=False) + config_subparser.add_argument('--upgrade', + help='Upgrade stx.conf', + action='store_true') config_subparser.set_defaults(handle=self.handleconfig.handleConfig) build_subparser = subparsers.add_parser('build',