Skip to content

Commit

Permalink
change home dir before run oc login (#235)
Browse files Browse the repository at this point in the history
* change home dir before run oc login

* change home dir in the right place

* Fix oc login after change HOME dir
  • Loading branch information
myakove authored Oct 10, 2023
1 parent 9114c40 commit 9fa75b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
27 changes: 23 additions & 4 deletions openshift_cli_installer/utils/cli_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import contextlib
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
Expand All @@ -8,6 +9,7 @@
from clouds.aws.aws_utils import set_and_verify_aws_credentials
from ocm_python_wrapper.cluster import Cluster
from ocp_resources.utils import TimeoutWatch
from simple_logger.logger import get_logger

from openshift_cli_installer.libs.managed_clusters.helpers import (
prepare_managed_clusters_data,
Expand Down Expand Up @@ -52,6 +54,8 @@
from openshift_cli_installer.utils.gcp import get_gcp_regions
from openshift_cli_installer.utils.general import tts

LOGGER = get_logger(name=__name__)


def get_clusters_by_type(clusters):
clusters_dict = {}
Expand Down Expand Up @@ -665,11 +669,12 @@ def assert_unique_cluster_names(clusters):

def save_kubeadmin_token_to_clusters_install_data(clusters):
# Do not run this function in parallel, get_kubeadmin_token() do `oc login`.
for cluster_data in clusters:
with get_kubeadmin_token(cluster_data=cluster_data) as kubeadmin_token:
cluster_data["kubeadmin-token"] = kubeadmin_token
with change_home_environment():
for cluster_data in clusters:
with get_kubeadmin_token(cluster_data=cluster_data) as kubeadmin_token:
cluster_data["kubeadmin-token"] = kubeadmin_token

dump_cluster_data_to_file(cluster_data=cluster_data)
dump_cluster_data_to_file(cluster_data=cluster_data)

return clusters

Expand All @@ -692,3 +697,17 @@ def assert_boolean_values(clusters, create):
msg=f"The following keys must be booleans: {non_bool_keys}",
)
raise click.Abort()


@contextlib.contextmanager
def change_home_environment():
home_atr = "HOME"
tmp_home_dir = "/tmp/"
LOGGER.info(f"Changing {home_atr} environment variable to {tmp_home_dir}")
current_home = os.environ.get(home_atr)
os.environ[home_atr] = tmp_home_dir
yield
LOGGER.info(
f"Changing {home_atr} environment variable to previous value. {current_home}"
)
os.environ[home_atr] = current_home
6 changes: 5 additions & 1 deletion openshift_cli_installer/utils/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ def get_kubeadmin_token(cluster_data):
os.path.join(cluster_data["install-dir"], "auth", "kubeadmin-password")
) as fd:
kubeadmin_password = fd.read()
run_command(shlex.split(f"oc login -u kubeadmin -p {kubeadmin_password}"))
run_command(
shlex.split(
f"oc login {cluster_data['api-url']} -u kubeadmin -p {kubeadmin_password}"
)
)
yield run_command(shlex.split("oc whoami -t"))[1].strip()
run_command(shlex.split("oc logout"))
22 changes: 1 addition & 21 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ openshift-python-utilities = "^5.0.0"
pyaml-env = "^1.2.1"
google-cloud-compute = "^1.14.1"
redhat-qe-cloud-tools = "^1.0.17"
python-simple-logger = "^1.0.7"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 9fa75b8

Please sign in to comment.