forked from cloudflare/cloudflared
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate-cloudflared.sh
More file actions
112 lines (97 loc) · 2.54 KB
/
update-cloudflared.sh
File metadata and controls
112 lines (97 loc) · 2.54 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 <cloudflared executable location>"
exit 1
}
stop_service() {
echo "→ Stopping cloudflared service…"
if command -v systemctl &>/dev/null; then
systemctl stop cloudflared || true
elif command -v service &>/dev/null; then
service cloudflared stop || true
elif command -v rcctl &>/dev/null; then
rcctl stop cloudflared || true
elif [ -x "/etc/rc.d/cloudflared" ]; then
/etc/rc.d/cloudflared stop || true
else
echo "Warning: couldn't stop cloudflared (no known service manager)" >&2
fi
}
start_service() {
echo "→ Starting cloudflared service…"
if command -v systemctl &>/dev/null; then
systemctl start cloudflared || true
elif command -v service &>/dev/null; then
service cloudflared start || true
elif command -v rcctl &>/dev/null; then
rcctl start cloudflared || true
elif [ -x "/etc/rc.d/cloudflared" ]; then
/etc/rc.d/cloudflared start || true
else
echo "Warning: couldn't start cloudflared (no known service manager)" >&2
fi
}
if [[ $# -ne 1 || $1 == -h || $1 == --help ]]; then
usage
fi
DEST="$1"
# Temporary file cleanup
TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT
# Detect OS and major version
OS="$(uname -s)"
case "$OS" in
FreeBSD)
RAW_VER="$(uname -r)"
MAJOR="${RAW_VER%%.*}"
TARGET_OS="freebsd${MAJOR}"
;;
NetBSD)
RAW_VER="$(uname -r)"
MAJOR="${RAW_VER%%.*}"
TARGET_OS="netbsd${MAJOR}"
;;
OpenBSD)
RAW_VER="$(uname -r)"
MAJOR="${RAW_VER%%.*}"
TARGET_OS="openbsd${MAJOR}"
;;
*)
echo "Error: Unsupported OS: $OS" >&2
exit 1
;;
esac
# Detect architecture
ARCH_RAW="$(uname -m)"
case "$ARCH_RAW" in
x86_64|amd64) TARGET_ARCH="amd64" ;;
aarch64|arm64) TARGET_ARCH="arm64" ;;
*)
echo "Error: Unsupported architecture: $ARCH_RAW" >&2
exit 1
;;
esac
FILENAME="cloudflared-${TARGET_OS}-${TARGET_ARCH}"
echo "→ Detected target: $FILENAME"
# Pull the download URL from GitHub API
API_URL="https://api.github.com/repos/kjake/cloudflared/releases/latest"
DOWNLOAD_URL="$(curl -sSL "$API_URL" \
| grep '"browser_download_url"' \
| grep "$FILENAME" \
| head -n1 \
| cut -d '"' -f4)"
if [ -z "$DOWNLOAD_URL" ]; then
echo "Error: could not find download for $FILENAME" >&2
exit 1
fi
echo "→ Downloading $DOWNLOAD_URL …"
curl -sSL "$DOWNLOAD_URL" -o "$TMP"
chmod +x "$TMP"
stop_service
echo "→ Installing new binary to $DEST"
mv -f "$TMP" "$DEST"
chmod +x "$DEST"
start_service
echo "Success: cloudflared has been updated to the latest release."
exit 0