Skip to content

Commit

Permalink
[installer] Use --dependency-update; Fix debugging; Remove junk dirs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Oct 15, 2024
1 parent ed1c7bc commit 6a9de6e
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 167 deletions.
21 changes: 13 additions & 8 deletions deploy/scripts/install-combine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ set -eo pipefail
# It should only be executed directly by developers. For general users, it is packaged in
# a stand-alone installer (see ./installer/README.md or ./installer/README.pdf).
#
# The options for this script and for the packaged installer are the same. Note that 2
# additional debugging options are available that aren't documented in the readme file:
# The options for this script and for the packaged installer are the same. Note that some
# additional dev options are available that aren't documented in the readme file:
# - debug - show more verbose output for debugging.
# - single-step - run the next "step" in the installation process and stop.
# - start-at <step-name> - start at the step named <step-name> and run to completion.
#
Expand Down Expand Up @@ -57,7 +58,7 @@ create-python-venv () {
python3 -m venv venv
source venv/bin/activate
echo "Install pip and pip-tools"
python -m pip install --upgrade pip pip-tools
python -m pip $((( DEBUG == 0)) && echo "-q") install --upgrade pip pip-tools
echo "Install dependencies"
python -m piptools sync requirements.txt
}
Expand All @@ -78,9 +79,9 @@ install-kubernetes () {
cd ${DEPLOY_DIR}/ansible

if [ -d "${DEPLOY_DIR}/airgap-images" ] ; then
ansible-playbook playbook_desktop_setup.yml -K -e k8s_user=`whoami` -e install_airgap_images=true
ansible-playbook playbook_desktop_setup.yml -K -e k8s_user=`whoami` -e install_airgap_images=true $(((DEBUG == 1)) && echo "-vv")
else
ansible-playbook playbook_desktop_setup.yml -K -e k8s_user=`whoami`
ansible-playbook playbook_desktop_setup.yml -K -e k8s_user=`whoami` $(((DEBUG == 1)) && echo "-vv")
fi
}

Expand All @@ -97,7 +98,7 @@ set-k3s-env () {
export KUBECONFIG=${K3S_CONFIG_FILE}
#####
# Start k3s if it is not running
if ! systemctl is-active --quiet k3s ; then
if ! systemctl is-active $(((DEBUG == 0)) && echo "--quiet") k3s ; then
sudo systemctl start k3s
fi
}
Expand Down Expand Up @@ -137,7 +138,7 @@ install-the-combine () {
cd ${DEPLOY_DIR}/scripts
set-combine-env
set-k3s-env
./setup_combine.py --tag ${COMBINE_VERSION} --repo public.ecr.aws/thecombine --target desktop ${SETUP_OPTS} --debug
./setup_combine.py --tag ${COMBINE_VERSION} --repo public.ecr.aws/thecombine --target desktop ${SETUP_OPTS} $(((DEBUG == 1)) && echo "--debug")
deactivate
}

Expand Down Expand Up @@ -175,7 +176,7 @@ next-state () {

# Verify that the required network devices have been setup for Kubernetes cluster
wait-for-k8s-interfaces () {
echo "Waiting for k8s interfaces"
echo "Waiting for k8s interfaces: $@"
for interface in $@ ; do
while ! ip link show $interface > /dev/null 2>&1 ; do
sleep 1
Expand All @@ -192,6 +193,7 @@ CONFIG_DIR=${HOME}/.config/combine
mkdir -p ${CONFIG_DIR}
SINGLE_STEP=0
IS_SERVER=0
DEBUG=0

# See if we need to continue from a previous install
STATE_FILE=${CONFIG_DIR}/install-state
Expand All @@ -211,6 +213,9 @@ while (( "$#" )) ; do
rm ${CONFIG_DIR}/env
fi
;;
debug)
DEBUG=1
;;
restart)
next-state "Pre-reqs"
;;
Expand Down
13 changes: 7 additions & 6 deletions deploy/scripts/kube_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,18 @@ def add_helm_opts(parser: argparse.ArgumentParser) -> None:
)


