Skip to content

Commit

Permalink
Merge pull request #285 from spivachuk/switch-to-cosmovisor-script
Browse files Browse the repository at this point in the history
Added `switch_to_cosmovisor` script
  • Loading branch information
ashcherbakov committed Mar 16, 2022
2 parents 56f8209 + a1cc94d commit a562490
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 12 deletions.
11 changes: 11 additions & 0 deletions deployment/scripts/run_dcl_node
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ if [[ "$verbosity" -ge 1 ]]; then
#echo -e "Parsed arguments:\n$parsed_args"
fi

sudo chown "$NODE_USER" ./dcld
sudo chmod u+x ./dcld

echo "Configuring CLI"

# Note. we consider that cli would connect to local node (default setting)
Expand Down Expand Up @@ -491,6 +494,11 @@ echo -e "\nOptionally, edit '$CONFIG_FILE' in order to set different setting (li
echo "Locating the genesis app version to $GENESIS_APP_DIR directory"
mkdir -p "$GENESIS_APP_DIR"
cp -f ./dcld "$GENESIS_APP_DIR"/
sudo chown "$NODE_USER" "$GENESIS_APP_DIR"/dcld
# Execution permissions on `dcld` are granted to all (i.e. User, Group and Others classes)
# because cosmovisor requires execution permission on the application binary to be granted to Others class:
# https://github.com/cosmos/cosmos-sdk/blob/cosmovisor/v1.0.0/cosmovisor/upgrade.go#L164
sudo chmod a+x "$GENESIS_APP_DIR"/dcld

echo "Running the node"
run_node
Expand All @@ -501,6 +509,9 @@ wait_node_up
echo -e "\nexport PATH=\$PATH:$CURRENT_APP_DIR" >> "$HOME"/.profile
source "$HOME"/.profile

echo "Added '${CURRENT_APP_DIR}' to PATH to maintain 'dcld' command associated with the latest installed app binary."
echo "Execute 'source ~/.profile' or restart shell to take the PATH change effective."

STATUS="$(dcld status)"
VAL_ID="$(_jq "$STATUS"| grep '"id"' | awk '{print $NF}' | sed -e 's/^"//' -e 's/[",]\+$//')"
VAL_ADDR="$(dcld tendermint show-address)"
Expand Down
147 changes: 147 additions & 0 deletions deployment/scripts/switch_to_cosmovisor
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/bin/bash
# Copyright 2020 DSR Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
set -o pipefail

GENESIS_APP_DIR="$HOME/.dcl/cosmovisor/genesis/bin"
CURRENT_APP_DIR="$HOME/.dcl/cosmovisor/current/bin"

DEF_NODE_USER="ubuntu"
CURR_USER="${USER:-$(whoami 2>/dev/null)}"
NODE_USER="${CURR_USER:-$DEF_NODE_USER}"

function wait_node_up {
local _timeout="${1:-60}"
local _try=1

echo -e "Waiting the node becomes up"
until ./dcld status >/dev/null 2>&1
do
if [[ "$_try" -gt "$_timeout" ]]; then
echo -e "\nERROR: dcld node seems not ready after $_timeout seconds."
return 1
fi
echo -n "."
_try=$(( $_try + 1 ))
sleep 1
done
echo -e "\n\tNode is responding"
}

### Preliminary checks

if [[ ! -d "/etc/systemd/system" ]]; then
echo "Error. Not a systemd system. This script supports systemd systems only."
exit 1
fi

if [[ ! -f "./dcld" ]]; then
echo "Error. './dcld' file to install not found"
exit 1
fi

if [[ ! -f "./cosmovisor" ]]; then
echo "Error. './cosmovisor' file not found"
exit 1
fi

if [[ ! -f "./cosmovisor.service" ]]; then
echo "Error. './cosmovisor.service' file not found"
exit 1
fi

OLD_DCLD="$(which dcld)"

if [[ -z "$OLD_DCLD" ]]; then
echo "Error. No installed 'dcld' found"
exit 1
fi

BIN_DIR="$(dirname "$OLD_DCLD")"

if [[ ! -f "/etc/systemd/system/dcld.service" ]]; then
echo "Error. '/etc/systemd/system/dcld.service' file not found"
exit 1
fi

OLD_NODE_USER=$(cat /etc/systemd/system/dcld.service | sed -n "s~^User=\([a-z_][a-z0-9_-]*[$]\?\)$~\1~p")

