Skip to content

Commit

Permalink
Merge pull request #21 from RedHatQE/upload-to-s3-any-cluster-platform
Browse files Browse the repository at this point in the history
Upload to s3 any cluster platform
  • Loading branch information
rnetser authored Jul 19, 2023
2 parents ae94a80 + fc98670 commit a3c889e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ To pull the image: `podman pull quay.io/openshift-cli-installer`
* `--clusters-install-data-directory`: Clusters configurations are written to `<clusters-install-data-directory><platform><cluster name>`; write permissions are needed.
* `<cluster directory>/auth` contains `kubeconfig` and `kubeadmin-password` files
* `--parallel`: To create / destroy clusters in parallel
* Pass `--s3-bucket-name` (and optionally `--s3-bucket-path`) to backup <cluster directory> in an S3 bucket.


* AWS IPI clusters:
* The installer output is saved in the <cluster directory>.
* The data is used for cluster destroy.
* Pass `--s3-bucket-name` (and optionally `--s3-bucket-path`) to backup <cluster directory> in an S3 bucket.
* `base_domain` cluster parameter is mandatory
* `--registry-config-file`: registry-config json file path, can be obtained from [openshift local cluster](https://console.redhat.com/openshift/create/local)

Expand Down
14 changes: 10 additions & 4 deletions app/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def create_openshift_cluster(cluster_data, s3_bucket_name=None, s3_bucket_path=N
)

elif cluster_platform in (ROSA_STR, HYPERSHIFT_STR):
rosa_create_cluster(cluster_data=cluster_data)
rosa_create_cluster(
cluster_data=cluster_data,
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
)


def destroy_openshift_cluster(cluster_data):
Expand Down Expand Up @@ -265,9 +269,6 @@ def main(
clusters=clusters, registry_config_file=registry_config_file
)
if create:
kwargs.update(
{"s3_bucket_name": s3_bucket_name, "s3_bucket_path": s3_bucket_path}
)
clusters = create_install_config_file(
clusters=cluster, registry_config_file=registry_config_file
)
Expand All @@ -284,6 +285,11 @@ def main(
ocm_env=ocm_env,
)

if create:
kwargs.update(
{"s3_bucket_name": s3_bucket_name, "s3_bucket_path": s3_bucket_path}
)

processes = []
action_func = create_openshift_cluster if create else destroy_openshift_cluster

Expand Down
18 changes: 6 additions & 12 deletions app/libs/aws_ipi_clusters.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json
import os
import shlex
import shutil

import click
import shortuuid
import yaml
from clouds.aws.session_clients import s3_client
from jinja2 import DebugUndefined, Environment, FileSystemLoader, meta
from ocp_utilities.utils import run_command
from utils.helpers import zip_and_upload_to_s3

# TODO: enable spot
"""
Expand Down Expand Up @@ -133,16 +131,12 @@ def create_or_destroy_aws_ipi_cluster(
check=False,
)
if action == "create" and s3_bucket_name:
zip_file = shutil.make_archive(
base_name=f"{install_dir}-{shortuuid.uuid()}",
format="zip",
root_dir=install_dir,
)
s3_client().upload_file(
Filename=zip_file,
Bucket=s3_bucket_name,
Key=os.path.join(s3_bucket_path or "", os.path.split(zip_file)[-1]),
zip_and_upload_to_s3(
install_dir=install_dir,
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
)

if not res:
click.echo(f"Failed to run cluster {action}\nERR: {err}\nOUT: {out}")
raise click.Abort()
20 changes: 18 additions & 2 deletions app/libs/rosa_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ocp_resources.utils import TimeoutSampler
from python_terraform import IsNotFlagged, Terraform, TerraformCommandError
from utils.const import HYPERSHIFT_STR, ROSA_STR
from utils.helpers import get_ocm_client
from utils.helpers import get_ocm_client, zip_and_upload_to_s3


def tts(ts):
Expand Down Expand Up @@ -239,7 +239,7 @@ def prepare_managed_clusters_data(clusters, ocm_token, ocm_env):
return clusters


def rosa_create_cluster(cluster_data):
def rosa_create_cluster(cluster_data, s3_bucket_name=None, s3_bucket_path=None):
hosted_cp_arg = "--hosted-cp"
_platform = cluster_data["platform"]
ignore_keys = (
Expand Down Expand Up @@ -275,6 +275,14 @@ def rosa_create_cluster(cluster_data):

dump_cluster_data_to_file(cluster_data=cluster_data)

zip_base_name = None
if s3_bucket_name:
zip_base_name = zip_and_upload_to_s3(
install_dir=cluster_data["install-dir"],
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
)

rosa.cli.execute(
command=command,
ocm_env=ocm_env_url,
Expand All @@ -288,6 +296,14 @@ def rosa_create_cluster(cluster_data):
cluster_object.wait_for_cluster_ready(wait_timeout=cluster_data["timeout"])
set_cluster_auth(cluster_data=cluster_data, cluster_object=cluster_object)

if s3_bucket_name:
zip_and_upload_to_s3(
install_dir=cluster_data["install-dir"],
s3_bucket_name=s3_bucket_name,
s3_bucket_path=s3_bucket_path,
base_name=zip_base_name,
)

if _platform == ROSA_STR:
wait_for_osd_cluster_ready_job(ocp_client=cluster_object.ocp_client)

Expand Down
64 changes: 64 additions & 0 deletions app/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,74 @@
import os
import shutil
from functools import wraps
from time import sleep

import shortuuid
from clouds.aws.session_clients import s3_client
from ocm_python_wrapper.ocm_client import OCMPythonClient


# TODO: Move to own repository.
def ignore_exceptions(logger=None, retry=None):
def wrapper(func):
@wraps(func)
def inner(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as ex:
if retry:
for _ in range(0, retry):
try:
return func(*args, **kwargs)
except Exception:
sleep(1)

if logger:
logger.info(ex)
return None

return inner

return wrapper


def remove_terraform_folder_from_install_dir(install_dir):
"""
.terraform folder created when call terraform.init() and it's take more space.
"""
folders_to_remove = []
for root, dirs, files in os.walk(install_dir):
for _dir in dirs:
if _dir == ".terraform":
folders_to_remove.append(os.path.join(root, _dir))

for folder in folders_to_remove:
shutil.rmtree(folder)


def get_ocm_client(ocm_token, ocm_env):
return OCMPythonClient(
token=ocm_token,
endpoint="https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token",
api_host=ocm_env,
discard_unknown_keys=True,
).client


@ignore_exceptions()
def zip_and_upload_to_s3(install_dir, s3_bucket_name, s3_bucket_path, base_name=None):
remove_terraform_folder_from_install_dir(install_dir=install_dir)

_base_name = base_name or f"{install_dir}-{shortuuid.uuid()}"
zip_file = shutil.make_archive(
base_name=_base_name,
format="zip",
root_dir=install_dir,
)
s3_client().upload_file(
Filename=zip_file,
Bucket=s3_bucket_name,
Key=os.path.join(s3_bucket_path or "", os.path.split(zip_file)[-1]),
)

return _base_name

0 comments on commit a3c889e

Please sign in to comment.