From 10503a44a0a573cf5f570bf9bfe662e2c3f3edfa Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 17 Feb 2024 18:26:24 -0800 Subject: [PATCH] add forgejo-runner playbook + role --- .gitignore | 1 + forgejo-runner-playbook.yaml | 7 ++ inventory.yml | 7 ++ roles/forgejo-runner/handlers/main.yml | 17 ++++ roles/forgejo-runner/tasks/main.yml | 56 +++++++++++++ .../templates/forgejo-runner.service | 34 ++++++++ .../templates/forgejo-runner.yaml | 83 +++++++++++++++++++ .../templates/podman.service-override.conf | 2 + .../templates/podman.socket-override.conf | 2 + roles/monitoring/templates/promtail.yml | 2 +- 10 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 forgejo-runner-playbook.yaml create mode 100644 roles/forgejo-runner/handlers/main.yml create mode 100644 roles/forgejo-runner/tasks/main.yml create mode 100644 roles/forgejo-runner/templates/forgejo-runner.service create mode 100644 roles/forgejo-runner/templates/forgejo-runner.yaml create mode 100644 roles/forgejo-runner/templates/podman.service-override.conf create mode 100644 roles/forgejo-runner/templates/podman.socket-override.conf diff --git a/.gitignore b/.gitignore index 956d472..2416204 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /secrets +secrets.yaml diff --git a/forgejo-runner-playbook.yaml b/forgejo-runner-playbook.yaml new file mode 100644 index 0000000..12624fd --- /dev/null +++ b/forgejo-runner-playbook.yaml @@ -0,0 +1,7 @@ +- hosts: forgejo_runners + vars: + ansible_user: root + roles: + - base + - monitoring + - forgejo-runner diff --git a/inventory.yml b/inventory.yml index 3c83f3d..6b6dea0 100644 --- a/inventory.yml +++ b/inventory.yml @@ -36,3 +36,10 @@ home_k8s: home_network: true k8s-node-3: home_network: true + +forgejo_runners: + hosts: + forgejo-runner-0: + home_network: true + forgejo-runner-1: + home_network: true diff --git a/roles/forgejo-runner/handlers/main.yml b/roles/forgejo-runner/handlers/main.yml new file mode 100644 index 0000000..bd3498b --- /dev/null +++ b/roles/forgejo-runner/handlers/main.yml @@ -0,0 +1,17 @@ +- name: systemctl daemon-reload + command: systemctl daemon-reload + +- name: restart podman.service + service: + name: podman.service + state: restarted + +- name: restart podman.socket + service: + name: podman.socket + state: restarted + +- name: restart forgejo-runner + service: + name: forgejo-runner + state: restarted diff --git a/roles/forgejo-runner/tasks/main.yml b/roles/forgejo-runner/tasks/main.yml new file mode 100644 index 0000000..88e89fe --- /dev/null +++ b/roles/forgejo-runner/tasks/main.yml @@ -0,0 +1,56 @@ +- name: Install podman + apt: + name: [podman] + +- name: make some directories + file: + path: "{{ item }}" + state: directory + with_items: + - /etc/forgejo-runner + - /opt/shared-with-host + - /var/forgejo-runner + - /etc/systemd/system/podman.service.d + - /etc/systemd/system/podman.socket.d + +- name: configure podman service + template: + src: podman.service-override.conf + dest: /etc/systemd/system/podman.service.d/override.conf + notify: + - systemctl daemon-reload + - restart podman.service + +- name: configure podman socket + template: + src: podman.socket-override.conf + dest: /etc/systemd/system/podman.socket.d/override.conf + notify: + - systemctl daemon-reload + - restart podman.socket + +- name: enable podman + service: + name: "{{ item }}" + enabled: true + with_items: [podman.socket, podman.service] + +- name: configure forgejo-runner + template: + src: forgejo-runner.yaml + dest: /etc/forgejo-runner/config.yaml + notify: + - restart forgejo-runner + +- name: install forgejo-runner.service + template: + src: forgejo-runner.service + dest: /etc/systemd/system/forgejo-runner.service + notify: + - systemctl daemon-reload + - restart forgejo-runner + +- name: register the runner + command: podman run --rm --user root -tiv /etc/forgejo-runner:/etc/forgejo-runner:ro -v /var/forgejo-runner:/data code.forgejo.org/forgejo/runner:3.3.0 forgejo-runner register --instance https://git.janky.solutions --name "{{ inventory_hostname }}" --token "{{ lookup('ansible.builtin.ini', 'registration_token section=forgejo_runner file=secrets/git.janky.solutions.ini') }}" --no-interactive + args: + creates: /var/forgejo-runner/.runner diff --git a/roles/forgejo-runner/templates/forgejo-runner.service b/roles/forgejo-runner/templates/forgejo-runner.service new file mode 100644 index 0000000..8917cb0 --- /dev/null +++ b/roles/forgejo-runner/templates/forgejo-runner.service @@ -0,0 +1,34 @@ +[Unit] +Description=Forgejo Runner +Wants=network.target + +[Service] +Environment=PODMAN_SYSTEMD_UNIT=%n +Restart=on-failure +TimeoutStopSec=70 +ExecStartPre=/bin/rm -f %t/forgejo-runner.service.pid %t/forgejo-runner.service.ctr-id +ExecStartPre=-/usr/bin/podman pull code.forgejo.org/forgejo/runner:3.3.0 +ExecStart=/usr/bin/podman run \ + --cidfile=%t/%n.ctr-id \ + --cgroups=no-conmon \ + --rm \ + --sdnotify=conmon \ + --user=0 \ + -d \ + --conmon-pidfile %t/forgejo-runner.service.pid \ + --replace \ + --env DOCKER_HOST=unix:///run/podman/podman.sock \ + --name forgejo-runner \ + -v /run/podman/podman.sock:/run/podman/podman.sock \ + -v /etc/forgejo-runner:/etc/forgejo-runner:ro \ + -v /opt/shared-with-host:/opt/shared-with-host \ + -v /var/forgejo-runner:/data code.forgejo.org/forgejo/runner:3.3.0 forgejo-runner daemon \ + -c /etc/forgejo-runner/config.yaml +ExecStop=/usr/bin/podman stop --ignore --cidfile %t/forgejo-runner.service.ctr-id -t 10 +ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/forgejo-runner.service.ctr-id +PIDFile=%t/forgejo-runner.service.pid +Type=forking +RestartSec=60 + +[Install] +WantedBy=multi-user.target default.target diff --git a/roles/forgejo-runner/templates/forgejo-runner.yaml b/roles/forgejo-runner/templates/forgejo-runner.yaml new file mode 100644 index 0000000..8e3ca1d --- /dev/null +++ b/roles/forgejo-runner/templates/forgejo-runner.yaml @@ -0,0 +1,83 @@ +log: + # The level of logging, can be trace, debug, info, warn, error, fatal + level: debug + +runner: + # Where to store the registration result. + file: .runner + # Execute how many tasks concurrently at the same time. + capacity: 1 + # Extra environment variables to run jobs. + envs: + GO_PROXY: goproxy.home.finn.io + # Extra environment variables to run jobs from a file. + # It will be ignored if it's empty or the file doesn't exist. + env_file: .env + # The timeout for a job to be finished. + # Please note that the Forgejo instance also has a timeout (3h by default) for the job. + # So the job could be stopped by the Forgejo instance if it's timeout is shorter than this. + timeout: 3h + # Whether skip verifying the TLS certificate of the Forgejo instance. + insecure: false + # The timeout for fetching the job from the Forgejo instance. + fetch_timeout: 5s + # The interval for fetching the job from the Forgejo instance. + fetch_interval: 2s + # The labels of a runner are used to determine which jobs the runner can run, and how to run them. + # Like: ["macos-arm64:host", "ubuntu-latest:docker://node:16-bullseye", "ubuntu-22.04:docker://node:16-bullseye"] + # If it's empty when registering, it will ask for inputting labels. + # If it's empty when execute `deamon`, will use labels in `.runner` file. + labels: [] + +cache: + # Enable cache server to use actions/cache. + enabled: true + # The directory to store the cache data. + # If it's empty, the cache data will be stored in $HOME/.cache/actcache. + dir: "" + # The host of the cache server. + # It's not for the address to listen, but the address to connect from job containers. + # So 0.0.0.0 is a bad choice, leave it empty to detect automatically. + host: "" + # The port of the cache server. + # 0 means to use a random available port. + port: 0 + # The external cache server URL. Valid only when enable is true. + # If it's specified, act_runner will use this URL as the ACTIONS_CACHE_URL rather than start a server by itself. + # The URL should generally end with "/". + external_server: "" + +container: + # Specifies the network to which the container will connect. + # Could be host, bridge or the name of a custom network. + # If it's empty, create a network automatically. + network: "" + # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker). + privileged: true + # And other options to be used when the container is started (eg, --add-host=my.forgejo.url:host-gateway). + options: + # The parent directory of a job's working directory. + # If it's empty, /workspace will be used. + workdir_parent: + # Volumes (including bind mounts) can be mounted to containers. Glob syntax is supported, see https://github.com/gobwas/glob + # You can specify multiple volumes. If the sequence is empty, no volumes can be mounted. + # For example, if you only allow containers to mount the `data` volume and all the json files in `/src`, you should change the config to: + # valid_volumes: + # - data + # - /src/*.json + # If you want to allow any volume, please use the following configuration: + # valid_volumes: + # - '**' + valid_volumes: [] + # overrides the docker client host with the specified one. + # If it's empty, act_runner will find an available docker host automatically. + # If it's "-", act_runner will find an available docker host automatically, but the docker host won't be mounted to the job containers and service containers. + # If it's not empty or "-", the specified docker host will be used. An error will be returned if it doesn't work. + docker_host: "unix:///run/podman/podman.sock" + # Pull docker image(s) even if already present + force_pull: false + +host: + # The parent directory of a job's working directory. + # If it's empty, $HOME/.cache/act/ will be used. + workdir_parent: diff --git a/roles/forgejo-runner/templates/podman.service-override.conf b/roles/forgejo-runner/templates/podman.service-override.conf new file mode 100644 index 0000000..0ef3e2e --- /dev/null +++ b/roles/forgejo-runner/templates/podman.service-override.conf @@ -0,0 +1,2 @@ +[Service] +Environment=LOGGING="--log-level=warn" diff --git a/roles/forgejo-runner/templates/podman.socket-override.conf b/roles/forgejo-runner/templates/podman.socket-override.conf new file mode 100644 index 0000000..fef45d6 --- /dev/null +++ b/roles/forgejo-runner/templates/podman.socket-override.conf @@ -0,0 +1,2 @@ +[Socket] +SocketGroup=debian diff --git a/roles/monitoring/templates/promtail.yml b/roles/monitoring/templates/promtail.yml index a5735bb..e1b5512 100644 --- a/roles/monitoring/templates/promtail.yml +++ b/roles/monitoring/templates/promtail.yml @@ -1,6 +1,6 @@ server: log_level: warn - http_listen_port: 0 + http_listen_port: 9080 grpc_listen_port: 0 clients: