Initial version

This commit is contained in:
David Boreham 2023-07-29 13:35:04 -06:00
parent 63a94bae0e
commit fd647fd2de
5 changed files with 85 additions and 47 deletions

View File

@ -13,9 +13,10 @@
# 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/>.
from dataclasses import dataclass
from app.util import get_yaml
from app.deploy_types import DeployCommandContext, DeploymentContext
from app.stack_state import State
from app.deploy_util import VolumeMapping, run_container_command
default_spec_file_content = """config:
node_moniker: my-node-name
@ -25,38 +26,26 @@ default_spec_file_content = """config:
init_help_text = """Add helpful text here on setting config variables.
"""
@dataclass
class VolumeMapping:
host_path: str
container_path: str
# In order to make this, we need the ability to run the stack
# In theory we can make this same way as we would run deploy up
def run_container_command(ctx, ontainer, command, mounts):
deploy_context = ctx.obj
pass
def setup(ctx):
def setup(command_context: DeployCommandContext):
node_moniker = "dbdb-node"
chain_id = "laconic_81337-1"
mounts = [
VolumeMapping("./path", "~/.laconicd")
]
output, status = run_container_command(ctx, "laconicd", f"laconicd init {node_moniker} --chain-id {chain_id}", mounts)
output, status = run_container_command(command_context.cluster_context, "laconicd", f"laconicd init {node_moniker} --chain-id {chain_id}", mounts)
def init(command_context):
def init(command_context: DeployCommandContext):
print(init_help_text)
yaml = get_yaml()
return yaml.load(default_spec_file_content)
def get_state(command_context):
def get_state(command_context: DeployCommandContext):
print("Here we get state")
return State.CONFIGURED
def change_state(command_context):
def change_state(command_context: DeployCommandContext):
pass

View File

@ -27,17 +27,12 @@ from python_on_whales import DockerClient, DockerException
import click
from pathlib import Path
from app.util import include_exclude_check, get_parsed_stack_config, global_options2
from app.deploy_types import ClusterContext, DeployCommandContext
from app.deployment_create import create as deployment_create
from app.deployment_create import init as deployment_init
from app.deployment_create import setup as deployment_setup
class DeployCommandContext(object):
def __init__(self, cluster_context, docker):
self.cluster_context = cluster_context
self.docker = docker
@click.group()
@click.option("--include", help="only start these components")
@click.option("--exclude", help="don\'t start these components")
@ -313,17 +308,7 @@ def _make_cluster_context(ctx, stack, include, exclude, cluster, env_file):
if ctx.verbose:
print(f"files: {compose_files}")
return cluster_context(cluster, compose_files, pre_start_commands, post_start_commands, cluster_config, env_file)
class cluster_context:
def __init__(self, cluster, compose_files, pre_start_commands, post_start_commands, config, env_file) -> None:
self.cluster = cluster
self.compose_files = compose_files
self.pre_start_commands = pre_start_commands
self.post_start_commands = post_start_commands
self.config = config
self.env_file = env_file
return ClusterContext(cluster, compose_files, pre_start_commands, post_start_commands, cluster_config, env_file)
def _convert_to_new_format(old_pod_array):

47
app/deploy_types.py Normal file
View File

@ -0,0 +1,47 @@
# Copyright © 2023 Cerc
# 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/>.
from typing import List
from dataclasses import dataclass
from pathlib import Path
from python_on_whales import DockerClient
@dataclass
class ClusterContext:
cluster: str
compose_files: List[str]
pre_start_commands: List[str]
post_start_commands: List[str]
config: str
env_file: str
@dataclass
class DeployCommandContext:
cluster_context: ClusterContext
docker: DockerClient
@dataclass
class DeploymentContext:
stack: str
deployment_dir: Path
command_context: DeployCommandContext
@dataclass
class VolumeMapping:
host_path: str
container_path: str

20
app/deploy_util.py Normal file
View File

@ -0,0 +1,20 @@
# Copyright © 2022, 2023 Cerc
# 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/>.
from dataclasses import dataclass
from app.deploy_types import DeploymentContext, VolumeMapping
def run_container_command(ctx: DeploymentContext, container, command, mounts):
print("Write some code!")

View File

@ -21,11 +21,7 @@ from pathlib import Path
from shutil import copyfile, copytree
import sys
from app.util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options, get_yaml
@dataclass
class DeploymentContext:
stack: str
deployment_dir: Path
import app.deploy_types
def _make_default_deployment_dir():
@ -94,7 +90,7 @@ def _fixup_pod_file(pod, spec, compose_dir):
pod["volumes"][volume] = new_volume_spec
def call_stack_deploy_init(stack):
def call_stack_deploy_init(deployment_context):
# Link with the python file in the stack
# Call a function in it
# If no function found, return None
@ -102,19 +98,19 @@ def call_stack_deploy_init(stack):
spec = util.spec_from_file_location("commands", python_file_path)
imported_stack = util.module_from_spec(spec)
spec.loader.exec_module(imported_stack)
return imported_stack.init(None)
return imported_stack.init(deployment_context)
# TODO: fold this with function above
def call_stack_deploy_setup(stack):
def call_stack_deploy_setup(deployment_context):
# Link with the python file in the stack
# Call a function in it
# If no function found, return None
python_file_path = get_stack_file_path(stack).parent.joinpath("deploy", "commands.py")
python_file_path = get_stack_file_path(deployment_context.stack).parent.joinpath("deploy", "commands.py")
spec = util.spec_from_file_location("commands", python_file_path)
imported_stack = util.module_from_spec(spec)
spec.loader.exec_module(imported_stack)
return imported_stack.setup(None)
return imported_stack.setup(deployment_context)
# TODO: fold this with function above
@ -229,5 +225,6 @@ def create(ctx, spec_file, deployment_dir):
@click.option("--create-network", is_flag=True, default=False, help="Help goes here")
@click.pass_context
def setup(ctx, node_moniker, key_name, initialize_network, join_network, create_network):
stack = global_options(ctx).stack
call_stack_deploy_setup(stack)
stack_name = global_options(ctx).stack
deployment_context = DeploymentContext(stack_name, Path(deployment_dir))
call_stack_deploy_setup(deployment_context)