-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
1 changed file
with
67 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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)) | ||
|