From 86a444c1109a17edc4b9b347801004240cf3d61e Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Sat, 12 Aug 2023 16:01:42 +0200 Subject: [PATCH] added a bunch of features, but runner doesn't start if docker not present ?? --- README.md | 78 +++++++++++++++++++- defaults/main.yml | 15 ++-- handlers/main.yml | 12 +-- molecule/with_docker_host/converge.yml | 7 ++ molecule/with_docker_host/group_vars/all.yml | 36 +++++++++ molecule/with_docker_host/molecule.yml | 37 ++++++++++ molecule/with_docker_host/verify.yml | 19 +++++ tasks/configure.yml | 20 +---- tasks/install_docker.yml | 12 +++ tasks/install_host.yml | 2 + tasks/main.yml | 5 ++ tasks/prerequisites.yml | 3 +- tasks/register.yml | 28 +++++++ templates/config.yaml.j2 | 2 +- templates/docker-compose.yml.j2 | 2 +- templates/gitea-runner.service.j2 | 29 ++++++++ templates/gitea-runnner.service.j2 | 13 ---- 17 files changed, 271 insertions(+), 49 deletions(-) create mode 100644 molecule/with_docker_host/converge.yml create mode 100644 molecule/with_docker_host/group_vars/all.yml create mode 100644 molecule/with_docker_host/molecule.yml create mode 100644 molecule/with_docker_host/verify.yml create mode 100644 tasks/register.yml create mode 100644 templates/gitea-runner.service.j2 delete mode 100644 templates/gitea-runnner.service.j2 diff --git a/README.md b/README.md index f801562..30ee5f0 100644 --- a/README.md +++ b/README.md @@ -6,21 +6,91 @@ This role installs and configure a gitea act runner on **debian-based** distribu Requirements ------------ -None. +If `deploy_gitea_runner_deploy_method` is set to `docker`, this role assumes you have `docker`, `docker-compose` installed on the target hosts. The role will not install these components, but you can install them using the [install_docker](https://github.com/ednxzu/install_docker) role. Role Variables -------------- Available variables are listed below, along with default values. A sample file for the default values is available in `default/deploy_gitea_runner.yml.sample` in case you need it for any `group_vars` or `host_vars` configuration. ```yaml -your_defaults_here: default_value # by default, set to default_value +deploy_gitea_runner_version: latest # by default, set to latest ``` -A quick description of the variable, what it does, and how to use it. +This variable defines the version that will be deployed to your host. In case you use `deploy_gitea_runner_deploy_method: host`, this has to match a release version on [gitea act runner repository](https://gitea.com/gitea/act_runner/releases). If using `deploy_gitea_runner_deploy_method: docker`, this has to match a tag on the [gitea act runner docker registry](https://hub.docker.com/r/gitea/act_runner/tags) + +```yaml +deploy_gitea_runner_deploy_method: host # by default, set to host +``` +This variable defines whether the gitea-runner should be deployed as a binary on the host, or as a docker container. This defaults to `host` but can also be `docker`. + +```yaml +deploy_gitea_runner_directory: /opt/gitea-actions # by default, set to /opt/gitea-actions +``` +This variable defines where to store the files for the gitea-runner (config, potential docker-compose, etc...) + +```yaml +deploy_gitea_runner_timezone: "Europe/Paris" # by default, set to Europe/Paris +``` +This variable is only used for if `deploy_gitea_runner_deploy_method` is `docker`, to set the timezone inside the container. + +```yaml +deploy_gitea_runner_register: false # by default, set to false +``` +This variable sets whether or not the role will register the runner against your gitea instance. It will only register if it cannot find the `.runner` file that is generated when registering, and if `deploy_gitea_runner_server_token` is not empty. If `deploy_gitea_runner_deploy_method` is `docker`, this has no impact, since the registration will be handle automatically when to container starts up, given that you have provided a valid URL and token (either via the role's variable, or manually after deploying). + +```yaml +deploy_gitea_runner_start_service: false # by default, set to false +``` +This variable sets whether to start the service immediately or not. In case you manually register the runner after deployment, this should be set to `false`. + +```yaml +deploy_gitea_runner_server_url: https://git.example.com # by default, set to https://git.example.com +``` +This is the url of your gitea instance, and should be resolvable by the runner. + +```yaml +deploy_gitea_runner_server_token: "" # by default, set to an empty string +``` +This is your gitea token. if it isn't set, you cannot run auto-registration. THIS IS A SENSITIVE VALUE, AND SHOULD NOT APPEAR IN CLEAR TEXT IN YOUR REPOSITORY. + +```yaml +deploy_gitea_runner_name: gitea-runner # by default, set to gitea-runner +``` +This is the name under which the runner will register itself against your gitea server. + +```yaml +deploy_gitea_runner_config: # by default, set to the following + log: + level: info + runner: + file: "{{ deploy_gitea_runner_directory }}/.runner" # this HAS TO BE .runner if deploy_gitea_runner_deploy_method is docker + capacity: 1 + timeout: 3h + insecure: false + fetch_timeout: 5s + fetch_interval: 2s + labels: [] + cache: + enabled: true + dir: "{{ deploy_gitea_runner_directory }}/cache" # this HAS TO BE /cache if deploy_gitea_runner_deploy_method is docker + host: + port: 0 + external_server: + container: + network: "" + privileged: false + options: + workdir_parent: + valid_volumes: [] + docker_host: "" + host: + workdir_parent: +``` +This is the config file for gitea, put into a variable. The default values are from the default config.yaml generated when running `act_runner generate-config`. Some of the values, like `cache.dir` and `runner.file` have to be set to specific values in case you're running this role with `deploy_gitea_runner_deploy_method` set to `docker`. The rest is configurable according to the standard documentation. Dependencies ------------ - +None. Example Playbook ---------------- diff --git a/defaults/main.yml b/defaults/main.yml index 33cb2dd..26aa9eb 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,6 +4,7 @@ deploy_gitea_runner_version: latest deploy_gitea_runner_deploy_method: host # deployment method, either host or docker. deploy_gitea_runner_directory: /opt/gitea-actions deploy_gitea_runner_timezone: "Europe/Paris" +deploy_gitea_runner_register: false deploy_gitea_runner_start_service: false deploy_gitea_runner_server_url: https://git.example.com deploy_gitea_runner_server_token: "" @@ -12,7 +13,7 @@ deploy_gitea_runner_config: log: level: info runner: - file: .runner + file: "{{ deploy_gitea_runner_directory }}/.runner" # this HAS TO BE .runner if deploy_gitea_runner_deploy_method is docker capacity: 1 timeout: 3h insecure: false @@ -21,16 +22,16 @@ deploy_gitea_runner_config: labels: [] cache: enabled: true - dir: - host: - port: 0 + dir: "{{ deploy_gitea_runner_directory }}/cache" # this HAS TO BE /cache if deploy_gitea_runner_deploy_method is docker + host: 127.0.0.1 + port: 9898 external_server: container: network: "" privileged: false - options: + options: "" workdir_parent: valid_volumes: [] - docker_host: "" + docker_host: "-" host: - workdir_parent: \ No newline at end of file + workdir_parent: diff --git a/handlers/main.yml b/handlers/main.yml index 0215ee4..353118f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -5,16 +5,16 @@ daemon_reload: true listen: "systemctl-daemon-reload" -- name: "Enable gitea-runnner service" +- name: "Enable gitea-runner service" ansible.builtin.service: - name: gitea-runnner + name: gitea-runner enabled: true - listen: "systemctl-enable-gitea-runnner" + listen: "systemctl-enable-gitea-runner" -- name: "Start gitea-runnner service" +- name: "Start gitea-runner service" ansible.builtin.service: - name: gitea-runnner + name: gitea-runner state: restarted - listen: "systemctl-restart-gitea-runnner" + listen: "systemctl-restart-gitea-runner" throttle: 1 when: deploy_gitea_runner_start_service diff --git a/molecule/with_docker_host/converge.yml b/molecule/with_docker_host/converge.yml new file mode 100644 index 0000000..ea9f7d8 --- /dev/null +++ b/molecule/with_docker_host/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.deploy_gitea_runner" + ansible.builtin.include_role: + name: "ednxzu.deploy_gitea_runner" diff --git a/molecule/with_docker_host/group_vars/all.yml b/molecule/with_docker_host/group_vars/all.yml new file mode 100644 index 0000000..a3c9772 --- /dev/null +++ b/molecule/with_docker_host/group_vars/all.yml @@ -0,0 +1,36 @@ +--- +deploy_gitea_runner_version: latest +deploy_gitea_runner_deploy_method: host # deployment method, either host or docker. +deploy_gitea_runner_directory: /opt/gitea-actions +deploy_gitea_runner_timezone: "Europe/Paris" +deploy_gitea_runner_register: true +deploy_gitea_runner_start_service: true +deploy_gitea_runner_server_url: https://git.example.com +deploy_gitea_runner_server_token: "" +deploy_gitea_runner_name: gitea-runner +deploy_gitea_runner_config: + log: + level: info + runner: + file: "{{ deploy_gitea_runner_directory }}/.runner" # this HAS TO BE .runner if deploy_gitea_runner_deploy_method is docker + capacity: 1 + timeout: 3h + insecure: false + fetch_timeout: 5s + fetch_interval: 2s + labels: ["ubuntu-latest"] + cache: + enabled: true + dir: "{{ deploy_gitea_runner_directory }}/cache" # this HAS TO BE /cache if deploy_gitea_runner_deploy_method is docker + host: 127.0.0.1 + port: 9898 + external_server: + container: + network: "" + privileged: false + options: "" + workdir_parent: + valid_volumes: [] + docker_host: "-" + host: + workdir_parent: diff --git a/molecule/with_docker_host/molecule.yml b/molecule/with_docker_host/molecule.yml new file mode 100644 index 0000000..e89eede --- /dev/null +++ b/molecule/with_docker_host/molecule.yml @@ -0,0 +1,37 @@ +--- +dependency: + name: galaxy + options: + requirements-file: ./requirements.yml +driver: + name: docker +platforms: + - name: instance + image: geerlingguy/docker-${MOLECULE_TEST_OS}-ansible + command: "" + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup + cgroupns_mode: host + privileged: true + pre_build_image: true +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp/.ansible +verifier: + name: ansible +scenario: + name: with_docker_host + test_sequence: + - dependency + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/with_docker_host/verify.yml b/molecule/with_docker_host/verify.yml new file mode 100644 index 0000000..bf52da2 --- /dev/null +++ b/molecule/with_docker_host/verify.yml @@ -0,0 +1,19 @@ +--- +- name: Verify + hosts: all + gather_facts: false + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == 'root' diff --git a/tasks/configure.yml b/tasks/configure.yml index bfa9c23..d721661 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -2,27 +2,14 @@ # task/configure file for deploy_adguard - name: "Copy gitea-runnner.service file" ansible.builtin.template: - src: "gitea-runnner.service.j2" - dest: "/etc/systemd/system/gitea-runnner.service" + src: "gitea-runner.service.j2" + dest: "/etc/systemd/system/gitea-runner.service" owner: root group: root mode: '0644' notify: - systemctl-daemon-reload -- name: "Configure docker-compose deployment" - block: - - name: "Copy docker-compose.yml template" - ansible.builtin.template: - src: "docker-compose.yml.j2" - dest: "{{ deploy_gitea_runner_directory }}/docker-compose.yml" - owner: root - group: root - mode: '0600' - notify: - - systemctl-enable-gitea-runnner - - systemctl-restart-gitea-runnner - - name: "Copy config.yaml" ansible.builtin.template: src: "config.yaml.j2" @@ -32,4 +19,5 @@ mode: '0600' when: deploy_gitea_runner_config != {} notify: - - systemctl-restart-gitea-runnner + - systemctl-enable-gitea-runner + - systemctl-restart-gitea-runner diff --git a/tasks/install_docker.yml b/tasks/install_docker.yml index db1e30c..6f2b030 100644 --- a/tasks/install_docker.yml +++ b/tasks/install_docker.yml @@ -1,2 +1,14 @@ --- # task/install_docker file for deploy_gitea_runner +- name: "Configure docker-compose deployment" + block: + - name: "Copy docker-compose.yml template" + ansible.builtin.template: + src: "docker-compose.yml.j2" + dest: "{{ deploy_gitea_runner_directory }}/docker-compose.yml" + owner: root + group: root + mode: '0600' + notify: + - systemctl-enable-gitea-runner + - systemctl-restart-gitea-runner diff --git a/tasks/install_host.yml b/tasks/install_host.yml index 3c7d504..509b049 100644 --- a/tasks/install_host.yml +++ b/tasks/install_host.yml @@ -46,6 +46,8 @@ owner: root group: root mode: '0755' + notify: + - systemctl-restart-gitea-runner - name: "Update version file" ansible.builtin.copy: diff --git a/tasks/main.yml b/tasks/main.yml index 60f2522..d668e36 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -8,3 +8,8 @@ - name: "Import configure.yml" ansible.builtin.include_tasks: configure.yml + +- name: "Import register.yml" + ansible.builtin.include_tasks: register.yml + when: deploy_gitea_runner_register + and deploy_gitea_runner_deploy_method == 'host' diff --git a/tasks/prerequisites.yml b/tasks/prerequisites.yml index 6488383..2ed2c03 100644 --- a/tasks/prerequisites.yml +++ b/tasks/prerequisites.yml @@ -15,10 +15,11 @@ owner: root group: root mode: '0755' + when: deploy_gitea_runner_deploy_method == "docker" - name: "Create gitea_actions/cache directory" ansible.builtin.file: - path: "{{ deploy_gitea_runner_directory }}/cache" + path: "{{ deploy_gitea_runner_config['cache']['dir'] if deploy_gitea_runner_deploy_method == 'host' else deploy_gitea_runner_directory + '/data' }}" state: directory owner: root group: root diff --git a/tasks/register.yml b/tasks/register.yml new file mode 100644 index 0000000..bb7d678 --- /dev/null +++ b/tasks/register.yml @@ -0,0 +1,28 @@ +--- +# task/register file for deploy_gitea_runner +- name: "Verify runner isnt already register" + ansible.builtin.stat: + path: "{{ deploy_gitea_runner_directory }}/.runner" + register: stat_gitea_runner_file + +- name: "Register gitea-runner" + when: not stat_gitea_runner_file.stat.exists and + not deploy_gitea_runner_deploy_method == 'docker' and + not deploy_gitea_runner_server_token == '' + block: + - name: "Register gitea-runner" + ansible.builtin.command: "{{ deploy_gitea_runner_path }} register --no-interactive --instance $GITEA_URL --token $GITEA_TOKEN --name $GITEA_RUNNER_NAME --config $CONFIG_FILE" + environment: + GITEA_URL: "{{ deploy_gitea_runner_server_url }}" + GITEA_TOKEN: "{{ deploy_gitea_runner_server_token }}" + GITEA_RUNNER_NAME: "{{ deploy_gitea_runner_name }}" + CONFIG_FILE: "{{ deploy_gitea_runner_directory }}/config.yaml" + changed_when: false + register: register_gitea_runner + + - name: "Ensure registration is successful" + ansible.builtin.assert: + that: + - register_gitea_runner.rc == 0 + success_msg: "Runner {{ deploy_gitea_runner_name }} registered correctly !" + fail_msg: "Runner {{ deploy_gitea_runner_name }} Did not register correctly: {{ register_gitea_runner.stderr }}" diff --git a/templates/config.yaml.j2 b/templates/config.yaml.j2 index b388546..5da3552 100644 --- a/templates/config.yaml.j2 +++ b/templates/config.yaml.j2 @@ -1,2 +1,2 @@ # {{ ansible_managed }} -{{ deploy_gitea_runner_config | to_nice_yaml }} \ No newline at end of file +{{ deploy_gitea_runner_config | to_yaml }} diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 index 4123b52..6303141 100644 --- a/templates/docker-compose.yml.j2 +++ b/templates/docker-compose.yml.j2 @@ -3,7 +3,7 @@ version: '3.9' services: runner: - image: gitea/act_runner:latest + image: gitea/act_runner:{{ deploy_gitea_runner_version }} configs: - source: config_yaml target: /config.yaml diff --git a/templates/gitea-runner.service.j2 b/templates/gitea-runner.service.j2 new file mode 100644 index 0000000..699a546 --- /dev/null +++ b/templates/gitea-runner.service.j2 @@ -0,0 +1,29 @@ +{% if deploy_gitea_runner_deploy_method == 'docker' %} +[Unit] +Description=Act runner is a runner for Gitea based on Gitea fork of act. +After=docker.service +Requires=docker.service + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml up --detach" +ExecStop=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml down" + +[Install] +WantedBy=multi-user.target +{% elif deploy_gitea_runner_deploy_method == 'host' %} +[Unit] +Description=Act runner is a runner for Gitea based on Gitea fork of act. +After=network.target +Requires=network.target + +[Service] +Type=simple +EnvironmentFile=-{{ deploy_gitea_runner_directory }}/gitea-runner.env +WorkingDirectory={{ deploy_gitea_runner_directory }} +ExecStart={{ deploy_gitea_runner_path }} daemon + +[Install] +WantedBy=multi-user.target +{% endif %} \ No newline at end of file diff --git a/templates/gitea-runnner.service.j2 b/templates/gitea-runnner.service.j2 deleted file mode 100644 index 82546b0..0000000 --- a/templates/gitea-runnner.service.j2 +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=Act runner is a runner for Gitea based on Gitea fork of act. -After=docker.service -Requires=docker.service - -[Service] -Type=oneshot -RemainAfterExit=yes -ExecStart=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml up --detach" -ExecStop=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml down" - -[Install] -WantedBy=multi-user.target \ No newline at end of file