diff --git a/playbooks/remote_puppet_git.yaml b/playbooks/remote_puppet_git.yaml index 88980fd4e8..a05e2f6286 100644 --- a/playbooks/remote_puppet_git.yaml +++ b/playbooks/remote_puppet_git.yaml @@ -20,6 +20,14 @@ facts: project_config_ref: "{{ hostvars.localhost.gitinfo.after }}" +- hosts: "gitea:!disabled" + name: "Create repos on gitea servers" + strategy: free + max_fail_percentage: 1 + roles: + - role: gitea-git-repos + project_config_ref: "{{ hostvars.localhost.gitinfo.after }}" + - hosts: "review:!disabled" name: "Puppet-git: Run puppet on the Gerrit server" strategy: free diff --git a/playbooks/roles/gitea-git-repos/README.rst b/playbooks/roles/gitea-git-repos/README.rst new file mode 100644 index 0000000000..6770339df7 --- /dev/null +++ b/playbooks/roles/gitea-git-repos/README.rst @@ -0,0 +1 @@ +Create git repos on a gitea server diff --git a/playbooks/roles/gitea-git-repos/defaults/main.yaml b/playbooks/roles/gitea-git-repos/defaults/main.yaml new file mode 100644 index 0000000000..8b811e9b93 --- /dev/null +++ b/playbooks/roles/gitea-git-repos/defaults/main.yaml @@ -0,0 +1,3 @@ +gitea_url: https://localhost:3000 +project_config_ref: master +project_config_dest: /tmp/project_config diff --git a/playbooks/roles/gitea-git-repos/tasks/main.yaml b/playbooks/roles/gitea-git-repos/tasks/main.yaml new file mode 100644 index 0000000000..e052afae65 --- /dev/null +++ b/playbooks/roles/gitea-git-repos/tasks/main.yaml @@ -0,0 +1,37 @@ +- name: Clone project-config + git: + repo: https://git.openstack.org/openstack-infra/project-config + dest: "{{ project_config_dest }}" + version: "{{ project_config_ref }}" +- name: Get Gerrit project list + set_fact: + gerrit_projects: "{{ lookup('file', project_config_dest + '/gerrit/projects.yaml') | from_yaml }}" +- name: Parse Gerrit org list + set_fact: + gerrit_orgs: "{{ gerrit_projects | map(attribute='project') | map('regex_search', '^(.*?)/') | list | unique | select | map('regex_replace', '/', '') | list }}" +- name: debug + debug: + msg: "{{ gerrit_orgs }}" +- name: Get Gitea org list + # We assume that all the orgs we are interested in are owned by root + uri: + url: "{{ gitea_url }}/api/v1/user/orgs" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 200 + register: gitea_org_list +- name: Parse Gitea org list + set_fact: + gitea_orgs: "{{ gitea_org_list.json | map(attribute='username') | list }}" +- name: Create orgs + loop: "{{ gerrit_orgs }}" + loop_control: + loop_var: org + include_tasks: 'setup-org.yaml' +- name: Create repos + loop: "{{ gerrit_projects }}" + loop_control: + loop_var: project + include_tasks: 'setup-repo.yaml' diff --git a/playbooks/roles/gitea-git-repos/tasks/setup-org.yaml b/playbooks/roles/gitea-git-repos/tasks/setup-org.yaml new file mode 100644 index 0000000000..5a40b83e27 --- /dev/null +++ b/playbooks/roles/gitea-git-repos/tasks/setup-org.yaml @@ -0,0 +1,56 @@ +- name: Process org + debug: + msg: "Processing org {{ org }}" +- name: Create org + when: org not in gitea_orgs + uri: + url: "{{ gitea_url }}/api/v1/admin/users/root/orgs" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 201 + method: POST + body_format: json + body: + username: "{{ org }}" +- name: Get org team list + uri: + url: "{{ gitea_url }}/api/v1/orgs/{{ org }}/teams" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 200 + register: gitea_org_team_list +- name: Get org owners + uri: + url: "{{ gitea_url }}/api/v1/teams/{{ (gitea_org_team_list.json | selectattr('name', 'equalto', 'Owners') | list)[0]['id'] }}/members" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 200 + register: gitea_org_members +- name: Add Gerrit user to org + when: "'gerrit' not in gitea_org_members.json | map(attribute='username')" + uri: + url: "{{ gitea_url }}/api/v1/teams/{{ (gitea_org_team_list.json | selectattr('name', 'equalto', 'Owners') | list)[0]['id'] }}/members/gerrit" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 204 + method: PUT +- name: Get org repo list + uri: + url: "{{ gitea_url }}/api/v1/orgs/{{ org }}/repos" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 200 + register: gitea_org_repo_list +- name: Parse org repo list + set_fact: + gitea_org_repos: "{{ gitea_org_repo_list.json | map(attribute='name') | list }}" diff --git a/playbooks/roles/gitea-git-repos/tasks/setup-repo.yaml b/playbooks/roles/gitea-git-repos/tasks/setup-repo.yaml new file mode 100644 index 0000000000..fdc8d9fcf6 --- /dev/null +++ b/playbooks/roles/gitea-git-repos/tasks/setup-repo.yaml @@ -0,0 +1,41 @@ +- name: debug + debug: + msg: "{{ project }}" +- name: Parse project name + set_fact: + org: "{{ project.project | regex_replace('^(.*)/(.*)$', '\\1') }}" + repo: "{{ project.project | regex_replace('^(.*)/(.*)$', '\\2') }}" +- name: Create repo + when: repo not in gitea_org_repos + uri: + url: "{{ gitea_url }}/api/v1/org/{{ org }}/repos" + user: root + password: "{{ gitea_root_password }}" + force_basic_auth: true + validate_certs: false + status_code: 201 + method: POST + body_format: json + body: + auto_init: false + description: "{{ project.description | default('') }}" + name: "{{ repo }}" + private: false + register: create_repo_result +- name: Get created repo id + when: "create_repo_result.json is defined" + set_fact: + repo_id: "{{ create_repo_result.json['id'] }}" +- name: Prepare sql query + when: "repo_id is defined" + set_fact: + sql_statement: | + start transaction; + delete from repo_unit where repo_id = {{ repo_id }} and `type` in (2, 3, 5, 7); + insert into repo_unit (repo_id, `type`, config, created_unix) values ({{ repo_id }}, 7, "{""ExternalTrackerURL"":""https://storyboard.openstack.org/#!/project/{{ org }}/{{ repo }}"",""ExternalTrackerFormat"":""https://storyboard.openstack.org/#!/story/{index}"",""ExternalTrackerStyle"":""""}", unix_timestamp()); + commit; +- name: Adjust repo settings + when: "sql_statement is defined" + become: true + command: | + docker exec giteadocker_mariadb_1 bash -c 'mysql --password="$MYSQL_ROOT_PASSWORD" -e "{{ sql_statement }}" gitea'