-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathautoupdate.sh
More file actions
171 lines (141 loc) · 4.64 KB
/
autoupdate.sh
File metadata and controls
171 lines (141 loc) · 4.64 KB
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
170
171
#!/usr/bin/env bash
# Autoupdate script for MANTIS validator
#
# This script periodically checks the GitHub repository for new commits on the
# specified branch, pulls updates, and restarts the pm2 process named
# "validator" if it is already running. If the process is not found, it will
# start it using the provided wallet.name and wallet.hotkey parameters, which
# can be supplied via CLI flags or loaded from a .env file.
#
# Usage examples:
# ./autoupdate.sh # Uses .env for wallet vars
# ./autoupdate.sh -w mywallet -k myhotkey # CLI overrides .env values
# ./autoupdate.sh -i 120 # Check every 120 seconds
#
# Flags:
# -w|--wallet.name The wallet name to use when starting validator
# -k|--wallet.hotkey The wallet hotkey to use when starting validator
# -b|--branch Git branch to track (default: main)
# -i|--interval Seconds between update checks (default: 60)
set -euo pipefail
##############################
# Helper functions
##############################
log() {
# Timestamped logging helper
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
print_usage() {
cat <<EOF
Usage: $0 [options]
Options:
-w, --wallet.name <wallet_name>
-k, --wallet.hotkey <wallet_hotkey>
-b, --branch <branch> (default: main)
-i, --interval <seconds> (default: 60)
EOF
}
##############################
# Parse CLI arguments
##############################
BRANCH="main"
INTERVAL=60
WALLET_NAME=""
WALLET_HOTKEY=""
while [[ $# -gt 0 ]]; do
case "$1" in
-w|--wallet.name)
WALLET_NAME="$2"; shift 2;;
-k|--wallet.hotkey)
WALLET_HOTKEY="$2"; shift 2;;
-b|--branch)
BRANCH="$2"; shift 2;;
-i|--interval)
INTERVAL="$2"; shift 2;;
-h|--help)
print_usage; exit 0;;
*)
echo "Unknown parameter: $1" >&2
print_usage; exit 1;;
esac
done
##############################
# Load .env if present
##############################
if [[ -f .env ]]; then
# shellcheck disable=SC1091
set -a
source .env
set +a
fi
# Environment variables take precedence over .env, CLI overrides all.
: "${WALLET_NAME:=${WALLET_NAME:-${wallet_name:-}}}"
: "${WALLET_HOTKEY:=${WALLET_HOTKEY:-${wallet_hotkey:-}}}"
if [[ -z "$WALLET_NAME" || -z "$WALLET_HOTKEY" ]]; then
log "wallet.name and wallet.hotkey must be provided via CLI or .env"
exit 1
fi
# Ensure we are running from the repository root
SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
cd "$SCRIPT_DIR"
##############################
# Core logic
##############################
check_for_updates() {
local remote_commit local_commit
# Fetch remote changes but don't merge yet
git fetch origin "$BRANCH" &>/dev/null || { log "Failed to fetch remote repository"; return 1; }
remote_commit=$(git rev-parse "origin/$BRANCH")
local_commit=$(git rev-parse "HEAD")
if [[ "$remote_commit" != "$local_commit" ]]; then
log "New commit detected. Pulling changes..."
git pull --rebase origin "$BRANCH" || { log "git pull failed"; return 1; }
return 0 # Updates applied
else
return 1 # No updates
fi
}
restart_or_start_validator() {
if pm2 list | grep -q "validator"; then
log "Restarting existing pm2 process 'validator'..."
pm2 restart validator
else
log "Starting pm2 process 'validator'..."
pm2 start validator.py -- --wallet.name "$WALLET_NAME" --wallet.hotkey "$WALLET_HOTKEY"
fi
# Begin or refresh log streaming
stream_validator_logs
}
ensure_validator_running() {
if ! pm2 list | grep -q "validator"; then
restart_or_start_validator
else
# Validator already up: ensure logs streaming is active
if [[ -z "$LOG_PID" || ! -e /proc/$LOG_PID ]]; then
stream_validator_logs
fi
fi
}
# ────────────────────────────────────────────────────────────
# Log streaming
# ────────────────────────────────────────────────────────────
LOG_PID=""
stream_validator_logs() {
if [[ -n "$LOG_PID" && -e /proc/$LOG_PID ]]; then
kill "$LOG_PID" || true
fi
pm2 logs validator --lines 20 --raw &
LOG_PID=$!
}
# Clean up background log stream on exit
trap '[[ -n "$LOG_PID" && -e /proc/$LOG_PID ]] && kill "$LOG_PID"' EXIT
log "Starting autoupdate loop (branch=$BRANCH, interval=${INTERVAL}s)"
ensure_validator_running # Ensure validator is up on startup
while true; do
if check_for_updates; then
restart_or_start_validator
else
ensure_validator_running
fi
sleep "$INTERVAL"
done