Skip to content

Commit a562490

Browse files
authored
Merge pull request #285 from spivachuk/switch-to-cosmovisor-script
Added `switch_to_cosmovisor` script
2 parents 56f8209 + a1cc94d commit a562490

7 files changed

+226
-12
lines changed

deployment/scripts/run_dcl_node

+11
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ if [[ "$verbosity" -ge 1 ]]; then
452452
#echo -e "Parsed arguments:\n$parsed_args"
453453
fi
454454

455+
sudo chown "$NODE_USER" ./dcld
456+
sudo chmod u+x ./dcld
457+
455458
echo "Configuring CLI"
456459

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

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

512+
echo "Added '${CURRENT_APP_DIR}' to PATH to maintain 'dcld' command associated with the latest installed app binary."
513+
echo "Execute 'source ~/.profile' or restart shell to take the PATH change effective."
514+
504515
STATUS="$(dcld status)"
505516
VAL_ID="$(_jq "$STATUS"| grep '"id"' | awk '{print $NF}' | sed -e 's/^"//' -e 's/[",]\+$//')"
506517
VAL_ADDR="$(dcld tendermint show-address)"
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/bin/bash
2+
# Copyright 2020 DSR Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -eu
17+
set -o pipefail
18+
19+
GENESIS_APP_DIR="$HOME/.dcl/cosmovisor/genesis/bin"
20+
CURRENT_APP_DIR="$HOME/.dcl/cosmovisor/current/bin"
21+
22+
DEF_NODE_USER="ubuntu"
23+
CURR_USER="${USER:-$(whoami 2>/dev/null)}"
24+
NODE_USER="${CURR_USER:-$DEF_NODE_USER}"
25+
26+
function wait_node_up {
27+
local _timeout="${1:-60}"
28+
local _try=1
29+
30+
echo -e "Waiting the node becomes up"
31+
until ./dcld status >/dev/null 2>&1
32+
do
33+
if [[ "$_try" -gt "$_timeout" ]]; then
34+
echo -e "\nERROR: dcld node seems not ready after $_timeout seconds."
35+
return 1
36+
fi
37+
echo -n "."
38+
_try=$(( $_try + 1 ))
39+
sleep 1
40+
done
41+
echo -e "\n\tNode is responding"
42+
}
43+
44+
### Preliminary checks
45+
46+
if [[ ! -d "/etc/systemd/system" ]]; then
47+
echo "Error. Not a systemd system. This script supports systemd systems only."
48+
exit 1
49+
fi
50+
51+
if [[ ! -f "./dcld" ]]; then
52+
echo "Error. './dcld' file to install not found"
53+
exit 1
54+
fi
55+
56+
if [[ ! -f "./cosmovisor" ]]; then
57+
echo "Error. './cosmovisor' file not found"
58+
exit 1
59+
fi
60+
61+
if [[ ! -f "./cosmovisor.service" ]]; then
62+
echo "Error. './cosmovisor.service' file not found"
63+
exit 1
64+
fi
65+
66+
OLD_DCLD="$(which dcld)"
67+
68+
if [[ -z "$OLD_DCLD" ]]; then
69+
echo "Error. No installed 'dcld' found"
70+
exit 1
71+
fi
72+
73+
BIN_DIR="$(dirname "$OLD_DCLD")"
74+
75+
if [[ ! -f "/etc/systemd/system/dcld.service" ]]; then
76+
echo "Error. '/etc/systemd/system/dcld.service' file not found"
77+
exit 1
78+
fi
79+
80+
OLD_NODE_USER=$(cat /etc/systemd/system/dcld.service | sed -n "s~^User=\([a-z_][a-z0-9_-]*[$]\?\)$~\1~p")
81+
82+
if [[ "$NODE_USER" != "$OLD_NODE_USER" ]]; then
83+
echo "Error. Wrong current user: '${NODE_USER}'. Expected user: '${OLD_NODE_USER}', on behalf of whom 'dcld' service is launched."
84+
exit 1
85+
fi
86+
87+
if ! systemctl is-active --quiet dcld; then
88+
echo "Error. 'dcld' service is not running. Transition to cosmovisor will not be performed."
89+
exit 1
90+
fi
91+
92+
### Old stand-alone dcld removal
93+
94+
sudo systemctl stop dcld
95+
echo "Stopped 'dcld' service"
96+
97+
sudo systemctl disable dcld
98+
99+
sudo rm -f /etc/systemd/system/dcld.service
100+
echo "Removed 'dcld' service"
101+
102+
sudo rm -f "$BIN_DIR"/dcld
103+
echo "Removed old stand-alone 'dcld' binary from '${BIN_DIR}'"
104+
105+
### Cosmovisor and new dcld installation
106+
107+
sudo chown "$NODE_USER" ./dcld
108+
sudo chmod u+x ./dcld
109+
110+
sudo cp -f ./cosmovisor "$BIN_DIR"/
111+
sudo chown "$NODE_USER" "$BIN_DIR"/cosmovisor
112+
sudo chmod u+x "$BIN_DIR"/cosmovisor
113+
echo "Copied 'cosmovisor' to '${BIN_DIR}'"
114+
115+
mkdir -p "$GENESIS_APP_DIR"
116+
echo "Created '${GENESIS_APP_DIR}' directory treated by cosmovisor as path to genesis app version"
117+
118+
sudo cp -f ./dcld "$GENESIS_APP_DIR"/
119+
sudo chown "$NODE_USER" "$GENESIS_APP_DIR"/dcld
120+
# Execution permissions on `dcld` are granted to all (i.e. User, Group and Others classes)
121+
# because cosmovisor requires execution permission on the application binary to be granted to Others class:
122+
# https://github.com/cosmos/cosmos-sdk/blob/cosmovisor/v1.0.0/cosmovisor/upgrade.go#L164
123+
sudo chmod a+x "$GENESIS_APP_DIR"/dcld
124+
echo "Copied new 'dcld' binary to '${GENESIS_APP_DIR}/'"
125+
126+
# set up systemd cosmovisor.service
127+
sed -i -r "s~^User=ubuntu$~User=${NODE_USER}~" ./cosmovisor.service
128+
sed -i -r "s~\"DAEMON_HOME=/var/lib/ubuntu/.dcl\"~\"DAEMON_HOME=${HOME}/.dcl\"~" ./cosmovisor.service
129+
130+
sudo cp -f ./cosmovisor.service /etc/systemd/system/
131+
echo "Added 'cosmovisor' service"
132+
133+
sudo systemctl enable cosmovisor
134+
135+
sudo systemctl start cosmovisor
136+
echo "Started 'cosmovisor' service"
137+
138+
echo -e "\tUse 'systemctl status cosmovisor' to get the node service status."
139+
echo "Use 'journalctl -u cosmovisor.service -f' to see node logs."
140+
141+
echo -e "\nexport PATH=\$PATH:$CURRENT_APP_DIR" >> "$HOME"/.profile
142+
143+
echo "Added '${CURRENT_APP_DIR}' to PATH to maintain 'dcld' command associated with the latest installed app binary."
144+
echo "Execute 'source ~/.profile' or restart shell to take the PATH change effective."
145+
146+
wait_node_up
147+
echo "Done"

