From f69090efebf9f88530d2c90161e4d3f00593b27b Mon Sep 17 00:00:00 2001 From: L3D Date: Thu, 21 Mar 2024 15:23:41 +0100 Subject: [PATCH] Create systemd unit and register --- README.md | 2 ++ defaults/main.yml | 4 ++++ handlers/main.yml | 16 ++++++++++++++++ tasks/configure_runner.yml | 21 +++++++++++++++++++++ tasks/main.yml | 9 +++++++++ tasks/systemd.yml | 28 ++++++++++++++++++++++++++++ templates/forgejo_runner.service.j2 | 16 ++++++++++++++++ 7 files changed, 96 insertions(+) create mode 100644 handlers/main.yml create mode 100644 tasks/configure_runner.yml create mode 100644 tasks/systemd.yml create mode 100644 templates/forgejo_runner.service.j2 diff --git a/README.md b/README.md index 14ada9f..3717245 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,5 @@ Ansible role to install and Setup forgeo runner. (Work in Progress...) https://forgejo.org/docs/next/admin/actions/ + +Steps diff --git a/defaults/main.yml b/defaults/main.yml index 2fbbb24..442059c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -7,5 +7,9 @@ forgejo_runner__user_home: '/var/lib/forgejo-runner' forgejo_runner__full_executable_path: '/usr/local/bin/forgejo_runner' forgejo_runner__gpg_id: 'EB114F5E6C0DC2BCDD183550A4B61A2DC5923710' +forgejo_runner__instance_address: '' +forgejo_runner__token: '' +forgejo_runner__mode: "daemon" + # should we do a version check? (recomended) submodules_versioncheck: false diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..a33e7a9 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,16 @@ +--- +- name: "Restart forgejo-runner" + listen: "systemctl restart forgejo-runner" + become: true + ansible.builtin.systemd: + name: 'forgejo-runner' + state: 'restarted' + when: ansible_service_mgr == "systemd" + +- name: "Reload systemd" + listen: 'systemctl daemon-reload' + become: true + ansible.builtin.systemd: + daemon_reload: true + when: ansible_service_mgr == "systemd" + notify: 'systemctl restart forgejo-runner' diff --git a/tasks/configure_runner.yml b/tasks/configure_runner.yml new file mode 100644 index 0000000..8c07dfe --- /dev/null +++ b/tasks/configure_runner.yml @@ -0,0 +1,21 @@ +--- +- name: Make Sure Forgejo runner instace address is set + ansible.builtin.fail: + msg: "Please set forgejo_runner__instance_address" + when: forgejo_runner__instance_address == '' + +- name: Make Sure Forgejo runner token is set + ansible.builtin.fail: + msg: "Please set forgejo_runner__token" + when: forgejo_runner__token == '' + +- name: Register Forgejo runner + ansible.builtin.command: + chdir: "{{ forgejo_runner__user_home }}" + cmd: | + {{ forgejo_runner__full_executable_path }} register --no-interactive \ + --token '{{ forgejo_runner__token }}' \ + --instance '{{ forgejo_runner__instance_address }}' \ + --name '{{ inventory_hostname }}@ansible' + creates: "{{ forgejo_runner__user_home }}/.runner" + become: true diff --git a/tasks/main.yml b/tasks/main.yml index b08c810..8493c40 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -15,3 +15,12 @@ - name: Install forgejo runner binary ansible.builtin.include_tasks: file: 'install_runner.yml' + +- name: Configure forgejo runner + ansible.builtin.include_tasks: + file: 'configure_runner.yml' + +- name: Start forgejo runner as systemd service + ansible.builtin.include_tasks: + file: 'systemd.yml' + when: ansible_service_mgr == "systemd" diff --git a/tasks/systemd.yml b/tasks/systemd.yml new file mode 100644 index 0000000..34daca7 --- /dev/null +++ b/tasks/systemd.yml @@ -0,0 +1,28 @@ +--- +- name: "Setup systemd service" + become: true + when: ansible_os_family == "Debian" + ansible.builtin.template: + src: 'forgejo_runner.service.j2' + dest: '/lib/systemd/system/forgejo-runner.service' + owner: 'root' + group: 'root' + mode: '0644' + notify: + - 'systemctl restart forgejo-runner' + - 'systemctl daemon-reload' + +- name: "Reload systemd" + become: true + ansible.builtin.systemd: + daemon_reload: true + notify: 'systemctl restart forgejo-runner' + +- name: "Start and enable forgejo-runner" + become: true + ansible.builtin.systemd: + name: 'forgejo-runner' + state: started + enabled: true + when: ansible_service_mgr == "systemd" + notify: 'systemctl restart forgejo-runner' diff --git a/templates/forgejo_runner.service.j2 b/templates/forgejo_runner.service.j2 new file mode 100644 index 0000000..6cd449e --- /dev/null +++ b/templates/forgejo_runner.service.j2 @@ -0,0 +1,16 @@ +{{ ansible_managed | comment }} +[Unit] +Description=Forgejo runner +After=network.target + +[Service] +Type=simple +User={{ forgejo_runner__user }} +Group={{ forgejo_runner__group }} +WorkingDirectory={{ forgejo_runner__user_home }} +ExecStart={{ forgejo_runner__full_executable_path }} {{ forgejo_runner__mode }} +Restart=always +RestartSec=10s + +[Install] +WantedBy=multi-user.target