From 4aca54018a93ab6d2a8500243f26784b1a46dde7 Mon Sep 17 00:00:00 2001 From: Michele Baldessari Date: Tue, 12 Nov 2024 20:29:33 +0100 Subject: [PATCH] Add an imperative job to create the manuela-dev repo in gitea This leverages the imperative framework with its default settings. This means it will run every 10 minutes and is idempotent, meaning that if the manuela-dev repo does not exist in gitea it will be cloned from upstream, but if it exists nothing will be done. Co-Authored-By: Aros Ekos --- .../playbooks/clone_upstream_repos_gitea.yml | 59 +++++++++++++++++++ values-datacenter.yaml | 12 ++++ 2 files changed, 71 insertions(+) create mode 100644 ansible/playbooks/clone_upstream_repos_gitea.yml diff --git a/ansible/playbooks/clone_upstream_repos_gitea.yml b/ansible/playbooks/clone_upstream_repos_gitea.yml new file mode 100644 index 000000000..1078bdd55 --- /dev/null +++ b/ansible/playbooks/clone_upstream_repos_gitea.yml @@ -0,0 +1,59 @@ +--- +- name: Industrial repo cloning + hosts: localhost + connection: local + gather_facts: false + tasks: + - name: Fail if any needed parameter is undefined + ansible.builtin.assert: + that: + - repo_name is defined + - upstream_repo_url is defined + + - name: Find needed route + ansible.builtin.shell: | + oc get routes -n vp-gitea gitea-route -o jsonpath='{.spec.host}' + register: route_raw + + - name: Find gitea_admin username + ansible.builtin.shell: | + oc extract -n vp-gitea secret/gitea-admin-secret --to=- --keys=username 2>/dev/null + register: gitea_username_raw + + - name: Find gitea_admin password + ansible.builtin.shell: | + oc extract -n vp-gitea secret/gitea-admin-secret --to=- --keys=password 2>/dev/null + register: gitea_password_raw + + - name: Set some basic facts + ansible.builtin.set_fact: + gitea_route: "https://{{ route_raw.stdout }}" + gitea_username: "{{ gitea_username_raw.stdout }}" + gitea_password: "{{ gitea_password_raw.stdout }}" + + - name: Set some more route facts + ansible.builtin.set_fact: + gitea_repos_route: "{{ gitea_route }}/api/v1/repos/{{ gitea_username }}/{{ repo_name }}" + gitea_migrate_route: "{{ gitea_route }}/api/v1/repos/migrate" + + - name: Does repo already exist + ansible.builtin.uri: + url: "{{ gitea_repos_route }}" + register: repo_exists + failed_when: false + + - name: If the repo already exists then stop here + ansible.builtin.meta: end_play + when: repo_exists.status == 200 + + - name: Migrate repository + ansible.builtin.uri: + url: "{{ gitea_migrate_route }}" + user: "{{ gitea_username }}" + password: "{{ gitea_password }}" + force_basic_auth: true + method: POST + body_format: json + body: "{ \"repo_name\": \"{{ repo_name }}\", \"clone_addr\": \"{{ upstream_repo_url }}\" }" + return_content: true + status_code: [201] diff --git a/values-datacenter.yaml b/values-datacenter.yaml index 83d24fdd6..8b72938e0 100644 --- a/values-datacenter.yaml +++ b/values-datacenter.yaml @@ -221,6 +221,18 @@ clusterGroup: # repoURL: https://github.com/vendor/applications.git # targetRevision: main + imperative: + # For additional overrides that apply to the jobs, please refer to + # https://github.com/validatedpatterns/clustergroup-chart/ + # schedule: "*/10 * * * *" # Default schedule + jobs: + - name: clone-upstream-repos-gitea + playbook: ./ansible/playbooks/clone_upstream_repos_gitea.yml + extravars: + - "repo_name=manuela-dev" + - "upstream_repo_url=https://github.com/validatedpatterns-demos/manuela-dev" + timeout: 180 + managedClusterGroups: factory: name: factory