195 lines
7.7 KiB
YAML
195 lines
7.7 KiB
YAML
---
|
|
- name: Setup Gitea
|
|
hosts: gitea:&hard_disk
|
|
gather_facts: yes
|
|
become: false
|
|
run_once: true
|
|
|
|
vars:
|
|
applications: "{{ hard_disk__applications }}"
|
|
|
|
tasks:
|
|
- name: Deploy gitea Docker Compose configuration
|
|
include_role:
|
|
name: arcodange.factory.deploy_docker_compose
|
|
vars:
|
|
app_name: "{{ app.name }}"
|
|
dockercompose_content: "{{ app.conf.dockercompose }}"
|
|
partition: "{{ app.conf.partition }}"
|
|
app_owner: "{{ app.conf.owner | default('pi') }}"
|
|
app_group: "{{ app.conf.group | default('docker') }}"
|
|
loop: "{{ applications | dict2items(key_name='name', value_name='conf') }}"
|
|
loop_control:
|
|
loop_var: app
|
|
label: "{{ app.name }}"
|
|
when: app.name == 'gitea'
|
|
|
|
- name: Deploy Gitea
|
|
include_role:
|
|
name: deploy_gitea
|
|
vars:
|
|
app_name: gitea
|
|
partition: "{{ applications.gitea.partition }}"
|
|
gitea_container_name: "{{ applications.gitea.dockercompose.services.gitea.container_name }}"
|
|
postgres_host: |-
|
|
{{ applications.gitea.dockercompose.services.gitea.environment.GITEA__database__HOST }}
|
|
postgres_db: |-
|
|
{{ applications.gitea.dockercompose.services.gitea.environment.GITEA__database__NAME }}
|
|
postgres_user: |-
|
|
{{ applications.gitea.dockercompose.services.gitea.environment.GITEA__database__USER }}
|
|
postgres_password: |-
|
|
{{ applications.gitea.dockercompose.services.gitea.environment.GITEA__database__PASSWD }}
|
|
|
|
- name: Create admin user
|
|
block:
|
|
- name: Use default user identity
|
|
when: gitea_user is undefined or gitea_user.name is undefined or gitea_user.email is undefined
|
|
ansible.builtin.set_fact:
|
|
gitea_base_url: http://{{ inventory_hostname }}:3000
|
|
gitea_user:
|
|
name: arcodange
|
|
email: arcodange@gmail.com
|
|
avatar_img_path: '{{ inventory_dir }}/../img/arcodange.jpeg'
|
|
|
|
- name: List admin users
|
|
ansible.builtin.shell:
|
|
cmd: >-
|
|
docker exec -u git {{ applications.gitea.dockercompose.services.gitea.container_name }}
|
|
gitea admin user list --admin
|
|
| awk '{print $2}'
|
|
| tail -n +2
|
|
# tail -n +2 skips the 2-1 ("N-1" with N=2) first lines (so the first line returned by the command)
|
|
register:
|
|
gitea_admin_users_list_cmd
|
|
|
|
- name: Create admin user
|
|
when: gitea_user.name not in gitea_admin_users_list_cmd.stdout.split()
|
|
ansible.builtin.command: >-
|
|
docker exec -u git {{ applications.gitea.dockercompose.services.gitea.container_name }}
|
|
gitea admin user create
|
|
--username {{ gitea_user.name }}
|
|
--email {{ gitea_user.name }}
|
|
--random-password
|
|
|
|
- name: Generate Gitea Token
|
|
include_role:
|
|
name: arcodange.factory.gitea_token
|
|
|
|
- name: Lecture bases64 de l'avatar de l'admin
|
|
local_action:
|
|
module: slurp
|
|
path: '{{ gitea_user.avatar_img_path | realpath }}'
|
|
register:
|
|
gitea_user_avatar_img
|
|
no_log: True
|
|
|
|
- name: Upload l'avatar de l'admin
|
|
uri:
|
|
url: "{{ gitea_base_url }}/api/v1/user/avatar"
|
|
method: POST
|
|
headers:
|
|
Content-Type: application/json
|
|
Accept: application/json
|
|
Authorization: "token {{ gitea_api_token }}"
|
|
body_format: json
|
|
body:
|
|
image: "{{ gitea_user_avatar_img['content'] }}"
|
|
status_code: 204
|
|
|
|
- name: Enregistrer sa clée publique SSH
|
|
when: ssh_public_key_found
|
|
block:
|
|
- name: Récupérer les clés SSH existantes pour l'utilisateur
|
|
uri:
|
|
url: "{{ gitea_base_url }}/api/v1/user/keys"
|
|
method: GET
|
|
headers:
|
|
Authorization: "token {{ gitea_api_token }}"
|
|
return_content: yes
|
|
register: existing_ssh_keys
|
|
|
|
- name: Vérifier si la clé SSH existe déjà
|
|
set_fact:
|
|
ssh_key_exists: "{{ ' '.join([ssh_key_algorithm, ssh_public_key]) in existing_ssh_keys.json | map(attribute='key') | list }}"
|
|
|
|
- name: Ajouter une clé SSH publique à l'utilisateur dans Gitea si elle n'existe pas
|
|
uri:
|
|
url: "{{ gitea_base_url }}/api/v1/user/keys"
|
|
method: POST
|
|
headers:
|
|
Content-Type: application/json
|
|
Accept: application/json
|
|
Authorization: "token {{ gitea_api_token }}"
|
|
body_format: json
|
|
body:
|
|
title: "{{ ssh_key_title }}" # Un nom descriptif pour la clé SSH
|
|
key: "{{ ssh_public_key }}" # Le contenu de la clé publique SSH
|
|
status_code: 201
|
|
when: not ssh_key_exists
|
|
register: add_ssh_key_result
|
|
|
|
- name: Vérifier la nouvelle clé SSH ajoutée avec succès
|
|
when: not ssh_key_exists and add_ssh_key_result.status == 201
|
|
debug:
|
|
msg: >-
|
|
Clé SSH ajoutée avec succès.
|
|
Visitez https://gitea.arcodange.duckdns.org/user/settings/keys?verify_ssh={{ add_ssh_key_result.json.fingerprint }}
|
|
pour vérifier la signature de vos commits avec cette clé.
|
|
|
|
- set_fact:
|
|
gitea_org_name: arcodange-org
|
|
gitea_org_full_name: Arcodange
|
|
gitea_org_description: '🏹💻🪽'
|
|
gitea_org_website: https://www.arcodange.duckdns.org
|
|
gitea_org_location: Paris
|
|
gitea_org_avatar_img_path: '{{ inventory_dir }}/../img/arcodange-org.jpeg'
|
|
|
|
- name: Vérifier si l'organisation existe dans Gitea
|
|
uri:
|
|
url: "{{ gitea_base_url }}/api/v1/orgs/{{ gitea_org_name }}"
|
|
method: GET
|
|
headers:
|
|
Authorization: "token {{ gitea_api_token }}"
|
|
status_code: 200
|
|
register: gitea_org_check
|
|
ignore_errors: yes
|
|
|
|
- when: gitea_org_check.status != 200
|
|
block:
|
|
- name: Créer l'organisation dans Gitea quand elle n'existe pas
|
|
uri:
|
|
url: "{{ gitea_base_url }}/api/v1/orgs"
|
|
method: POST
|
|
headers:
|
|
Content-Type: application/json
|
|
Accept: application/json
|
|
Authorization: "token {{ gitea_api_token }}"
|
|
body_format: json
|
|
body:
|
|
username: "{{ gitea_org_name }}"
|
|
full_name: "{{ gitea_org_full_name }}"
|
|
description: "{{ gitea_org_description }}"
|
|
visibility: public
|
|
website: "{{ gitea_org_website }}"
|
|
location: "{{ gitea_org_location }}"
|
|
status_code: 201
|
|
|
|
- name: Lecture bases64 de l'avatar de l'organization
|
|
local_action:
|
|
module: slurp
|
|
path: '{{ gitea_org_avatar_img_path | realpath }}'
|
|
register:
|
|
gitea_org_avatar_img
|
|
|
|
- name: Upload l'avatar de l'organization
|
|
uri:
|
|
url: "{{ gitea_base_url }}/api/v1/orgs/{{ gitea_org_name }}/avatar"
|
|
method: POST
|
|
headers:
|
|
Content-Type: application/json
|
|
Accept: application/json
|
|
Authorization: "token {{ gitea_api_token }}"
|
|
body_format: json
|
|
body:
|
|
image: "{{ gitea_org_avatar_img['content'] }}"
|
|
status_code: 204 |