Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment configuration & connect script #5

Merged
merged 9 commits into from
Dec 12, 2018
40 changes: 31 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ubuntu:xenial
FROM ubuntu:xenial as builder

WORKDIR /substrate
WORKDIR /build

# install tools and dependencies
RUN apt -y update && \
Expand All @@ -9,10 +9,6 @@ RUN apt -y update && \
make cmake ca-certificates g++ zip dpkg-dev python rhash rpm openssl gettext\
build-essential pkg-config libssl-dev libudev-dev ruby-dev time

#install nodejs
RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - && \
apt-get install -y nodejs

# install rustup
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

Expand Down Expand Up @@ -48,14 +44,40 @@ ENV CXX g++
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

COPY . /substrate
COPY . /build

RUN /bin/bash build.sh

RUN cargo build && cargo test

EXPOSE 30333 9933 9944

FROM ubuntu:xenial

WORKDIR /runtime

RUN apt -y update && \
apt install -y --no-install-recommends \
openssl \
curl \
libssl-dev dnsutils

RUN mkdir -p /runtime/target/debug/
COPY --from=builder /build/target/debug/node ./target/debug/node
COPY --from=builder /build/start-node.sh ./start-node.sh

RUN chmod a+x *.sh
RUN ls -la .

CMD ["cargo", "run", "--", "dev"]
# expose node ports
EXPOSE 30333 9933 9944

#
# Pass the node start command to the docker run command
#
# To start Alice boot node:
# ./start-node --account-name Alice --telemetry
#
# To start a node that connects to Alice:
# ./start-node.sh --account-name Charly --connect-to Alice -t
#
CMD ["echo","\"Please provide a startup command.\""]
46 changes: 41 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
# substrate-poc
A new SRML-based Substrate node, ready for hacking
# prototype-chain

## Run inside docker container
Substrate node implementation for the KILT prototype

## Running a local node that connects to KILT prototype testnet in AWS

There are master boot nodes running in the KILT testnet:

* Alice (bootnode-alice.kilt-prototype.tk)
* Bob (bootnode-bob.kilt-prototype.tk)

To start a node and connect to Alice you can use the shell script `start-node.sh`:

```
./start-node.sh --account-name Charly --connect-to Alice
```

You can use any of the accounts declared in the chain spec to connect (Alice, Bob, Charly, Dave, Eve, Ferdie).

Run `./start-node.sh --help` for more information.

### Running a node inside a docker container

Make sure to have the `awscli` installed. Otherwise Install it via `brew install awscli` (Mac).
You also need to have your docker daemon system running (on mac, just download and install the docker application).

Login to Amazon ECR

```
$(aws ecr get-login --no-include-email --region eu-central-1)
```

Pull the latest image from Amazon ECR

```
docker build -t substrate-poc .
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 --publish-all=true -it substrate-poc
docker pull 348099934012.dkr.ecr.eu-central-1.amazonaws.com/kilt/prototype-chain:latest
```

Run the image and pass the command to start a node

```
docker run 348099934012.dkr.ecr.eu-central-1.amazonaws.com/kilt/prototype-chain ./start-node.sh --account-name Charly --connect-to Alice
```

The node should be connected to the KILT testnet.
5 changes: 1 addition & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ PROJECT_ROOT=`pwd`

export CARGO_INCREMENTAL=0

bold=$(tput bold)
normal=$(tput sgr0)

# Save current directory.
pushd . >/dev/null

for SRC in runtime/wasm
do
echo "$PROJECT_ROOT/$SRC"
echo "${bold}Building webassembly binary in $SRC...${normal}"
echo "Building webassembly binary in $SRC..."
cd "$PROJECT_ROOT/$SRC"

chmod a+x build.sh
Expand Down
4 changes: 2 additions & 2 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ phases:
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region eu-central-1 --no-include-email)
- REPOSITORY_URI=348099934012.dkr.ecr.eu-central-1.amazonaws.com/kilt/substrate-poc
- REPOSITORY_URI=348099934012.dkr.ecr.eu-central-1.amazonaws.com/kilt/prototype-chain
- 'IMAGE_TAG=$(sed -n ''s/version = "\(.*\)"$/\1/p'' Cargo.toml)'
- docker pull $REPOSITORY_URI:latest || true
build:
Expand All @@ -22,6 +22,6 @@ phases:
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"substrate-poc","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- printf '[{"name":"prototype-bootnode","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files: imagedefinitions.json
10 changes: 5 additions & 5 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Alternative {
/// Whatever the current runtime is, with just Alice as an auth.
Development,
/// Whatever the current runtime is, with simple Alice/Bob auths.
LocalTestnet,
KiltTestnet,
}

