-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
137 lines (116 loc) · 2.93 KB
/
install.sh
File metadata and controls
137 lines (116 loc) · 2.93 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
#!/usr/bin/env bash
set -euo pipefail
# pg-node installer (Linux only)
# Downloads the latest GitHub release for the detected arch and installs to /usr/local/bin/<name>
REPO_OWNER="PasarGuard"
REPO_NAME="node-serviced"
APP_NAME="pg-node"
INSTALL_PATH="/usr/local/bin/${APP_NAME}"
API_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases/latest"
ARCH=""
OS="linux"
TMPDIR=""
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >&2
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--name)
if [[ -z "${2:-}" ]]; then
log "--name requires a value"; exit 1
fi
APP_NAME="$2"
INSTALL_PATH="/usr/local/bin/${APP_NAME}"
shift 2
;;
--name=*)
APP_NAME="${1#*=}"
if [[ -z "$APP_NAME" ]]; then
log "--name requires a value"; exit 1
fi
INSTALL_PATH="/usr/local/bin/${APP_NAME}"
shift
;;
*)
log "Unknown option: $1"
exit 1
;;
esac
done
}
cleanup() {
if [[ -n "${TMPDIR:-}" && -d "$TMPDIR" ]]; then
rm -rf "$TMPDIR"
fi
}
trap cleanup EXIT
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
armv7l|armv7) ARCH="armv7" ;;
armv6l|armv6) ARCH="armv6" ;;
*) log "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
}
require_linux() {
if [[ "$(uname -s)" != "Linux" ]]; then
log "This installer supports Linux only."
exit 1
}
}
require_tools() {
for bin in curl tar; do
if ! command -v "$bin" >/dev/null 2>&1; then
log "Missing required tool: $bin"
exit 1
fi
done
}
fetch_latest_url() {
local asset_name="${APP_NAME}_*_Linux_${ARCH}.tar.gz"
local url
url=$(curl -sSf "$API_URL" | grep -oE "\"browser_download_url\": \"[^\"]*${asset_name}\"" | head -n1 | cut -d\" -f4)
if [[ -z "$url" ]]; then
log "Could not find a release asset for arch=${ARCH} and name=${APP_NAME}"
exit 1
fi
echo "$url"
}
download_and_install() {
TMPDIR=$(mktemp -d)
local url="$1"
local archive="$TMPDIR/${APP_NAME}.tar.gz"
log "Downloading: $url"
curl -fL "$url" -o "$archive"
log "Extracting archive"
tar -xzf "$archive" -C "$TMPDIR"
local bin_path="$TMPDIR/${APP_NAME}"
if [[ ! -f "$bin_path" ]]; then
# Fallback to legacy binary name if archive name differs
if [[ -f "$TMPDIR/node-serviced" ]]; then
bin_path="$TMPDIR/node-serviced"
else
log "Binary ${APP_NAME} not found in archive"
exit 1
fi
fi
log "Installing to $INSTALL_PATH"
install -m 0755 "$bin_path" "$INSTALL_PATH"
}
main() {
parse_args "$@"
require_linux
require_tools
detect_arch
if [[ "$APP_NAME" != "pg-node" ]]; then
log "Using custom name: $APP_NAME (asset must exist with this prefix)"
fi
log "Detected linux/${ARCH}"
local url
url=$(fetch_latest_url)
download_and_install "$url"
log "Install complete: $INSTALL_PATH"
}
main "$@"