-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgcrun.sh
More file actions
executable file
·168 lines (143 loc) · 5.04 KB
/
sgcrun.sh
File metadata and controls
executable file
·168 lines (143 loc) · 5.04 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
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
NO_COLOR="\033[0m"
COLOR_RED="\033[1;31m"
COLOR_GREEN="\033[1;32m"
COLOR_YELLOW="\033[1;33m"
COLOR_PURPLE="\033[0;35m"
COLOR_CYAN="\033[0;36m"
LOG_PATH="/dev/null"
log () {
echo -e "$1"
echo -e "$1" >> "$LOG_PATH"
}
header() {
log "$NO_COLOR "
log "$NO_COLOR ███████╗ ██████╗ ██████╗██████╗ ██╗ ██╗███╗ ██╗ "
log "$NO_COLOR ██╔════╝██╔════╝ ██╔════╝██╔══██╗██║ ██║████╗ ██║ "
log "$NO_COLOR ███████╗██║ ███╗██║ ██████╔╝██║ ██║██╔██╗ ██║ "
log "$NO_COLOR ╚════██║██║ ██║██║ ██╔══██╗██║ ██║██║╚██╗██║ "
log "$NO_COLOR ███████║╚██████╔╝╚██████╗██║ ██║╚██████╔╝██║ ╚████║ "
log "$NO_COLOR ╚══════╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ "
log "$NO_COLOR "
}
ensure_binary() {
BINARY_PATH=$(command -v "$1")
if [ $? -ne 0 ]
then
log "${COLOR_RED}error: ${NO_COLOR}unable to locate '$1' utility"
log ""
exit 1
fi
}
error() {
log "${COLOR_RED}error: ${NO_COLOR}$1"
log ""
exit 1
}
header
ensure_binary "basename"
ensure_binary "realpath"
ensure_binary "date"
ensure_binary "echo"
ensure_binary "whoami"
ensure_binary "openssl"
ensure_binary "docker"
ensure_binary "curl"
ensure_binary "jq"
log "${COLOR_CYAN}info: ${NO_COLOR}script - $0"
log "${COLOR_CYAN}info: ${NO_COLOR}user - $(whoami 2>> /dev/null)"
log "${COLOR_CYAN}info: ${NO_COLOR}date - $(date -uIseconds 2>> /dev/null)"
##################################################
# Try to parse 'SGCRUN_LOG' environment variable #
##################################################
if [ -n "$SGCRUN_LOG" ]
then
SGCRUN_LOG_REALPATH=$(realpath "$SGCRUN_LOG" 2> /dev/null)
if [ -f "$SGCRUN_LOG_REALPATH" ]
then
LOG_PATH="$SGCRUN_LOG_REALPATH"
else
log "${COLOR_YELLOW}warning: ${NO_COLOR}SGCRUN_LOG was set but log file does not exist"
fi
fi
log "${COLOR_CYAN}info: ${NO_COLOR}log file - $LOG_PATH"
#################
# Validate ARGS #
#################
SGCRUN_ORGANIZATION="$1"
[[ "$SGCRUN_ORGANIZATION" =~ ^[0-9A-Za-z_-]+$ ]] || error "invalid github organization"
log "${COLOR_CYAN}info: ${NO_COLOR}organization - $SGCRUN_ORGANIZATION"
################
# Validate ENV #
################
[[ "$SGCRUN_GITHUB_TOKEN" =~ ^([0-9A-Za-z\_\-]+)$ ]] || error "unable to read SGCRUN_GITHUB_TOKEN"
#########################
# Create NAME Variables #
#########################
SGCRUN_RUNNER_DATE=$(date -u +"%Y%m%d%H%M%S" 2>> "$LOG_PATH")
if [ $? -ne 0 ]
then
log "${COLOR_RED}error: ${NO_COLOR}unable to create date name"
log ""
exit 1
fi
SGCRUN_RUNNER_HEX=$(openssl rand -hex 4 2>> "$LOG_PATH")
if [ $? -ne 0 ]
then
log "${COLOR_RED}error: ${NO_COLOR}unable to create hex name"
log ""
exit 1
fi
SGCRUN_RUNNER_NAME="sgcrun-$SGCRUN_RUNNER_DATE-$SGCRUN_RUNNER_HEX"
log "${COLOR_CYAN}info: ${NO_COLOR}runner - $SGCRUN_RUNNER_NAME"
#############################
# Fetch runner REGISTRATION #
#############################
SGCRUN_GITHUB_API_RESPONSE=$(
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $SGCRUN_GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/orgs/$SGCRUN_ORGANIZATION/actions/runners/registration-token" \
2>> "$LOG_PATH"
)
if [ $? -ne 0 ]
then
log "${COLOR_RED}error: ${NO_COLOR}unable to fetch registration token"
log ""
exit 1
fi
SGCRUN_GITHUB_REGISTRATION_TOKEN=$(echo $SGCRUN_GITHUB_API_RESPONSE 2>> "$LOG_PATH" | jq -r ".token" 2>> "$LOG_PATH")
if [ $? -ne 0 ]
then
log "${COLOR_RED}error: ${NO_COLOR}unable to parse registration token"
log ""
exit 1
fi
[[ "$SGCRUN_GITHUB_REGISTRATION_TOKEN" =~ ^([0-9A-Za-z]+)$ ]] || error "invalid github registration token"
###########################
# Create DOCKER container #
###########################
docker run \
--detach \
--restart no \
--rm \
--name $SGCRUN_RUNNER_NAME \
--env RUNNER_NAME=$SGCRUN_RUNNER_NAME \
--env RUNNER_ORG=$SGCRUN_ORGANIZATION \
--env RUNNER_TOKEN=$SGCRUN_GITHUB_REGISTRATION_TOKEN \
--env RUNNER_LABELS=sgcrun \
--env RUNNER_EPHEMERAL=true \
--volume /runner \
--privileged \
summerwind/actions-runner-dind:ubuntu-22.04 >> "$LOG_PATH" 2>> "$LOG_PATH"
if [ $? -ne 0 ]
then
log "${COLOR_RED}error: ${NO_COLOR}unable to create docker container temporal file"
log ""
exit 1
fi
log ""
log "${COLOR_GREEN}success: ${NO_COLOR}created self-hosted github containerized runner."
log ""