Skip to content

Commit

Permalink
Merge pull request #4438 from GeorgianaElena/deployer-exec-root-homes
Browse files Browse the repository at this point in the history
[deployer] Add a `deployer exec root-homes cluster` command
  • Loading branch information
GeorgianaElena authored Jul 17, 2024
2 parents e237889 + a3db2b6 commit b6834df
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions deployer/commands/exec/infra_components.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import subprocess

import typer
Expand All @@ -17,6 +18,105 @@
UBUNTU_IMAGE = "ubuntu:22.04"


@exec_app.command()
def root_homes(
cluster_name: str = typer.Argument(..., help="Name of cluster to operate on"),
):
"""
Pop an interactive shell with the entire nfs file system of the given cluster mounted on /root-homes
"""
config_file_path = find_absolute_path_to_cluster_file(cluster_name)
with open(config_file_path) as f:
cluster = Cluster(yaml.load(f), config_file_path.parent)

with cluster.auth():
hubs = cluster.hubs
# Get a hub so we can extract the nfs ip and base share name from its config
hub = next((hub for hub in hubs if hub.spec["name"] == "staging"), None)
if hub:
namespace = "staging"
else:
hub = hubs[0]
namespace = hub.spec["name"]

for values_file in hub.spec["helm_chart_values_files"]:
if "secret" not in os.path.basename(values_file):
values_file = config_file_path.parent.joinpath(values_file)
config = yaml.load(values_file)

if config.get("basehub", {}):
server_ip = (
config["basehub"].get("nfs", {}).get("pv", {}).get("serverIP", "")
)
base_share_name = (
config["basehub"]
.get("nfs", {})
.get("pv", {})
.get("baseShareName", "")
)
else:
server_ip = config.get("nfs", {}).get("pv", {}).get("serverIP", "")
base_share_name = (
config.get("nfs", {}).get("pv", {}).get("baseShareName", "")
)
if server_ip and base_share_name:
break

pod_name = f"{cluster_name}-root-home-shell"
pod = {
"apiVersion": "v1",
"kind": "Pod",
"spec": {
"terminationGracePeriodSeconds": 1,
"automountServiceAccountToken": False,
"volumes": [
{
"name": "root-homes",
"nfs": {"server": server_ip, "path": base_share_name},
}
],
"containers": [
{
"name": pod_name,
# Use ubuntu image so we get better gnu rm
"image": UBUNTU_IMAGE,
"stdin": True,
"stdinOnce": True,
"tty": True,
"volumeMounts": [
{
"name": "root-homes",
"mountPath": "/root-homes",
}
],
}
],
},
}

cmd = [
"kubectl",
"-n",
namespace,
"run",
"--rm", # Remove pod when we're done
"-it", # Give us a shell!
"--overrides",
json.dumps(pod),
"--image",
# Use ubuntu image so we get GNU rm and other tools
# Should match what we have in our pod definition
UBUNTU_IMAGE,
pod_name,
"--",
"/bin/bash",
"-l",
]

with cluster.auth():
subprocess.check_call(cmd)


@exec_app.command()
def homes(
cluster_name: str = typer.Argument(..., help="Name of cluster to operate on"),
Expand Down

0 comments on commit b6834df

Please sign in to comment.