-
Notifications
You must be signed in to change notification settings - Fork 174
/
cnode.sh
executable file
·169 lines (146 loc) · 5.9 KB
/
cnode.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
# shellcheck disable=SC2086
#shellcheck source=/dev/null
. "$(dirname $0)"/env offline
######################################
# User Variables - Change as desired #
# Common variables set in env file #
######################################
#CPU_CORES=4 # Number of CPU cores cardano-node process has access to (please don't set higher than physical core count, recommended to set atleast to 4)
#MEMPOOL_BYTES=8388608 # Override mempool in bytes (Default: Do not override)
#CNODE_LISTEN_IP4=0.0.0.0 # IP to use for listening (only applicable to Node Connection Port) for IPv4
#CNODE_LISTEN_IP6=:: # IP to use for listening (only applicable to Node Connection Port) for IPv6
######################################
# Do NOT modify code below #
######################################
#####################
# Functions #
#####################
usage() {
cat <<-EOF
Usage: $(basename "$0") [-d]
Cardano Node wrapper script !!
-d Deploy cnode as a systemd service
-s Stop cnode using SIGINT
EOF
exit 1
}
set_defaults() {
[[ -z ${CPU_CORES} ]] && CPU_CORES=4
[[ -n ${CPU_CORES} ]] && CPU_RUNTIME=( "+RTS" "-N${CPU_CORES}" "-RTS" )
[[ -z ${CNODE_LISTEN_IP4} ]] && CNODE_LISTEN_IP4=0.0.0.0
[[ -z ${CNODE_LISTEN_IP6} ]] && CNODE_LISTEN_IP6=::
[[ ! -d "${LOG_DIR}/archive" ]] && mkdir -p "${LOG_DIR}/archive"
host_addr=()
[[ ${IP_VERSION} = "4" || ${IP_VERSION} = "mix" ]] && host_addr+=("--host-addr" "${CNODE_LISTEN_IP4}")
[[ ${IP_VERSION} = "6" || ${IP_VERSION} = "mix" ]] && host_addr+=("--host-ipv6-addr" "${CNODE_LISTEN_IP6}")
[[ -z ${MEMPOOL_BYTES} ]] && MEMPOOL_OVERRIDE="" || MEMPOOL_OVERRIDE="--mempool-capacity-override ${MEMPOOL_BYTES}"
}
pre_startup_sanity() {
# Check if node is already running, or if stale socket file is left
if [[ -S "${CARDANO_NODE_SOCKET_PATH}" ]]; then
if pgrep -f "$(basename ${CNODEBIN}).*.${CARDANO_NODE_SOCKET_PATH}"; then
echo "ERROR: A Cardano node is already running, please terminate this node before starting a new one with this script."
exit 1
else
unlink "${CARDANO_NODE_SOCKET_PATH}"
echo "INFO: Cleaned-up stale socket file"
fi
fi
# Move logs to archive
[[ $(find "${LOG_DIR}"/node*.json 2>/dev/null | wc -l) -gt 0 ]] && mv "${LOG_DIR}"/node*.json "${LOG_DIR}"/archive/
}
mithril_snapshot_download() {
[[ -z "${MITHRIL_CLIENT}" ]] && MITHRIL_CLIENT="${CNODE_HOME}"/scripts/mithril-client.sh
if [[ ! -f "${MITHRIL_CLIENT}" ]] || [[ ! -e "${MITHRIL_CLIENT}" ]]; then
echo "ERROR: Could not locate mithril-client.sh script or script is not executable. Skipping mithril cardano-db snapshot download!!"
else
"${MITHRIL_CLIENT}" cardano-db download
fi
}
stop_node() {
CNODE_PID=$(pgrep -fn "$(basename ${CNODEBIN}).*.--port ${CNODE_PORT}" 2>/dev/null) # env was only called in offline mode
kill -2 ${CNODE_PID} 2>/dev/null
# touch clean "${CNODE_HOME}"/db/clean # Disabled as it's a bit hacky, but only runs when SIGINT is passed to node process. Should not be needed if node does it's job
printf " Sending SIGINT to cardano-node process.."
sleep 5
exit 0
}
deploy_systemd() {
echo "Deploying ${CNODE_VNAME} as systemd service.."
sudo bash -c "cat <<-'EOF' > /etc/systemd/system/${CNODE_VNAME}.service
[Unit]
Description=Cardano Node
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=600
StartLimitBurst=5
[Service]
Type=simple
Restart=on-failure
RestartSec=60
User=${USER}
LimitNOFILE=1048576
WorkingDirectory=${CNODE_HOME}/scripts
ExecStart=/bin/bash -l -c \"exec ${CNODE_HOME}/scripts/cnode.sh\"
ExecStop=/bin/bash -l -c \"exec ${CNODE_HOME}/scripts/cnode.sh -s\"
KillSignal=SIGINT
SuccessExitStatus=143
SyslogIdentifier=${CNODE_VNAME}
TimeoutStopSec=60
[Install]
WantedBy=multi-user.target
EOF" && echo "${CNODE_VNAME}.service deployed successfully!!" && sudo systemctl daemon-reload && sudo systemctl enable ${CNODE_VNAME}.service
}
###################
# Execution #
###################
# Parse command line options
while getopts :ds opt; do
case ${opt} in
d ) DEPLOY_SYSTEMD="Y" ;;
s ) STOP_NODE="Y" ;;
\? ) usage ;;
esac
done
# Check if env file is missing in current folder (no update checks as will mostly run as daemon), source env if present
[[ ! -f "$(dirname $0)"/env ]] && echo -e "\nCommon env file missing, please ensure latest guild-deploy.sh was run and this script is being run from ${CNODE_HOME}/scripts folder! \n" && exit 1
. "$(dirname $0)"/env offline
case $? in
1) echo -e "ERROR: Failed to load common env file\nPlease verify set values in 'User Variables' section in env file or log an issue on GitHub" && exit 1;;
2) clear ;;
esac
[[ "${STOP_NODE}" == "Y" ]] && stop_node
# Set defaults and do basic sanity checks
set_defaults
#Deploy systemd if -d argument was specified
if [[ "${DEPLOY_SYSTEMD}" == "Y" ]]; then
deploy_systemd && exit 0
exit 2
fi
pre_startup_sanity
# Download the latest mithril snapshot before starting node
if [[ "${MITHRIL_DOWNLOAD}" == "Y" ]]; then
mithril_snapshot_download
fi
# Run Node
if [[ -f "${POOL_DIR}/${POOL_OPCERT_FILENAME}" && -f "${POOL_DIR}/${POOL_VRF_SK_FILENAME}" && -f "${POOL_DIR}/${POOL_HOTKEY_SK_FILENAME}" ]]; then
exec "${CNODEBIN}" "${CPU_RUNTIME[@]}" run \
--topology "${TOPOLOGY}" \
--config "${CONFIG}" \
--database-path "${DB_DIR}" \
--socket-path "${CARDANO_NODE_SOCKET_PATH}" \
--shelley-kes-key "${POOL_DIR}/${POOL_HOTKEY_SK_FILENAME}" \
--shelley-vrf-key "${POOL_DIR}/${POOL_VRF_SK_FILENAME}" \
--shelley-operational-certificate "${POOL_DIR}/${POOL_OPCERT_FILENAME}" \
--port ${CNODE_PORT} \
${MEMPOOL_OVERRIDE} "${host_addr[@]}"
else
exec "${CNODEBIN}" "${CPU_RUNTIME[@]}" run \
--topology "${TOPOLOGY}" \
--config "${CONFIG}" \
--database-path "${DB_DIR}" \
--socket-path "${CARDANO_NODE_SOCKET_PATH}" \
--port ${CNODE_PORT} \
${MEMPOOL_OVERRIDE} "${host_addr[@]}"
fi