From 2bee3c5f92e47580a4b5a1849cd1ee939232fcda Mon Sep 17 00:00:00 2001 From: jk464 Date: Thu, 15 Jun 2023 10:39:56 +0100 Subject: [PATCH] Added get-dependencies option to install_pack.py --- CHANGELOG.rst | 1 + st2common/st2common/cmd/install_pack.py | 104 ++++++++++++++++-------- 2 files changed, 73 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 96941c1401..9084b01a0c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -28,6 +28,7 @@ Added * Expose environment variable ST2_ACTION_DEBUG to all StackStorm actions. Contributed by @maxfactor1 +* Added get-dependencies option to install_pack.py #5994 3.8.0 - November 18, 2022 ------------------------- diff --git a/st2common/st2common/cmd/install_pack.py b/st2common/st2common/cmd/install_pack.py index 42c2267012..a62bf1d66a 100644 --- a/st2common/st2common/cmd/install_pack.py +++ b/st2common/st2common/cmd/install_pack.py @@ -14,6 +14,7 @@ # limitations under the License. import sys +from os import listdir from oslo_config import cfg @@ -21,9 +22,11 @@ from st2common import log as logging from st2common.config import do_register_cli_opts from st2common.script_setup import setup as common_setup +from st2common.util.pack import get_pack_metadata from st2common.util.pack_management import download_pack from st2common.util.pack_management import get_and_set_proxy_config from st2common.util.virtualenvs import setup_pack_virtualenv +from st2common.content.utils import get_pack_base_path, get_packs_base_paths __all__ = ["main"] @@ -53,10 +56,77 @@ def _register_cli_opts(): help="True to force pack installation and ignore install " "lock file if it exists.", ), + cfg.BoolOpt( + "get-dependencies", + default=False, + help="True to install pack dependencies", + ), ] do_register_cli_opts(cli_opts) +def get_pack_dependencies(pack, verify_ssl, force, dependencies, proxy_config): + pack_path = get_pack_base_path(pack) + + try: + pack_metadata = get_pack_metadata(pack_dir=pack_path) + result = pack_metadata.get("dependencies", None) + if result: + LOG.info('Getting pack dependencies for pack "%s"' % (pack)) + download_packs(result, verify_ssl, force, dependencies, proxy_config) + LOG.info('Successfully got pack dependencies for pack "%s"' % (pack)) + except IOError: + LOG.error("Could not open pack.yaml at location %s" % pack_path) + result = None + + +def download_packs(packs, verify_ssl, force, dependencies, proxy_config): + packs_base_paths = get_packs_base_paths() + + for pack in packs: + for pack_dir in packs_base_paths: + if pack in listdir(pack_dir): + LOG.info('Pack (%s) already installed in "%s"' % (pack, pack_dir)) + break + else: + # 1. Download the pack + LOG.info('Installing pack "%s"' % (pack)) + result = download_pack( + pack=pack, + verify_ssl=verify_ssl, + force=force, + proxy_config=proxy_config, + force_permissions=True, + ) + + # Raw pack name excluding the version + pack_name = result[1] + success = result[2][0] + + if success: + LOG.info('Successfully installed pack "%s"' % (pack_name)) + else: + error = result[2][1] + LOG.error('Failed to install pack "%s": %s' % (pack_name, error)) + sys.exit(2) + + # 2. Setup pack virtual environment + LOG.info('Setting up virtualenv for pack "%s"' % (pack_name)) + setup_pack_virtualenv( + pack_name=pack_name, + update=False, + logger=LOG, + proxy_config=proxy_config, + no_download=True, + ) + LOG.info('Successfully set up virtualenv for pack "%s"' % (pack_name)) + + if dependencies: + get_pack_dependencies( + pack_name, verify_ssl, force, dependencies, proxy_config + ) + + def main(argv): _register_cli_opts() @@ -71,40 +141,10 @@ def main(argv): packs = cfg.CONF.pack verify_ssl = cfg.CONF.verify_ssl force = cfg.CONF.force + dependencies = cfg.CONF.get_dependencies proxy_config = get_and_set_proxy_config() - for pack in packs: - # 1. Download the pack - LOG.info('Installing pack "%s"' % (pack)) - result = download_pack( - pack=pack, - verify_ssl=verify_ssl, - force=force, - proxy_config=proxy_config, - force_permissions=True, - ) - - # Raw pack name excluding the version - pack_name = result[1] - success = result[2][0] - - if success: - LOG.info('Successfully installed pack "%s"' % (pack_name)) - else: - error = result[2][1] - LOG.error('Failed to install pack "%s": %s' % (pack_name, error)) - sys.exit(2) - - # 2. Setup pack virtual environment - LOG.info('Setting up virtualenv for pack "%s"' % (pack_name)) - setup_pack_virtualenv( - pack_name=pack_name, - update=False, - logger=LOG, - proxy_config=proxy_config, - no_download=True, - ) - LOG.info('Successfully set up virtualenv for pack "%s"' % (pack_name)) + download_packs(packs, verify_ssl, force, dependencies, proxy_config) return 0