vps-agentctl.sh
· 9.7 KiB · Bash
Brut
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
readonly PROGRAM="vps-agent"
readonly BINARY="/usr/local/bin/vps-agent"
readonly PREVIOUS="/usr/local/lib/vps-agent/vps-agent.previous"
readonly CONFIG_DIR="/etc/vps-agent"
readonly CREDENTIAL_DIR="${CONFIG_DIR}/credentials"
readonly STATE_DIR="/var/lib/vps-agent"
readonly UNIT="/etc/systemd/system/vps-agent.service"
readonly DEFAULT_RELEASES="https://gitea.hyperdog.dev/dizyaka/vps-control/releases"
VERSION="latest"
RELEASES_URL="${VPS_AGENT_RELEASES_URL:-$DEFAULT_RELEASES}"
PURGE=false
ASSUME_YES=false
TMP_DIR=""
DOWNLOADED_BINARY=""
usage() {
printf '%s\n' "Usage: $0 <install|update|remove|rollback|status|restart|version> [options]" \
" --version VERSION release tag, or latest" \
" --releases-url URL releases root (default: VPS_AGENT_RELEASES_URL)" \
" --purge remove configuration and credentials" \
" --yes confirm destructive purge"
}
die() { printf 'error: %s\n' "$*" >&2; exit 1; }
info() { printf '==> %s\n' "$*"; }
cleanup() {
if [[ -n "$TMP_DIR" && -d "$TMP_DIR" ]]; then
rm -rf -- "$TMP_DIR"
fi
}
trap cleanup EXIT
require_root() { [[ ${EUID:-$(id -u)} -eq 0 ]] || die "run this action as root"; }
check_host() {
[[ "$(uname -s)" == "Linux" ]] || die "only Linux is supported"
command -v systemctl >/dev/null || die "systemd is required"
[[ -d /run/systemd/system ]] || die "systemd is not running"
}
release_arch() {
case "$(uname -m)" in
x86_64|amd64) printf 'amd64\n' ;;
aarch64|arm64) printf 'arm64\n' ;;
*) die "unsupported architecture: $(uname -m)" ;;
esac
}
download_release() {
command -v curl >/dev/null || die "curl is required"
command -v sha256sum >/dev/null || die "sha256sum is required"
TMP_DIR="$(mktemp -d -t vps-agent.XXXXXXXX)"
chmod 700 "$TMP_DIR"
local artifact="vps-agent-linux-$(release_arch)"
local release_url
if [[ "$VERSION" == "latest" ]]; then
release_url="${RELEASES_URL%/}/latest/download"
else
[[ "$VERSION" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.-]+)?$ ]] || die "invalid release version"
release_url="${RELEASES_URL%/}/download/${VERSION}"
fi
info "downloading ${artifact} (${VERSION})"
curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \
--output "$TMP_DIR/$artifact" "$release_url/$artifact"
curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \
--output "$TMP_DIR/SHA256SUMS" "$release_url/SHA256SUMS"
local expected actual
expected="$(awk -v name="$artifact" '$2 == name || $2 == "*" name { print $1; exit }' "$TMP_DIR/SHA256SUMS")"
[[ "$expected" =~ ^[a-fA-F0-9]{64}$ ]] || die "artifact checksum is absent or malformed"
actual="$(sha256sum "$TMP_DIR/$artifact" | awk '{print $1}')"
[[ "$actual" == "$expected" ]] || die "checksum verification failed"
if [[ -n "${VPS_AGENT_MINISIGN_PUBLIC_KEY:-}" ]]; then
command -v minisign >/dev/null || die "minisign is required when a public key is configured"
curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \
--output "$TMP_DIR/SHA256SUMS.minisig" "$release_url/SHA256SUMS.minisig"
minisign -Vm "$TMP_DIR/SHA256SUMS" -P "$VPS_AGENT_MINISIGN_PUBLIC_KEY"
fi
chmod 0755 "$TMP_DIR/$artifact"
"$TMP_DIR/$artifact" --version >/dev/null || die "downloaded binary self-check failed"
DOWNLOADED_BINARY="$TMP_DIR/$artifact"
}
install_unit() {
if [[ -f "$(cd "$(dirname "$0")" && pwd)/../agent/packaging/vps-agent.service" ]]; then
install -o root -g root -m 0644 \
"$(cd "$(dirname "$0")" && pwd)/../agent/packaging/vps-agent.service" "$UNIT"
return
fi
install -o root -g root -m 0644 /dev/null "$UNIT"
tee "$UNIT" >/dev/null <<'EOF'
[Unit]
Description=VPS Control monitoring agent
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=vps-agent
Group=vps-agent
ExecStart=/usr/local/bin/vps-agent --config /etc/vps-agent/config.toml
Restart=on-failure
RestartSec=5s
TimeoutStopSec=15s
MemoryHigh=48M
MemoryMax=64M
CPUQuota=10%
TasksMax=64
NoNewPrivileges=true
PrivateTmp=true
PrivateDevices=true
ProtectHome=true
ProtectSystem=strict
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectKernelLogs=true
ProtectControlGroups=true
ProtectClock=true
ProtectHostname=true
RestrictSUIDSGID=true
LockPersonality=true
RestrictRealtime=true
RestrictNamespaces=true
CapabilityBoundingSet=
AmbientCapabilities=
SystemCallArchitectures=native
UMask=0077
ProcSubset=all
ProtectProc=invisible
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK
[Install]
WantedBy=multi-user.target
EOF
}
create_config() {
[[ -f "$CONFIG_DIR/config.toml" ]] && return
install -o root -g vps-agent -m 0640 /dev/null "$CONFIG_DIR/config.toml"
tee "$CONFIG_DIR/config.toml" >/dev/null <<'EOF'
[server]
listen = "127.0.0.1:9105"
[auth]
mode = "token"
token_file = "/etc/vps-agent/credentials/control-plane.token"
[limits]
max_concurrent_requests = 32
request_timeout_seconds = 5
max_body_bytes = 65536
read_requests_per_second = 10.0
write_requests_per_second = 2.0
burst = 20.0
command_timeout_seconds = 4
[services]
allowed = []
[firewall]
management_enabled = false
rollback_timeout_seconds = 60
max_snapshots = 5
[logging]
level = "info"
[telemetry]
connections_enabled = true
process_info_enabled = true
[logs]
enabled = false
allowed_units = ["vps-agent.service"]
max_entries_per_request = 200
max_message_bytes = 16384
[tls]
enabled = false
certificate_file = ""
private_key_file = ""
EOF
}
create_token() {
local token_file="$CREDENTIAL_DIR/control-plane.token"
[[ -f "$token_file" ]] && return
if command -v openssl >/dev/null; then
umask 077
openssl rand -hex 32 > "$token_file"
else
umask 077
od -An -N32 -tx1 /dev/urandom | tr -d ' \n' > "$token_file"
printf '\n' >> "$token_file"
fi
chown vps-agent:vps-agent "$token_file"
chmod 0600 "$token_file"
info "created agent token at $token_file; copy it through a secure channel"
}
wait_healthy() {
local attempts=15
while (( attempts > 0 )); do
if systemctl is-active --quiet vps-agent.service; then
return 0
fi
sleep 1
attempts=$((attempts - 1))
done
systemctl status --no-pager vps-agent.service || true
return 1
}
do_install() {
require_root; check_host
[[ ! -e "$BINARY" ]] || die "$BINARY already exists; use update"
local downloaded
download_release
downloaded="$DOWNLOADED_BINARY"
getent group vps-agent >/dev/null || groupadd --system vps-agent
id -u vps-agent >/dev/null 2>&1 || useradd --system --gid vps-agent --home-dir /nonexistent --shell /usr/sbin/nologin vps-agent
install -d -o root -g vps-agent -m 0750 "$CONFIG_DIR" "$CREDENTIAL_DIR"
install -d -o vps-agent -g vps-agent -m 0700 "$STATE_DIR"
install -d -o root -g root -m 0755 "$(dirname "$PREVIOUS")"
install -o root -g root -m 0755 "$downloaded" "$BINARY"
create_config
create_token
install_unit
systemctl daemon-reload
systemctl enable --now vps-agent.service
wait_healthy || die "agent failed to become healthy"
systemctl status --no-pager vps-agent.service
}
do_update() {
require_root; check_host
[[ -x "$BINARY" ]] || die "agent is not installed"
local downloaded staged
download_release
downloaded="$DOWNLOADED_BINARY"
staged="$(dirname "$BINARY")/.vps-agent.new"
install -o root -g root -m 0755 "$downloaded" "$staged"
install -o root -g root -m 0755 "$BINARY" "$PREVIOUS"
mv -f -- "$staged" "$BINARY"
systemctl restart vps-agent.service
if ! wait_healthy; then
info "health check failed; restoring previous binary"
install -o root -g root -m 0755 "$PREVIOUS" "$BINARY"
systemctl restart vps-agent.service
wait_healthy || die "rollback also failed; inspect systemctl status"
die "update rolled back"
fi
info "updated to $($BINARY --version)"
}
do_rollback() {
require_root; check_host
[[ -x "$PREVIOUS" ]] || die "no previous binary is available"
local current="$(dirname "$BINARY")/.vps-agent.current"
install -o root -g root -m 0755 "$BINARY" "$current"
install -o root -g root -m 0755 "$PREVIOUS" "$BINARY"
mv -f -- "$current" "$PREVIOUS"
systemctl restart vps-agent.service
wait_healthy || die "rolled-back version did not become healthy"
}
do_remove() {
require_root; check_host
if [[ "$PURGE" == true && "$ASSUME_YES" != true ]]; then
[[ -t 0 ]] || die "--purge requires an interactive terminal or --yes"
read -r -p "Permanently delete $CONFIG_DIR and credentials? [y/N] " answer
[[ "$answer" == "y" || "$answer" == "Y" ]] || die "purge cancelled"
fi
systemctl disable --now vps-agent.service 2>/dev/null || true
rm -f -- "$UNIT" "$BINARY" "$PREVIOUS"
rm -rf -- "$STATE_DIR"
systemctl daemon-reload
if [[ "$PURGE" == true ]]; then
rm -rf -- "$CONFIG_DIR"
info "removed configuration and credentials; this cannot be recovered"
else
info "preserved $CONFIG_DIR (use remove --purge to delete it)"
fi
userdel vps-agent 2>/dev/null || true
groupdel vps-agent 2>/dev/null || true
}
[[ $# -ge 1 ]] || { usage; exit 2; }
ACTION="$1"; shift
while [[ $# -gt 0 ]]; do
case "$1" in
--version) [[ $# -ge 2 ]] || die "--version needs a value"; VERSION="$2"; shift 2 ;;
--releases-url) [[ $# -ge 2 ]] || die "--releases-url needs a value"; RELEASES_URL="$2"; shift 2 ;;
--purge) PURGE=true; shift ;;
--yes) ASSUME_YES=true; shift ;;
-h|--help) usage; exit 0 ;;
*) die "unknown option: $1" ;;
esac
done
case "$ACTION" in
install) do_install ;;
update) do_update ;;
remove) do_remove ;;
rollback) do_rollback ;;
status) systemctl status --no-pager vps-agent.service ;;
restart) require_root; systemctl restart vps-agent.service; wait_healthy ;;
version) [[ -x "$BINARY" ]] && "$BINARY" --version || die "agent is not installed" ;;
*) usage; exit 2 ;;
esac
| 1 | #!/usr/bin/env bash |
| 2 | set -Eeuo pipefail |
| 3 | IFS=$'\n\t' |
| 4 | |
| 5 | readonly PROGRAM="vps-agent" |
| 6 | readonly BINARY="/usr/local/bin/vps-agent" |
| 7 | readonly PREVIOUS="/usr/local/lib/vps-agent/vps-agent.previous" |
| 8 | readonly CONFIG_DIR="/etc/vps-agent" |
| 9 | readonly CREDENTIAL_DIR="${CONFIG_DIR}/credentials" |
| 10 | readonly STATE_DIR="/var/lib/vps-agent" |
| 11 | readonly UNIT="/etc/systemd/system/vps-agent.service" |
| 12 | readonly DEFAULT_RELEASES="https://gitea.hyperdog.dev/dizyaka/vps-control/releases" |
| 13 | |
| 14 | VERSION="latest" |
| 15 | RELEASES_URL="${VPS_AGENT_RELEASES_URL:-$DEFAULT_RELEASES}" |
| 16 | PURGE=false |
| 17 | ASSUME_YES=false |
| 18 | TMP_DIR="" |
| 19 | DOWNLOADED_BINARY="" |
| 20 | |
| 21 | usage() { |
| 22 | printf '%s\n' "Usage: $0 <install|update|remove|rollback|status|restart|version> [options]" \ |
| 23 | " --version VERSION release tag, or latest" \ |
| 24 | " --releases-url URL releases root (default: VPS_AGENT_RELEASES_URL)" \ |
| 25 | " --purge remove configuration and credentials" \ |
| 26 | " --yes confirm destructive purge" |
| 27 | } |
| 28 | |
| 29 | die() { printf 'error: %s\n' "$*" >&2; exit 1; } |
| 30 | info() { printf '==> %s\n' "$*"; } |
| 31 | |
| 32 | cleanup() { |
| 33 | if [[ -n "$TMP_DIR" && -d "$TMP_DIR" ]]; then |
| 34 | rm -rf -- "$TMP_DIR" |
| 35 | fi |
| 36 | } |
| 37 | trap cleanup EXIT |
| 38 | |
| 39 | require_root() { [[ ${EUID:-$(id -u)} -eq 0 ]] || die "run this action as root"; } |
| 40 | |
| 41 | check_host() { |
| 42 | [[ "$(uname -s)" == "Linux" ]] || die "only Linux is supported" |
| 43 | command -v systemctl >/dev/null || die "systemd is required" |
| 44 | [[ -d /run/systemd/system ]] || die "systemd is not running" |
| 45 | } |
| 46 | |
| 47 | release_arch() { |
| 48 | case "$(uname -m)" in |
| 49 | x86_64|amd64) printf 'amd64\n' ;; |
| 50 | aarch64|arm64) printf 'arm64\n' ;; |
| 51 | *) die "unsupported architecture: $(uname -m)" ;; |
| 52 | esac |
| 53 | } |
| 54 | |
| 55 | download_release() { |
| 56 | command -v curl >/dev/null || die "curl is required" |
| 57 | command -v sha256sum >/dev/null || die "sha256sum is required" |
| 58 | TMP_DIR="$(mktemp -d -t vps-agent.XXXXXXXX)" |
| 59 | chmod 700 "$TMP_DIR" |
| 60 | local artifact="vps-agent-linux-$(release_arch)" |
| 61 | local release_url |
| 62 | if [[ "$VERSION" == "latest" ]]; then |
| 63 | release_url="${RELEASES_URL%/}/latest/download" |
| 64 | else |
| 65 | [[ "$VERSION" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.-]+)?$ ]] || die "invalid release version" |
| 66 | release_url="${RELEASES_URL%/}/download/${VERSION}" |
| 67 | fi |
| 68 | info "downloading ${artifact} (${VERSION})" |
| 69 | curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \ |
| 70 | --output "$TMP_DIR/$artifact" "$release_url/$artifact" |
| 71 | curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \ |
| 72 | --output "$TMP_DIR/SHA256SUMS" "$release_url/SHA256SUMS" |
| 73 | |
| 74 | local expected actual |
| 75 | expected="$(awk -v name="$artifact" '$2 == name || $2 == "*" name { print $1; exit }' "$TMP_DIR/SHA256SUMS")" |
| 76 | [[ "$expected" =~ ^[a-fA-F0-9]{64}$ ]] || die "artifact checksum is absent or malformed" |
| 77 | actual="$(sha256sum "$TMP_DIR/$artifact" | awk '{print $1}')" |
| 78 | [[ "$actual" == "$expected" ]] || die "checksum verification failed" |
| 79 | |
| 80 | if [[ -n "${VPS_AGENT_MINISIGN_PUBLIC_KEY:-}" ]]; then |
| 81 | command -v minisign >/dev/null || die "minisign is required when a public key is configured" |
| 82 | curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 \ |
| 83 | --output "$TMP_DIR/SHA256SUMS.minisig" "$release_url/SHA256SUMS.minisig" |
| 84 | minisign -Vm "$TMP_DIR/SHA256SUMS" -P "$VPS_AGENT_MINISIGN_PUBLIC_KEY" |
| 85 | fi |
| 86 | chmod 0755 "$TMP_DIR/$artifact" |
| 87 | "$TMP_DIR/$artifact" --version >/dev/null || die "downloaded binary self-check failed" |
| 88 | DOWNLOADED_BINARY="$TMP_DIR/$artifact" |
| 89 | } |
| 90 | |
| 91 | install_unit() { |
| 92 | if [[ -f "$(cd "$(dirname "$0")" && pwd)/../agent/packaging/vps-agent.service" ]]; then |
| 93 | install -o root -g root -m 0644 \ |
| 94 | "$(cd "$(dirname "$0")" && pwd)/../agent/packaging/vps-agent.service" "$UNIT" |
| 95 | return |
| 96 | fi |
| 97 | install -o root -g root -m 0644 /dev/null "$UNIT" |
| 98 | tee "$UNIT" >/dev/null <<'EOF' |
| 99 | [Unit] |
| 100 | Description=VPS Control monitoring agent |
| 101 | After=network-online.target |
| 102 | Wants=network-online.target |
| 103 | [Service] |
| 104 | Type=simple |
| 105 | User=vps-agent |
| 106 | Group=vps-agent |
| 107 | ExecStart=/usr/local/bin/vps-agent --config /etc/vps-agent/config.toml |
| 108 | Restart=on-failure |
| 109 | RestartSec=5s |
| 110 | TimeoutStopSec=15s |
| 111 | MemoryHigh=48M |
| 112 | MemoryMax=64M |
| 113 | CPUQuota=10% |
| 114 | TasksMax=64 |
| 115 | NoNewPrivileges=true |
| 116 | PrivateTmp=true |
| 117 | PrivateDevices=true |
| 118 | ProtectHome=true |
| 119 | ProtectSystem=strict |
| 120 | ProtectKernelTunables=true |
| 121 | ProtectKernelModules=true |
| 122 | ProtectKernelLogs=true |
| 123 | ProtectControlGroups=true |
| 124 | ProtectClock=true |
| 125 | ProtectHostname=true |
| 126 | RestrictSUIDSGID=true |
| 127 | LockPersonality=true |
| 128 | RestrictRealtime=true |
| 129 | RestrictNamespaces=true |
| 130 | CapabilityBoundingSet= |
| 131 | AmbientCapabilities= |
| 132 | SystemCallArchitectures=native |
| 133 | UMask=0077 |
| 134 | ProcSubset=all |
| 135 | ProtectProc=invisible |
| 136 | RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK |
| 137 | [Install] |
| 138 | WantedBy=multi-user.target |
| 139 | EOF |
| 140 | } |
| 141 | |
| 142 | create_config() { |
| 143 | [[ -f "$CONFIG_DIR/config.toml" ]] && return |
| 144 | install -o root -g vps-agent -m 0640 /dev/null "$CONFIG_DIR/config.toml" |
| 145 | tee "$CONFIG_DIR/config.toml" >/dev/null <<'EOF' |
| 146 | [server] |
| 147 | listen = "127.0.0.1:9105" |
| 148 | [auth] |
| 149 | mode = "token" |
| 150 | token_file = "/etc/vps-agent/credentials/control-plane.token" |
| 151 | [limits] |
| 152 | max_concurrent_requests = 32 |
| 153 | request_timeout_seconds = 5 |
| 154 | max_body_bytes = 65536 |
| 155 | read_requests_per_second = 10.0 |
| 156 | write_requests_per_second = 2.0 |
| 157 | burst = 20.0 |
| 158 | command_timeout_seconds = 4 |
| 159 | [services] |
| 160 | allowed = [] |
| 161 | [firewall] |
| 162 | management_enabled = false |
| 163 | rollback_timeout_seconds = 60 |
| 164 | max_snapshots = 5 |
| 165 | [logging] |
| 166 | level = "info" |
| 167 | [telemetry] |
| 168 | connections_enabled = true |
| 169 | process_info_enabled = true |
| 170 | [logs] |
| 171 | enabled = false |
| 172 | allowed_units = ["vps-agent.service"] |
| 173 | max_entries_per_request = 200 |
| 174 | max_message_bytes = 16384 |
| 175 | [tls] |
| 176 | enabled = false |
| 177 | certificate_file = "" |
| 178 | private_key_file = "" |
| 179 | EOF |
| 180 | } |
| 181 | |
| 182 | create_token() { |
| 183 | local token_file="$CREDENTIAL_DIR/control-plane.token" |
| 184 | [[ -f "$token_file" ]] && return |
| 185 | if command -v openssl >/dev/null; then |
| 186 | umask 077 |
| 187 | openssl rand -hex 32 > "$token_file" |
| 188 | else |
| 189 | umask 077 |
| 190 | od -An -N32 -tx1 /dev/urandom | tr -d ' \n' > "$token_file" |
| 191 | printf '\n' >> "$token_file" |
| 192 | fi |
| 193 | chown vps-agent:vps-agent "$token_file" |
| 194 | chmod 0600 "$token_file" |
| 195 | info "created agent token at $token_file; copy it through a secure channel" |
| 196 | } |
| 197 | |
| 198 | wait_healthy() { |
| 199 | local attempts=15 |
| 200 | while (( attempts > 0 )); do |
| 201 | if systemctl is-active --quiet vps-agent.service; then |
| 202 | return 0 |
| 203 | fi |
| 204 | sleep 1 |
| 205 | attempts=$((attempts - 1)) |
| 206 | done |
| 207 | systemctl status --no-pager vps-agent.service || true |
| 208 | return 1 |
| 209 | } |
| 210 | |
| 211 | do_install() { |
| 212 | require_root; check_host |
| 213 | [[ ! -e "$BINARY" ]] || die "$BINARY already exists; use update" |
| 214 | local downloaded |
| 215 | download_release |
| 216 | downloaded="$DOWNLOADED_BINARY" |
| 217 | getent group vps-agent >/dev/null || groupadd --system vps-agent |
| 218 | id -u vps-agent >/dev/null 2>&1 || useradd --system --gid vps-agent --home-dir /nonexistent --shell /usr/sbin/nologin vps-agent |
| 219 | install -d -o root -g vps-agent -m 0750 "$CONFIG_DIR" "$CREDENTIAL_DIR" |
| 220 | install -d -o vps-agent -g vps-agent -m 0700 "$STATE_DIR" |
| 221 | install -d -o root -g root -m 0755 "$(dirname "$PREVIOUS")" |
| 222 | install -o root -g root -m 0755 "$downloaded" "$BINARY" |
| 223 | create_config |
| 224 | create_token |
| 225 | install_unit |
| 226 | systemctl daemon-reload |
| 227 | systemctl enable --now vps-agent.service |
| 228 | wait_healthy || die "agent failed to become healthy" |
| 229 | systemctl status --no-pager vps-agent.service |
| 230 | } |
| 231 | |
| 232 | do_update() { |
| 233 | require_root; check_host |
| 234 | [[ -x "$BINARY" ]] || die "agent is not installed" |
| 235 | local downloaded staged |
| 236 | download_release |
| 237 | downloaded="$DOWNLOADED_BINARY" |
| 238 | staged="$(dirname "$BINARY")/.vps-agent.new" |
| 239 | install -o root -g root -m 0755 "$downloaded" "$staged" |
| 240 | install -o root -g root -m 0755 "$BINARY" "$PREVIOUS" |
| 241 | mv -f -- "$staged" "$BINARY" |
| 242 | systemctl restart vps-agent.service |
| 243 | if ! wait_healthy; then |
| 244 | info "health check failed; restoring previous binary" |
| 245 | install -o root -g root -m 0755 "$PREVIOUS" "$BINARY" |
| 246 | systemctl restart vps-agent.service |
| 247 | wait_healthy || die "rollback also failed; inspect systemctl status" |
| 248 | die "update rolled back" |
| 249 | fi |
| 250 | info "updated to $($BINARY --version)" |
| 251 | } |
| 252 | |
| 253 | do_rollback() { |
| 254 | require_root; check_host |
| 255 | [[ -x "$PREVIOUS" ]] || die "no previous binary is available" |
| 256 | local current="$(dirname "$BINARY")/.vps-agent.current" |
| 257 | install -o root -g root -m 0755 "$BINARY" "$current" |
| 258 | install -o root -g root -m 0755 "$PREVIOUS" "$BINARY" |
| 259 | mv -f -- "$current" "$PREVIOUS" |
| 260 | systemctl restart vps-agent.service |
| 261 | wait_healthy || die "rolled-back version did not become healthy" |
| 262 | } |
| 263 | |
| 264 | do_remove() { |
| 265 | require_root; check_host |
| 266 | if [[ "$PURGE" == true && "$ASSUME_YES" != true ]]; then |
| 267 | [[ -t 0 ]] || die "--purge requires an interactive terminal or --yes" |
| 268 | read -r -p "Permanently delete $CONFIG_DIR and credentials? [y/N] " answer |
| 269 | [[ "$answer" == "y" || "$answer" == "Y" ]] || die "purge cancelled" |
| 270 | fi |
| 271 | systemctl disable --now vps-agent.service 2>/dev/null || true |
| 272 | rm -f -- "$UNIT" "$BINARY" "$PREVIOUS" |
| 273 | rm -rf -- "$STATE_DIR" |
| 274 | systemctl daemon-reload |
| 275 | if [[ "$PURGE" == true ]]; then |
| 276 | rm -rf -- "$CONFIG_DIR" |
| 277 | info "removed configuration and credentials; this cannot be recovered" |
| 278 | else |
| 279 | info "preserved $CONFIG_DIR (use remove --purge to delete it)" |
| 280 | fi |
| 281 | userdel vps-agent 2>/dev/null || true |
| 282 | groupdel vps-agent 2>/dev/null || true |
| 283 | } |
| 284 | |
| 285 | [[ $# -ge 1 ]] || { usage; exit 2; } |
| 286 | ACTION="$1"; shift |
| 287 | while [[ $# -gt 0 ]]; do |
| 288 | case "$1" in |
| 289 | --version) [[ $# -ge 2 ]] || die "--version needs a value"; VERSION="$2"; shift 2 ;; |
| 290 | --releases-url) [[ $# -ge 2 ]] || die "--releases-url needs a value"; RELEASES_URL="$2"; shift 2 ;; |
| 291 | --purge) PURGE=true; shift ;; |
| 292 | --yes) ASSUME_YES=true; shift ;; |
| 293 | -h|--help) usage; exit 0 ;; |
| 294 | *) die "unknown option: $1" ;; |
| 295 | esac |
| 296 | done |
| 297 | |
| 298 | case "$ACTION" in |
| 299 | install) do_install ;; |
| 300 | update) do_update ;; |
| 301 | remove) do_remove ;; |
| 302 | rollback) do_rollback ;; |
| 303 | status) systemctl status --no-pager vps-agent.service ;; |
| 304 | restart) require_root; systemctl restart vps-agent.service; wait_healthy ;; |
| 305 | version) [[ -x "$BINARY" ]] && "$BINARY" --version || die "agent is not installed" ;; |
| 306 | *) usage; exit 2 ;; |
| 307 | esac |
| 308 |