Skip to content
Snippets Groups Projects
Commit e3117b17 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Allow child environments to remove modules.

The case which prompted this change:
parent contains hugonikanor-letsencrypt, child instead wants the
(unrelated) module puppetlabs-letsencrypt. Since these collide
hugonikanor-letsencrypt must be removed before the new one can be added.

Removal of modules only works on explicitly noted, and those modules
might still be pulled in as dependencies.
parent c26cf8f0
Branches
No related tags found
No related merge requests found
...@@ -194,16 +194,33 @@ def parse_puppetfile(data: dict[str, Any], ...@@ -194,16 +194,33 @@ def parse_puppetfile(data: dict[str, Any],
"""Parse data from puppetfile dictionary.""" """Parse data from puppetfile dictionary."""
pf = PuppetFile() pf = PuppetFile()
if subpath := data.get('include'):
if not path:
raise ValueError('include only possible when we have a source file')
inc_path = os.path.join(os.path.dirname(path), subpath)
parent = load_puppetfile(inc_path)
pf.include(parent)
if config: if config:
pf.config = config pf.config = config
removed: list[str] = []
for module in data['modules']: for module in data['modules']:
if config: if config:
module['config'] = config module['config'] = config
m = parse_module_dict(module) m = parse_module_dict(module)
if m == 'remove':
removed.append(module['name'])
else:
m.explicit = True m.explicit = True
pf.modules[m.name] = m pf.modules[m.name] = m
for r in removed:
# This WILL fail if we try to remove a non-added module.
# Possibly catch this and instead throw a better error message
# that this is by design.
del pf.modules[r]
if env := data.get('environment'): if env := data.get('environment'):
pf.environment_name = env pf.environment_name = env
elif path: elif path:
...@@ -215,13 +232,6 @@ def parse_puppetfile(data: dict[str, Any], ...@@ -215,13 +232,6 @@ def parse_puppetfile(data: dict[str, Any],
if hiera := data.get('hiera'): if hiera := data.get('hiera'):
pf.hiera = hiera pf.hiera = hiera
if subpath := data.get('include'):
if not path:
raise ValueError('include only possible when we have a source file')
path = os.path.join(os.path.dirname(path), subpath)
parent = load_puppetfile(path)
pf.include(parent)
return pf return pf
......
...@@ -16,16 +16,21 @@ The implementations are in ...@@ -16,16 +16,21 @@ The implementations are in
from .base import PuppetModule from .base import PuppetModule
from .git import GitPuppetModule from .git import GitPuppetModule
from .forge import ForgePuppetModule from .forge import ForgePuppetModule
from typing import (
Literal,
)
def parse_module_dict(d: dict) -> PuppetModule: def parse_module_dict(d: dict) -> PuppetModule | Literal['remove']:
""" """
Parse dict describing module into module object. Parse dict describing module into module object.
Currently, it becomes a `GitPuppetModule` if the `git` key is Currently, it becomes a `GitPuppetModule` if the `git` key is
present, and a `ForgePuppetModule` otherwise. present, and a `ForgePuppetModule` otherwise.
""" """
if 'git' in d: if 'remove' in d and d['remove']:
return 'remove'
elif 'git' in d:
return GitPuppetModule(**d) return GitPuppetModule(**d)
else: else:
return ForgePuppetModule(**d) return ForgePuppetModule(**d)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment