Skip to content

Commit 68884fd

Browse files
authored
fix: run the GitLab Runner deregistration process at shutdown (#1034)
## Description We have a script which removes the GitLab Runner from GitLab in case it is shutdown. This script wasn't run automatically. This PR adds a Systemd service which starts the removal script at shutdown automatically. Closes #1014 ## Migrations required No ## Verification Verified manually that the Runner is removed from GitLab at shutdown.
1 parent 7ebfc0a commit 68884fd

File tree

1 file changed

+31
-68
lines changed

1 file changed

+31
-68
lines changed

template/gitlab-runner.tftpl

+31-68
Original file line numberDiff line numberDiff line change
@@ -155,77 +155,40 @@ then
155155
docker-machine rm -y dummy-machine
156156
unset HOME
157157
unset USER
158-
159158
fi
160159

161-
# A small script to remove this runner from being registered with Gitlab.
162-
cat <<REM > /etc/rc.d/init.d/remove_gitlab_registration
160+
# A small script to remove this runner from being registered with Gitlab. Executed at shutdown.
161+
cat <<EOF > /etc/systemd/system/remove-gitlab-registration.service
162+
[Unit]
163+
Description=Remove the GitLab Runner from GitLab at shutdown
164+
After=network-online.target
165+
Wants=network-online.target
166+
Before=shutdown.target reboot.target halt.target kexec.target
167+
168+
[Service]
169+
Type=oneshot
170+
RemainAfterExit=yes
171+
ExecStart=/bin/true
172+
ExecStop=/opt/remove_gitlab_registration.sh
173+
174+
[Install]
175+
WantedBy=multi-user.target
176+
177+
EOF
178+
179+
cat <<EOF > /opt/remove_gitlab_registration.sh
163180
#!/bin/bash
164-
# chkconfig: 1356 99 03
165-
# description: cleans up gitlab runner key
166-
# processname: remove_runner_key
167-
# /etc/rc.d/init.d/remove_gitlab_registration
168-
lockfile=/var/lock/subsys/remove_gitlab_registration
169-
170-
# This lockfile is necessary so that we'll run the cleanup later.
171-
start() {
172-
logger "Setting up Runner Removal Lockfile"
173-
touch \$lockfile
174-
}
175-
176-
# Overwrite token in SSM with null and remove runner from Gitlab
177-
stop() {
178-
logger "Removing Gitlab Runner Token"
179-
aws ssm put-parameter --overwrite --type SecureString --name "${secure_parameter_store_runner_token_key}" --region "${secure_parameter_store_region}" --value="null" 2>&1 | logger &
180-
curl -sS ${curl_cacert} --request DELETE "${runners_gitlab_url}/api/v4/runners" --form "token=$token" 2>&1 | logger &
181-
retval=\$?
182-
rm -f \$lockfile
183-
return \$retval
184-
}
185-
186-
# Map these to start just to be redunant.
187-
# We don't want to run Stop outside of shutdown.
188-
restart() {
189-
start
190-
}
191-
reload() {
192-
start
193-
}
194-
195-
# Do nothing - there's no status.
196-
status() {
197-
:
198-
}
199-
200-
case "\$1" in
201-
start)
202-
\$1
203-
;;
204-
stop)
205-
\$1
206-
;;
207-
restart)
208-
\$1
209-
;;
210-
status)
211-
\$1
212-
;;
213-
*)
214-
echo "Usage: \$0 {start|stop|status|restart}"
215-
exit 2
216-
;;
217-
esac
218-
REM
219-
220-
chmod a+x /etc/init.d/remove_gitlab_registration
221-
222-
# Use chkconfig to link into the runlevel 0 (shutdown) directories
223-
# This adds "start" scripts to levels 1,3,5, and 6, and a "stop" to the others.
224-
# This way we'll not be assigned jobs if we're shutting down, and clean up in Gitlab.
225-
chkconfig --add remove_gitlab_registration
226-
227-
# As noted above, this does nothing more than make the lockfile.
228-
service remove_gitlab_registration start
181+
echo "Removing Gitlab Runner ..."
182+
aws ssm put-parameter --overwrite --type SecureString --name "${secure_parameter_store_runner_token_key}" --region "${secure_parameter_store_region}" --value="null" 2>&1
183+
curl -sS ${curl_cacert} --request DELETE "${runners_gitlab_url}/api/v4/runners" --form "token=$token" 2>&1
184+
185+
EOF
186+
187+
chmod a+x /opt/remove_gitlab_registration.sh
188+
systemctl enable remove-gitlab-registration.service
189+
190+
# start the service. Otherwise the stop action will not be triggered at shutdown.
191+
service remove-gitlab-registration start
229192

230193
if ! ( rpm -q gitlab-runner >/dev/null )
231194
then

0 commit comments

Comments
 (0)