Commit bb06ab6c authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add config file

parent 0530bbc6
Loading
Loading
Loading
Loading
+51 −13
Original line number Diff line number Diff line
@@ -2,9 +2,16 @@

set -eu

declare -r CONFIG_BASE=/etc/gitlab-runner
declare -r CONFIG_FILE=${CONFIG_BASE}/registration.conf

test -e ${CONFIG_FILE} &&
	source ${CONFIG_FILE}


usage() {
	cat <<-END
	Usage: $0 [-h] [-d DESCRIPTION] [-u COORDINATOR] NAME TOKEN
	Usage: $0 [-h] [-d DESCRIPTION] [-u COORDINATOR] TOKEN [NAME]

	Options:
	  -h --help         Print this message and exit
@@ -12,8 +19,10 @@ usage() {
	  -d --description  A description for the GitLab UI (defaults to NAME)

	Arguments:
	  NAME   An instance name to identify this runner
	  TOKEN  A registration token provided by the co-ordinator (GitLab) instance
	  NAME   An instance name to identify this runner.
	         Defaults to "${HOSTNAME}/%02i" where "%i" is an auto-generated
	         index.
	END
}

@@ -36,10 +45,9 @@ done

trap '{ echo; usage; } >&2' EXIT
set -- "${ARGS[@]}"
: ${NAME:=${1? An instance name is required}} \
  ${TOKEN:=${2? A registration token is required}} \
  ${DESCRIPTION:=${NAME}} \
  ${COORDINATOR_URL:=https://code.kodo.org.uk/} \
: ${TOKEN:=${1? A registration token is required}} \
  ${NAME:=${2-${HOSTNAME}/%i}} \
  ${COORDINATOR_URL? Need COORDINATOR_URL in configs or environment, or --url} \
shift 2
if [[ $# -eq 0 ]]; then
	echo >&2 "Unknown arguments: $*"
@@ -47,16 +55,46 @@ if [[ $# -eq 0 ]]; then
fi
trap - EXIT

UNIT=$(systemd-escape -m gitlab-runner@${NAME})
DIR=${UNIT%.service}
DIR=${DIR#gitlab-runner@}.d
mkdir -p --mode=0750 ${CONFIG_BASE}

set_name() {
	local name=$(printf "${NAME}" ${1-})
	DEFAULT_DESCRIPTION=${name}
	SYSTEMD_UNIT=$(systemd-escape -m gitlab-runner@${name})
	name=${SYSTEMD_UNIT%.service}
	RUNNER_CONFIG=${CONFIG_BASE}/${name#gitlab-runner@}.d
}

find_name() {
	local index=0

	if [[ $(printf "${NAME}" 1) = $(printf "${NAME}" 2) ]]; then
		# No formatting marks in NAME
		set_name
		if [[ -e ${RUNNER_CONFIG} ]]; then
			echo >&2 "Config already exists: ${RUNNER_CONFIG}"
			echo >&2 "(with name: ${NAME})"
			exit 1
		fi
		return
	fi

	while let "++index <= ${MAX_INDEX-99}" && set_name $index; do
		[[ -e ${RUNNER_CONFIG} ]] || return 0
	done

	echo >&2 "No free config slots found with ${NAME}"
	exit 1
}

find_name

docker run --rm -t -i -v /etc/gitlab-runner/${DIR}:/etc/gitlab-runner \
docker run --rm -t -i -v ${RUNNER_CONFIG}:/etc/gitlab-runner \
	gitlab/gitlab-runner register \
	--non-interactive \
	--url ${COORDINATOR_URL} \
	--registration-token ${TOKEN} \
	--description ${NAME} \
	--description ${DESCRIPTION-$DEFAULT_DESCRIPTION} \
	--executor docker \
	--docker-image docker:stable \
	--docker-privileged=true \
@@ -64,5 +102,5 @@ docker run --rm -t -i -v /etc/gitlab-runner/${DIR}:/etc/gitlab-runner \
	--run-untagged=true \
	--locked=false

systemctl enable ${UNIT}
systemctl start ${UNIT}
systemctl enable ${SYSTEMD_UNIT}
systemctl start ${SYSTEMD_UNIT}