Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
# Usage:
# ./setup-cosmos-full-node-azure.bash MONIKER_PREFIX SSH_PRIVATE_KEY_PATH PERSISTENT_PEER...
# To debug this script run it with TRACE=1 in the enviornment
# -x option will trace each command that is run
if [[ "$TRACE" ]]; then
set -x
VERBOSE="--verbose"
fi
# Set Bash Options.
#
# -e option will cause the script to exit immediately when a command fails.
#
# -o pipefail option sets the exit code of a pipeline to that of the rightmost command to exit with a
# non-zero status, or zero if all commands of the pipeline exit successfully.
set -eo pipefail
# Parse script args
MONIKER_PREFIX="$1"
SSH_PRIVATE_KEY_PATH="$2"
shift 2
PEERS="$*"
MONIKER="$MONIKER_PREFIX-$(tr -dc 'a-f0-9' < /dev/urandom | head -c8 || true)"
# Create a resource group that will contain our Cosmos node VM
az group create "$VERBOSE" --name "$MONIKER" --location eastus --tags "type=cosmos"
# Create our Cosmos node VM
# TODO: The size of the VM required needs exploration.
az vm create "$VERBOSE" \
--resource-group "$MONIKER" \
--name "$MONIKER" \
--size Standard_DS4_v2 \
--image credativ:Debian:9:9.20190221.0 \
--admin-username cosmos \
--ssh-key-value "$SSH_PRIVATE_KEY_PATH.pub"
# open ports 26656-26657
az vm open-port "$VERBOSE" --resource-group "$MONIKER" --name "$MONIKER" --port 26656-26657
# extract IP of the new VM from azure
IP=$(az vm list-ip-addresses "$VERBOSE" \
--resource-group "$MONIKER" \
--name "$MONIKER" \
--query [0].virtualMachine.network.publicIpAddresses[0].ipAddress \
-o tsv)
# copy the setup-cosmos-full-node.bash script to vm
scp -i "$SSH_PRIVATE_KEY_PATH" -o "StrictHostKeyChecking no" \
setup-cosmos-full-node.bash cosmos@${IP}:/tmp/setup-cosmos-full-node.bash
# run setup-cosmos-full-node.bash script in vm
ssh -i "$SSH_PRIVATE_KEY_PATH" -o "StrictHostKeyChecking no" \
-t cosmos@${IP} "TRACE=$TRACE /tmp/setup-cosmos-full-node.bash $MONIKER $PEERS"
echo "Started: $MONIKER ($IP)"
#!/usr/bin/env bash
# Usage:
# ./setup-cosmos-full-node.bash NODE_MONIKER PERSISTENT_PEERS...
# To debug this script run it with TRACE=1 in the enviornment
# -x option will trace each command that is run
[[ "$TRACE" ]] && set -x
# Set Bash Options.
#
# -e option will cause the script to exit immediately when a command fails.
#
# -o pipefail option sets the exit code of a pipeline to that of the rightmost command to exit with a
# non-zero status, or zero if all commands of the pipeline exit successfully.
set -eo pipefail
# Parse script args
MONIKER="$1"
shift
PEERS="$*"
# replace space with comma in peer list
PEERS=${PEERS// /,}
# Download go, confirm checksum
wget https://dl.google.com/go/go1.12.linux-amd64.tar.gz
echo "750a07fef8579ae4839458701f4df690e0b20b8bcce33b437e4df89c451b6f13 go1.12.linux-amd64.tar.gz" | sha256sum -c -
# Untar and install go to $HOME/go
tar xvf go1.12.linux-amd64.tar.gz
echo 'export GOROOT=$HOME/go' > .profile
# setup GOPATH
mkdir workspace
echo 'export GOPATH=$HOME/workspace' >> .profile
# setup PATH
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> .profile
source .profile
# install tools needed to build cosmos-sdk
sudo apt-get update
sudo apt-get install -y build-essential git
# clone source, build and install cosmos-sdk
mkdir -p "$GOPATH/src/github.com/cosmos"
cd "$GOPATH/src/github.com/cosmos"
git clone https://github.com/cosmos/cosmos-sdk
cd cosmos-sdk
git checkout v0.33.0
make update_tools
make vendor-deps
make install
cd "$HOME"
gaiad init "$MONIKER"
# setup genesis file
rm "$HOME/.gaiad/config/genesis.json"
wget https://raw.githubusercontent.com/cosmos/launch/master/genesis.json
echo "73a866b21723ecbc28b6d15951b2eb3aa2f2443650ff6df489bf55ac5edceefa genesis.json" | sha256sum -c -
mv genesis.json "$HOME/.gaiad/config/genesis.json"
# add persistent_peers to config.toml
sed -i "s/persistent_peers.*/persistent_peers = \"$PEERS\"/g" "$HOME/.gaiad/config/config.toml"
# setup gaiad systemd service
sudo cp /home/cosmos/workspace/bin/gaia* /usr/local/bin/
cat << 'EOF' |sudo tee /lib/systemd/system/gaiad.service
[Unit]
Description=gaiad
[Service]
Type=simple
Restart=always
RestartSec=5s
User=cosmos
ExecStart=/usr/local/bin/gaiad start --log_level="*:info"
[Install]
WantedBy=multi-user.target
EOF
# start the gaiad service
sudo systemctl start gaiad
sudo systemctl enable gaiad
sudo systemctl status gaiad --no-pager --full

Setup Cosmos Full Node In Azure

This gist contains two scripts

  • setup-cosmos-full-node-azure.bash which creates a vm in azure and then sets up a cosmos full node in that vm.
  • setup-cosmos-full-node.bash which sets up a cosmos full node locally on the machine it is running on.

How to use

Clone this gist and make sure Azure CLI is setup.

The azure script expects to be run as follows, all arguments are required:

./setup-cosmos-full-node-azure.bash MONIKER_PREFIX SSH_PRIVATE_KEY_PATH PERSISTENT_PEER...

This will take the first argument MONIKER_PREFIX and generate a random moniker of the form $MONIKER_PREFIX-c1b7d70d

Path to the private ssh key is required because the script will ssh into the machine to provision. The public key is assumed to be at $SSH_PRIVATE_KEY_PATH.pub. Also note that azure only supports RSA keys for SSH.

There must be at least one persistent peer of the form 89e4b72625c0a13d6f62e3cd9d40bfc444cbfa77@34.65.6.52:26656. Multiple peers may be passed in, see example below.

For example:

./setup-cosmos-full-node-azure.bash cosmos ~/.ssh/id_rsa "89e4b72625c0a13d6f62e3cd9d40bfc444cbfa77@34.65.6.52:26656" "2626942148fd39830cb7a3acccb235fab0332d86@173.212.199.36:26656"

Running the script will:

  1. Create a resource group called cosmos in your azure account
  2. Create a new virtual machine in that resource group call cosmos-full-node based on the Debian image credativ:Debian:9:9.20190221.0
  3. Copy the ./setup-cosmos-full-node.bash script into that VM over ssh
  4. Run the ./setup-cosmos-full-node.bash script to install cosmos-sdk and start gaiad as a systemd service
  5. Join the Cosmos mainnet

Once the script finishes it will print the IP of the newly created node, you can check status of the node by running:

curl http://IP:26657/status

To see the logs of the running node, ssh into the machine and run:

sudo journalctl -f -u gaiad.service

Debug

To have the script print out every command it runs invoke it with TRACE=1

For example:

TRACE=1 ./setup-cosmos-full-node-azure.bash cosmos ~/.ssh/id_rsa "89e4b72625c0a13d6f62e3cd9d40bfc444cbfa77@34.65.6.52:26656" "2626942148fd39830cb7a3acccb235fab0332d86@173.212.199.36:26656"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.