Skip to content

Commit

Permalink
refactor: Move parsing of overlay ppa config to functions.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fnordahl committed Oct 22, 2024
1 parent bef1bdb commit 2fa70d8
Showing 1 changed file with 67 additions and 23 deletions.
90 changes: 67 additions & 23 deletions zaza/utilities/deployment_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]"
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:
Expand All @@ -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'):
Expand All @@ -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))
Expand Down

0 comments on commit 2fa70d8

Please sign in to comment.