impl Alternative {
Expand All @@ -39,9 +39,9 @@ impl Alternative {
None,
None
),
Alternative::LocalTestnet => ChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
Alternative::KiltTestnet => ChainSpec::from_genesis(
"KILT Testnet",
"kilt_testnet",
|| testnet_genesis(vec![
ed25519::Pair::from_seed(b"Alice ").public().into(),
ed25519::Pair::from_seed(b"Bob ").public().into(),
Expand All @@ -67,7 +67,7 @@ impl Alternative {
pub(crate) fn from(s: &str) -> Option<Self> {
match s {
"dev" => Some(Alternative::Development),
"local" => Some(Alternative::LocalTestnet),
"kilt-testnet" => Some(Alternative::KiltTestnet),
_ => None,
}
}
Expand Down
141 changes: 141 additions & 0 deletions start-node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

# start-node.sh - A script to start a node within the KILT test network

##### Constants

CHAIN_NAME="kilt-testnet"
ALICE_BOOT_NODE_KEY=0000000000000000000000000000000000000000000000000000000000000001
ALICE_BOOT_NODE_KEY_HASH=QmQZ8TjTqeDj3ciwr93EJ95hxfDsb9pEYDizUAbWpigtQN
BOB_BOOT_NODE_KEY=0000000000000000000000000000000000000000000000000000000000000002
BOB_BOOT_NODE_KEY_HASH=QmXiB3jqqn2rpiKU7k1h7NJYeBg8WNSx9DiTRKz9ti2KSK
TELEMETRY_URL=ws://telemetry-backend.kilt-prototype.tk:1024

##### Functions

lookup_boot_node() {
boot_node_domain="bootnode-${bootnode}.kilt-prototype.tk"
echo "Performing lookup for boot node ${boot_node_domain}"
if [[ "$bootnode" = "Alice" ]]; then
alice_boot_node_ip=`dig ${boot_node_domain} A +short`
boot_node_ipfs=/ip4/${alice_boot_node_ip}/tcp/30333/p2p/${ALICE_BOOT_NODE_KEY_HASH}
elif [[ "$bootnode" = "Bob" ]]; then
bob_boot_node_ip=`dig ${boot_node_domain} A +short`
boot_node_ipfs=/ip4/${bob_boot_node_ip}/tcp/30333/p2p/${BOB_BOOT_NODE_KEY_HASH}
fi
}



usage()
{
cat <<HELP_USAGE
Usage:
$0 -a <account-name> [...]

If you want to start a boot node, just use "Alice" or "Bob" as account name.

-a, --account-name ACCOUNT_NAME The name of the account to start the node with (Alice | Bob | Charly | Dave | Eve | Ferdie).
-n, --node-name NODE_NAME The arbitrary name of the node (e.g. "charly-node-1234")
-c, --connect-to BOOT_NODE_NAME The name of the boot node to connect to ("alice" | "bob")
-d, --dry-run Flag indicating to only show the resulting command instead of executing it
-t, --telemetry Flag indicating whether or not to send data to the telemetry server

Examples:

Start Alice (boot node):
./start-node.sh -a Alice

Start Bob (boot node) that connects to Alice:
./start-node.sh -a Bob -c Alice

Start Charly (normal node) that connects to Alice:
./start-node.sh -a Charly -c Alice -n charly-node-123
HELP_USAGE
}

##### Main


bootnode=
node_name=
account_name=
telemetry=0
dry_run=0

while [[ "$1" != "" ]]; do
case $1 in
-a | --account-name ) shift
account_name=$1
;;
-n | --node-name ) shift
node_name=$1
;;
-c | --connect-to ) shift
bootnode=$1
;;
-t | --telemetry ) telemetry=1
;;
-d | --dry-run ) dry_run=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done


arg_boot_node_connect=
arg_node_key=
arg_node_name=
arg_telemetry=
arg_account_name=

if [[ -z "$account_name" ]]; then
usage
exit 1
fi

if [[ "$account_name" = "Alice" ]]; then
arg_node_key=" --node-key ${ALICE_BOOT_NODE_KEY}"
elif [[ "$account_name" = "Bob" ]]; then
arg_node_key=" --node-key ${BOB_BOOT_NODE_KEY}"
fi
arg_account_name=" --key ${account_name}"

echo "Starting KILT node with account '${account_name}'"
if [[ ! -z "$bootnode" ]]; then
echo "Trying to connect to boot node '$bootnode'..."
lookup_boot_node
if [[ -z "$boot_node_ipfs" ]]; then
echo "Boot node address lookup failed for boot node named '$bootnode'"
exit 1
else
echo "Boot-node IPFS location: $boot_node_ipfs"
arg_boot_node_connect=" --bootnodes ${boot_node_ipfs}"
fi
fi

if [[ ! -z "$node_name" ]]; then
random_suffix=`cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 5`
node_name="${node_name}-${random_suffix}"
arg_node_name=" --name ${node_name}"
fi

if [[ "$telemetry" = "1" ]]; then
arg_telemetry=" --telemetry-url ${TELEMETRY_URL}"
fi

command="./target/debug/node --chain ${CHAIN_NAME} --validator --port 30333 --ws-port 9944 --ws-external --rpc-external${arg_account_name}${arg_node_key}${arg_boot_node_connect}${arg_node_name}${arg_telemetry}"

if [[ "$dry_run" = "1" ]]; then
echo "Dry run."
echo "Command: $command"
exit 0
fi

echo "Running: $command"
`${command}`