Skip to content

Commit

Permalink
Exit after deletion of all clusters or from s3 bucket (#181)
Browse files Browse the repository at this point in the history
* Exit after deletion of all clusters or from s3 bucket

* remove return
  • Loading branch information
rnetser authored Sep 19, 2023
1 parent 3b52d04 commit 89fded8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 76 deletions.
25 changes: 14 additions & 11 deletions openshift_cli_installer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from clouds.aws.aws_utils import set_and_verify_aws_credentials
from pyaml_env import parse_config

from openshift_cli_installer.libs.destroy_clusters import destroy_clusters
from openshift_cli_installer.libs.managed_clusters.acm_clusters import (
install_and_attach_for_acm,
)
from openshift_cli_installer.utils.cli_utils import (
destroy_s3_or_all_clusters,
get_clusters_by_type,
is_region_support_hypershift,
prepare_aws_ipi_clusters,
Expand Down Expand Up @@ -211,16 +211,6 @@ def main(**kwargs):
aws_secret_access_key = user_kwargs.get("aws_secret_access_key")
aws_account_id = user_kwargs.get("aws_account_id")

destroy_s3_or_all_clusters(
destroy_clusters_from_s3_config_files=destroy_clusters_from_s3_config_files,
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
clusters_install_data_directory=clusters_install_data_directory,
registry_config_file=registry_config_file,
destroy_all_clusters=destroy_all_clusters,
ocm_token=ocm_token,
)

verify_user_input(
action=action,
clusters=clusters,
Expand All @@ -232,8 +222,21 @@ def main(**kwargs):
aws_secret_access_key=aws_secret_access_key,
aws_account_id=aws_account_id,
ocm_token=ocm_token,
destroy_clusters_from_s3_config_files=destroy_clusters_from_s3_config_files,
s3_bucket_name=s3_bucket_name,
)

if destroy_clusters_from_s3_config_files or destroy_all_clusters:
return destroy_clusters(
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
clusters_install_data_directory=clusters_install_data_directory,
registry_config_file=registry_config_file,
clusters_yaml_files=destroy_clusters_from_s3_config_files,
destroy_all_clusters=destroy_all_clusters,
ocm_token=ocm_token,
)

clusters = add_ocm_client_and_env_to_cluster_dict(
clusters=clusters, ocm_token=ocm_token
)
Expand Down
2 changes: 2 additions & 0 deletions openshift_cli_installer/libs/destroy_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
create_or_destroy_aws_ipi_cluster,
download_openshift_install_binary,
)
from openshift_cli_installer.utils.cli_utils import assert_registry_config_file_exists
from openshift_cli_installer.utils.clusters import (
add_ocm_client_and_env_to_cluster_dict,
)
Expand Down Expand Up @@ -280,6 +281,7 @@ def destroy_clusters(

aws_clusters = clusters_data_dict["aws"]
if aws_clusters:
assert_registry_config_file_exists(registry_config_file=registry_config_file)
download_openshift_install_binary(
clusters=aws_clusters, registry_config_file=registry_config_file
)
Expand Down
113 changes: 48 additions & 65 deletions openshift_cli_installer/utils/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import rosa.cli
from ocp_resources.utils import TimeoutWatch

from openshift_cli_installer.libs.destroy_clusters import destroy_clusters
from openshift_cli_installer.libs.managed_clusters.helpers import (
prepare_managed_clusters_data,
)
Expand Down Expand Up @@ -208,44 +207,57 @@ def verify_user_input(
aws_secret_access_key,
aws_account_id,
ocm_token,
destroy_clusters_from_s3_config_files,
s3_bucket_name,
):
if not action:
click.secho(
f"'action' must be provided, supported actions: `{CREATE_STR}`,"
f" `{DESTROY_STR}`",
fg=ERROR_LOG_COLOR,
)
raise click.Abort()
abort_no_ocm_token(ocm_token=ocm_token)

if not clusters:
click.secho(
"At least one '--cluster' option must be provided.", fg=ERROR_LOG_COLOR
)
raise click.Abort()
if destroy_clusters_from_s3_config_files:
if not s3_bucket_name:
click.secho(
"`--s3-bucket-name` must be provided when running with"
" `--destroy-clusters-from-s3-config-files`",
fg=ERROR_LOG_COLOR,
)
raise click.Abort()

abort_no_ocm_token(ocm_token=ocm_token)
is_platform_supported(clusters=clusters)
assert_aws_ipi_user_input(
clusters=clusters,
ssh_key_file=ssh_key_file,
docker_config_file=docker_config_file,
registry_config_file=registry_config_file,
)
assert_osd_user_input(
clusters=clusters,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_account_id=aws_account_id,
)
assert_acm_clusters_user_input(
action=action,
clusters=clusters,
ssh_key_file=ssh_key_file,
private_ssh_key_file=private_ssh_key_file,
registry_config_file=registry_config_file,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
)
else:
if not action:
click.secho(
f"'action' must be provided, supported actions: `{CREATE_STR}`,"
f" `{DESTROY_STR}`",
fg=ERROR_LOG_COLOR,
)
raise click.Abort()

if not clusters:
click.secho(
"At least one '--cluster' option must be provided.", fg=ERROR_LOG_COLOR
)
raise click.Abort()

is_platform_supported(clusters=clusters)
assert_aws_ipi_user_input(
clusters=clusters,
ssh_key_file=ssh_key_file,
docker_config_file=docker_config_file,
registry_config_file=registry_config_file,
)
assert_osd_user_input(
clusters=clusters,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_account_id=aws_account_id,
)
assert_acm_clusters_user_input(
action=action,
clusters=clusters,
ssh_key_file=ssh_key_file,
private_ssh_key_file=private_ssh_key_file,
registry_config_file=registry_config_file,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
)


def assert_aws_ipi_user_input(
Expand Down Expand Up @@ -312,35 +324,6 @@ def assert_acm_clusters_user_input(
raise click.Abort()


def destroy_s3_or_all_clusters(
destroy_clusters_from_s3_config_files,
s3_bucket_name,
s3_bucket_path,
clusters_install_data_directory,
registry_config_file,
destroy_all_clusters,
ocm_token,
):
if destroy_clusters_from_s3_config_files and not s3_bucket_name:
click.secho(
"`--s3-bucket-name` must be provided when running with"
" `--destroy-clusters-from-s3-config-files`",
fg=ERROR_LOG_COLOR,
)
raise click.Abort()

if destroy_all_clusters or destroy_clusters_from_s3_config_files:
return destroy_clusters(
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
clusters_install_data_directory=clusters_install_data_directory,
registry_config_file=registry_config_file,
clusters_yaml_files=destroy_clusters_from_s3_config_files,
destroy_all_clusters=destroy_all_clusters,
ocm_token=ocm_token,
)


def prepare_aws_ipi_clusters(
aws_ipi_clusters,
clusters_install_data_directory,
Expand Down

0 comments on commit 89fded8

Please sign in to comment.