From ad6f43a75d2318898699df23bfe7d2e47a524dff Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 22 Feb 2023 10:53:54 +0000 Subject: [PATCH] restore code from #13039 --- bin/node-template/Cargo.toml | 8 +++ bin/node-template/README.md | 7 ++- bin/node-template/docker-compose.yml | 2 +- bin/node-template/entrypoint.sh | 40 ++++++++++++++ bin/node-template/scripts/docker_run.sh | 70 +++++++++++++++++++++++-- 5 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 bin/node-template/Cargo.toml create mode 100755 bin/node-template/entrypoint.sh diff --git a/bin/node-template/Cargo.toml b/bin/node-template/Cargo.toml new file mode 100644 index 0000000000000..538fd8d1e19cd --- /dev/null +++ b/bin/node-template/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] +members = [ + "node", + "pallets/template", + "runtime", +] +[profile.release] +panic = "unwind" diff --git a/bin/node-template/README.md b/bin/node-template/README.md index c42b58449565d..76fa707adf676 100644 --- a/bin/node-template/README.md +++ b/bin/node-template/README.md @@ -162,20 +162,23 @@ First, install [Docker](https://docs.docker.com/get-docker/) and [Docker Compose Then run the following command to start a single node development chain. +> If you get an error that tcp port address is already in use then find an available port to use for the host port in docker-compose.yml (i.e. ":9944"). + ```sh ./scripts/docker_run.sh ``` This command compiles the code and starts a local development network. You can also replace the default command (`cargo build --release && ./target/release/node-template --dev --ws-external`) by appending your own. +Note: It is recommended to provide a custom `--base-path` to store the chain database. For example: ```sh # Run Substrate node without re-compiling -./scripts/docker_run.sh ./target/release/node-template --dev --ws-external +./scripts/docker_run.sh ./target/release/node-template --dev --ws-external --base-path=/data # Purge the local dev chain -./scripts/docker_run.sh ./target/release/node-template purge-chain --dev +./scripts/docker_run.sh ./target/release/node-template purge-chain --dev --base-path=/data -y # Check whether the code is compilable ./scripts/docker_run.sh cargo check diff --git a/bin/node-template/docker-compose.yml b/bin/node-template/docker-compose.yml index bc1922f47d963..247c331e95b62 100644 --- a/bin/node-template/docker-compose.yml +++ b/bin/node-template/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3.2" +services: services: dev: diff --git a/bin/node-template/entrypoint.sh b/bin/node-template/entrypoint.sh new file mode 100755 index 0000000000000..8e3934c44f574 --- /dev/null +++ b/bin/node-template/entrypoint.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +set -e + +args=$@ +cargo build --release + +# we do not need to use `useradd` to create another non-root user since we are +# using a pre-built image that has already created one called 'nonroot': +# see https://hub.docker.com/r/paritytech/ci-linux/tags > "production" > Layer 12 +usermod -u 1000 -s /bin/sh -d /var/www/node-template nonroot +mkdir -p /data /var/www/node-template/.local/share/node-template +# change owner to non-root user of static symlink directory for storing chain data +chown -R nonroot:nonroot /data +# change owner to non-root user for hidden subdirectories of their home directory +chown -R nonroot:nonroot /var/www/node-template/.[^.]* +# change owner to non-root user for non-hidden subdirectories of their home directory +chown -R nonroot:nonroot /var/www/node-template + +# ignore warning: `Not copying any file from skel directory into it.` +symlink=/var/www/node-template/.local/share/node-template/data +# create symlink if not already exist +[ ! -L ${symlink} ] && ln -s /data /var/www/node-template/.local/share/node-template +# copy skel files to user nonroot home directory if it already exists +cp -r /etc/skel/. /var/www/node-template +# sanity checks +ldd -d -r -v /var/www/node-template/target/release/node-template + +# switch to non-root user. show version of node-template. +su -c "printf \"\n*** Changed to the home directory ${PWD} of user: \" && id \ + && /var/www/node-template/target/release/node-template --version" nonroot + +# handle when arguments not provided. run arguments provided to script. +if [ "$args" = "" ] ; then + su -c "printf \"Note: Please try providing an argument to the script.\n\n\"" nonroot + exit 1 +else + su -c "printf \"*** Running the provided arguments: $args\n\n\"" nonroot + su -c "$args" nonroot +fi diff --git a/bin/node-template/scripts/docker_run.sh b/bin/node-template/scripts/docker_run.sh index 0bac44b4cfb3b..e9da66e5c1552 100755 --- a/bin/node-template/scripts/docker_run.sh +++ b/bin/node-template/scripts/docker_run.sh @@ -2,9 +2,71 @@ # This script is meant to be run on Unix/Linux based systems set -e -echo "*** Start Substrate node template ***" - +printf "*** Start Substrate node template ***\n" +SCRIPT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")") +PARENT_DIR=$(dirname "$SCRIPT_DIR") cd $(dirname ${BASH_SOURCE[0]})/.. -docker-compose down --remove-orphans -docker-compose run --rm --service-ports dev $@ +modify_first_line_of_docker_compose() { + local first_line=$1 + sed -i "1s/.*/$first_line/" $PARENT_DIR/docker-compose.yml +} + +printf "Searching for docker-compose executable...\n" + +if ! [ -x "$(command -v docker-compose)" ]; then + printf "Skipping docker-compose since executable is not installed.\n" +else + printf "Detected docker-compose executable.\n" + exit_code=$(docker-compose version; status=$?; echo $status) + # get the last character captured by $? since it may also include command output first + last_char=${exit_code: -1} + + # prefix last char with random character incase last char is 0 but not an exit code + if [[ "x${last_char}" == "x0" && "$(docker-compose version)" =~ " 1." ]]; then + printf "Detected legacy docker-compose version 1.x. Using Compose File Format 1.\n" + # temporarily comment out first line `services:` of ../docker-compose.yml + modify_first_line_of_docker_compose "#services:" + docker-compose down --remove-orphans + docker-compose run --rm --service-ports dev $@ + # uncomment again the first line `services:` of ../docker-compose.yml + modify_first_line_of_docker_compose "services:" + exit + fi + + exit_code=$(docker-compose compose version; status=$?; echo $status) + last_char=${exit_code: -1} + + if [[ "x${last_char}" == "x0" && "$(docker-compose compose version)" =~ " v2." ]]; then + printf "Detected legacy docker-compose version 2.x. Using Compose File Format 2+.\n" + # switch back to default `services:` incase was temporarily commented out + modify_first_line_of_docker_compose "services:" + docker-compose compose down --remove-orphans + docker-compose compose run --rm --service-ports dev $@ + exit + fi + + printf "Unknown or unsupported version of docker-compose. Skipping...\n" +fi + +if ! [ -x "$(command -v docker)" ]; then + printf "Skipping docker since docker executable is not installed.\n" +else + printf "Detected docker executable.\n" + exit_code=$(docker compose version; status=$?; echo $status) + last_char=${exit_code: -1} + + if [[ "x${last_char}" == "x0" ]]; then + printf "Detected docker compose subcommand.\n" + # switch back to default `services:` incase was temporarily commented out + modify_first_line_of_docker_compose "services:" + docker compose down --remove-orphans + docker compose run --rm --service-ports dev $@ + else + printf "Skipping docker since docker executable subcommand not supported.\n" + fi + exit +fi + +printf "Error: Unable to detect any docker compose installation.\n" +exit 1