From 7cee7156bcde8bc396ac4b6581bf2cae02eea0e9 Mon Sep 17 00:00:00 2001 From: Tobias Henkel Date: Wed, 24 Oct 2018 09:05:14 +0200 Subject: [PATCH] Add prepare-workspace-git role Currently the incremental push of the git repos to the nodes is split into a part in project-config and a part in zuul-jobs and only works within the OpenStack context. The prepare-workspace-git role is an attempt to make this generically available in zuul-jobs. It first checks for each needed project if it's cached. If yes it's cloned to the destination, if not it's just initialized as an empty git repo. After that we can generically use git operations to sync the correct state of the repos to the destination. Change-Id: I87938a02d51e561b25ca2cb20f53d62f3cd3ae73 --- roles/prepare-workspace-git/README.rst | 17 ++++++ .../prepare-workspace-git/defaults/main.yaml | 1 + roles/prepare-workspace-git/tasks/main.yaml | 56 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 roles/prepare-workspace-git/README.rst create mode 100644 roles/prepare-workspace-git/defaults/main.yaml create mode 100644 roles/prepare-workspace-git/tasks/main.yaml diff --git a/roles/prepare-workspace-git/README.rst b/roles/prepare-workspace-git/README.rst new file mode 100644 index 000000000..e1e47118e --- /dev/null +++ b/roles/prepare-workspace-git/README.rst @@ -0,0 +1,17 @@ +Mirror the local git repos to remote nodes + +This role uses git operations (unlike :zuul:role:`prepare-workspace` +which uses rsync) to mirror the locally prepared git repos to the remote +nodes while taking advantage of cached repos on the node if they exist. +This role works generically regardless of the existence of a cached +repo on the node. + +The cached repos need to be placed using the canonical name under the +`cached_repos_root` directory. + +**Role Variables** + +.. zuul:rolevar:: cached_repos_root + :default: /opt/git + + The root of the cached repos. diff --git a/roles/prepare-workspace-git/defaults/main.yaml b/roles/prepare-workspace-git/defaults/main.yaml new file mode 100644 index 000000000..b0c54a382 --- /dev/null +++ b/roles/prepare-workspace-git/defaults/main.yaml @@ -0,0 +1 @@ +cached_repos_root: /opt/git diff --git a/roles/prepare-workspace-git/tasks/main.yaml b/roles/prepare-workspace-git/tasks/main.yaml new file mode 100644 index 000000000..89922cdde --- /dev/null +++ b/roles/prepare-workspace-git/tasks/main.yaml @@ -0,0 +1,56 @@ +- name: Find locally cached git repos + stat: + path: "{{ cached_repos_root }}/{{ item.canonical_name }}" + with_items: "{{ zuul.projects.values() | list }}" + register: cached_repos + +# We do a bare clone here first so that we skip creating a working copy that +# will be overwritten later anyway. +- name: Clone cached repo to workspace + shell: | + set -e + git clone --bare {{ cached_repos_root }}/{{ item.0.canonical_name }} {{ ansible_user_dir }}/{{ item.0.src_dir}}/.git + cd {{ ansible_user_dir }}/{{ item.0.src_dir }} + git config --local --bool core.bare false + args: + creates: "{{ ansible_user_dir }}/{{ item.0.src_dir}}" + when: item.1.stat.exists + with_together: + - "{{ zuul.projects.values() | list }}" + - "{{ cached_repos.results }}" + # ANSIBLE0006: If we use the git module, we get warning + # ANSIBLE0004 since we do not give an explicit version + tags: + - skip_ansible_lint + +- name: Initialize non-cached repos + command: "git init {{ ansible_user_dir }}/{{ item.0.src_dir}}" + args: + creates: "{{ ansible_user_dir }}/{{ item.0.src_dir}}" + when: not item.1.stat.exists + with_together: + - "{{ zuul.projects.values() | list }}" + - "{{ cached_repos.results }}" + # ANSIBLE0006: If we use the git module, we get warning + # ANSIBLE0004 since we do not give an explicit version + tags: + - skip_ansible_lint + +- name: Remove origin from local git repos and replace it by the zuul fake origin + # To be idempotent, remove origin only if it's found in the local list. + shell: | + set -e + git remote -v | grep origin && git remote rm origin || true + git remote add origin file:///dev/null + args: + chdir: "{{ ansible_user_dir }}/{{ item.src_dir}}" + with_items: "{{ zuul.projects.values() | list }}" + # ANSIBLE0006: git remote is not supported by ansible module + tags: + - skip_ansible_lint + +# TODO(tobiash): we might want to deprecate the role mirror-workspace-git-repos +# and move it here. +- name: Synchronize repos + import_role: + name: mirror-workspace-git-repos