2023-09-10 19:28:26 +00:00
|
|
|
# Copyright © 2022, 2023 Vulcanize
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import click
|
2023-07-24 02:54:05 +00:00
|
|
|
from importlib import util
|
2023-06-27 22:58:41 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2023-10-09 20:54:55 +00:00
|
|
|
from typing import List
|
2023-09-04 18:14:05 +00:00
|
|
|
import random
|
2023-10-09 20:54:55 +00:00
|
|
|
from shutil import copy, copyfile, copytree
|
2023-12-06 05:56:58 +00:00
|
|
|
from secrets import token_hex
|
2023-06-27 22:58:41 +00:00
|
|
|
import sys
|
2023-11-20 16:12:57 +00:00
|
|
|
from stack_orchestrator import constants
|
2023-11-28 05:02:16 +00:00
|
|
|
from stack_orchestrator.opts import opts
|
2023-11-07 07:06:55 +00:00
|
|
|
from stack_orchestrator.util import (get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config,
|
|
|
|
global_options, get_yaml, get_pod_list, get_pod_file_path, pod_has_scripts,
|
2023-11-29 04:14:02 +00:00
|
|
|
get_pod_script_paths, get_plugin_code_paths, error_exit, env_var_map_from_file)
|
2023-11-08 08:11:00 +00:00
|
|
|
from stack_orchestrator.deploy.deploy_types import LaconicStackSetupCommand
|
2023-11-07 07:06:55 +00:00
|
|
|
from stack_orchestrator.deploy.deployer_factory import getDeployerConfigGenerator
|
2023-11-08 08:11:00 +00:00
|
|
|
from stack_orchestrator.deploy.deployment_context import DeploymentContext
|
2023-07-25 16:16:19 +00:00
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
def _make_default_deployment_dir():
|
2023-11-20 16:12:57 +00:00
|
|
|
return Path("deployment-001")
|
2023-06-27 22:58:41 +00:00
|
|
|
|
2023-08-17 19:49:56 +00:00
|
|
|
|
2023-08-11 20:25:54 +00:00
|
|
|
def _get_ports(stack):
|
|
|
|
ports = {}
|
|
|
|
parsed_stack = get_parsed_stack_config(stack)
|
2023-10-09 20:54:55 +00:00
|
|
|
pods = get_pod_list(parsed_stack)
|
2023-08-11 20:25:54 +00:00
|
|
|
yaml = get_yaml()
|
|
|
|
for pod in pods:
|
2023-10-09 20:54:55 +00:00
|
|
|
pod_file_path = get_pod_file_path(parsed_stack, pod)
|
2023-08-11 20:25:54 +00:00
|
|
|
parsed_pod_file = yaml.load(open(pod_file_path, "r"))
|
|
|
|
if "services" in parsed_pod_file:
|
|
|
|
for svc_name, svc in parsed_pod_file["services"].items():
|
|
|
|
if "ports" in svc:
|
|
|
|
# Ports can appear as strings or numbers. We normalize them as strings.
|
2023-09-04 19:00:23 +00:00
|
|
|
ports[svc_name] = [str(x) for x in svc["ports"]]
|
2023-08-11 20:25:54 +00:00
|
|
|
return ports
|
2023-06-27 22:58:41 +00:00
|
|
|
|
2023-08-17 19:49:56 +00:00
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
def _get_named_volumes(stack):
|
|
|
|
# Parse the compose files looking for named volumes
|
2024-01-31 05:09:48 +00:00
|
|
|
named_volumes = {
|
|
|
|
"rw": [],
|
|
|
|
"ro": []
|
|
|
|
}
|
2023-06-27 22:58:41 +00:00
|
|
|
parsed_stack = get_parsed_stack_config(stack)
|
2023-10-09 20:54:55 +00:00
|
|
|
pods = get_pod_list(parsed_stack)
|
2023-07-24 02:54:05 +00:00
|
|
|
yaml = get_yaml()
|
2024-01-31 05:09:48 +00:00
|
|
|
|
|
|
|
def find_vol_usage(parsed_pod_file, vol):
|
|
|
|
ret = {}
|
|
|
|
if "services" in parsed_pod_file:
|
|
|
|
for svc_name, svc in parsed_pod_file["services"].items():
|
|
|
|
if "volumes" in svc:
|
|
|
|
for svc_volume in svc["volumes"]:
|
|
|
|
parts = svc_volume.split(":")
|
|
|
|
if parts[0] == vol:
|
|
|
|
ret[svc_name] = {
|
|
|
|
"volume": parts[0],
|
|
|
|
"mount": parts[1],
|
|
|
|
"options": parts[2] if len(parts) == 3 else None
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
for pod in pods:
|
2023-10-09 20:54:55 +00:00
|
|
|
pod_file_path = get_pod_file_path(parsed_stack, pod)
|
2023-06-27 22:58:41 +00:00
|
|
|
parsed_pod_file = yaml.load(open(pod_file_path, "r"))
|
|
|
|
if "volumes" in parsed_pod_file:
|
|
|
|
volumes = parsed_pod_file["volumes"]
|
|
|
|
for volume in volumes.keys():
|
2024-01-31 05:09:48 +00:00
|
|
|
for vu in find_vol_usage(parsed_pod_file, volume).values():
|
|
|
|
read_only = vu["options"] == "ro"
|
|
|
|
if read_only:
|
|
|
|
if vu["volume"] not in named_volumes["rw"] and vu["volume"] not in named_volumes["ro"]:
|
|
|
|
named_volumes["ro"].append(vu["volume"])
|
|
|
|
else:
|
|
|
|
if vu["volume"] not in named_volumes["rw"]:
|
|
|
|
named_volumes["rw"].append(vu["volume"])
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
return named_volumes
|
|
|
|
|
|
|
|
|
2023-06-28 03:18:04 +00:00
|
|
|
# If we're mounting a volume from a relatie path, then we
|
|
|
|
# assume the directory doesn't exist yet and create it
|
|
|
|
# so the deployment will start
|
|
|
|
# Also warn if the path is absolute and doesn't exist
|
|
|
|
def _create_bind_dir_if_relative(volume, path_string, compose_dir):
|
|
|
|
path = Path(path_string)
|
|
|
|
if not path.is_absolute():
|
|
|
|
absolute_path = Path(compose_dir).parent.joinpath(path)
|
|
|
|
absolute_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
else:
|
|
|
|
if not path.exists():
|
|
|
|
print(f"WARNING: mount path for volume {volume} does not exist: {path_string}")
|
|
|
|
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
# See: https://stackoverflow.com/questions/45699189/editing-docker-compose-yml-with-pyyaml
|
2023-06-28 03:18:04 +00:00
|
|
|
def _fixup_pod_file(pod, spec, compose_dir):
|
2023-08-17 19:49:56 +00:00
|
|
|
# Fix up volumes
|
|
|
|
if "volumes" in spec:
|
|
|
|
spec_volumes = spec["volumes"]
|
|
|
|
if "volumes" in pod:
|
|
|
|
pod_volumes = pod["volumes"]
|
|
|
|
for volume in pod_volumes.keys():
|
|
|
|
if volume in spec_volumes:
|
|
|
|
volume_spec = spec_volumes[volume]
|
|
|
|
volume_spec_fixedup = volume_spec if Path(volume_spec).is_absolute() else f".{volume_spec}"
|
|
|
|
_create_bind_dir_if_relative(volume, volume_spec, compose_dir)
|
|
|
|
new_volume_spec = {"driver": "local",
|
|
|
|
"driver_opts": {
|
2024-01-31 05:09:48 +00:00
|
|
|
"type": "none",
|
|
|
|
"device": volume_spec_fixedup,
|
|
|
|
"o": "bind"
|
2023-06-27 22:58:41 +00:00
|
|
|
}
|
2023-08-17 19:49:56 +00:00
|
|
|
}
|
|
|
|
pod["volumes"][volume] = new_volume_spec
|
2024-01-31 05:09:48 +00:00
|
|
|
|
|
|
|
# Fix up configmaps
|
|
|
|
if "configmaps" in spec:
|
|
|
|
spec_cfgmaps = spec["configmaps"]
|
|
|
|
if "volumes" in pod:
|
|
|
|
pod_volumes = pod["volumes"]
|
|
|
|
for volume in pod_volumes.keys():
|
|
|
|
if volume in spec_cfgmaps:
|
|
|
|
volume_cfg = spec_cfgmaps[volume]
|
|
|
|
# Just make the dir (if necessary)
|
|
|
|
_create_bind_dir_if_relative(volume, volume_cfg, compose_dir)
|
|
|
|
|
2023-08-17 19:49:56 +00:00
|
|
|
# Fix up ports
|
2023-11-21 23:04:36 +00:00
|
|
|
if "network" in spec and "ports" in spec["network"]:
|
|
|
|
spec_ports = spec["network"]["ports"]
|
2023-08-17 19:49:56 +00:00
|
|
|
for container_name, container_ports in spec_ports.items():
|
|
|
|
if container_name in pod["services"]:
|
|
|
|
pod["services"][container_name]["ports"] = container_ports
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
2023-11-08 08:11:00 +00:00
|
|
|
def _commands_plugin_paths(stack_name: str):
|
|
|
|
plugin_paths = get_plugin_code_paths(stack_name)
|
2023-10-27 18:57:13 +00:00
|
|
|
ret = [p.joinpath("deploy", "commands.py") for p in plugin_paths]
|
|
|
|
return ret
|
2023-10-09 20:54:55 +00:00
|
|
|
|
|
|
|
|
2023-10-10 21:32:07 +00:00
|
|
|
# See: https://stackoverflow.com/a/54625079/1701505
|
|
|
|
def _has_method(o, name):
|
|
|
|
return callable(getattr(o, name, None))
|
|
|
|
|
|
|
|
|
2023-07-30 04:38:46 +00:00
|
|
|
def call_stack_deploy_init(deploy_command_context):
|
2023-07-24 02:54:05 +00:00
|
|
|
# Link with the python file in the stack
|
|
|
|
# Call a function in it
|
|
|
|
# If no function found, return None
|
2023-11-08 08:11:00 +00:00
|
|
|
python_file_paths = _commands_plugin_paths(deploy_command_context.stack)
|
2023-10-27 18:57:13 +00:00
|
|
|
|
|
|
|
ret = None
|
|
|
|
init_done = False
|
|
|
|
for python_file_path in python_file_paths:
|
|
|
|
if python_file_path.exists():
|
|
|
|
spec = util.spec_from_file_location("commands", python_file_path)
|
|
|
|
imported_stack = util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(imported_stack)
|
|
|
|
if _has_method(imported_stack, "init"):
|
|
|
|
if not init_done:
|
|
|
|
ret = imported_stack.init(deploy_command_context)
|
|
|
|
init_done = True
|
|
|
|
else:
|
|
|
|
# TODO: remove this restriction
|
|
|
|
print(f"Skipping init() from plugin {python_file_path}. Only one init() is allowed.")
|
|
|
|
return ret
|
2023-07-24 02:54:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
# TODO: fold this with function above
|
2023-08-23 21:20:28 +00:00
|
|
|
def call_stack_deploy_setup(deploy_command_context, parameters: LaconicStackSetupCommand, extra_args):
|
2023-07-24 02:54:05 +00:00
|
|
|
# Link with the python file in the stack
|
|
|
|
# Call a function in it
|
|
|
|
# If no function found, return None
|
2023-11-08 08:11:00 +00:00
|
|
|
python_file_paths = _commands_plugin_paths(deploy_command_context.stack)
|
2023-10-27 18:57:13 +00:00
|
|
|
for python_file_path in python_file_paths:
|
|
|
|
if python_file_path.exists():
|
|
|
|
spec = util.spec_from_file_location("commands", python_file_path)
|
|
|
|
imported_stack = util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(imported_stack)
|
|
|
|
if _has_method(imported_stack, "setup"):
|
|
|
|
imported_stack.setup(deploy_command_context, parameters, extra_args)
|
2023-07-24 02:54:05 +00:00
|
|
|
|
|
|
|
|
2023-07-25 16:16:19 +00:00
|
|
|
# TODO: fold this with function above
|
2023-08-23 21:20:28 +00:00
|
|
|
def call_stack_deploy_create(deployment_context, extra_args):
|
2023-07-25 16:16:19 +00:00
|
|
|
# Link with the python file in the stack
|
|
|
|
# Call a function in it
|
|
|
|
# If no function found, return None
|
2023-11-08 08:11:00 +00:00
|
|
|
python_file_paths = _commands_plugin_paths(deployment_context.stack.name)
|
2023-10-27 18:57:13 +00:00
|
|
|
for python_file_path in python_file_paths:
|
|
|
|
if python_file_path.exists():
|
|
|
|
spec = util.spec_from_file_location("commands", python_file_path)
|
|
|
|
imported_stack = util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(imported_stack)
|
|
|
|
if _has_method(imported_stack, "create"):
|
|
|
|
imported_stack.create(deployment_context, extra_args)
|
2023-07-25 16:16:19 +00:00
|
|
|
|
|
|
|
|
2023-07-18 14:59:07 +00:00
|
|
|
# Inspect the pod yaml to find config files referenced in subdirectories
|
|
|
|
# other than the one associated with the pod
|
|
|
|
def _find_extra_config_dirs(parsed_pod_file, pod):
|
|
|
|
config_dirs = set()
|
|
|
|
services = parsed_pod_file["services"]
|
|
|
|
for service in services:
|
|
|
|
service_info = services[service]
|
|
|
|
if "volumes" in service_info:
|
|
|
|
for volume in service_info["volumes"]:
|
|
|
|
if ":" in volume:
|
|
|
|
host_path = volume.split(":")[0]
|
|
|
|
if host_path.startswith("../config"):
|
|
|
|
config_dir = host_path.split("/")[2]
|
|
|
|
if config_dir != pod:
|
|
|
|
config_dirs.add(config_dir)
|
|
|
|
return config_dirs
|
|
|
|
|
|
|
|
|
2023-09-04 18:14:05 +00:00
|
|
|
def _get_mapped_ports(stack: str, map_recipe: str):
|
|
|
|
port_map_recipes = ["any-variable-random", "localhost-same", "any-same", "localhost-fixed-random", "any-fixed-random"]
|
|
|
|
ports = _get_ports(stack)
|
|
|
|
if ports:
|
|
|
|
# Implement any requested mapping recipe
|
|
|
|
if map_recipe:
|
|
|
|
if map_recipe in port_map_recipes:
|
|
|
|
for service in ports.keys():
|
|
|
|
ports_array = ports[service]
|
|
|
|
for x in range(0, len(ports_array)):
|
|
|
|
orig_port = ports_array[x]
|
2023-09-19 19:27:34 +00:00
|
|
|
# Strip /udp suffix if present
|
|
|
|
bare_orig_port = orig_port.replace("/udp", "")
|
2023-09-04 19:00:23 +00:00
|
|
|
random_port = random.randint(20000, 50000) # Beware: we're relying on luck to not collide
|
2023-09-04 18:14:05 +00:00
|
|
|
if map_recipe == "any-variable-random":
|
|
|
|
# This is the default so take no action
|
|
|
|
pass
|
|
|
|
elif map_recipe == "localhost-same":
|
|
|
|
# Replace instances of "- XX" with "- 127.0.0.1:XX"
|
2023-09-19 19:27:34 +00:00
|
|
|
ports_array[x] = f"127.0.0.1:{bare_orig_port}:{orig_port}"
|
2023-09-04 18:14:05 +00:00
|
|
|
elif map_recipe == "any-same":
|
|
|
|
# Replace instances of "- XX" with "- 0.0.0.0:XX"
|
2023-09-19 19:27:34 +00:00
|
|
|
ports_array[x] = f"0.0.0.0:{bare_orig_port}:{orig_port}"
|
2023-09-04 18:14:05 +00:00
|
|
|
elif map_recipe == "localhost-fixed-random":
|
|
|
|
# Replace instances of "- XX" with "- 127.0.0.1:<rnd>:XX"
|
|
|
|
ports_array[x] = f"127.0.0.1:{random_port}:{orig_port}"
|
|
|
|
elif map_recipe == "any-fixed-random":
|
|
|
|
# Replace instances of "- XX" with "- 0.0.0.0:<rnd>:XX"
|
|
|
|
ports_array[x] = f"0.0.0.0:{random_port}:{orig_port}"
|
|
|
|
else:
|
|
|
|
print("Error: bad map_recipe")
|
|
|
|
else:
|
|
|
|
print(f"Error: --map-ports-to-host must specify one of: {port_map_recipes}")
|
|
|
|
sys.exit(1)
|
|
|
|
return ports
|
|
|
|
|
|
|
|
|
2023-10-03 18:49:15 +00:00
|
|
|
def _parse_config_variables(variable_values: str):
|
|
|
|
result = None
|
|
|
|
if variable_values:
|
|
|
|
value_pairs = variable_values.split(",")
|
|
|
|
if len(value_pairs):
|
|
|
|
result_values = {}
|
|
|
|
for value_pair in value_pairs:
|
|
|
|
variable_value_pair = value_pair.split("=")
|
|
|
|
if len(variable_value_pair) != 2:
|
|
|
|
print(f"ERROR: config argument is not valid: {variable_values}")
|
|
|
|
sys.exit(1)
|
|
|
|
variable_name = variable_value_pair[0]
|
|
|
|
variable_value = variable_value_pair[1]
|
|
|
|
result_values[variable_name] = variable_value
|
2023-11-29 04:14:02 +00:00
|
|
|
result = result_values
|
2023-10-03 18:49:15 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.command()
|
2023-10-03 18:49:15 +00:00
|
|
|
@click.option("--config", help="Provide config variables for the deployment")
|
2023-11-29 04:14:02 +00:00
|
|
|
@click.option("--config-file", help="Provide config variables in a file for the deployment")
|
2023-11-20 16:12:57 +00:00
|
|
|
@click.option("--kube-config", help="Provide a config file for a k8s deployment")
|
2023-11-21 03:23:55 +00:00
|
|
|
@click.option("--image-registry", help="Provide a container image registry url for this k8s cluster")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.option("--output", required=True, help="Write yaml spec file here")
|
2023-09-04 18:14:05 +00:00
|
|
|
@click.option("--map-ports-to-host", required=False,
|
2023-09-04 19:00:23 +00:00
|
|
|
help="Map ports to the host as one of: any-variable-random (default), "
|
|
|
|
"localhost-same, any-same, localhost-fixed-random, any-fixed-random")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.pass_context
|
2023-11-29 04:14:02 +00:00
|
|
|
def init(ctx, config, config_file, kube_config, image_registry, output, map_ports_to_host):
|
2023-06-27 22:58:41 +00:00
|
|
|
stack = global_options(ctx).stack
|
2023-11-20 16:12:57 +00:00
|
|
|
deployer_type = ctx.obj.deployer.type
|
2023-11-28 05:02:16 +00:00
|
|
|
deploy_command_context = ctx.obj
|
|
|
|
return init_operation(
|
|
|
|
deploy_command_context,
|
|
|
|
stack, deployer_type,
|
2023-11-29 04:14:02 +00:00
|
|
|
config, config_file,
|
|
|
|
kube_config,
|
2023-11-28 05:02:16 +00:00
|
|
|
image_registry,
|
|
|
|
output,
|
|
|
|
map_ports_to_host)
|
|
|
|
|
|
|
|
|
|
|
|
# The init command's implementation is in a separate function so that we can
|
|
|
|
# call it from other commands, bypassing the click decoration stuff
|
2023-11-29 04:14:02 +00:00
|
|
|
def init_operation(deploy_command_context, stack, deployer_type, config,
|
|
|
|
config_file, kube_config, image_registry, output, map_ports_to_host):
|
2023-12-06 05:56:58 +00:00
|
|
|
|
2023-11-28 05:02:16 +00:00
|
|
|
default_spec_file_content = call_stack_deploy_init(deploy_command_context)
|
2023-11-21 03:23:55 +00:00
|
|
|
spec_file_content = {"stack": stack, constants.deploy_to_key: deployer_type}
|
2023-11-20 16:12:57 +00:00
|
|
|
if deployer_type == "k8s":
|
2023-11-28 05:02:16 +00:00
|
|
|
if kube_config is None:
|
|
|
|
error_exit("--kube-config must be supplied with --deploy-to k8s")
|
|
|
|
if image_registry is None:
|
|
|
|
error_exit("--image-registry must be supplied with --deploy-to k8s")
|
2023-11-20 16:12:57 +00:00
|
|
|
spec_file_content.update({constants.kube_config_key: kube_config})
|
2023-11-21 03:23:55 +00:00
|
|
|
spec_file_content.update({constants.image_resigtry_key: image_registry})
|
|
|
|
else:
|
|
|
|
# Check for --kube-config supplied for non-relevant deployer types
|
|
|
|
if kube_config is not None:
|
|
|
|
error_exit(f"--kube-config is not allowed with a {deployer_type} deployment")
|
|
|
|
if image_registry is not None:
|
|
|
|
error_exit(f"--image-registry is not allowed with a {deployer_type} deployment")
|
2023-07-25 16:16:19 +00:00
|
|
|
if default_spec_file_content:
|
|
|
|
spec_file_content.update(default_spec_file_content)
|
2023-10-03 18:49:15 +00:00
|
|
|
config_variables = _parse_config_variables(config)
|
2023-11-29 04:14:02 +00:00
|
|
|
# Implement merge, since update() overwrites
|
2023-10-03 18:49:15 +00:00
|
|
|
if config_variables:
|
2023-10-31 17:29:19 +00:00
|
|
|
orig_config = spec_file_content.get("config", {})
|
2023-11-29 04:14:02 +00:00
|
|
|
new_config = config_variables
|
2023-10-03 18:49:15 +00:00
|
|
|
merged_config = {**new_config, **orig_config}
|
|
|
|
spec_file_content.update({"config": merged_config})
|
2023-11-29 04:14:02 +00:00
|
|
|
if config_file:
|
|
|
|
config_file_path = Path(config_file)
|
|
|
|
if not config_file_path.exists():
|
|
|
|
error_exit(f"config file: {config_file} does not exist")
|
|
|
|
config_file_variables = env_var_map_from_file(config_file_path)
|
|
|
|
if config_file_variables:
|
|
|
|
orig_config = spec_file_content.get("config", {})
|
|
|
|
new_config = config_file_variables
|
|
|
|
merged_config = {**new_config, **orig_config}
|
|
|
|
spec_file_content.update({"config": merged_config})
|
2023-08-11 20:25:54 +00:00
|
|
|
|
2023-09-04 18:14:05 +00:00
|
|
|
ports = _get_mapped_ports(stack, map_ports_to_host)
|
2023-11-21 23:04:36 +00:00
|
|
|
spec_file_content.update({"network": {"ports": ports}})
|
2023-08-11 20:25:54 +00:00
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
named_volumes = _get_named_volumes(stack)
|
|
|
|
if named_volumes:
|
|
|
|
volume_descriptors = {}
|
2024-01-31 05:09:48 +00:00
|
|
|
configmap_descriptors = {}
|
|
|
|
for named_volume in named_volumes["rw"]:
|
2023-07-24 02:54:05 +00:00
|
|
|
volume_descriptors[named_volume] = f"./data/{named_volume}"
|
2024-01-31 05:09:48 +00:00
|
|
|
for named_volume in named_volumes["ro"]:
|
2024-02-03 02:05:15 +00:00
|
|
|
if "k8s" in deployer_type and "config" in named_volume:
|
|
|
|
configmap_descriptors[named_volume] = f"./data/{named_volume}"
|
|
|
|
else:
|
|
|
|
volume_descriptors[named_volume] = f"./data/{named_volume}"
|
2024-01-31 21:11:32 +00:00
|
|
|
if volume_descriptors:
|
|
|
|
spec_file_content["volumes"] = volume_descriptors
|
2024-01-31 05:09:48 +00:00
|
|
|
if configmap_descriptors:
|
|
|
|
spec_file_content["configmaps"] = configmap_descriptors
|
2023-08-11 20:25:54 +00:00
|
|
|
|
2023-12-06 05:56:58 +00:00
|
|
|
if opts.o.debug:
|
|
|
|
print(f"Creating spec file for stack: {stack} with content: {spec_file_content}")
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
with open(output, "w") as output_file:
|
2023-12-06 05:56:58 +00:00
|
|
|
get_yaml().dump(spec_file_content, output_file)
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
2023-10-03 18:49:15 +00:00
|
|
|
def _write_config_file(spec_file: Path, config_env_file: Path):
|
|
|
|
spec_content = get_parsed_deployment_spec(spec_file)
|
2023-10-09 20:54:55 +00:00
|
|
|
# Note: we want to write an empty file even if we have no config variables
|
|
|
|
with open(config_env_file, "w") as output_file:
|
|
|
|
if "config" in spec_content and spec_content["config"]:
|
|
|
|
config_vars = spec_content["config"]
|
|
|
|
if config_vars:
|
2023-10-03 18:49:15 +00:00
|
|
|
for variable_name, variable_value in config_vars.items():
|
|
|
|
output_file.write(f"{variable_name}={variable_value}\n")
|
|
|
|
|
|
|
|
|
2023-11-20 16:12:57 +00:00
|
|
|
def _write_kube_config_file(external_path: Path, internal_path: Path):
|
|
|
|
if not external_path.exists():
|
|
|
|
error_exit(f"Kube config file {external_path} does not exist")
|
|
|
|
copyfile(external_path, internal_path)
|
|
|
|
|
|
|
|
|
2023-10-09 20:54:55 +00:00
|
|
|
def _copy_files_to_directory(file_paths: List[Path], directory: Path):
|
|
|
|
for path in file_paths:
|
|
|
|
# Using copy to preserve the execute bit
|
|
|
|
copy(path, os.path.join(directory, os.path.basename(path)))
|
|
|
|
|
|
|
|
|
2023-12-06 05:56:58 +00:00
|
|
|
def _create_deployment_file(deployment_dir: Path):
|
|
|
|
deployment_file_path = deployment_dir.joinpath(constants.deployment_file_name)
|
|
|
|
cluster = f"{constants.cluster_name_prefix}{token_hex(8)}"
|
|
|
|
with open(deployment_file_path, "w") as output_file:
|
|
|
|
output_file.write(f"{constants.cluster_id_key}: {cluster}\n")
|
|
|
|
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.command()
|
|
|
|
@click.option("--spec-file", required=True, help="Spec file to use to create this deployment")
|
|
|
|
@click.option("--deployment-dir", help="Create deployment files in this directory")
|
2023-08-23 21:20:28 +00:00
|
|
|
# TODO: Hack
|
|
|
|
@click.option("--network-dir", help="Network configuration supplied in this directory")
|
2023-09-04 21:13:23 +00:00
|
|
|
@click.option("--initial-peers", help="Initial set of persistent peers")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.pass_context
|
2023-09-04 21:13:23 +00:00
|
|
|
def create(ctx, spec_file, deployment_dir, network_dir, initial_peers):
|
2023-11-28 05:02:16 +00:00
|
|
|
deployment_command_context = ctx.obj
|
|
|
|
return create_operation(deployment_command_context, spec_file, deployment_dir, network_dir, initial_peers)
|
|
|
|
|
|
|
|
|
|
|
|
# The init command's implementation is in a separate function so that we can
|
|
|
|
# call it from other commands, bypassing the click decoration stuff
|
|
|
|
def create_operation(deployment_command_context, spec_file, deployment_dir, network_dir, initial_peers):
|
2023-06-27 22:58:41 +00:00
|
|
|
parsed_spec = get_parsed_deployment_spec(spec_file)
|
2023-11-08 08:11:00 +00:00
|
|
|
stack_name = parsed_spec["stack"]
|
2023-11-21 03:23:55 +00:00
|
|
|
deployment_type = parsed_spec[constants.deploy_to_key]
|
2023-06-27 22:58:41 +00:00
|
|
|
stack_file = get_stack_file_path(stack_name)
|
|
|
|
parsed_stack = get_parsed_stack_config(stack_name)
|
2023-11-28 05:02:16 +00:00
|
|
|
if opts.o.debug:
|
2023-06-27 22:58:41 +00:00
|
|
|
print(f"parsed spec: {parsed_spec}")
|
|
|
|
if deployment_dir is None:
|
2023-11-20 16:12:57 +00:00
|
|
|
deployment_dir_path = _make_default_deployment_dir()
|
|
|
|
else:
|
|
|
|
deployment_dir_path = Path(deployment_dir)
|
|
|
|
if deployment_dir_path.exists():
|
|
|
|
error_exit(f"{deployment_dir_path} already exists")
|
|
|
|
os.mkdir(deployment_dir_path)
|
2023-06-27 22:58:41 +00:00
|
|
|
# Copy spec file and the stack file into the deployment dir
|
2023-11-29 04:14:02 +00:00
|
|
|
copyfile(spec_file, deployment_dir_path.joinpath(constants.spec_file_name))
|
2023-11-20 16:12:57 +00:00
|
|
|
copyfile(stack_file, deployment_dir_path.joinpath(os.path.basename(stack_file)))
|
2023-12-06 05:56:58 +00:00
|
|
|
_create_deployment_file(deployment_dir_path)
|
2023-10-03 18:49:15 +00:00
|
|
|
# Copy any config varibles from the spec file into an env file suitable for compose
|
2023-11-29 04:14:02 +00:00
|
|
|
_write_config_file(spec_file, deployment_dir_path.joinpath(constants.config_file_name))
|
2023-11-20 16:12:57 +00:00
|
|
|
# Copy any k8s config file into the deployment dir
|
|
|
|
if deployment_type == "k8s":
|
|
|
|
_write_kube_config_file(Path(parsed_spec[constants.kube_config_key]),
|
|
|
|
deployment_dir_path.joinpath(constants.kube_config_filename))
|
2023-06-27 22:58:41 +00:00
|
|
|
# Copy the pod files into the deployment dir, fixing up content
|
2023-10-09 20:54:55 +00:00
|
|
|
pods = get_pod_list(parsed_stack)
|
2023-11-20 16:12:57 +00:00
|
|
|
destination_compose_dir = deployment_dir_path.joinpath("compose")
|
2023-06-27 22:58:41 +00:00
|
|
|
os.mkdir(destination_compose_dir)
|
2023-11-20 16:12:57 +00:00
|
|
|
destination_pods_dir = deployment_dir_path.joinpath("pods")
|
2023-10-09 20:54:55 +00:00
|
|
|
os.mkdir(destination_pods_dir)
|
2023-10-25 04:13:09 +00:00
|
|
|
data_dir = Path(__file__).absolute().parent.parent.joinpath("data")
|
2023-07-24 02:54:05 +00:00
|
|
|
yaml = get_yaml()
|
2023-06-27 22:58:41 +00:00
|
|
|
for pod in pods:
|
2023-10-09 20:54:55 +00:00
|
|
|
pod_file_path = get_pod_file_path(parsed_stack, pod)
|
2023-06-27 22:58:41 +00:00
|
|
|
parsed_pod_file = yaml.load(open(pod_file_path, "r"))
|
2023-07-18 14:59:07 +00:00
|
|
|
extra_config_dirs = _find_extra_config_dirs(parsed_pod_file, pod)
|
2023-11-20 16:12:57 +00:00
|
|
|
destination_pod_dir = destination_pods_dir.joinpath(pod)
|
2023-10-09 20:54:55 +00:00
|
|
|
os.mkdir(destination_pod_dir)
|
2023-11-28 05:02:16 +00:00
|
|
|
if opts.o.debug:
|
2023-07-18 14:59:07 +00:00
|
|
|
print(f"extra config dirs: {extra_config_dirs}")
|
2023-06-28 03:18:04 +00:00
|
|
|
_fixup_pod_file(parsed_pod_file, parsed_spec, destination_compose_dir)
|
2023-11-20 16:12:57 +00:00
|
|
|
with open(destination_compose_dir.joinpath("docker-compose-%s.yml" % pod), "w") as output_file:
|
2023-06-27 22:58:41 +00:00
|
|
|
yaml.dump(parsed_pod_file, output_file)
|
|
|
|
# Copy the config files for the pod, if any
|
2023-07-18 14:59:07 +00:00
|
|
|
config_dirs = {pod}
|
|
|
|
config_dirs = config_dirs.union(extra_config_dirs)
|
|
|
|
for config_dir in config_dirs:
|
|
|
|
source_config_dir = data_dir.joinpath("config", config_dir)
|
|
|
|
if os.path.exists(source_config_dir):
|
2023-11-20 16:12:57 +00:00
|
|
|
destination_config_dir = deployment_dir_path.joinpath("config", config_dir)
|
2023-07-18 14:59:07 +00:00
|
|
|
# If the same config dir appears in multiple pods, it may already have been copied
|
|
|
|
if not os.path.exists(destination_config_dir):
|
|
|
|
copytree(source_config_dir, destination_config_dir)
|
2023-10-09 20:54:55 +00:00
|
|
|
# Copy the script files for the pod, if any
|
|
|
|
if pod_has_scripts(parsed_stack, pod):
|
2023-11-20 16:12:57 +00:00
|
|
|
destination_script_dir = destination_pod_dir.joinpath("scripts")
|
2023-10-09 20:54:55 +00:00
|
|
|
os.mkdir(destination_script_dir)
|
|
|
|
script_paths = get_pod_script_paths(parsed_stack, pod)
|
|
|
|
_copy_files_to_directory(script_paths, destination_script_dir)
|
2023-07-25 16:16:19 +00:00
|
|
|
# Delegate to the stack's Python code
|
2023-07-31 19:55:03 +00:00
|
|
|
# The deploy create command doesn't require a --stack argument so we need to insert the
|
|
|
|
# stack member here.
|
|
|
|
deployment_command_context.stack = stack_name
|
2023-11-08 08:11:00 +00:00
|
|
|
deployment_context = DeploymentContext()
|
2023-11-20 16:12:57 +00:00
|
|
|
deployment_context.init(deployment_dir_path)
|
2023-11-06 06:21:53 +00:00
|
|
|
# Call the deployer to generate any deployer-specific files (e.g. for kind)
|
2024-02-05 19:39:04 +00:00
|
|
|
deployer_config_generator = getDeployerConfigGenerator(deployment_type, deployment_context)
|
2023-11-20 16:12:57 +00:00
|
|
|
# TODO: make deployment_dir_path a Path above
|
|
|
|
deployer_config_generator.generate(deployment_dir_path)
|
2024-01-10 04:53:43 +00:00
|
|
|
call_stack_deploy_create(deployment_context, [network_dir, initial_peers, deployment_command_context])
|
2023-07-24 02:54:05 +00:00
|
|
|
|
|
|
|
|
2023-08-23 21:20:28 +00:00
|
|
|
# TODO: this code should be in the stack .py files but
|
|
|
|
# we haven't yet figured out how to integrate click across
|
|
|
|
# the plugin boundary
|
2023-07-24 02:54:05 +00:00
|
|
|
@click.command()
|
2023-08-23 21:20:28 +00:00
|
|
|
@click.option("--node-moniker", help="Moniker for this node")
|
|
|
|
@click.option("--chain-id", help="The new chain id")
|
|
|
|
@click.option("--key-name", help="Name for new node key")
|
|
|
|
@click.option("--gentx-files", help="List of comma-delimited gentx filenames from other nodes")
|
|
|
|
@click.option("--genesis-file", help="Genesis file for the network")
|
|
|
|
@click.option("--initialize-network", is_flag=True, default=False, help="Initialize phase")
|
|
|
|
@click.option("--join-network", is_flag=True, default=False, help="Join phase")
|
|
|
|
@click.option("--create-network", is_flag=True, default=False, help="Create phase")
|
|
|
|
@click.option("--network-dir", help="Directory for network files")
|
2023-07-30 04:38:46 +00:00
|
|
|
@click.argument('extra_args', nargs=-1)
|
2023-07-24 02:54:05 +00:00
|
|
|
@click.pass_context
|
2023-08-23 21:20:28 +00:00
|
|
|
def setup(ctx, node_moniker, chain_id, key_name, gentx_files, genesis_file, initialize_network, join_network, create_network,
|
|
|
|
network_dir, extra_args):
|
|
|
|
parmeters = LaconicStackSetupCommand(chain_id, node_moniker, key_name, initialize_network, join_network, create_network,
|
|
|
|
gentx_files, genesis_file, network_dir)
|
|
|
|
call_stack_deploy_setup(ctx.obj, parmeters, extra_args)
|