diff --git a/scripts/config.env.example b/scripts/config.env.example new file mode 100644 index 0000000000000000000000000000000000000000..36c91a10053ceaf903eddb64b800f9aef665da29 --- /dev/null +++ b/scripts/config.env.example @@ -0,0 +1,7 @@ +GITLAB_URL="" +RUNNER_TYPE="" +PRIVATE_TOKEN="" +PROJECT_ID="" +GROUP_ID="" +DESCRIPTION="" +TAG_LIST="" diff --git a/scripts/gl-runner-configuration.sh b/scripts/gl-runner-configuration.sh new file mode 100755 index 0000000000000000000000000000000000000000..74a91c0eea0114f6378feaba08e4abe246c2491e --- /dev/null +++ b/scripts/gl-runner-configuration.sh @@ -0,0 +1,101 @@ +#!/bin/bash + +# Uage helper +usage() { + echo "Usage: $0 [-c config_file] [-u gitlab_url] [-p project_id] [-g group_id] [-t private_token] [-r runner_type] [-d description] [-T tag_list]" + echo "Runner types: project | group | shared" + exit 1 +} + +# Default config file path (if any) +CONFIG_FILE="./config.env" + +# Manually parse -c <file> early, remove from args +for i in "$@"; do + if [[ "$i" == "-c" ]]; then + CONFIG_FILE="$2" + shift 2 + break + fi +done + +# Load from config file (if exists) +if [ -f "$CONFIG_FILE" ]; then + # Load configuration values + echo "Loading config from $CONFIG_FILE..." + source "$CONFIG_FILE" +else + echo "Warning: Configuration file $CONFIG_FILE not found. Using default values or CLI arguments." +fi + +# Parse command-line arguments to override config file values +OPTIND=1 +while getopts "u:p:g:t:r:d:T:c:" opt; do + case $opt in + u) GITLAB_URL="$OPTARG" ;; + p) PROJECT_ID="$OPTARG" ;; + g) GROUP_ID="$OPTARG" ;; + t) PRIVATE_TOKEN="$OPTARG" ;; + r) RUNNER_TYPE="$OPTARG" ;; + d) DESCRIPTION="$OPTARG" ;; + T) TAG_LIST="$OPTARG" ;; + *) usage ;; + esac +done + +# Validate required parameters +if [ -z "$GITLAB_URL" ] || [ -z "$RUNNER_TYPE" ] || [ -z "$PRIVATE_TOKEN" ]; then + echo "Error: Missing required parameters." + usage +fi + +# Set default description if not provided +DESCRIPTION=${DESCRIPTION:-"Automated Runner"} +TAG_LIST=${TAG_LIST:-"build,deploy"} + +# Define API endpoint and additional data based on runner type +case "$RUNNER_TYPE" in + project) + if [ -z "$PROJECT_ID" ]; then + echo "Error: PROJECT_ID is required for project runners." + exit 1 + fi + EXTRA_DATA="--data runner_type=project_type --data project_id=$PROJECT_ID" + ;; + group) + if [ -z "$GROUP_ID" ]; then + echo "Error: GROUP_ID is required for group runners." + exit 1 + fi + EXTRA_DATA="--data runner_type=group_type --data group_id=$GROUP_ID" + ;; + shared) + EXTRA_DATA="--data runner_type=instance_type" + ;; + *) + echo "Error: Invalid runner type. Choose from 'project', 'group', or 'shared'." + usage + ;; +esac + +# Create GitLab runner config +echo "Creating GitLab runner config..." +RESPONSE=$(curl --silent --request POST --url "$GITLAB_URL/api/v4/user/runners" \ + --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" \ + --data "description=$DESCRIPTION" \ + --data "tag_list=$TAG_LIST" \ + $EXTRA_DATA) + +# Extract token and id from the response +RUNNER_TOKEN=$(echo $RESPONSE | jq -r '.token') +RUNNER_ID=$(echo "$RESPONSE" | jq -r '.id') + +# Check if the runner creation was successful +if [ -z "$RUNNER_TOKEN" ]; then + echo "Failed to create runner on GitLab!" + exit 1 +fi + +echo "Runner created successfully. Check the token in file .gl-auth-token-<runner_id>" +echo $RUNNER_TOKEN 2>&1 | tee ~/.gl-auth-token-$RUNNER_ID > /dev/null + diff --git a/scripts/gl-runner-vm.sh b/scripts/gl-runner-vm.sh new file mode 100755 index 0000000000000000000000000000000000000000..f493aa879f47bb9233e1f593b71b91ce938c6d10 --- /dev/null +++ b/scripts/gl-runner-vm.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Load configuration +CONFIG_FILE="server_config.cfg" +if [[ -f "$CONFIG_FILE" ]]; then + source "$CONFIG_FILE" +else + echo "Configuration file $CONFIG_FILE not found!" + exit 1 +fi + +# Set runner name and floating IP +export RUNNER_NAME="${RUNNER_NAME_PREFIX}-$(date +'%FT%T')" +export FLOATING_IP="$FLOATING_IP" + +# Create the server and capture the instance ID +echo "Creating server $RUNNER_NAME..." +openstack server create --flavor "$FLAVOR" \ + --image "$IMAGE" \ + --key-name "$KEY_NAME" \ + --network "$NETWORK" \ + $(for group in "${SECURITY_GROUPS[@]}"; do echo --security-group "$group"; done) \ + "$RUNNER_NAME" + +# Get the instance ID of the created server +RUNNER_INSTANCE_ID=$(openstack server list --name "$RUNNER_NAME" -f value -c ID) + +# Wait for the server to become ACTIVE +echo "Waiting for server $RUNNER_NAME (ID: $RUNNER_INSTANCE_ID) to be ACTIVE..." +server_status=$(openstack server show "$RUNNER_INSTANCE_ID" -f value -c status) +while [[ "$server_status" != "ACTIVE" ]]; do + echo "Current status: $server_status. Waiting..." + sleep 5 # Wait for 5 seconds before checking again + server_status=$(openstack server show "$RUNNER_INSTANCE_ID" -f value -c status) +done + +echo "Server $RUNNER_NAME is now ACTIVE." + +# Add the floating IP to the server +echo "Assigning floating IP $FLOATING_IP to server $RUNNER_NAME..." +openstack server add floating ip "$RUNNER_INSTANCE_ID" "$FLOATING_IP" + +echo "Floating IP assigned successfully." + diff --git a/scripts/register-runner.sh b/scripts/register-runner.sh new file mode 100755 index 0000000000000000000000000000000000000000..7071bdb45210b149aff9cdd815db6d962f712bab --- /dev/null +++ b/scripts/register-runner.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +# Load configuration from config.cfg +CONFIG_FILE="config.cfg" + +# Ensure the config file exists +if [ ! -f "$CONFIG_FILE" ]; then + echo "Configuration file $CONFIG_FILE not found!" + exit 1 +fi + +# Read configuration values +source "$CONFIG_FILE" + +# Default values (can be overridden by config file or CLI arguments) +GITLAB_INSTANCE_URL=${GITLAB_INSTANCE_URL:-"https://gitlab.example.com/"} +RUNNER_AUTH_TOKEN=${RUNNER_AUTH_TOKEN:-""} +RUNNER_NAME=${RUNNER_NAME:-"MyGitLabRunner"} +RUNNER_TAGS=${RUNNER_TAGS:-"docker,shell"} +EXECUTOR=${EXECUTOR:-"docker"} + +# Parse command-line arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --url) GITLAB_INSTANCE_URL="$2"; shift ;; + --token) RUNNER_AUTH_TOKEN="$2"; shift ;; + --name) RUNNER_NAME="$2"; shift ;; + --tags) RUNNER_TAGS="$2"; shift ;; + --executor) EXECUTOR="$2"; shift ;; + *) echo "Unknown parameter passed: $1"; exit 1 ;; + esac + shift +done + +# Validate required variables +if [ -z "$GITLAB_INSTANCE_URL" ] || [ -z "$RUNNER_AUTH_TOKEN" ]; then + echo "Error: GITLAB_INSTANCE_URL and RUNNER_AUTH_TOKEN are required!" + exit 1 +fi + +# Update and install necessary packages +sudo apt-get update +sudo apt-get install -y ca-certificates curl gnupg lsb-release + +# Create Docker GPG key and repo +sudo install -m 0755 -d /etc/apt/keyrings +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg +sudo chmod a+r /etc/apt/keyrings/docker.gpg +echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ + $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + +# Install Docker Engine +sudo apt-get update +sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin + +# Install GitLab Runner binary +sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 +sudo chmod +x /usr/local/bin/gitlab-runner + +# Create a GitLab Runner user +sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash + +# Install GitLab Runner as a service +sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner +sudo gitlab-runner start + +# Register GitLab Runner +sudo gitlab-runner register --non-interactive \ +--url "$GITLAB_INSTANCE_URL" \ +--registration-token "$RUNNER_AUTH_TOKEN" \ +--executor "$EXECUTOR" \ +--docker-image "alpine:latest" \ +--description "$RUNNER_NAME" \ +--tag-list "$RUNNER_TAGS" + +# Verify Docker is installed +sudo docker run hello-world + +# Output success message +echo "GitLab runner setup is complete." +echo "Check the GitLab project CI/CD settings to ensure the runner is connected." + diff --git a/scripts/server_config.cfg.example b/scripts/server_config.cfg.example new file mode 100644 index 0000000000000000000000000000000000000000..b0ec271d5cec8145cf355429ce312de42102f1c9 --- /dev/null +++ b/scripts/server_config.cfg.example @@ -0,0 +1,9 @@ +# server_config.cfg +RUNNER_NAME_PREFIX="gl-runner" +FLOATING_IP="" +FLAVOR="" +IMAGE="" +KEY_NAME="" +NETWORK="" +SECURITY_GROUPS=("" "") +