From 2fa70d842968003207a723191fb3e49d52441da4 Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Tue, 22 Oct 2024 16:36:26 +0200 Subject: [PATCH] refactor: Move parsing of overlay ppa config to functions. The overlay ppa config has multiple uses, move parsing from the function that builds model-level cloud-init user data to separate functions. Signed-off-by: Frode Nordahl --- zaza/utilities/deployment_env.py | 90 ++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/zaza/utilities/deployment_env.py b/zaza/utilities/deployment_env.py index 1631627d..5ea73e6e 100644 --- a/zaza/utilities/deployment_env.py +++ b/zaza/utilities/deployment_env.py @@ -92,6 +92,61 @@ def get_overlay_ppas(model_alias='default_alias'): may be used to specify one or more PPAs that will be enabled for all units in the model. + Please refer to docstring of parse_overlay_ppa function for more + information. + + :param model_alias: Name of model alias, defaults to 'default_alias'. + :type model_alias: Option[str] + :returns: List of overlay PPAs. + :rtype: Option[List[Any]] + """ + config = zaza.global_options.get_options() + try: + return config[model_alias].overlay_ppas + except KeyError: + try: + return config.overlay_ppas + except KeyError: + pass + return None + + +def _parse_overlay_ppa_v1(overlay_ppa): + """Parse simple ppa:xxx/yyy formatted overlay_ppa config. + + Example YAML excerpt: + overlay_ppas: + - ppa:ubuntu-security-proposed/ppa + + :param overlay_ppa: Element of overlay_ppas configuration. + :type overlay_ppa: str + """ + return {'source': overlay_ppa} + + +def _parse_overlay_ppa_v2(overlay_ppa): + """Parse advanced format overlay_ppa config. + + Example YAML excerpt: + overlay_ppas: + - source: "deb https://user:pass@private-ppa.launchpad" + key: | + -----BEGIN PGP PUBLIC KEY BLOCK----- + .... + -----END PGP PUBLIC KEY BLOCK----- + :param overlay_ppa: Element of overlay_ppas configuration. + :type overlay_ppa: Dict[str,str] + :returns: Parsed PPA configuration. + :rtype: Dict[str,str] + :raises: TypeError + """ + return {'source': overlay_ppa['source'], + 'key': overlay_ppa['key']} + + +def parse_overlay_ppa(overlay_ppa): + """Parse multiple versions of overlay_ppas elements. + The tests_options section needs to look like: tests_options: @@ -117,20 +172,17 @@ def get_overlay_ppas(model_alias='default_alias'): .... -----END PGP PUBLIC KEY BLOCK----- - :param model: Name of model alias - :type bundle: str - :returns: List of overlay PPAs - :rtype: list[str] + :param overlay_ppa: Element of overlay_ppas configuration. + :type overlay_ppa: Any + :returns: Parsed overlay configuration. + :rtype: Dict[str,str] """ - config = zaza.global_options.get_options() + if not isinstance(overlay_ppa, list): + raise ValueError try: - return config[model_alias].overlay_ppas - except KeyError: - try: - return config.overlay_ppas - except KeyError: - pass - return None + return _parse_overlay_ppa_v2(overlay_ppa) + except (TypeError): + return _parse_overlay_ppa_v1(overlay_ppa) def get_cloudinit_userdata(model_alias='default_alias'): @@ -157,17 +209,9 @@ def get_cloudinit_userdata(model_alias='default_alias'): overlay_ppas = get_overlay_ppas(model_alias) if overlay_ppas: for index, overlay_ppa in enumerate(overlay_ppas): - try: - # NOTE: support private PPAs with source and key keys. - cloud_config['apt']['sources']["overlay-ppa-{}".format(index)] = { # noqa - 'source': overlay_ppa['source'], - 'key': overlay_ppa['key'], - } - except (KeyError, TypeError): - # NOTE: simple ppa:xxx/yyy format for backwards compatibility - cloud_config['apt']['sources']["overlay-ppa-{}".format(index)] = { # noqa - 'source': overlay_ppa - } + cloud_config["apt"]["sources"][ + "overlay-ppa-{}".format(index) + ] = parse_overlay_ppa(overlay_ppa) cloudinit_userdata = "#cloud-config\n{}".format( yaml.safe_dump(cloud_config))