aboutsummaryrefslogtreecommitdiffstats
path: root/modules/scripts/warp-start.sh
blob: a1f3c76bac31a35d73b1610c6a9c6ebf51b473bb (plain)
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
set -euo pipefail

LOGFILE="/var/log/warp-svc.log"
DAEMON_TIMEOUT=30  # seconds

start_daemon() {
	if ! pgrep -x warp-svc >/dev/null 2>&1; then
		echo "Starting warp-svc daemon..."
		sudo nohup warp-svc >>"$LOGFILE" 2>&1 &
	else
		echo "warp-svc already running."
	fi
}

wait_for_daemon() {
	echo "Waiting for warp-svc to become ready..."
	for ((i=1; i<=DAEMON_TIMEOUT; i++)); do
		if warp-cli status >/dev/null 2>&1; then
			echo "warp-svc is ready."
			return 0
		fi
		sleep 1
	done
	
	echo "ERROR: warp-svc did not become ready within ${DAEMON_TIMEOUT}s" >&2
	exit 1
}

is_registered() {
	if warp-cli status 2>&1 | grep -qi "Registration missing"; then
		return 1
	fi
	return 0
}

register_client() {
	echo "Registering client..."
	sudo warp-cli registration new
}

connect_client() {
	echo "Connecting to WARP..."
	sudo warp-cli connect
}

main() {
	[[ "${EUID:-$(id -u)}" -eq 0 ]] || {
		if [[ -t 2 ]]; then
			echo -e "\e[31mERROR:\e[0m Must be run as root" >&2
		else
			echo "ERROR: Must be run as root" >&2
		fi
		exit 1
	}
	
	start_daemon
	wait_for_daemon
	
	if ! is_registered; then
		register_client
	else
		echo "Client already registered."
	fi
	
	connect_client
	
	echo "Current status:"
	warp-cli status
}

main