From 831a8cd5f790a0cb8c9c8b43955507b2c91eea5b Mon Sep 17 00:00:00 2001 From: David Boreham Date: Tue, 27 Jun 2023 16:58:41 -0600 Subject: [PATCH] Deployments feature (#433) Basic deployments framework --- app/base.py | 2 +- .../docker-compose-fixturenet-laconicd.yml | 3 + .../docker-compose-mainnet-laconicd.yml | 30 ++++ .../mainnet-laconicd/create-fixturenet.sh | 118 +++++++++++++++ .../mainnet-laconicd/export-myaddress.sh | 2 + .../config/mainnet-laconicd/export-mykey.sh | 2 + .../registry-cli-config-template.yml | 9 ++ app/data/pod-list.txt | 1 - app/data/stacks/mainnet-laconic/README.md | 2 + .../stacks/mainnet-laconic/deploy/commands.py | 57 +++++++ app/data/stacks/mainnet-laconic/stack.yml | 31 ++++ .../test/run-mainnet-laconic-test.sh | 48 ++++++ app/{deploy_system.py => deploy.py} | 138 +++++++++++------ app/deployment.py | 140 ++++++++++++++++++ app/deployment_create.py | 139 +++++++++++++++++ app/util.py | 32 +++- cli.py | 8 +- requirements.txt | 1 + 18 files changed, 710 insertions(+), 53 deletions(-) create mode 100644 app/data/compose/docker-compose-mainnet-laconicd.yml create mode 100644 app/data/config/mainnet-laconicd/create-fixturenet.sh create mode 100644 app/data/config/mainnet-laconicd/export-myaddress.sh create mode 100644 app/data/config/mainnet-laconicd/export-mykey.sh create mode 100644 app/data/config/mainnet-laconicd/registry-cli-config-template.yml create mode 100644 app/data/stacks/mainnet-laconic/README.md create mode 100644 app/data/stacks/mainnet-laconic/deploy/commands.py create mode 100644 app/data/stacks/mainnet-laconic/stack.yml create mode 100755 app/data/stacks/mainnet-laconic/test/run-mainnet-laconic-test.sh rename app/{deploy_system.py => deploy.py} (86%) create mode 100644 app/deployment.py create mode 100644 app/deployment_create.py diff --git a/app/base.py b/app/base.py index 526c7c6c..a7721141 100644 --- a/app/base.py +++ b/app/base.py @@ -15,7 +15,7 @@ import os from abc import ABC, abstractmethod -from .deploy_system import get_stack_status +from .deploy import get_stack_status from decouple import config diff --git a/app/data/compose/docker-compose-fixturenet-laconicd.yml b/app/data/compose/docker-compose-fixturenet-laconicd.yml index 21c9fb77..5037687c 100644 --- a/app/data/compose/docker-compose-fixturenet-laconicd.yml +++ b/app/data/compose/docker-compose-fixturenet-laconicd.yml @@ -25,3 +25,6 @@ services: image: cerc/laconic-registry-cli:local volumes: - ../config/fixturenet-laconicd/registry-cli-config-template.yml:/registry-cli-config-template.yml + +volumes: + laconicd-data: diff --git a/app/data/compose/docker-compose-mainnet-laconicd.yml b/app/data/compose/docker-compose-mainnet-laconicd.yml new file mode 100644 index 00000000..4de68d7b --- /dev/null +++ b/app/data/compose/docker-compose-mainnet-laconicd.yml @@ -0,0 +1,30 @@ +services: + laconicd: + restart: no + image: cerc/laconicd:local + command: ["sh", "/docker-entrypoint-scripts.d/create-fixturenet.sh"] + volumes: + # The cosmos-sdk node's database directory: + - laconicd-data:/root/.laconicd/data + # TODO: look at folding these scripts into the container + - ../config/mainnet-laconicd/create-fixturenet.sh:/docker-entrypoint-scripts.d/create-fixturenet.sh + - ../config/mainnet-laconicd/export-mykey.sh:/docker-entrypoint-scripts.d/export-mykey.sh + - ../config/mainnet-laconicd/export-myaddress.sh:/docker-entrypoint-scripts.d/export-myaddress.sh + # TODO: determine which of the ports below is really needed + ports: + - "6060" + - "26657" + - "26656" + - "9473:9473" + - "8545" + - "8546" + - "9090" + - "9091" + - "1317" + cli: + image: cerc/laconic-registry-cli:local + volumes: + - ../config/mainnet-laconicd/registry-cli-config-template.yml:/registry-cli-config-template.yml + +volumes: + laconicd-data: diff --git a/app/data/config/mainnet-laconicd/create-fixturenet.sh b/app/data/config/mainnet-laconicd/create-fixturenet.sh new file mode 100644 index 00000000..9c30bff8 --- /dev/null +++ b/app/data/config/mainnet-laconicd/create-fixturenet.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +# TODO: this file is now an unmodified copy of cerc-io/laconicd/init.sh +# so we should have a mechanism to bundle it inside the container rather than link from here +# at deploy time. + +KEY="mykey" +CHAINID="laconic_9000-1" +MONIKER="localtestnet" +KEYRING="test" +KEYALGO="eth_secp256k1" +LOGLEVEL="info" +# trace evm +TRACE="--trace" +# TRACE="" + +# validate dependencies are installed +command -v jq > /dev/null 2>&1 || { echo >&2 "jq not installed. More info: https://stedolan.github.io/jq/download/"; exit 1; } + +# remove existing daemon and client +rm -rf ~/.laconic* + +make install + +laconicd config keyring-backend $KEYRING +laconicd config chain-id $CHAINID + +# if $KEY exists it should be deleted +laconicd keys add $KEY --keyring-backend $KEYRING --algo $KEYALGO + +# Set moniker and chain-id for Ethermint (Moniker can be anything, chain-id must be an integer) +laconicd init $MONIKER --chain-id $CHAINID + +# Change parameter token denominations to aphoton +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["staking"]["params"]["bond_denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["crisis"]["constant_fee"]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["gov"]["deposit_params"]["min_deposit"][0]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["mint"]["params"]["mint_denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +# Custom modules +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["record_rent"]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_rent"]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_auction_commit_fee"]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_auction_reveal_fee"]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_auction_minimum_bid"]["denom"]="aphoton"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + +if [[ "$TEST_REGISTRY_EXPIRY" == "true" ]]; then + echo "Setting timers for expiry tests." + + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["record_rent_duration"]="60s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_grace_period"]="60s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_rent_duration"]="60s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +fi + +if [[ "$TEST_AUCTION_ENABLED" == "true" ]]; then + echo "Enabling auction and setting timers." + + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_auction_enabled"]=true' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_rent_duration"]="60s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_grace_period"]="300s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_auction_commits_duration"]="60s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + cat $HOME/.laconicd/config/genesis.json | jq '.app_state["registry"]["params"]["authority_auction_reveals_duration"]="60s"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json +fi + +# increase block time (?) +cat $HOME/.laconicd/config/genesis.json | jq '.consensus_params["block"]["time_iota_ms"]="1000"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + +# Set gas limit in genesis +cat $HOME/.laconicd/config/genesis.json | jq '.consensus_params["block"]["max_gas"]="10000000"' > $HOME/.laconicd/config/tmp_genesis.json && mv $HOME/.laconicd/config/tmp_genesis.json $HOME/.laconicd/config/genesis.json + +# disable produce empty block +if [[ "$OSTYPE" == "darwin"* ]]; then + sed -i '' 's/create_empty_blocks = true/create_empty_blocks = false/g' $HOME/.laconicd/config/config.toml + else + sed -i 's/create_empty_blocks = true/create_empty_blocks = false/g' $HOME/.laconicd/config/config.toml +fi + +if [[ $1 == "pending" ]]; then + if [[ "$OSTYPE" == "darwin"* ]]; then + sed -i '' 's/create_empty_blocks_interval = "0s"/create_empty_blocks_interval = "30s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_propose = "3s"/timeout_propose = "30s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_propose_delta = "500ms"/timeout_propose_delta = "5s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_prevote = "1s"/timeout_prevote = "10s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_prevote_delta = "500ms"/timeout_prevote_delta = "5s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_precommit = "1s"/timeout_precommit = "10s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_precommit_delta = "500ms"/timeout_precommit_delta = "5s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_commit = "5s"/timeout_commit = "150s"/g' $HOME/.laconicd/config/config.toml + sed -i '' 's/timeout_broadcast_tx_commit = "10s"/timeout_broadcast_tx_commit = "150s"/g' $HOME/.laconicd/config/config.toml + else + sed -i 's/create_empty_blocks_interval = "0s"/create_empty_blocks_interval = "30s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_propose = "3s"/timeout_propose = "30s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_propose_delta = "500ms"/timeout_propose_delta = "5s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_prevote = "1s"/timeout_prevote = "10s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_prevote_delta = "500ms"/timeout_prevote_delta = "5s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_precommit = "1s"/timeout_precommit = "10s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_precommit_delta = "500ms"/timeout_precommit_delta = "5s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_commit = "5s"/timeout_commit = "150s"/g' $HOME/.laconicd/config/config.toml + sed -i 's/timeout_broadcast_tx_commit = "10s"/timeout_broadcast_tx_commit = "150s"/g' $HOME/.laconicd/config/config.toml + fi +fi + +# Allocate genesis accounts (cosmos formatted addresses) +laconicd add-genesis-account $KEY 100000000000000000000000000aphoton --keyring-backend $KEYRING + +# Sign genesis transaction +laconicd gentx $KEY 1000000000000000000000aphoton --keyring-backend $KEYRING --chain-id $CHAINID + +# Collect genesis tx +laconicd collect-gentxs + +# Run this to ensure everything worked and that the genesis file is setup correctly +laconicd validate-genesis + +if [[ $1 == "pending" ]]; then + echo "pending mode is on, please wait for the first block committed." +fi + +# Start the node (remove the --pruning=nothing flag if historical queries are not needed) +laconicd start --pruning=nothing --evm.tracer=json $TRACE --log_level $LOGLEVEL --minimum-gas-prices=0.0001aphoton --json-rpc.api eth,txpool,personal,net,debug,web3,miner --api.enable --gql-server --gql-playground diff --git a/app/data/config/mainnet-laconicd/export-myaddress.sh b/app/data/config/mainnet-laconicd/export-myaddress.sh new file mode 100644 index 00000000..e454c0b0 --- /dev/null +++ b/app/data/config/mainnet-laconicd/export-myaddress.sh @@ -0,0 +1,2 @@ +#!/bin/sh +laconicd keys show mykey | grep address | cut -d ' ' -f 3 diff --git a/app/data/config/mainnet-laconicd/export-mykey.sh b/app/data/config/mainnet-laconicd/export-mykey.sh new file mode 100644 index 00000000..1a5be86e --- /dev/null +++ b/app/data/config/mainnet-laconicd/export-mykey.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo y | laconicd keys export mykey --unarmored-hex --unsafe diff --git a/app/data/config/mainnet-laconicd/registry-cli-config-template.yml b/app/data/config/mainnet-laconicd/registry-cli-config-template.yml new file mode 100644 index 00000000..16432c18 --- /dev/null +++ b/app/data/config/mainnet-laconicd/registry-cli-config-template.yml @@ -0,0 +1,9 @@ +services: + cns: + restEndpoint: 'http://laconicd:1317' + gqlEndpoint: 'http://laconicd:9473/api' + userKey: REPLACE_WITH_MYKEY + bondId: + chainId: laconic_9000-1 + gas: 250000 + fees: 200000aphoton diff --git a/app/data/pod-list.txt b/app/data/pod-list.txt index 70fc5330..90777a0e 100644 --- a/app/data/pod-list.txt +++ b/app/data/pod-list.txt @@ -5,7 +5,6 @@ go-ethereum-foundry ipld-eth-beacon-db ipld-eth-beacon-indexer ipld-eth-server -lighthouse laconicd fixturenet-laconicd fixturenet-eth diff --git a/app/data/stacks/mainnet-laconic/README.md b/app/data/stacks/mainnet-laconic/README.md new file mode 100644 index 00000000..67984e5b --- /dev/null +++ b/app/data/stacks/mainnet-laconic/README.md @@ -0,0 +1,2 @@ +# Laconic Mainnet Deployment (experimental) + diff --git a/app/data/stacks/mainnet-laconic/deploy/commands.py b/app/data/stacks/mainnet-laconic/deploy/commands.py new file mode 100644 index 00000000..0ac4845f --- /dev/null +++ b/app/data/stacks/mainnet-laconic/deploy/commands.py @@ -0,0 +1,57 @@ +# 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 . + +import click +import os +from shutil import copyfile +import sys +from .util import get_stack_config_filename, get_parsed_deployment_spec + +default_spec_file_content = """stack: mainnet-laconic +data_dir: /my/path +node_name: my-node-name +""" + + +def make_default_deployment_dir(): + return "deployment-001" + +@click.command() +@click.option("--output", required=True, help="Write yaml spec file here") +@click.pass_context +def init(ctx, output): + with open(output, "w") as output_file: + output_file.write(default_spec_file_content) + + +@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") +@click.pass_context +def create(ctx, spec_file, deployment_dir): + # This function fails with a useful error message if the file doens't exist + parsed_spec = get_parsed_deployment_spec(spec_file) + if ctx.debug: + print(f"parsed spec: {parsed_spec}") + if deployment_dir is None: + deployment_dir = make_default_deployment_dir() + if os.path.exists(deployment_dir): + print(f"Error: {deployment_dir} already exists") + sys.exit(1) + os.mkdir(deployment_dir) + # Copy spec file and the stack file into the deployment dir + copyfile(spec_file, os.path.join(deployment_dir, os.path.basename(spec_file))) + stack_file = get_stack_config_filename(parsed_spec.stack) + copyfile(stack_file, os.path.join(deployment_dir, os.path.basename(stack_file))) diff --git a/app/data/stacks/mainnet-laconic/stack.yml b/app/data/stacks/mainnet-laconic/stack.yml new file mode 100644 index 00000000..b5e1f16c --- /dev/null +++ b/app/data/stacks/mainnet-laconic/stack.yml @@ -0,0 +1,31 @@ +version: "1.0" +name: mainnet-laconic +description: "Mainnet laconic node" +repos: + - cerc-io/laconicd + - lirewine/debug + - lirewine/crypto + - lirewine/gem + - lirewine/sdk + - cerc-io/laconic-sdk + - cerc-io/laconic-registry-cli + - cerc-io/laconic-console +npms: + - laconic-sdk + - laconic-registry-cli + - debug + - crypto + - sdk + - gem + - laconic-console +containers: + - cerc/laconicd + - cerc/laconic-registry-cli + - cerc/laconic-console-host +pods: + - mainnet-laconicd + - fixturenet-laconic-console +config: + cli: + key: laconicd.mykey + address: laconicd.myaddress diff --git a/app/data/stacks/mainnet-laconic/test/run-mainnet-laconic-test.sh b/app/data/stacks/mainnet-laconic/test/run-mainnet-laconic-test.sh new file mode 100755 index 00000000..3e25f5dc --- /dev/null +++ b/app/data/stacks/mainnet-laconic/test/run-mainnet-laconic-test.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +set -e +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi +# Dump environment variables for debugging +echo "Environment variables:" +env +# Test laconic stack +echo "Running laconic stack test" +# Bit of a hack, test the most recent package +TEST_TARGET_SO=$( ls -t1 ./package/laconic-so* | head -1 ) +# Set a non-default repo dir +export CERC_REPO_BASE_DIR=~/stack-orchestrator-test/repo-base-dir +echo "Testing this package: $TEST_TARGET_SO" +echo "Test version command" +reported_version_string=$( $TEST_TARGET_SO version ) +echo "Version reported is: ${reported_version_string}" +echo "Cloning repositories into: $CERC_REPO_BASE_DIR" +rm -rf $CERC_REPO_BASE_DIR +mkdir -p $CERC_REPO_BASE_DIR +# Test bringing the test container up and down +# with and without volume removal +$TEST_TARGET_SO --stack test setup-repositories +$TEST_TARGET_SO --stack test build-containers +$TEST_TARGET_SO --stack test deploy up +$TEST_TARGET_SO --stack test deploy down +# The next time we bring the container up the volume will be old (from the previous run above) +$TEST_TARGET_SO --stack test deploy up +log_output_1=$( $TEST_TARGET_SO --stack test deploy logs ) +if [[ "$log_output_1" == *"Filesystem is old"* ]]; then + echo "Retain volumes test: passed" +else + echo "Retain volumes test: FAILED" + exit 1 +fi +$TEST_TARGET_SO --stack test deploy down --delete-volumes +# Now when we bring the container up the volume will be new again +$TEST_TARGET_SO --stack test deploy up +log_output_2=$( $TEST_TARGET_SO --stack test deploy logs ) +if [[ "$log_output_2" == *"Filesystem is fresh"* ]]; then + echo "Delete volumes test: passed" +else + echo "Delete volumes test: FAILED" + exit 1 +fi +$TEST_TARGET_SO --stack test deploy down --delete-volumes +echo "Test passed" diff --git a/app/deploy_system.py b/app/deploy.py similarity index 86% rename from app/deploy_system.py rename to app/deploy.py index 2782ce5d..3f769f3e 100644 --- a/app/deploy_system.py +++ b/app/deploy.py @@ -21,12 +21,15 @@ import os import sys from dataclasses import dataclass from decouple import config +from importlib import resources import subprocess from python_on_whales import DockerClient, DockerException import click -import importlib.resources from pathlib import Path -from .util import include_exclude_check, get_parsed_stack_config +from .util import include_exclude_check, get_parsed_stack_config, global_options2 +from .deployment_create import create as deployment_create +from .deployment_create import init as deployment_init + class DeployCommandContext(object): def __init__(self, cluster_context, docker): @@ -43,44 +46,40 @@ class DeployCommandContext(object): def command(ctx, include, exclude, env_file, cluster): '''deploy a stack''' - cluster_context = _make_cluster_context(ctx.obj, include, exclude, cluster, env_file) - - # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ - docker = DockerClient(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster, - compose_env_file=cluster_context.env_file) - - ctx.obj = DeployCommandContext(cluster_context, docker) + if ctx.parent.obj.debug: + print(f"ctx.parent.obj: {ctx.parent.obj}") + ctx.obj = create_deploy_context(global_options2(ctx), global_options2(ctx).stack, include, exclude, cluster, env_file) # Subcommand is executed now, by the magic of click -@command.command() -@click.argument('extra_args', nargs=-1) # help: command: up -@click.pass_context -def up(ctx, extra_args): +def create_deploy_context(global_context, stack, include, exclude, cluster, env_file): + cluster_context = _make_cluster_context(global_context, stack, include, exclude, cluster, env_file) + # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ + docker = DockerClient(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster, + compose_env_file=cluster_context.env_file) + return DeployCommandContext(cluster_context, docker) + + +def up_operation(ctx, services_list): global_context = ctx.parent.parent.obj - extra_args_list = list(extra_args) or None + deploy_context = ctx.obj if not global_context.dry_run: - cluster_context = ctx.obj.cluster_context + cluster_context = deploy_context.cluster_context container_exec_env = _make_runtime_env(global_context) for attr, value in container_exec_env.items(): os.environ[attr] = value if global_context.verbose: - print(f"Running compose up with container_exec_env: {container_exec_env}, extra_args: {extra_args_list}") + print(f"Running compose up with container_exec_env: {container_exec_env}, extra_args: {services_list}") for pre_start_command in cluster_context.pre_start_commands: _run_command(global_context, cluster_context.cluster, pre_start_command) - ctx.obj.docker.compose.up(detach=True, services=extra_args_list) + deploy_context.docker.compose.up(detach=True, services=services_list) for post_start_command in cluster_context.post_start_commands: _run_command(global_context, cluster_context.cluster, post_start_command) - _orchestrate_cluster_config(global_context, cluster_context.config, ctx.obj.docker, container_exec_env) + _orchestrate_cluster_config(global_context, cluster_context.config, deploy_context.docker, container_exec_env) -@command.command() -@click.option("--delete-volumes/--preserve-volumes", default=False, help="delete data volumes") -@click.argument('extra_args', nargs=-1) # help: command: down -@click.pass_context -def down(ctx, delete_volumes, extra_args): +def down_operation(ctx, delete_volumes, extra_args_list): global_context = ctx.parent.parent.obj - extra_args_list = list(extra_args) or None if not global_context.dry_run: if global_context.verbose: print("Running compose down") @@ -91,9 +90,7 @@ def down(ctx, delete_volumes, extra_args): ctx.obj.docker.compose.down(timeout=timeout_arg, volumes=delete_volumes) -@command.command() -@click.pass_context -def ps(ctx): +def ps_operation(ctx): global_context = ctx.parent.parent.obj if not global_context.dry_run: if global_context.verbose: @@ -118,10 +115,7 @@ def ps(ctx): print("No containers running") -@command.command() -@click.argument('extra_args', nargs=-1) # help: command: port -@click.pass_context -def port(ctx, extra_args): +def port_operation(ctx, extra_args): global_context = ctx.parent.parent.obj extra_args_list = list(extra_args) or None if not global_context.dry_run: @@ -136,10 +130,7 @@ def port(ctx, extra_args): print(f"{mapped_port_data[0]}:{mapped_port_data[1]}") -@command.command() -@click.argument('extra_args', nargs=-1) # help: command: exec -@click.pass_context -def exec(ctx, extra_args): +def exec_operation(ctx, extra_args): global_context = ctx.parent.parent.obj extra_args_list = list(extra_args) or None if not global_context.dry_run: @@ -157,10 +148,7 @@ def exec(ctx, extra_args): print(f"container command returned error exit status") -@command.command() -@click.argument('extra_args', nargs=-1) # help: command: logs -@click.pass_context -def logs(ctx, extra_args): +def logs_operation(ctx, extra_args): global_context = ctx.parent.parent.obj extra_args_list = list(extra_args) or None if not global_context.dry_run: @@ -170,12 +158,56 @@ def logs(ctx, extra_args): print(logs_output) +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: up +@click.pass_context +def up(ctx, extra_args): + extra_args_list = list(extra_args) or None + up_operation(ctx, extra_args_list) + + +@command.command() +@click.option("--delete-volumes/--preserve-volumes", default=False, help="delete data volumes") +@click.argument('extra_args', nargs=-1) # help: command: down +@click.pass_context +def down(ctx, delete_volumes, extra_args): + extra_args_list = list(extra_args) or None + down_operation(ctx, delete_volumes, extra_args_list) + + +@command.command() +@click.pass_context +def ps(ctx): + ps_operation(ctx) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: port +@click.pass_context +def port(ctx, extra_args): + port_operation(ctx, extra_args) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: exec +@click.pass_context +def exec(ctx, extra_args): + exec_operation(ctx, extra_args) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: logs +@click.pass_context +def logs(ctx, extra_args): + logs_operation(ctx, extra_args) + + def get_stack_status(ctx, stack): ctx_copy = copy.copy(ctx) ctx_copy.stack = stack - cluster_context = _make_cluster_context(ctx_copy, None, None, None, None) + cluster_context = _make_cluster_context(ctx_copy, stack, None, None, None, None) docker = DockerClient(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster) # TODO: refactor to avoid duplicating this code above if ctx.verbose: @@ -200,7 +232,8 @@ def _make_runtime_env(ctx): return container_exec_env -def _make_cluster_context(ctx, include, exclude, cluster, env_file): +# stack has to be either PathLike pointing to a stack yml file, or a string with the name of a known stack +def _make_cluster_context(ctx, stack, include, exclude, cluster, env_file): if ctx.local_stack: dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")] @@ -208,14 +241,20 @@ def _make_cluster_context(ctx, include, exclude, cluster, env_file): else: dev_root_path = os.path.expanduser(config("CERC_REPO_BASE_DIR", default="~/cerc")) - # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure - compose_dir = Path(__file__).absolute().parent.joinpath("data", "compose") + # TODO: huge hack, fix this + # If the caller passed a path for the stack file, then we know that we can get the compose files + # from the same directory + if isinstance(stack, os.PathLike): + compose_dir = stack.parent.joinpath("compose") + else: + # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure + compose_dir = Path(__file__).absolute().parent.joinpath("data", "compose") if cluster is None: # Create default unique, stable cluster name from confile file path and stack name if provided # TODO: change this to the config file path path = os.path.realpath(sys.argv[0]) - unique_cluster_descriptor = f"{path},{ctx.stack},{include},{exclude}" + unique_cluster_descriptor = f"{path},{stack},{include},{exclude}" if ctx.debug: print(f"pre-hash descriptor: {unique_cluster_descriptor}") hash = hashlib.md5(unique_cluster_descriptor.encode()).hexdigest() @@ -225,12 +264,12 @@ def _make_cluster_context(ctx, include, exclude, cluster, env_file): # See: https://stackoverflow.com/a/20885799/1701505 from . import data - with importlib.resources.open_text(data, "pod-list.txt") as pod_list_file: + with resources.open_text(data, "pod-list.txt") as pod_list_file: all_pods = pod_list_file.read().splitlines() pods_in_scope = [] - if ctx.stack: - stack_config = get_parsed_stack_config(ctx.stack) + if stack: + stack_config = get_parsed_stack_config(stack) # TODO: syntax check the input here pods_in_scope = stack_config['pods'] cluster_config = stack_config['config'] if 'config' in stack_config else None @@ -342,6 +381,7 @@ def _orchestrate_cluster_config(ctx, cluster_config, docker, container_exec_env) f" = {pd.source_container}.{pd.source_variable}") # TODO: add a timeout waiting_for_data = True + destination_output = "*** no output received yet ***" while waiting_for_data: # TODO: fix the script paths so they're consistent between containers source_value = None @@ -376,3 +416,7 @@ def _orchestrate_cluster_config(ctx, cluster_config, docker, container_exec_env) waiting_for_data = False if ctx.debug: print(f"destination output: {destination_output}") + + +command.add_command(deployment_init) +command.add_command(deployment_create) diff --git a/app/deployment.py b/app/deployment.py new file mode 100644 index 00000000..fa2236aa --- /dev/null +++ b/app/deployment.py @@ -0,0 +1,140 @@ +# 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 . + +import click +from dataclasses import dataclass +from pathlib import Path +import sys +from .deploy import up_operation, down_operation, ps_operation, port_operation, exec_operation, logs_operation, create_deploy_context +from .util import global_options + + +@dataclass +class DeploymentContext: + dir: Path + + +@click.group() +@click.option("--dir", required=True, help="path to deployment directory") +@click.pass_context +def command(ctx, dir): + # 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 + ctx.obj = DeploymentContext(dir_path) + + +def make_deploy_context(ctx): + # Get the stack config file name + stack_file_path = ctx.obj.dir.joinpath("stack.yml") + # TODO: add cluster name and env file here + return create_deploy_context(ctx.parent.parent.obj, stack_file_path, None, None, None, None) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: up +@click.pass_context +def up(ctx, extra_args): + ctx.obj = make_deploy_context(ctx) + services_list = list(extra_args) or None + up_operation(ctx, services_list) + + +# start is the preferred alias for up +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: up +@click.pass_context +def start(ctx, extra_args): + ctx.obj = make_deploy_context(ctx) + services_list = list(extra_args) or None + up_operation(ctx, services_list) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: down +@click.pass_context +def down(ctx, extra_args): + # Get the stack config file name + # TODO: add cluster name and env file here + ctx.obj = make_deploy_context(ctx) + down_operation(ctx, extra_args, None) + + +# stop is the preferred alias for down +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: down +@click.pass_context +def stop(ctx, extra_args): + # TODO: add cluster name and env file here + ctx.obj = make_deploy_context(ctx) + down_operation(ctx, extra_args, None) + + +@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 +@click.pass_context +def port(ctx, extra_args): + port_operation(ctx, extra_args) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: exec +@click.pass_context +def exec(ctx, extra_args): + ctx.obj = make_deploy_context(ctx) + exec_operation(ctx, extra_args) + + +@command.command() +@click.argument('extra_args', nargs=-1) # help: command: logs +@click.pass_context +def logs(ctx, extra_args): + ctx.obj = make_deploy_context(ctx) + logs_operation(ctx, extra_args) + + +@command.command() +@click.pass_context +def status(ctx): + print(f"Context: {ctx.parent.obj}") + + + +#from importlib import resources, util +# TODO: figure out how to do this dynamically +#stack = "mainnet-laconic" +#module_name = "commands" +#spec = util.spec_from_file_location(module_name, "./app/data/stacks/" + stack + "/deploy/commands.py") +#imported_stack = util.module_from_spec(spec) +#spec.loader.exec_module(imported_stack) +#command.add_command(imported_stack.init) +#command.add_command(imported_stack.create) diff --git a/app/deployment_create.py b/app/deployment_create.py new file mode 100644 index 00000000..d1416389 --- /dev/null +++ b/app/deployment_create.py @@ -0,0 +1,139 @@ +# 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 . + +import click +import os +from pathlib import Path +from shutil import copyfile, copytree +import sys +import ruamel.yaml +from .util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options + + +def _get_yaml(): + # See: https://stackoverflow.com/a/45701840/1701505 + yaml = ruamel.yaml.YAML() + yaml.preserve_quotes = True + yaml.indent(sequence=3, offset=1) + return yaml + + +def _make_default_deployment_dir(): + return "deployment-001" + + +def _get_compose_file_dir(): + # TODO: refactor to use common code with deploy command + # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure + data_dir = Path(__file__).absolute().parent.joinpath("data") + source_compose_dir = data_dir.joinpath("compose") + return source_compose_dir + + +def _get_named_volumes(stack): + # Parse the compose files looking for named volumes + named_volumes = [] + parsed_stack = get_parsed_stack_config(stack) + pods = parsed_stack["pods"] + yaml = _get_yaml() + for pod in pods: + pod_file_path = os.path.join(_get_compose_file_dir(), f"docker-compose-{pod}.yml") + 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(): + # Volume definition looks like: + # 'laconicd-data': None + named_volumes.append(volume) + return named_volumes + + +# See: https://stackoverflow.com/questions/45699189/editing-docker-compose-yml-with-pyyaml +def _fixup_pod_file(pod, spec): + # 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] + new_volume_spec = {"name": volume, + "driver_opts": { + "type": "none", + "device": volume_spec, + "o": "bind" + } + } + pod["volumes"][volume] = new_volume_spec + + +@click.command() +@click.option("--output", required=True, help="Write yaml spec file here") +@click.pass_context +def init(ctx, output): + yaml = _get_yaml() + stack = global_options(ctx).stack + verbose = global_options(ctx).verbose + spec_file_content = {"stack": stack} + if verbose: + print(f"Creating spec file for stack: {stack}") + named_volumes = _get_named_volumes(stack) + if named_volumes: + volume_descriptors = {} + for named_volume in named_volumes: + volume_descriptors[named_volume] = f"./data/{named_volume}" + spec_file_content["volumes"] = volume_descriptors + with open(output, "w") as output_file: + yaml.dump(spec_file_content, output_file) + + +@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") +@click.pass_context +def create(ctx, spec_file, deployment_dir): + # This function fails with a useful error message if the file doens't exist + parsed_spec = get_parsed_deployment_spec(spec_file) + stack_name = parsed_spec['stack'] + stack_file = get_stack_file_path(stack_name) + parsed_stack = get_parsed_stack_config(stack_name) + if global_options(ctx).debug: + print(f"parsed spec: {parsed_spec}") + if deployment_dir is None: + deployment_dir = _make_default_deployment_dir() + if os.path.exists(deployment_dir): + print(f"Error: {deployment_dir} already exists") + sys.exit(1) + os.mkdir(deployment_dir) + # Copy spec file and the stack file into the deployment dir + copyfile(spec_file, os.path.join(deployment_dir, os.path.basename(spec_file))) + copyfile(stack_file, os.path.join(deployment_dir, os.path.basename(stack_file))) + # Copy the pod files into the deployment dir, fixing up content + pods = parsed_stack['pods'] + destination_compose_dir = os.path.join(deployment_dir, "compose") + os.mkdir(destination_compose_dir) + data_dir = Path(__file__).absolute().parent.joinpath("data") + yaml = _get_yaml() + for pod in pods: + pod_file_path = os.path.join(_get_compose_file_dir(), f"docker-compose-{pod}.yml") + parsed_pod_file = yaml.load(open(pod_file_path, "r")) + _fixup_pod_file(parsed_pod_file, parsed_spec) + with open(os.path.join(destination_compose_dir, os.path.basename(pod_file_path)), "w") as output_file: + yaml.dump(parsed_pod_file, output_file) + # Copy the config files for the pod, if any + source_config_dir = data_dir.joinpath("config", pod) + if os.path.exists(source_config_dir): + copytree(source_config_dir, os.path.join(deployment_dir, "config", pod)) diff --git a/app/util.py b/app/util.py index 127de213..69eda4af 100644 --- a/app/util.py +++ b/app/util.py @@ -30,10 +30,16 @@ def include_exclude_check(s, include, exclude): return s not in exclude_list -def get_parsed_stack_config(stack): +def get_stack_file_path(stack): # In order to be compatible with Python 3.8 we need to use this hack to get the path: # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure stack_file_path = Path(__file__).absolute().parent.joinpath("data", "stacks", stack, "stack.yml") + return stack_file_path + + +# Caller can pass either the name of a stack, or a path to a stack file +def get_parsed_stack_config(stack): + stack_file_path = stack if isinstance(stack, os.PathLike) else get_stack_file_path(stack) try: with stack_file_path: stack_config = yaml.safe_load(open(stack_file_path, "r")) @@ -48,3 +54,27 @@ def get_parsed_stack_config(stack): print(f"Error: stack: {stack} does not exist") print(f"Exiting, error: {error}") sys.exit(1) + + +def get_parsed_deployment_spec(spec_file): + spec_file_path = Path(spec_file) + try: + with spec_file_path: + deploy_spec = yaml.safe_load(open(spec_file_path, "r")) + return deploy_spec + except FileNotFoundError as error: + # We try here to generate a useful diagnostic error + print(f"Error: spec file: {spec_file_path} does not exist") + print(f"Exiting, error: {error}") + sys.exit(1) + + +# TODO: this is fragile wrt to the subcommand depth +# See also: https://github.com/pallets/click/issues/108 +def global_options(ctx): + return ctx.parent.parent.obj + + +# TODO: hack +def global_options2(ctx): + return ctx.parent.obj diff --git a/cli.py b/cli.py index b0d2f34c..e9a11d25 100644 --- a/cli.py +++ b/cli.py @@ -19,8 +19,9 @@ from dataclasses import dataclass from app import setup_repositories from app import build_containers from app import build_npms -from app import deploy_system +from app import deploy from app import version +from app import deployment CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) @@ -54,6 +55,7 @@ def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_err cli.add_command(setup_repositories.command, "setup-repositories") cli.add_command(build_containers.command, "build-containers") cli.add_command(build_npms.command, "build-npms") -cli.add_command(deploy_system.command, "deploy") # deploy is an alias for deploy-system -cli.add_command(deploy_system.command, "deploy-system") +cli.add_command(deploy.command, "deploy") # deploy is an alias for deploy-system +cli.add_command(deploy.command, "deploy-system") +cli.add_command(deployment.command, "deployment") cli.add_command(version.command, "version") diff --git a/requirements.txt b/requirements.txt index 8249a55a..6264dcb6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ tqdm>=4.64.0 python-on-whales>=0.58.0 click>=8.1.3 pyyaml>=6.0 +ruamel.yaml>=0.17.32