if [[ "$NODE_USER" != "$OLD_NODE_USER" ]]; then
echo "Error. Wrong current user: '${NODE_USER}'. Expected user: '${OLD_NODE_USER}', on behalf of whom 'dcld' service is launched."
exit 1
fi

if ! systemctl is-active --quiet dcld; then
echo "Error. 'dcld' service is not running. Transition to cosmovisor will not be performed."
exit 1
fi

### Old stand-alone dcld removal

sudo systemctl stop dcld
echo "Stopped 'dcld' service"

sudo systemctl disable dcld

sudo rm -f /etc/systemd/system/dcld.service
echo "Removed 'dcld' service"

sudo rm -f "$BIN_DIR"/dcld
echo "Removed old stand-alone 'dcld' binary from '${BIN_DIR}'"

### Cosmovisor and new dcld installation

sudo chown "$NODE_USER" ./dcld
sudo chmod u+x ./dcld

sudo cp -f ./cosmovisor "$BIN_DIR"/
sudo chown "$NODE_USER" "$BIN_DIR"/cosmovisor
sudo chmod u+x "$BIN_DIR"/cosmovisor
echo "Copied 'cosmovisor' to '${BIN_DIR}'"

mkdir -p "$GENESIS_APP_DIR"
echo "Created '${GENESIS_APP_DIR}' directory treated by cosmovisor as path to genesis app version"

sudo cp -f ./dcld "$GENESIS_APP_DIR"/
sudo chown "$NODE_USER" "$GENESIS_APP_DIR"/dcld
# Execution permissions on `dcld` are granted to all (i.e. User, Group and Others classes)
# because cosmovisor requires execution permission on the application binary to be granted to Others class:
# https://github.com/cosmos/cosmos-sdk/blob/cosmovisor/v1.0.0/cosmovisor/upgrade.go#L164
sudo chmod a+x "$GENESIS_APP_DIR"/dcld
echo "Copied new 'dcld' binary to '${GENESIS_APP_DIR}/'"

# set up systemd cosmovisor.service
sed -i -r "s~^User=ubuntu$~User=${NODE_USER}~" ./cosmovisor.service
sed -i -r "s~\"DAEMON_HOME=/var/lib/ubuntu/.dcl\"~\"DAEMON_HOME=${HOME}/.dcl\"~" ./cosmovisor.service

sudo cp -f ./cosmovisor.service /etc/systemd/system/
echo "Added 'cosmovisor' service"

sudo systemctl enable cosmovisor

sudo systemctl start cosmovisor
echo "Started 'cosmovisor' service"

echo -e "\tUse 'systemctl status cosmovisor' to get the node service status."
echo "Use 'journalctl -u cosmovisor.service -f' to see node logs."

echo -e "\nexport PATH=\$PATH:$CURRENT_APP_DIR" >> "$HOME"/.profile

echo "Added '${CURRENT_APP_DIR}' to PATH to maintain 'dcld' command associated with the latest installed app binary."
echo "Execute 'source ~/.profile' or restart shell to take the PATH change effective."

wait_node_up
echo "Done"
8 changes: 6 additions & 2 deletions docs/advanced/running-genesis-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ The following components will be needed:

### Deployment steps

1. Put `cosmovisor` binary to `/usr/bin/` and configure permissions.
1. Put `cosmovisor` binary to `/usr/bin/`, set proper owner and execution permissions.

2. Create `$HOME/.dcl/cosmovisor/genesis/bin` directory and copy `dcld` binary to it.
2. Locate the genesis app version to genesis application version directory:
* Create `$HOME/.dcl/cosmovisor/genesis/bin` directory.
* Copy `dcld` binary to it, set proper owner and execution permissions.
Please note that execution permissions on `dcld` should be granted to all (i.e. User, Group and Others classes)
because cosmovisor requires execution permission on the application binary to be granted to Others class.

3. Choose the chain ID. Every network (for example, test-net, main-net, etc.)
must have a unique chain ID.
Expand Down
8 changes: 6 additions & 2 deletions docs/advanced/running-observer-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ and contains the genesis and persistent_peers files.

### Deployment steps

1. Put `cosmovisor` binary to `/usr/bin/` and configure permissions.
1. Put `cosmovisor` binary to `/usr/bin/`, set proper owner and execution permissions.

