From a7e949b65673f1cd38da2ae7496c75ca4bc7c222 Mon Sep 17 00:00:00 2001
From: Kristian Klausen <kristian@klausen.dk>
Date: Sun, 27 Oct 2024 14:03:21 +0100
Subject: [PATCH] gitlab_runner: Move VM memory to a variable instead of
 hardcoding it twice

It was forgotten once[1] to update it in both places, so avoid that
issue in the future, by moving it to a variable.

[1] c370c9d0 ("gitlab_runner: Update concurreny math to reflect the new VM size")
---
 roles/gitlab_runner/defaults/main.yml                         | 1 +
 roles/gitlab_runner/tasks/main.yml                            | 4 +++-
 roles/gitlab_runner/templates/config.toml.j2                  | 2 +-
 .../domain_template.xml => templates/domain_template.xml.j2}  | 2 +-
 4 files changed, 6 insertions(+), 3 deletions(-)
 create mode 100644 roles/gitlab_runner/defaults/main.yml
 rename roles/gitlab_runner/{files/domain_template.xml => templates/domain_template.xml.j2} (94%)

diff --git a/roles/gitlab_runner/defaults/main.yml b/roles/gitlab_runner/defaults/main.yml
new file mode 100644
index 000000000..e69f3dad2
--- /dev/null
+++ b/roles/gitlab_runner/defaults/main.yml
@@ -0,0 +1 @@
+gitlab_runner_libvirt_vm_memory: 2048
diff --git a/roles/gitlab_runner/tasks/main.yml b/roles/gitlab_runner/tasks/main.yml
index 71eee4da4..1662bc4ea 100644
--- a/roles/gitlab_runner/tasks/main.yml
+++ b/roles/gitlab_runner/tasks/main.yml
@@ -79,10 +79,12 @@
       copy: src={{ item.src }} dest={{ item.dest }} owner=root group=root mode={{ item.mode }}
       loop:
         - {src: arch-boxes.asc, dest: /usr/local/lib/libvirt-executor/, mode: 644}
-        - {src: domain_template.xml, dest: /usr/local/lib/libvirt-executor/, mode: 644}
         - {src: libvirt-executor, dest: /usr/local/bin/, mode: 755}
         - {src: libvirt-executor-update-base-image, dest: /usr/local/bin/, mode: 755}
 
+    - name: Install libvirt-executor domain template
+      template: src=domain_template.xml.j2 dest=/usr/local/lib/libvirt-executor/domain_template.xml owner=root group=root mode=0644
+
     - name: Create SSH keys for libvirt-executor
       command: ssh-keygen -N "" -f /etc/libvirt-executor/id_ed25519 -t ed25519
       args:
diff --git a/roles/gitlab_runner/templates/config.toml.j2 b/roles/gitlab_runner/templates/config.toml.j2
index 6f8fee713..03cc16f4c 100644
--- a/roles/gitlab_runner/templates/config.toml.j2
+++ b/roles/gitlab_runner/templates/config.toml.j2
@@ -32,7 +32,7 @@ listen_address = ":9252"
   executor = "custom"
   builds_dir = "/builds"
   cache_dir = "/cache"
-  limit = {{ (ansible_memtotal_mb * 0.9 / 2048) | round | int }}
+  limit = {{ (ansible_memtotal_mb * 0.9 / gitlab_runner_libvirt_vm_memory) | round | int }}
   environment = ["ARCHIVER_STAGING_DIR=/var/tmp"]
   [runners.custom]
     prepare_exec = "/usr/local/bin/libvirt-executor"
diff --git a/roles/gitlab_runner/files/domain_template.xml b/roles/gitlab_runner/templates/domain_template.xml.j2
similarity index 94%
rename from roles/gitlab_runner/files/domain_template.xml
rename to roles/gitlab_runner/templates/domain_template.xml.j2
index 80ca49828..6ce09333d 100644
--- a/roles/gitlab_runner/files/domain_template.xml
+++ b/roles/gitlab_runner/templates/domain_template.xml.j2
@@ -1,6 +1,6 @@
 <domain type='kvm'>
   <name>$vm_name</name>
-  <memory unit='MiB'>2048</memory>
+  <memory unit='MiB'>{{ gitlab_runner_libvirt_vm_memory }}</memory>
   <vcpu>4</vcpu>
   <os>
     <type arch='x86_64' machine='q35'>hvm</type>
-- 
GitLab