Skip to content

Commit

Permalink
Add --remove-dependencies/--no-remove-dependencies options (#186)
Browse files Browse the repository at this point in the history
* Add --remove-dependencies/--no-remove-dependencies options

* Use '--remove-dependencies all' in deploy_ephemeral_db.sh

* Don't change this yet till new version releases on pypi
  • Loading branch information
bsquizz authored Feb 25, 2022
1 parent 3a8eb14 commit dc0b615
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
57 changes: 48 additions & 9 deletions bonfire/bonfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,21 +302,28 @@ def _validate_set_image_tag(ctx, param, value):
raise click.BadParameter("format must be '<image uri>=<tag>'")


def _validate_resource_arguments(ctx, param, value):
def _validate_opposing_opts(ctx, param, value):
opposite_option = {
"remove_resources": "no_remove_resources",
"no_remove_resources": "remove_resources",
"remove_dependencies": "no_remove_dependencies",
"no_remove_dependencies": "remove_dependencies",
}
opposite_option_value = ctx.params.get(opposite_option[param.name], "")

if any([val.startswith("-") for val in value]):
raise click.BadParameter("requires a component name or keyword 'all'")
if "all" in value and "all" in opposite_option_value:
raise click.BadParameter(
"--remove-resources/--no-remove-resources requires a component name or keyword 'all'"
)
if "all" in value and "all" in ctx.params.get(opposite_option[param.name], {}):
raise click.BadParameter(
"--remove-resources and --no-remove-resources can't be both set to 'all'"
f"'{param.opts[0]}' and its opposite option can't be both set to 'all'"
)
if param.name == "remove_resources" and not value:

# default values
if param.name == "remove_resources" and not value and not opposite_option_value:
value = ("all",)
if param.name == "no_remove_dependencies" and not value and not opposite_option_value:
value = ("all",)

return value


Expand Down Expand Up @@ -405,7 +412,7 @@ def _validate_resource_arguments(ctx, param, value):
),
type=str,
multiple=True,
callback=_validate_resource_arguments,
callback=_validate_opposing_opts,
),
click.option(
"--no-remove-resources",
Expand All @@ -415,7 +422,27 @@ def _validate_resource_arguments(ctx, param, value):
),
type=str,
multiple=True,
callback=_validate_resource_arguments,
callback=_validate_opposing_opts,
),
click.option(
"--remove-dependencies",
help=(
"Remove dependencies/optionalDependencies on ClowdApp configs "
"for specific components (default: none)"
),
type=str,
multiple=True,
callback=_validate_opposing_opts,
),
click.option(
"--no-remove-dependencies",
help=(
"Don't remove dependencies/optionalDependencies on ClowdApp configs "
"for specific components (default: all)"
),
type=str,
multiple=True,
callback=_validate_opposing_opts,
),
click.option(
"--single-replicas/--no-single-replicas",
Expand Down Expand Up @@ -741,6 +768,8 @@ def _process(
local_config_path,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
component_filter,
local,
Expand All @@ -758,6 +787,8 @@ def _process(
clowd_env,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
component_filter,
local,
Expand Down Expand Up @@ -788,6 +819,8 @@ def _cmd_process(
local_config_path,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
component_filter,
local,
Expand All @@ -809,6 +842,8 @@ def _cmd_process(
local_config_path,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
component_filter,
local,
Expand Down Expand Up @@ -898,6 +933,8 @@ def _cmd_config_deploy(
local_config_path,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
namespace,
name,
Expand Down Expand Up @@ -960,6 +997,8 @@ def _err_handler(err):
local_config_path,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
component_filter,
local,
Expand Down
30 changes: 30 additions & 0 deletions bonfire/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def _remove_resource_config(items):
log.debug("removed resources from ClowdApp '%s'", i["metadata"]["name"])


def _remove_dependency_config(items):
for i in items:
if i["kind"] != "ClowdApp":
continue

if i["spec"].get("dependencies"):
del i["spec"]["dependencies"]
log.debug("removed dependencies from ClowdApp '%s'", i["metadata"]["name"])
if i["spec"].get("optionalDependencies"):
del i["spec"]["optionalDependencies"]
log.debug("removed optionalDependencies from ClowdApp '%s'", i["metadata"]["name"])


def _set_replicas(items):
for i in items:
if i["kind"] != "ClowdApp":
Expand Down Expand Up @@ -313,6 +326,12 @@ def _validate(self):
self._validate_component_options(
all_components, self.no_remove_resources, "--no-remove-resources"
)
self._validate_component_options(
all_components, self.remove_dependencies, "--remove-dependencies"
)
self._validate_component_options(
all_components, self.no_remove_dependencies, "--no-remove-dependencies"
)
self._validate_component_options(all_components, self.component_filter, "--component")

def __init__(
Expand All @@ -326,6 +345,8 @@ def __init__(
clowd_env,
remove_resources,
no_remove_resources,
remove_dependencies,
no_remove_dependencies,
single_replicas,
component_filter,
local,
Expand All @@ -340,6 +361,8 @@ def __init__(
self.clowd_env = clowd_env
self.remove_resources = remove_resources
self.no_remove_resources = no_remove_resources
self.remove_dependencies = remove_dependencies
self.no_remove_dependencies = no_remove_dependencies
self.single_replicas = single_replicas
self.component_filter = component_filter
self.local = local
Expand Down Expand Up @@ -443,6 +466,13 @@ def _get_component_items(self, component_name):
):
_remove_resource_config(new_items)

if (
"all" not in self.no_remove_dependencies
and ("all" in self.remove_dependencies or component_name in self.remove_dependencies)
and component_name not in self.no_remove_dependencies
):
_remove_dependency_config(new_items)

if self.single_replicas:
_set_replicas(new_items)

Expand Down

0 comments on commit dc0b615

Please sign in to comment.