2. Create `$HOME/.dcl/cosmovisor/genesis/bin` directory and copy `dcld` binary to it.
2. Locate the genesis app version to genesis application version directory:
* Create `$HOME/.dcl/cosmovisor/genesis/bin` directory.
* Copy `dcld` binary to it, set proper owner and execution permissions.
Please note that execution permissions on `dcld` should be granted to all (i.e. User, Group and Others classes)
because cosmovisor requires execution permission on the application binary to be granted to Others class.

3. Configure CLI:
- `./dcld config chain-id testnet`
Expand Down
8 changes: 6 additions & 2 deletions docs/advanced/running-validator-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ and contains the genesis and persistent_peers files.

### Deployment steps

1. Put `cosmovisor` binary to `/usr/bin/` and configure permissions.
1. Put `cosmovisor` binary to `/usr/bin/`, set proper owner and execution permissions.

2. Create `$HOME/.dcl/cosmovisor/genesis/bin` directory and copy `dcld` binary to it.
2. Locate the genesis app version to genesis application version directory:
* Create `$HOME/.dcl/cosmovisor/genesis/bin` directory.
* Copy `dcld` binary to it, set proper owner and execution permissions.
Please note that execution permissions on `dcld` should be granted to all (i.e. User, Group and Others classes)
because cosmovisor requires execution permission on the application binary to be granted to Others class.

3. Configure CLI:
- `./dcld config chain-id <chain-id>`
Expand Down
48 changes: 48 additions & 0 deletions docs/advanced/switch-to-cosmovisor-how-to.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Switch to Cosmovisor: How To

This document describes the procedure of how to switch a node from direct use of
`dcld` binary to use of `cosmovisor` process manager which controls `dcld`
process and supports DCL application upgrades that include `dcld` binary updates
and store migrations.

Switching to use of `cosmovisor` is performed by `switch_to_cosmovisor` script.
This procedure does not include any store migrations. So it can be applied only
if the difference between the previously installed stand-alone `dcld` binary and
`dcld` binary to install with cosmovisor does not include any breaking changes
of the store.

**Pre-requisites:**

* `dcld` is launched as `dcld` systemd service.
* `dcld` service is currently in active state (i.e. running).
* The current user is the user on behalf of whom `dcld` service is launched.

**Steps:**

* Download new `dcld`, `cosmovisor` and `cosmovisor.service` from GitHub
[release page](https://github.com/zigbee-alliance/distributed-compliance-ledger/releases)

Example using curl:
```bash
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/dcld
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor.service
```

* Download `switch_to_cosmovisor` script from [repository](../../deployment/scripts/)

Example using curl:
```bash
curl -L -O https://raw.githubusercontent.com/zigbee-alliance/distributed-compliance-ledger/master/deployment/scripts/switch_to_cosmovisor
```

* Run `switch_to_cosmovisor` script:

```bash
./switch_to_cosmovisor
```

When it is done, it will print:
```
Done
```
8 changes: 2 additions & 6 deletions docs/running-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ rm -rf "$HOME/.dcl"
```bash
# release artifacts
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/dcld
# TODO: Add cosmovisor to release
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor.service

Expand All @@ -111,9 +110,8 @@ curl -L -O https://raw.githubusercontent.com/zigbee-alliance/distributed-complia
### Setup DCL binaries

* put `cosmovisor` binary in a folder listed in `$PATH` (e.g. `/usr/bin/`)
* set a proper owner and executable permissions
* create `$HOME/.dcl/cosmovisor/genesis/bin` directory for the genesis version of the application binary
* copy `dcld` binary to the created directory, but do not remove it from the current working directory (to be able to perform initialization steps below)
* set owner of `cosmovisor` binary to the user who will be used for `cosmovisor` service to run as
* set executable permission on `cosmovisor` binary for owner

<!-- markdownlint-disable MD033 -->
<details>
Expand All @@ -124,8 +122,6 @@ curl -L -O https://raw.githubusercontent.com/zigbee-alliance/distributed-complia
sudo cp -f ./cosmovisor -t /usr/bin
sudo chown ubuntu /usr/bin/cosmovisor
sudo chmod u+x /usr/bin/cosmovisor
mkdir -p "$HOME/.dcl/cosmovisor/genesis/bin"
cp -f ./dcld -t "$HOME/.dcl/cosmovisor/genesis/bin"
```

</p>
Expand Down

0 comments on commit a562490

Please sign in to comment.