docs/advanced/running-genesis-node.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ The following components will be needed:
3636

3737
### Deployment steps
3838

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

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

4347
3. Choose the chain ID. Every network (for example, test-net, main-net, etc.)
4448
must have a unique chain ID.

docs/advanced/running-observer-node.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ and contains the genesis and persistent_peers files.
4646

4747
### Deployment steps
4848

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

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

5357
3. Configure CLI:
5458
- `./dcld config chain-id testnet`

docs/advanced/running-validator-node.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ and contains the genesis and persistent_peers files.
4646

4747
### Deployment steps
4848

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

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

5357
3. Configure CLI:
5458
- `./dcld config chain-id <chain-id>`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Switch to Cosmovisor: How To
2+
3+
This document describes the procedure of how to switch a node from direct use of
4+
`dcld` binary to use of `cosmovisor` process manager which controls `dcld`
5+
process and supports DCL application upgrades that include `dcld` binary updates
6+
and store migrations.
7+
8+
Switching to use of `cosmovisor` is performed by `switch_to_cosmovisor` script.
9+
This procedure does not include any store migrations. So it can be applied only
10+
if the difference between the previously installed stand-alone `dcld` binary and
11+
`dcld` binary to install with cosmovisor does not include any breaking changes
12+
of the store.
13+
14+
**Pre-requisites:**
15+
16+
* `dcld` is launched as `dcld` systemd service.
17+
* `dcld` service is currently in active state (i.e. running).
18+
* The current user is the user on behalf of whom `dcld` service is launched.
19+
20+
**Steps:**
21+
22+
* Download new `dcld`, `cosmovisor` and `cosmovisor.service` from GitHub
23+
[release page](https://github.com/zigbee-alliance/distributed-compliance-ledger/releases)
24+
25+
Example using curl:
26+
```bash
27+
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/dcld
28+
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor
29+
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor.service
30+
```
31+
32+
* Download `switch_to_cosmovisor` script from [repository](../../deployment/scripts/)
33+
34+
Example using curl:
35+
```bash
36+
curl -L -O https://raw.githubusercontent.com/zigbee-alliance/distributed-compliance-ledger/master/deployment/scripts/switch_to_cosmovisor
37+
```
38+
39+
* Run `switch_to_cosmovisor` script:
40+
41+
```bash
42+
./switch_to_cosmovisor
43+
```
44+
45+
When it is done, it will print:
46+
```
47+
Done
48+
```

docs/running-node.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ rm -rf "$HOME/.dcl"
8787
```bash
8888
# release artifacts
8989
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/dcld
90-
# TODO: Add cosmovisor to release
9190
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor
9291
curl -L -O https://github.com/zigbee-alliance/distributed-compliance-ledger/releases/download/<release>/cosmovisor.service
9392

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

113112
* put `cosmovisor` binary in a folder listed in `$PATH` (e.g. `/usr/bin/`)
114-
* set a proper owner and executable permissions
115-
* create `$HOME/.dcl/cosmovisor/genesis/bin` directory for the genesis version of the application binary
116-
* 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)
113+
* set owner of `cosmovisor` binary to the user who will be used for `cosmovisor` service to run as
114+
* set executable permission on `cosmovisor` binary for owner
117115

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

131127
</p>

0 commit comments

Comments
 (0)