Repository navigation
Expand file tree
/
Copy pathexitpoint.sh
More file actions
executable file
·155 lines (126 loc) · 4.35 KB
/
Copy pathexitpoint.sh
File metadata and controls
executable file
·155 lines (126 loc) · 4.35 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
MAX_RETRIES=10
RETRY_DELAY=100 # seconds
REMOVE_URL="https://github.com/$1"
RUNNER_URL="https://github.com/$2"
GH_API_URL="https://api.github.com/repos/$2/actions/runners"
# Function to check if runner is online using GitHub API
check_runner_online() {
local AUTH="Authorization: Bearer $GITHUB_ACCESS_TOKEN"
local VERSION="X-GitHub-Api-Version: 2022-11-28"
local ACCEPT="Accept: application/vnd.github+json"
echo "Checking runner status via GitHub API..."
RESPONSE=$(curl -s -L -H "$AUTH" -H "$VERSION" -H "$ACCEPT" "$GH_API_URL")
if [ $? -ne 0 ]; then
echo "Error: Failed to query GitHub API"
return 1
fi
STATUS=$(echo "$RESPONSE" | jq -r --arg NAME "$RUNNER_NAME" '.runners[] | select(.name == $NAME).status')
if [ $? -ne 0 ]; then
echo "Error: Failed to parse API response"
return 1
fi
if [ "$STATUS" == "online" ]; then
echo "Runner is online according to GitHub API"
return 0
else
echo "Runner status: ${STATUS:-unknown}"
return 1
fi
}
# Function to register the runner
register_runner() {
# Stop the runner through supervisor
echo "Stopping runner..."
supervisorctl stop runner || true
# Remove the runner through config.sh
echo "Getting a Remove Token for ${REMOVE_URL}"
_PROTO="$(echo "${REMOVE_URL}" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
_URL="$(echo "${REMOVE_URL/${_PROTO}/}")"
_PATH="$(echo "${_URL}" | grep / | cut -d/ -f2-)"
REMOVE_TOKEN="$(curl -XPOST -fsSL \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${_PATH}/actions/runners/remove-token" \
| jq -r '.token')"
if [ -z "$REMOVE_TOKEN" ]; then
echo "Failed to get remove token"
exit 1
fi
echo "Forcefully removing old runner configuration"
./config.sh remove --token "$REMOVE_TOKEN"
# Forcefully remove old configuration
if [ -f .runner ] || [ -d "$RUNNER_WORK_DIRECTORY" ]; then
rm -f .env
rm -f .runner
rm -f .credentials
rm -f .credentials_rsaparams
rm -rf _diag "$RUNNER_WORK_DIRECTORY"
fi
# Register with new token
echo "Getting a Runner Token for ${RUNNER_URL}"
_PROTO="$(echo "${RUNNER_URL}" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
_URL="$(echo "${RUNNER_URL/${_PROTO}/}")"
_PATH="$(echo "${_URL}" | grep / | cut -d/ -f2-)"
RUNNER_TOKEN="$(curl -XPOST -fsSL \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${_PATH}/actions/runners/registration-token" \
| jq -r '.token')"
if [ -z "$RUNNER_TOKEN" ]; then
echo "Failed to get registration token"
exit 1
fi
echo "Registering new runner..."
./config.sh \
--url "$RUNNER_URL" \
--token "$RUNNER_TOKEN" \
--name "$RUNNER_NAME" \
--work "$RUNNER_WORK_DIRECTORY" \
$CONFIG_OPTS \
--replace \
--unattended
# Restart the runner through supervisor
echo "Starting runner..."
supervisorctl start runner
}
if [[ -z $RUNNER_TOKEN && -z $GITHUB_ACCESS_TOKEN ]]; then
echo "Error : You need to set RUNNER_TOKEN (or GITHUB_ACCESS_TOKEN) environment variable."
exit 1
fi
if [[ -z $RUNNER_NAME ]]; then
echo "RUNNER_NAME environment variable is not set, using '${HOSTNAME}'."
export RUNNER_NAME=${HOSTNAME}
fi
if [[ -z $RUNNER_WORK_DIRECTORY ]]; then
echo "RUNNER_WORK_DIRECTORY environment variable is not set, using '_work'."
export RUNNER_WORK_DIRECTORY="_work"
fi
if [[ -z $RUNNER_REPLACE_EXISTING ]]; then
export RUNNER_REPLACE_EXISTING="true"
fi
CONFIG_OPTS=""
if [ "$(echo $RUNNER_REPLACE_EXISTING | tr '[:upper:]' '[:lower:]')" == "true" ]; then
export CONFIG_OPTS="--replace"
fi
if [[ -n $RUNNER_LABELS ]]; then
export CONFIG_OPTS="${CONFIG_OPTS} --labels ${RUNNER_LABELS}"
fi
# Change to runner directory
cd /home/runner || { echo "Failed to cd to /home/runner"; exit 1; }
# Main execution with retries
retry_count=0
while [ $retry_count -lt $MAX_RETRIES ]; do
echo "Attempt $((retry_count + 1)) of $MAX_RETRIES"
register_runner
echo "Waiting $RETRY_DELAY seconds for runner to initialize..."
sleep $RETRY_DELAY
if check_runner_online; then
echo "Runner successfully registered and online"
exit 0
else
echo "Runner failed to come online"
((retry_count++))
fi
done
echo "ERROR: Failed to bring runner online after $MAX_RETRIES attempts"