From 7f400a0d329d7bc69fd1eb150a07d162cb5bf872 Mon Sep 17 00:00:00 2001
From: Benedikt Loeffler <benedikt.loeffler@bmw.de>
Date: Tue, 27 Jun 2023 14:21:51 +0200
Subject: [PATCH] Use zuul_workspace_root for prepare/mirror workspace roles

Use zuul_workspace_root as the destination folder for preparing and
mirroring the workspace, which points to ansible_user_dir by default.
(As is currently the case).
The override of this variable can be useful when using a static node
with multiple slots to use a separate workspace for each slot.

Change-Id: If50abc7ccc2dfb6f4ea9f9d5c6a5091ff6967aef
---
 roles/mirror-workspace-git-repos/README.rst         | 5 +++++
 roles/mirror-workspace-git-repos/defaults/main.yaml | 1 +
 roles/mirror-workspace-git-repos/tasks/main.yaml    | 8 ++++----
 roles/prepare-workspace-git/README.rst              | 5 +++++
 roles/prepare-workspace-git/defaults/main.yaml      | 1 +
 roles/prepare-workspace-git/tasks/main.yaml         | 8 ++++----
 6 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/roles/mirror-workspace-git-repos/README.rst b/roles/mirror-workspace-git-repos/README.rst
index e2a1fdac7..118856bc0 100644
--- a/roles/mirror-workspace-git-repos/README.rst
+++ b/roles/mirror-workspace-git-repos/README.rst
@@ -12,3 +12,8 @@ some or all of the git repos.
 
    If `true` git operations will be silenced and won't print every
    changed reference.
+
+.. zuul:rolevar:: zuul_workspace_root
+   :default: "{{ ansible_user_dir }}"
+
+   The root of the workspace.
diff --git a/roles/mirror-workspace-git-repos/defaults/main.yaml b/roles/mirror-workspace-git-repos/defaults/main.yaml
index b59b6d6b5..fa348b50a 100644
--- a/roles/mirror-workspace-git-repos/defaults/main.yaml
+++ b/roles/mirror-workspace-git-repos/defaults/main.yaml
@@ -1 +1,2 @@
 mirror_workspace_quiet: false
+zuul_workspace_root: "{{ ansible_user_dir }}"
diff --git a/roles/mirror-workspace-git-repos/tasks/main.yaml b/roles/mirror-workspace-git-repos/tasks/main.yaml
index a782f6101..54398b22c 100644
--- a/roles/mirror-workspace-git-repos/tasks/main.yaml
+++ b/roles/mirror-workspace-git-repos/tasks/main.yaml
@@ -3,7 +3,7 @@
     name: receive.denyCurrentBranch
     value: ignore
     scope: local
-    repo: "{{ ansible_user_dir }}/{{ zj_project.value.src_dir }}"
+    repo: "{{ zuul_workspace_root }}/{{ zj_project.value.src_dir }}"
   with_dict: "{{ zuul.projects }}"
   loop_control:
     loop_var: zj_project
@@ -11,9 +11,9 @@
 - name: Synchronize src repos to workspace directory
   command: |-
     {% if ansible_connection == "kubectl" %}
-      git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror "ext::kubectl --context {{ zuul.resources[inventory_hostname].context }} -n {{ zuul.resources[inventory_hostname].namespace }} exec -i {{ zuul.resources[inventory_hostname].pod }} -- %S {{ ansible_user_dir }}/{{ zj_project.value.src_dir }}"
+      git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror "ext::kubectl --context {{ zuul.resources[inventory_hostname].context }} -n {{ zuul.resources[inventory_hostname].namespace }} exec -i {{ zuul.resources[inventory_hostname].pod }} -- %S {{ zuul_workspace_root }}/{{ zj_project.value.src_dir }}"
     {% else %}
-      git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror git+ssh://{{ ansible_user }}@{{ ansible_host | ipwrap }}:{{ ansible_port }}/{{ ansible_user_dir }}/{{ zj_project.value.src_dir }}
+      git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror git+ssh://{{ ansible_user }}@{{ ansible_host | ipwrap }}:{{ ansible_port }}/{{ zuul_workspace_root }}/{{ zj_project.value.src_dir }}
     {% endif %}
   args:
     chdir: "{{ zuul.executor.work_root }}/{{ zj_project.value.src_dir }}"
@@ -52,7 +52,7 @@
     echo "{{ zj_project.value.canonical_name }} checked out to:"
     git log --pretty=oneline  -1
   args:
-    chdir: "{{ ansible_user_dir }}/{{ zj_project.value.src_dir }}"
+    chdir: "{{ zuul_workspace_root }}/{{ zj_project.value.src_dir }}"
   with_dict: "{{ zuul.projects }}"
   loop_control:
     loop_var: zj_project
diff --git a/roles/prepare-workspace-git/README.rst b/roles/prepare-workspace-git/README.rst
index e1e47118e..4e23c5761 100644
--- a/roles/prepare-workspace-git/README.rst
+++ b/roles/prepare-workspace-git/README.rst
@@ -15,3 +15,8 @@ The cached repos need to be placed using the canonical name under the
    :default: /opt/git
 
    The root of the cached repos.
+
+.. zuul:rolevar:: zuul_workspace_root
+   :default: "{{ ansible_user_dir }}"
+
+   The root of the workspace in which the repos are mirrored.
diff --git a/roles/prepare-workspace-git/defaults/main.yaml b/roles/prepare-workspace-git/defaults/main.yaml
index b0c54a382..032db3c2a 100644
--- a/roles/prepare-workspace-git/defaults/main.yaml
+++ b/roles/prepare-workspace-git/defaults/main.yaml
@@ -1 +1,2 @@
 cached_repos_root: /opt/git
+zuul_workspace_root: "{{ ansible_user_dir }}"
diff --git a/roles/prepare-workspace-git/tasks/main.yaml b/roles/prepare-workspace-git/tasks/main.yaml
index ad7da04bf..d1ff3717d 100644
--- a/roles/prepare-workspace-git/tasks/main.yaml
+++ b/roles/prepare-workspace-git/tasks/main.yaml
@@ -7,16 +7,16 @@
     if [ -d "{{ cached_repos_root }}/{{ zj_project.canonical_name }}" ] ; then
         # We do a bare clone here first so that we skip creating a working
         # copy that will be overwritten later anyway.
-        git clone --bare {{ cached_repos_root }}/{{ zj_project.canonical_name }} {{ ansible_user_dir }}/{{ zj_project.src_dir }}/.git
+        git clone --bare {{ cached_repos_root }}/{{ zj_project.canonical_name }} {{ zuul_workspace_root }}/{{ zj_project.src_dir }}/.git
     else
-        git init {{ ansible_user_dir }}/{{ zj_project.src_dir }}
+        git init {{ zuul_workspace_root }}/{{ zj_project.src_dir }}
     fi
-    cd {{ ansible_user_dir }}/{{ zj_project.src_dir }}
+    cd {{ zuul_workspace_root }}/{{ zj_project.src_dir }}
     git config --local --bool core.bare false
     git remote -v | grep origin && git remote rm origin || true
     git remote add origin file:///dev/null
   args:
-    creates: "{{ ansible_user_dir }}/{{ zj_project.src_dir }}"
+    creates: "{{ zuul_workspace_root }}/{{ zj_project.src_dir }}"
   with_items: "{{ zuul.projects.values() | list }}"
   loop_control:
     loop_var: zj_project