Skip to content

Commit

Permalink
restore code from paritytech#13039
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Feb 22, 2023
1 parent 86c6bb9 commit e321a3c
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bin/node/runtime/wasm/target/
polkadot.*
.DS_Store
.idea/
.cache/
nohup.out
rls*.log
*.orig
Expand All @@ -22,6 +23,10 @@ rls*.log
**/hfuzz_target/
**/hfuzz_workspace/
.cargo-remote.toml
.bash_logout
.bashrc
.profile
*.bin
*.iml
scripts/ci/node-template-release/Cargo.lock
bin/node-template/Cargo.lock
8 changes: 8 additions & 0 deletions bin/node-template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"node",
"pallets/template",
"runtime",
]
[profile.release]
panic = "unwind"
7 changes: 5 additions & 2 deletions bin/node-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. "<HOST_PORT>: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
Expand Down
2 changes: 1 addition & 1 deletion bin/node-template/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.2"
services:

services:
dev:
Expand Down
40 changes: 40 additions & 0 deletions bin/node-template/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
70 changes: 66 additions & 4 deletions bin/node-template/scripts/docker_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e321a3c

Please sign in to comment.