forked from zerotier/github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·72 lines (63 loc) · 2.18 KB
/
main.sh
File metadata and controls
executable file
·72 lines (63 loc) · 2.18 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
set -euo pipefail
IFS=$'\n\t'
echo "⏁ Installing ZeroTier"
# /home/runner/work/zerotier-github-action/zerotier-github-action/./
echo $GITHUB_ACTION_PATH
DAEMON_TIMEOUT=10
if [ -z "$(which zerotier-cli)" ]; then
case $(uname -s) in
MINGW64_NT?*)
pwsh "$GITHUB_ACTION_PATH/util/install.ps1"
ztcli="/c/Program Files (x86)/ZeroTier/One/zerotier-cli.bat"
member_id=$("${ztcli}" info | awk '{ print $3 }')
;;
*)
. $GITHUB_ACTION_PATH/util/install.sh &>/dev/null
member_id=$(sudo zerotier-cli info | awk '{ print $3 }')
;;
esac
fi
if [ -z "$(pgrep -f zerotier-one)" ]; then
echo "ZeroTier is not running. Starting it..."
sudo zerotier-one &
start_time=$(date +%s)
while [ "$(zerotier-cli info | awk '{ print $5 }')" != "ONLINE" ]; do
current_time=$(date +%s)
if [ $((current_time - start_time)) -ge $DAEMON_TIMEOUT ]; then
echo "ZeroTier failed to come online after $DAEMON_TIMEOUT seconds"
exit 1
fi
sleep 0.5
done
echo "ZeroTier started."
fi
ztcli="zerotier-cli"
member_id=$("${ztcli}" info | awk '{ print $3 }')
echo "⏁ Authorizing Runner to ZeroTier network"
MAX_RETRIES=10
RETRY_COUNT=0
while ! curl -s -X POST \
-H "Authorization: token $AUTH_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"Zerotier GitHub Member '"${GITHUB_SHA::7}"'", "description": "Member created by '"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"'", "config":{"authorized":true}}' \
"$API_URL/network/$NETWORK_ID/member/${member_id}" | grep '"authorized":true'; do
RETRY_COUNT=$((RETRY_COUNT + 1))
if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
echo "Reached maximum number of retries ($MAX_RETRIES). Exiting..."
exit 1
fi
echo "Authorization failed. Retrying in 2 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)"
sleep 2
done
echo "Member authorized successfully."
echo "⏁ Joining ZeroTier Network ID: $NETWORK_ID"
case $(uname -s) in
MINGW64_NT?*)
"${ztcli}" join $NETWORK_ID
while ! "${ztcli}" listnetworks | grep $NETWORK_ID | grep OK; do sleep 0.5; done
;;
*)
sudo zerotier-cli join $NETWORK_ID
while ! sudo zerotier-cli listnetworks | grep $NETWORK_ID | grep OK; do sleep 0.5; done
;;
esac