diff --git a/README.md b/README.md index f4a0378..0c23164 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,26 @@ After finish install the environment, please open a new terminal to continue. Th All done! All delphinus packages are in packages directory. + + +## Deploying the Delphinus Substrate Node + +From the delphinus-lerna directory, execute the deploy script +`bash deploy.sh` +to start the process for deploying a substrate node. + +For this script to run successfully, ensure that `Dockerfile` and `docker-compose.yaml` are in the `packages/substrate-node` directory. + +The script will first build the docker image which may take up to a few minutes to complete. + +If you encounter any errors during the docker image build process, some things to check are: + - Ensure delphinus-lerna environment is setup properly as there are several common packages required. + - Check relative paths of the `Dockerfile` in relation to the `deploy.sh` script. + +Once the docker image successfully builds, the script will execute `docker-compose` on the `docker-compose.yaml` file to start the container. + +If successful, the node should be operational and you should be able to see the logs of blocks being submitted, block hashes etc. + + + + diff --git a/deploy.sh b/deploy.sh index 43562c0..a9a9a8c 100644 --- a/deploy.sh +++ b/deploy.sh @@ -1,2 +1,149 @@ -DOCKER_BUILDKIT=1 docker build . -t delphinus-node -f packages/substrate-node/Dockerfile -docker-compose -f packages/substrate-node/docker-compose.yml up + +#!/usr/bin/env bash +set -ueo pipefail + +local_repo_path="$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" && pwd)" +machine= +domain= +repo="$local_repo_path" +private_key_file= +dry_run= +verbose= +logs_only= + + +function usage() { + cat <&2 + usage + exit 1 + ;; + esac +done + +use_ssh="$machine" +repo_name="$(basename "$repo")" +repo_path="$local_repo_path" +if [[ -n "$use_ssh" ]]; then + repo_path="$repo_name" +fi + +dockerfile_path="${repo_path}/packages/substrate-node/" + +function quote_command() { + python3 -c 'import shlex; import sys; print(shlex.join(sys.argv[1:]))' "$@" +} + +function run_command() { + command=("${@}") + if [[ -n "$dry_run" ]]; then + quote_command "${command[@]}" + else + if [[ -n "$verbose" ]]; then + quote_command "${command[@]}" + fi + "${command[@]}" + fi +} + +function run_maybe_remote_command() { + command=("${@}") + if [[ -n "$use_ssh" ]]; then + # ssh requires a single argument + command=(ssh "$machine" "$(quote_command "$@")") + fi + run_command "${command[@]}" +} + +function copy_repo() { + if [[ -z "$use_ssh" ]]; then + return + fi + run_command rsync -avz --progress --exclude target --cvs-exclude -h "$repo/" "$machine:$repo_name/" +} + +function start_node() { + + #Dockerfile required in substrate-node directory. + #docker-compose.yaml also required in susbtrate-node directory. + + #build context is from outside the substrate-node as required delphinuspackages are outside of substrate-node. + + + run_maybe_remote_command env DOCKER_BUILDKIT=1 docker build . -t delphinus-node -f "${dockerfile_path}/Dockerfile" + env=() + if [[ -n "$domain" ]]; then + env+=(NODE_DOMAIN="$domain") + fi + run_maybe_remote_command env "${env[@]}" docker-compose -f "$dockerfile_path/docker-compose.yml" up -d +} + +function follow_node_logs() { + run_maybe_remote_command docker-compose -f "$dockerfile_path/docker-compose.yml" logs -f +} + +function sanity_check() { + local_tools=(rsync python3) + remote_tools=(docker docker-compose) + if ! command -v "${local_tools[@]}" >/dev/null; then + echo "You need ${local_tools[*]} installed in the local machine." >&2 + exit 1 + fi + if ! run_maybe_remote_command command -v "${remote_tools[@]}" >/dev/null; then + echo "You need ${remote_tools[*]} installed in the remote machine." >&2 + exit 1 + fi + run_maybe_remote_command mkdir -p "$repo_name" + run_maybe_remote_command bash -c "docker network inspect caddy || docker network create caddy" >/dev/null 2>&1 +} + +function deploy() { + sanity_check + if [[ -z "$logs_only" ]]; then + copy_repo + start_node + fi + follow_node_logs +} + +deploy