def add_kube_opts(parser: argparse.ArgumentParser) -> None:
def add_kube_opts(parser: argparse.ArgumentParser, *, add_debug: bool = True) -> None:
"""Add commandline arguments for Kubernetes tools."""
parser.add_argument(
"--context",
help="Context in kubectl configuration file to be used.",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debugging output for helm commands.",
)
if add_debug:
parser.add_argument(
"--debug",
action="store_true",
help="Enable debugging output for helm commands.",
)
parser.add_argument(
"--kubeconfig",
help="Specify the kubectl configuration file to be used.",
Expand Down
132 changes: 82 additions & 50 deletions deploy/scripts/package_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
image names are extracted from the templates and then pulled from the repo and stored
in ../images as compressed tarballs; zstd compression is used.
"""

import argparse
import logging
import os
Expand All @@ -19,7 +20,7 @@
from utils import init_logging, run_cmd
import yaml

# Define configuration and output directories'
# Define configuration and output directories
scripts_dir = Path(__file__).resolve().parent
ansible_dir = scripts_dir.parent / "ansible"
helm_dir = scripts_dir.parent / "helm"
Expand Down Expand Up @@ -58,107 +59,134 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()


def package_k3s(dest_dir: Path) -> None:
def package_k3s(dest_dir: Path, *, debug: bool = False) -> None:
logging.info("Packaging k3s images.")
run_cmd(
[
"ansible-playbook",
"playbook_k3s_airgapped_files.yml",
"--extra-vars",
f"package_dir={dest_dir}",
],
cwd=str(ansible_dir),
)


def package_images(image_list: List[str], tar_file: Path) -> None:
ansible_cmd = [
"ansible-playbook",
"playbook_k3s_airgapped_files.yml",
"--extra-vars",
f"package_dir={dest_dir}",
]
if debug:
ansible_cmd.append("-vv")
run_cmd(ansible_cmd, cwd=str(ansible_dir), print_cmd=debug, print_output=debug)


def package_images(image_list: List[str], tar_file: Path, *, debug: bool = False) -> None:
container_cli_cmd = [os.getenv("CONTAINER_CLI", "docker")]
if container_cli_cmd[0] == "nerdctl":
container_cli_cmd.extend(["--namespace", "k8s.io"])

# Pull each image
for image in image_list:
pull_cmd = container_cli_cmd + ["pull", image]
logging.debug(f"Running {pull_cmd}")
run_cmd(pull_cmd)
run_cmd(pull_cmd, print_cmd=debug, print_output=debug)

# Save pulled images into a .tar archive
run_cmd(container_cli_cmd + ["save"] + image_list + ["-o", str(tar_file)])
save_cmd = container_cli_cmd + ["save"] + image_list + ["-o", str(tar_file)]
run_cmd(save_cmd, print_cmd=debug, print_output=debug)

# Compress the tarball
run_cmd(["zstd", "--rm", "--force", "--quiet", str(tar_file)])
tar_cmd = ["zstd", "--rm", "--force", str(tar_file)]
if not debug:
tar_cmd.append("--quiet")
run_cmd(tar_cmd, print_cmd=debug, print_output=debug)


def package_middleware(
config_file: str, *, cluster_type: str, image_dir: Path, chart_dir: Path
config_file: str, *, cluster_type: str, image_dir: Path, chart_dir: Path, debug: bool = False
) -> None:
logging.info("Packaging middleware images.")
# read in cluster configuration

# Read in cluster configuration
with open(config_file) as file:
config: Dict[str, Any] = yaml.safe_load(file)
# get current repos

# Get current repos
curr_repo_list: List[str] = []
middleware_images: List[str] = []
helm_list_cmd = ["helm", "repo", "list", "-o", "yaml"]
if debug:
helm_list_cmd.append("--debug")
helm_cmd_results = run_cmd(
["helm", "repo", "list", "-o", "yaml"], print_cmd=False, check_results=False
helm_list_cmd, check_results=False, print_cmd=debug, print_output=debug
)
if helm_cmd_results.returncode == 0:
curr_helm_repos = yaml.safe_load(helm_cmd_results.stdout)
for repo in curr_helm_repos:
curr_repo_list.append(repo["name"])

for chart_descr in config["clusters"][cluster_type]:
# add the chart's repo if we don't already have it
logging.debug(f"Chart: ${chart_descr}")

# Add the chart's repo if we don't already have it
repo = config[chart_descr]["repo"]
if repo["name"] not in curr_helm_repos:
run_cmd(["helm", "repo", "add", repo["name"], repo["url"]])
helm_add_cmd = ["helm", "repo", "add", repo["name"], repo["url"]]
if debug:
helm_add_cmd.append("--debug")
run_cmd(helm_add_cmd, print_cmd=debug, print_output=debug)
curr_repo_list.append(repo["name"])

# pull the middleware chart
# Pull the middleware chart
chart = config[chart_descr]["chart"]
dest_dir = chart_dir / chart["name"]
dest_dir.mkdir(mode=0o755, parents=True, exist_ok=True)
helm_cmd = ["helm", "pull", chart["reference"], "--destination", str(dest_dir)]
helm_pull_cmd = ["helm", "pull", chart["reference"], "--destination", str(dest_dir)]
if debug:
helm_pull_cmd.append("--debug")
if "version" in chart:
helm_cmd.extend(["--version", chart["version"]])
run_cmd(helm_cmd)
# render chart templates and extract images
helm_pull_cmd.extend(["--version", chart["version"]])
run_cmd(helm_pull_cmd, print_cmd=debug, print_output=debug)

# Render chart templates and extract images
for chart_file in dest_dir.glob("*.tgz"):
results = run_cmd(["helm", "template", chart_file])
results = run_cmd(["helm", "template", chart_file], print_cmd=debug)
for line in results.stdout.splitlines():
match = re.match(r'[-\s]+image:\s+"*([^"\n]*)"*', line)
if match:
if match and not match.group(1) in middleware_images:
logging.debug(f" - Found image {match.group(1)}")
middleware_images.append(match.group(1))

logging.debug(f"Middleware images: {middleware_images}")
package_images(middleware_images, image_dir / "middleware-airgap-images-amd64.tar")
package_images(
middleware_images, image_dir / "middleware-airgap-images-amd64.tar", debug=debug
)


def package_thecombine(tag: str, image_dir: Path) -> None:
def package_thecombine(tag: str, image_dir: Path, *, debug: bool = False) -> None:
logging.info(f"Packaging The Combine version {tag}.")
logging.debug("Create helm charts from templates")
combine_charts.generate(tag)

logging.debug(" - Get template for The Combine.")
results = run_cmd(
[
"helm",
"template",
"thecombine",
str(helm_dir / "thecombine"),
"--set",
"global.imageRegistry=public.ecr.aws/thecombine",
"--set",
f"global.imageTag={tag}",
]
)
helm_template_cmd = [
"helm",
"template",
"thecombine",
str(helm_dir / "thecombine"),
"--dependency-update",
"--set",
"global.imageRegistry=public.ecr.aws/thecombine",
"--set",
f"global.imageTag={tag}",
]
if debug:
helm_template_cmd.append("--debug")
results = run_cmd(helm_template_cmd, print_cmd=debug)

combine_images: List[str] = []
for line in results.stdout.splitlines():
match = re.match(r'^[-\s]+image:\s+"*([^"\n]*)"*', line)
if match:
image = match.group(1)
logging.debug(f" - Found image {image}")
if image not in combine_images:
logging.debug(f" - Found image {image}")
combine_images.append(image)
logging.debug(f"Combine images: {combine_images}")

# Logout of AWS to allow pulling the images
package_images(combine_images, image_dir / "combine-airgap-images-amd64.tar")
package_images(combine_images, image_dir / "combine-airgap-images-amd64.tar", debug=debug)


def main() -> None:
Expand All @@ -179,11 +207,15 @@ def main() -> None:
os.environ["AWS_DEFAULT_REGION"] = ""

# Update helm repos
package_k3s(image_dir)
package_k3s(image_dir, debug=args.debug)
package_middleware(
args.config, cluster_type="standard", image_dir=image_dir, chart_dir=chart_dir
args.config,
cluster_type="standard",
image_dir=image_dir,
chart_dir=chart_dir,
debug=args.debug,
)
package_thecombine(args.tag, image_dir)
package_thecombine(args.tag, image_dir, debug=args.debug)


if __name__ == "__main__":
Expand Down
11 changes: 9 additions & 2 deletions deploy/scripts/setup_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def parse_args() -> argparse.Namespace:
description="Build containerd container images for project.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
add_kube_opts(parser)
add_kube_opts(parser, add_debug=False)
add_helm_opts(parser)
parser.add_argument(
"--chart-dir", help="Directory for the chart files when doing an airgap installation."
Expand All @@ -37,12 +37,19 @@ def parse_args() -> argparse.Namespace:
help="Configuration file for the cluster type(s).",
default=str(scripts_dir / "setup_files" / "cluster_config.yaml"),
)
parser.add_argument(
logging_group = parser.add_mutually_exclusive_group()
logging_group.add_argument(
"--quiet",
"-q",
action="store_true",
help="Print less output information.",
)
logging_group.add_argument(
"--debug",
"-d",
action="store_true",
help="Print extra debugging information.",
)
parser.add_argument(
"--type",
"-t",
Expand Down
19 changes: 5 additions & 14 deletions deploy/scripts/setup_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def parse_args() -> argparse.Namespace:
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# Arguments used by the Kubernetes tools
add_kube_opts(parser)
add_kube_opts(parser, add_debug=False)
# Arguments used by Helm
add_helm_opts(parser)
# Arguments specific to setting up The Combine
Expand Down Expand Up @@ -187,8 +187,7 @@ def main() -> None:
if chart in installed_charts:
if args.clean:
# Delete existing chart if --clean specified
delete_cmd = helm_cmd + ["--namespace", chart_namespace, "delete", chart]
logging.debug(delete_cmd)
delete_cmd = helm_cmd + [f"--namespace={chart_namespace}", "delete", chart]
run_cmd(delete_cmd, print_cmd=not args.quiet, print_output=True)
else:
helm_action = HelmAction.UPGRADE
Expand All @@ -204,8 +203,8 @@ def main() -> None:
# Create the base helm install command
chart_dir = helm_dir / chart
helm_install_cmd = helm_cmd + [
"--namespace",
chart_namespace,
"--dependency-update",
f"--namespace={chart_namespace}",
helm_action.value,
chart,
str(chart_dir),
Expand Down Expand Up @@ -251,15 +250,7 @@ def main() -> None:
for variable in target_vars:
helm_install_cmd.extend(["--set", variable])

# Update chart dependencies
# Note that this operation is performed on the local helm charts
# so the kubeconfig and context arguments are not passed to the
# helm command.
helm_deps_cmd = ["helm", "dependency", "update", str(chart_dir)]
logging.debug(helm_deps_cmd)
run_cmd(helm_deps_cmd, print_cmd=not args.quiet, print_output=True)

logging.debug(helm_install_cmd)
# Install the chart
run_cmd(helm_install_cmd, print_cmd=not args.quiet, print_output=True)


Expand Down
Loading

0 comments on commit 6a9de6e

Please sign in to comment.