|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Operate the Consensus node boot service on macOS. |
| 4 | +# |
| 5 | +# sudo scripts/node-service.sh restart # restart, then wait for the node to serve |
| 6 | +# sudo scripts/node-service.sh status |
| 7 | +# sudo scripts/node-service.sh start|stop |
| 8 | +# scripts/node-service.sh ping # ask the orchestrator if this node is live |
| 9 | +# scripts/node-service.sh logs |
| 10 | +# |
| 11 | +# `restart` is the useful one: it relaunches the unit and then waits for the |
| 12 | +# ORCHESTRATOR to report this node active again. A live pid only says the unit |
| 13 | +# relaunched; only the orchestrator confirms the node reconnected and is serving. |
| 14 | +# |
| 15 | +# There is deliberately no "prove it started before login" command. Whether a given |
| 16 | +# start happened pre- or post-login is not something this machine can report |
| 17 | +# reliably after the fact, so the test that earns its keep is operational: restart |
| 18 | +# it and confirm the node comes back and serves. |
| 19 | +# |
| 20 | +# On Linux, use systemctl against systemd/consensus-node.service instead. |
| 21 | +# |
| 22 | +set -uo pipefail |
| 23 | + |
| 24 | +LABEL="com.consensus.node" |
| 25 | +PLIST="/Library/LaunchDaemons/${LABEL}.plist" |
| 26 | +SERVICE="system/${LABEL}" |
| 27 | + |
| 28 | +ONLINE_TIMEOUT_SECONDS="${CONSENSUS_ONLINE_TIMEOUT_SECONDS:-90}" |
| 29 | +DEFAULT_SERVER_URL="https://consensus.canister.software" |
| 30 | + |
| 31 | +red() { printf '\033[31m%s\033[0m\n' "$*"; } |
| 32 | +green() { printf '\033[32m%s\033[0m\n' "$*"; } |
| 33 | +warn() { printf '\033[33m%s\033[0m\n' "$*"; } |
| 34 | + |
| 35 | +need_root() { |
| 36 | + if [[ "${EUID}" -ne 0 ]]; then |
| 37 | + echo "This needs root (it talks to launchd's system domain):" >&2 |
| 38 | + echo " sudo $0 $1" >&2 |
| 39 | + exit 77 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +service_pid() { |
| 44 | + launchctl print "${SERVICE}" 2>/dev/null | sed -n 's/^[[:space:]]*pid = \([0-9]*\).*/\1/p' | head -1 |
| 45 | +} |
| 46 | + |
| 47 | +state_dir_from_plist() { |
| 48 | + [[ -f "${PLIST}" ]] || return 1 |
| 49 | + /usr/libexec/PlistBuddy -c "Print :EnvironmentVariables:CONSENSUS_STATE_DIR" "${PLIST}" 2>/dev/null |
| 50 | +} |
| 51 | + |
| 52 | +server_url_from_plist() { |
| 53 | + [[ -f "${PLIST}" ]] || { echo "${DEFAULT_SERVER_URL}"; return; } |
| 54 | + /usr/libexec/PlistBuddy -c "Print :EnvironmentVariables:CONSENSUS_SERVER_URL" "${PLIST}" 2>/dev/null \ |
| 55 | + || echo "${DEFAULT_SERVER_URL}" |
| 56 | +} |
| 57 | + |
| 58 | +node_id_from_state() { |
| 59 | + local state_dir; state_dir="$(state_dir_from_plist)" || state_dir="${HOME}/.consensus/node" |
| 60 | + [[ -f "${state_dir}/config.json" ]] || return 1 |
| 61 | + /usr/bin/python3 -c "import json,sys; print(json.load(open(sys.argv[1])).get('node_id') or '')" \ |
| 62 | + "${state_dir}/config.json" 2>/dev/null |
| 63 | +} |
| 64 | + |
| 65 | +# The signal that matters: does the orchestrator consider this node live? |
| 66 | +orchestrator_status() { |
| 67 | + local node_id server |
| 68 | + node_id="$(node_id_from_state)" || return 1 |
| 69 | + [[ -n "${node_id}" ]] || return 1 |
| 70 | + server="$(server_url_from_plist)" |
| 71 | + curl -fsS --max-time 10 "${server}/node/status/${node_id}" 2>/dev/null \ |
| 72 | + | /usr/bin/python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('status','unknown'))" 2>/dev/null |
| 73 | +} |
| 74 | + |
| 75 | +wait_until_online() { |
| 76 | + local deadline=$(( $(date +%s) + ONLINE_TIMEOUT_SECONDS )) status="" |
| 77 | + echo "Waiting for the orchestrator to report this node active (up to ${ONLINE_TIMEOUT_SECONDS}s)..." |
| 78 | + while (( $(date +%s) < deadline )); do |
| 79 | + status="$(orchestrator_status)" || status="" |
| 80 | + if [[ "${status}" == "active" ]]; then |
| 81 | + green " orchestrator reports: active — the node reconnected and is serving" |
| 82 | + return 0 |
| 83 | + fi |
| 84 | + sleep 5 |
| 85 | + done |
| 86 | + warn " orchestrator did not report 'active' within ${ONLINE_TIMEOUT_SECONDS}s (last: ${status:-unreachable})" |
| 87 | + echo " Check the logs: scripts/node-service.sh logs" |
| 88 | + return 1 |
| 89 | +} |
| 90 | + |
| 91 | +cmd_status() { |
| 92 | + need_root status |
| 93 | + if [[ ! -f "${PLIST}" ]]; then |
| 94 | + red "not installed — ${PLIST} is missing" |
| 95 | + echo "This node will NOT come back after a reboot until someone logs in." |
| 96 | + echo "Install the boot service with: sudo scripts/install-launchd.sh" |
| 97 | + return 1 |
| 98 | + fi |
| 99 | + echo "plist: ${PLIST}" |
| 100 | + local pid; pid="$(service_pid)" |
| 101 | + if [[ -z "${pid}" || "${pid}" == "0" ]]; then |
| 102 | + red "state: NOT RUNNING" |
| 103 | + launchctl print "${SERVICE}" 2>/dev/null | grep -E "last exit|state = " | sed 's/^[[:space:]]*/ /' |
| 104 | + return 1 |
| 105 | + fi |
| 106 | + green "state: running (pid ${pid})" |
| 107 | + local started; started="$(ps -o lstart= -p "${pid}" 2>/dev/null | sed 's/ */ /g;s/^ *//;s/ *$//')" |
| 108 | + [[ -n "${started}" ]] && echo "since: ${started}" |
| 109 | + local remote; remote="$(orchestrator_status)" || remote="" |
| 110 | + if [[ "${remote}" == "active" ]]; then |
| 111 | + green "node: active on the orchestrator" |
| 112 | + elif [[ -n "${remote}" ]]; then |
| 113 | + warn "node: orchestrator reports '${remote}'" |
| 114 | + else |
| 115 | + warn "node: could not reach the orchestrator (or no node_id in state)" |
| 116 | + fi |
| 117 | +} |
| 118 | + |
| 119 | +cmd_restart() { |
| 120 | + need_root restart |
| 121 | + [[ -f "${PLIST}" ]] || { red "not installed — run sudo scripts/install-launchd.sh"; exit 1; } |
| 122 | + echo "Restarting ${SERVICE}..." |
| 123 | + # kickstart -k kills the running instance and starts a fresh one — the same thing |
| 124 | + # launchd does on a crash. |
| 125 | + launchctl kickstart -k "${SERVICE}" || { red "kickstart failed"; exit 1; } |
| 126 | + sleep 2 |
| 127 | + local pid; pid="$(service_pid)" |
| 128 | + if [[ -z "${pid}" || "${pid}" == "0" ]]; then |
| 129 | + red "unit did not come back up" |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + green "unit relaunched (pid ${pid})" |
| 133 | + echo |
| 134 | + wait_until_online |
| 135 | +} |
| 136 | + |
| 137 | +cmd_start() { need_root start; launchctl bootstrap system "${PLIST}" && launchctl enable "${SERVICE}" && cmd_status; } |
| 138 | +cmd_stop() { need_root stop; launchctl bootout "${SERVICE}" && echo "stopped"; } |
| 139 | + |
| 140 | +cmd_ping() { |
| 141 | + local status; status="$(orchestrator_status)" || status="" |
| 142 | + if [[ "${status}" == "active" ]]; then |
| 143 | + green "active — the orchestrator sees this node" |
| 144 | + return 0 |
| 145 | + fi |
| 146 | + if [[ -n "${status}" ]]; then |
| 147 | + warn "orchestrator reports '${status}'" |
| 148 | + return 1 |
| 149 | + fi |
| 150 | + red "could not reach the orchestrator, or no node_id in state" |
| 151 | + return 1 |
| 152 | +} |
| 153 | + |
| 154 | +cmd_logs() { |
| 155 | + local state_dir; state_dir="$(state_dir_from_plist)" || state_dir="${HOME}/.consensus/node" |
| 156 | + echo "following ${state_dir} logs (ctrl-c to stop)" |
| 157 | + tail -f "${state_dir}/launchd.err.log" "${state_dir}/launchd.out.log" |
| 158 | +} |
| 159 | + |
| 160 | +case "${1:-}" in |
| 161 | + status) cmd_status ;; |
| 162 | + restart) cmd_restart ;; |
| 163 | + start) cmd_start ;; |
| 164 | + stop) cmd_stop ;; |
| 165 | + ping) cmd_ping ;; |
| 166 | + logs) cmd_logs ;; |
| 167 | + *) |
| 168 | + echo "Usage: $0 <restart|status|start|stop|ping|logs>" >&2 |
| 169 | + echo >&2 |
| 170 | + echo " restart relaunch the unit, then wait for the orchestrator to see it active" >&2 |
| 171 | + echo " ping ask the orchestrator whether this node is live" >&2 |
| 172 | + exit 64 |
| 173 | + ;; |
| 174 | +esac |
0 commit comments