|
| 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" |
0 commit comments