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
|
|
|
|
from pathlib import Path
|
|
|
|
import sys
|
2023-11-07 07:06:55 +00:00
|
|
|
from stack_orchestrator.deploy.deploy import up_operation, down_operation, ps_operation, port_operation
|
|
|
|
from stack_orchestrator.deploy.deploy import exec_operation, logs_operation, create_deploy_context
|
|
|
|
from stack_orchestrator.deploy.stack import Stack
|
|
|
|
from stack_orchestrator.deploy.spec import Spec
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeploymentContext:
|
|
|
|
dir: Path
|
2023-10-24 20:44:48 +00:00
|
|
|
spec: Spec
|
|
|
|
stack: Stack
|
2023-06-27 22:58:41 +00:00
|
|
|
|
2023-10-03 18:49:15 +00:00
|
|
|
def get_stack_file(self):
|
|
|
|
return self.dir.joinpath("stack.yml")
|
|
|
|
|
2023-10-24 20:44:48 +00:00
|
|
|
def get_spec_file(self):
|
|
|
|
return self.dir.joinpath("spec.yml")
|
|
|
|
|
2023-10-03 18:49:15 +00:00
|
|
|
def get_env_file(self):
|
|
|
|
return self.dir.joinpath("config.env")
|
|
|
|
|
|
|
|
# TODO: implement me
|
|
|
|
def get_cluster_name(self):
|
|
|
|
return None
|
|
|
|
|
2023-10-24 20:44:48 +00:00
|
|
|
def init(self, dir):
|
|
|
|
self.dir = dir
|
|
|
|
self.stack = Stack()
|
|
|
|
self.stack.init_from_file(self.get_stack_file())
|
|
|
|
self.spec = Spec()
|
|
|
|
self.spec.init_from_file(self.get_spec_file())
|
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.option("--dir", required=True, help="path to deployment directory")
|
|
|
|
@click.pass_context
|
|
|
|
def command(ctx, dir):
|
2023-10-24 20:44:48 +00:00
|
|
|
'''manage a deployment'''
|
2023-08-24 12:50:42 +00:00
|
|
|
|
2023-06-27 22:58:41 +00:00
|
|
|
# Check that --stack wasn't supplied
|
|
|
|
if ctx.parent.obj.stack:
|
|
|
|
print("Error: --stack can't be supplied with the deployment command")
|
|
|
|
sys.exit(1)
|
|
|
|
# Check dir is valid
|
|
|
|
dir_path = Path(dir)
|
|
|
|
if not dir_path.exists():
|
|
|
|
print(f"Error: deployment directory {dir} does not exist")
|
|
|
|
sys.exit(1)
|
|
|
|
if not dir_path.is_dir():
|
|
|
|
print(f"Error: supplied deployment directory path {dir} exists but is a file not a directory")
|
|
|
|
sys.exit(1)
|
|
|
|
# Store the deployment context for subcommands
|
2023-10-24 20:44:48 +00:00
|
|
|
deployment_context = DeploymentContext()
|
|
|
|
deployment_context.init(dir_path)
|
|
|
|
ctx.obj = deployment_context
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def make_deploy_context(ctx):
|
2023-10-24 20:44:48 +00:00
|
|
|
context: DeploymentContext = ctx.obj
|
|
|
|
stack_file_path = context.get_stack_file()
|
|
|
|
env_file = context.get_env_file()
|
|
|
|
cluster_name = context.get_cluster_name()
|
|
|
|
return create_deploy_context(ctx.parent.parent.obj, stack_file_path, None, None, cluster_name, env_file,
|
|
|
|
context.spec.obj["deploy-to"])
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
2023-07-25 16:16:19 +00:00
|
|
|
@click.option("--stay-attached/--detatch-terminal", default=False, help="detatch or not to see container stdout")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: up <service1> <service2>
|
|
|
|
@click.pass_context
|
2023-07-25 16:16:19 +00:00
|
|
|
def up(ctx, stay_attached, extra_args):
|
2023-06-27 22:58:41 +00:00
|
|
|
ctx.obj = make_deploy_context(ctx)
|
|
|
|
services_list = list(extra_args) or None
|
2023-07-25 16:16:19 +00:00
|
|
|
up_operation(ctx, services_list, stay_attached)
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# start is the preferred alias for up
|
|
|
|
@command.command()
|
2023-07-25 16:16:19 +00:00
|
|
|
@click.option("--stay-attached/--detatch-terminal", default=False, help="detatch or not to see container stdout")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: up <service1> <service2>
|
|
|
|
@click.pass_context
|
2023-07-25 16:16:19 +00:00
|
|
|
def start(ctx, stay_attached, extra_args):
|
2023-06-27 22:58:41 +00:00
|
|
|
ctx.obj = make_deploy_context(ctx)
|
|
|
|
services_list = list(extra_args) or None
|
2023-07-25 16:16:19 +00:00
|
|
|
up_operation(ctx, services_list, stay_attached)
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
2023-07-18 14:59:07 +00:00
|
|
|
@click.option("--delete-volumes/--preserve-volumes", default=False, help="delete data volumes")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: down <service1> <service2>
|
|
|
|
@click.pass_context
|
2023-07-18 14:59:07 +00:00
|
|
|
def down(ctx, delete_volumes, extra_args):
|
2023-06-27 22:58:41 +00:00
|
|
|
# Get the stack config file name
|
|
|
|
# TODO: add cluster name and env file here
|
|
|
|
ctx.obj = make_deploy_context(ctx)
|
2023-07-18 14:59:07 +00:00
|
|
|
down_operation(ctx, delete_volumes, extra_args)
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# stop is the preferred alias for down
|
|
|
|
@command.command()
|
2023-07-20 12:25:31 +00:00
|
|
|
@click.option("--delete-volumes/--preserve-volumes", default=False, help="delete data volumes")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: down <service1> <service2>
|
|
|
|
@click.pass_context
|
2023-07-20 12:25:31 +00:00
|
|
|
def stop(ctx, delete_volumes, extra_args):
|
2023-06-27 22:58:41 +00:00
|
|
|
# TODO: add cluster name and env file here
|
|
|
|
ctx.obj = make_deploy_context(ctx)
|
2023-07-25 16:16:19 +00:00
|
|
|
down_operation(ctx, delete_volumes, extra_args)
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
|
|
|
@click.pass_context
|
|
|
|
def ps(ctx):
|
|
|
|
ctx.obj = make_deploy_context(ctx)
|
|
|
|
ps_operation(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: port <service1> <service2>
|
|
|
|
@click.pass_context
|
|
|
|
def port(ctx, extra_args):
|
|
|
|
port_operation(ctx, extra_args)
|
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: exec <service> <command>
|
|
|
|
@click.pass_context
|
|
|
|
def exec(ctx, extra_args):
|
|
|
|
ctx.obj = make_deploy_context(ctx)
|
|
|
|
exec_operation(ctx, extra_args)
|
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
2023-08-01 04:10:37 +00:00
|
|
|
@click.option("--tail", "-n", default=None, help="number of lines to display")
|
|
|
|
@click.option("--follow", "-f", is_flag=True, default=False, help="follow log output")
|
2023-06-27 22:58:41 +00:00
|
|
|
@click.argument('extra_args', nargs=-1) # help: command: logs <service1> <service2>
|
|
|
|
@click.pass_context
|
2023-08-01 04:10:37 +00:00
|
|
|
def logs(ctx, tail, follow, extra_args):
|
2023-06-27 22:58:41 +00:00
|
|
|
ctx.obj = make_deploy_context(ctx)
|
2023-08-01 04:10:37 +00:00
|
|
|
logs_operation(ctx, tail, follow, extra_args)
|
2023-06-27 22:58:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@command.command()
|
|
|
|
@click.pass_context
|
|
|
|
def status(ctx):
|
|
|
|
print(f"Context: {ctx.parent.obj}")
|