|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | +# |
| 7 | +# OpenCRVS is also distributed under the terms of the Civil Registration |
| 8 | +# & Healthcare Disclaimer located at http://opencrvs.org/license. |
| 9 | +# |
| 10 | +# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. |
| 11 | + |
3 | 12 | printf """ |
4 | 13 | ----------------------------------- |
5 | 14 | ▶️ Running Node runner setup script |
6 | 15 | ----------------------------------- |
7 | | -
|
8 | 16 | """ |
9 | 17 |
|
10 | | - |
11 | 18 | set -o errexit # Stop on error (like `-e`) |
12 | 19 | set -o nounset # Stop on unset vars (like `-u`) |
13 | 20 | set -o pipefail # Fail on first failed command in a pipeline |
14 | 21 | set -o errtrace # Trap ERR in functions and subshells |
15 | 22 |
|
16 | 23 | trap 'echo "❌ Script failed on line $LINENO with exit code $?"' ERR |
17 | 24 |
|
18 | | -# --- DEFAULTS --- |
19 | | -RUNNER_DIR="/opt/github-runner" |
20 | | -RUNAS_USER="provision" |
21 | | -RUNAS_GROUP="application" |
22 | 25 | # --- USAGE --- |
23 | 26 | usage() { |
24 | | - echo "Usage: $0 [OPTIONS]" |
25 | | - echo "" |
26 | | - echo "Options:" |
27 | | - echo " --owner GitHub org or username (required)" |
28 | | - echo " --repo GitHub repository name (optional for org-level runner)" |
29 | | - echo " --token GitHub PAT or registration token (required)" |
30 | | - echo " --scope 'repo' or 'org' (default: repo)" |
31 | | - echo " --name Runner name (default: <hostname>-runner)" |
32 | | - echo " --labels Comma-separated list of runner labels" |
33 | | - echo " --dir Runner install directory (default: /opt/github-runner)" |
34 | | - echo " --env infrastructure environment name" |
35 | | - echo " -h, --help Show this help message" |
36 | | - echo "" |
| 27 | + echo """ |
| 28 | +Usage: $0 [OPTIONS] |
| 29 | +
|
| 30 | +Options: |
| 31 | + --owner GitHub org or username (required) |
| 32 | + --repo GitHub repository name (required) |
| 33 | + --env Infrastructure environment name(s) comma-separated (required) |
| 34 | + Runner will be used to provision infrastructure for these envs |
| 35 | + For example: dev,qa,staging or prod |
| 36 | + --token GitHub PAT or registration token (required) |
| 37 | + --name Runner name (default: <hostname>-runner) |
| 38 | + --dir Runner install directory (default: /opt/github-runner) |
| 39 | + -h, --help Show this help message |
| 40 | +""" |
37 | 41 | exit 1 |
38 | 42 | } |
39 | 43 |
|
40 | 44 | # --- PARSE OPTIONS --- |
41 | | -SCOPE="repo" |
42 | 45 | while [[ $# -gt 0 ]]; do |
43 | 46 | case "$1" in |
44 | 47 | --owner) GITHUB_OWNER="$2"; shift 2 ;; |
45 | 48 | --repo) REPO_NAME="$2"; shift 2 ;; |
46 | 49 | --token) GITHUB_TOKEN="$2"; shift 2 ;; |
47 | | - --scope) SCOPE="$2"; shift 2 ;; |
48 | | - --name) RUNNER_NAME="$2"; shift 2 ;; |
49 | | - --labels) LABELS="$2"; shift 2 ;; |
50 | 50 | --dir) RUNNER_DIR="$2"; shift 2 ;; |
51 | 51 | --env) ENV="$2"; shift 2 ;; |
52 | 52 | --runas-user) RUNAS_USER="$2"; shift 2 ;; |
53 | 53 | --runas-group) RUNAS_GROUP="$2"; shift 2 ;; |
| 54 | + --name) RUNNER_NAME="$2"; shift 2 ;; |
54 | 55 | -h|--help) usage ;; |
55 | 56 | *) echo "Unknown option: $1"; usage ;; |
56 | 57 | esac |
57 | 58 | done |
58 | 59 |
|
59 | 60 | # --- INTERACTIVE PROMPTS (IF NOT SET) --- |
60 | | -[[ -z "${ENV:-}" ]] && read -rp "Infrastructure environment name: " ENV |
61 | 61 | [[ -z "${GITHUB_OWNER:-}" ]] && read -rp "GitHub owner (or org): " GITHUB_OWNER |
62 | | -[[ "${SCOPE}" == "repo" && -z "${REPO_NAME:-}" ]] && read -rp "Repository name: " REPO_NAME |
| 62 | +[[ -z "${REPO_NAME:-}" ]] && read -rp "Repository name: " REPO_NAME |
| 63 | +[[ -z "${ENV:-}" ]] && read -rp "Infrastructure environment name(s): " ENV |
63 | 64 | [[ -z "${GITHUB_TOKEN:-}" ]] && read -rsp "GitHub token (no echo): " GITHUB_TOKEN && echo |
64 | | -[[ -z "${SCOPE:-}" ]] && read -rp "Scope (repo|org) [repo]: " SCOPE && SCOPE="${SCOPE:-repo}" |
65 | | -[[ -z "${RUNNER_NAME:-}" ]] && RUNNER_NAME="$(hostname)-runner" |
66 | 65 |
|
67 | | -# --- Add runner labels --- |
| 66 | +# --- OPTIONAL DETERMINE (IF NOT SET) --- |
| 67 | +# Runner install directory |
| 68 | +RUNNER_DIR=${RUNNER_DIR:-"/opt/github-runner"} |
| 69 | +# Runner name |
| 70 | +[[ -z "${RUNNER_NAME:-}" ]] && RUNNER_NAME="$(hostname)-runner" |
| 71 | +# Runner labels |
68 | 72 | LABELS="self-hosted,linux,node,${ENV}" |
69 | | - |
70 | | -# --- DETERMINE USER/GROUP TO RUN AS --- |
| 73 | +# Runner user and group |
71 | 74 | RUNAS_USER="${RUNAS_USER:-provision}" |
72 | 75 | RUNAS_GROUP="${RUNAS_GROUP:-application}" |
73 | 76 |
|
| 77 | + |
74 | 78 | # --- DETERMINE REGISTRATION URL --- |
75 | | -if [[ "$SCOPE" == "org" ]]; then |
76 | | - REG_URL="https://api.github.com/orgs/${GITHUB_OWNER}/actions/runners/registration-token" |
77 | | - RUNNER_SCOPE="https://github.com/${GITHUB_OWNER}" |
78 | | -elif [[ "$SCOPE" == "repo" ]]; then |
79 | | - REG_URL="https://api.github.com/repos/${GITHUB_OWNER}/${REPO_NAME}/actions/runners/registration-token" |
80 | | - RUNNER_SCOPE="https://github.com/${GITHUB_OWNER}/${REPO_NAME}" |
81 | | -else |
82 | | - echo "Invalid SCOPE value. Must be 'repo' or 'org'." |
83 | | - exit 1 |
84 | | -fi |
| 79 | +REG_URL="https://api.github.com/repos/${GITHUB_OWNER}/${REPO_NAME}/actions/runners/registration-token" |
| 80 | +RUNNER_SCOPE="https://github.com/${GITHUB_OWNER}/${REPO_NAME}" |
85 | 81 |
|
86 | 82 | # --- INSTALL DEPENDENCIES --- |
87 | 83 | echo "[+] Installing dependencies..." |
@@ -139,9 +135,8 @@ echo "[+] Installing systemd service..." |
139 | 135 |
|
140 | 136 | sudo ./svc.sh install |
141 | 137 |
|
142 | | -# Detect the systemd service name |
| 138 | +# Fix service to run as specific user/group |
143 | 139 | SERVICE_FILE_PATH=$(ls /etc/systemd/system/actions.runner.*.service 2>/dev/null | head -n1) |
144 | | - |
145 | 140 | if [[ -n "$SERVICE_FILE_PATH" ]]; then |
146 | 141 | echo "[+] Updating systemd unit to run as ${RUNAS_USER}:${RUNAS_GROUP}..." |
147 | 142 | sudo sed -i "s/^User=.*/User=${RUNAS_USER}/" "$SERVICE_FILE_PATH" |
|
151 | 146 | echo "⚠️ Could not find service file automatically — please verify installation." |
152 | 147 | fi |
153 | 148 |
|
154 | | - |
155 | 149 | sudo ./svc.sh start |
156 | 150 |
|
157 | 151 | echo "✅ Runner '${RUNNER_NAME}' is installed and started!" |
0 commit comments