diff --git a/roles/gitea/defaults/main.yaml b/roles/gitea/defaults/main.yaml index b00e416..1a48f1c 100644 --- a/roles/gitea/defaults/main.yaml +++ b/roles/gitea/defaults/main.yaml @@ -1,10 +1,20 @@ --- +gitea_go_arch_map: + i386: '386' + x86_64: 'amd64' + +gitea_checksums: + x86_64: sha256:3faa3e97a621c3b9ecba7917fd870c07c3c6c88c8cc7c29ecbf2c7b9802d91b0 + +gitea_go_arch: "{{ gitea_go_arch_map[ansible_architecture] | default('amd64') }}" +gitea_checksum: "{{ gitea_checksums[ansible_architecture] | default('amd64') }}" + gitea_service_name: gitea.service gitea_service_state: started gitea_service_enabled: yes gitea_version: 1.12.2 -gitea_url: "https://dl.gitea.io/gitea/{{ gitea_version }}/gitea-{{ gitea_version }}-linux-{{ gitea_arch }}" +gitea_url: "https://dl.gitea.io/gitea/{{ gitea_version }}/gitea-{{ gitea_version }}-linux-{{ gitea_go_arch }}" gitea_bin_path: /usr/local/bin gitea_var_path: /var/lib/gitea gitea_log_path: /var/log/gitea diff --git a/roles/gitea/tasks/configure.yaml b/roles/gitea/tasks/configure.yaml new file mode 100644 index 0000000..69b44f3 --- /dev/null +++ b/roles/gitea/tasks/configure.yaml @@ -0,0 +1,11 @@ +--- +- name: configure + template: + src: app.ini.j2 + dest: "{{ gitea_config_path }}" + owner: root + group: "{{ gitea_group }}" + mode: 0640 + notify: + - restart gitea + diff --git a/roles/gitea/tasks/default.yaml b/roles/gitea/tasks/default.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/gitea/tasks/install.yaml b/roles/gitea/tasks/install.yaml new file mode 100644 index 0000000..ed2f957 --- /dev/null +++ b/roles/gitea/tasks/install.yaml @@ -0,0 +1,71 @@ +--- +- name: "download gitea {{ gitea_version }}" + get_url: + url: "{{ gitea_url }}" + checksum: "{{ gitea_checksum }}" + dest: "{{ gitea_path }}" + owner: root + group: root + mode: 0755 + notify: + - restart gitea + retries: 3 + +- name: create group + user: + name: "{{ gitea_group }}" + system: yes + +- name: create user and group + user: + name: "{{ gitea_user }}" + group: "{{ gitea_group }}" + shell: "{{ gitea_shell }}" + comment: "{{ gitea_gecos }}" + system: yes + password: "{{ gitea_password }}" + home: "{{ gitea_home_path }}" + +- name: "create {{ gitea_etc_path }}" + file: + path: "{{ gitea_etc_path }}" + owner: root + group: "{{ gitea_group }}" + mode: 0750 + state: directory + +- name: "create {{ gitea_var_path }} tree" + file: + path: "{{ item }}" + owner: "{{ gitea_user }}" + group: "{{ gitea_group }}" + mode: 0750 + state: directory + with_items: "{{ gitea_var_tree }}" + +- name: "create {{ gitea_log_path }} path" + file: + path: "{{ gitea_log_path }}" + owner: "{{ gitea_user }}" + group: "{{ gitea_group }}" + mode: 0755 + state: directory + +- name: configure systemd unit + template: + src: gitea.service.j2 + dest: "{{ gitea_systemd_unit_path }}" + owner: root + group: root + mode: 0644 + notify: + - gitea daemon-reload + - restart gitea + when: ansible_service_mgr == 'systemd' + +- name: manage service + service: + name: "{{ gitea_service_name }}" + state: "{{ gitea_service_state }}" + enabled: "{{ gitea_service_enabled }}" + diff --git a/roles/gitea/tasks/main.yaml b/roles/gitea/tasks/main.yaml index dae9945..8c33141 100644 --- a/roles/gitea/tasks/main.yaml +++ b/roles/gitea/tasks/main.yaml @@ -1,93 +1,35 @@ --- -- name: gather architecture specific variables +- name: gather os specific variables include_vars: "{{ lookup('first_found', possible_files) }}" vars: possible_files: files: - - "{{ ansible_userspace_architecture }}.yaml" + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml" + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" - "default.yaml" paths: - vars -- name: "download gitea {{ gitea_version }}" - get_url: - url: "{{ gitea_url }}" - checksum: "{{ gitea_checksum }}" - dest: "{{ gitea_path }}" - owner: root - group: root - mode: 0755 - notify: - - restart gitea +- name: include os specific tasks + include_tasks: "{{ lookup('first_found', possible_files) }}" + vars: + possible_files: + files: + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml" + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" + - "default.yaml" + paths: + - tasks -- name: create group - user: - name: "{{ gitea_group }}" - system: yes +- include: pre.yaml -- name: create user and group - user: - name: "{{ gitea_user }}" - group: "{{ gitea_group }}" - shell: "{{ gitea_shell }}" - comment: "{{ gitea_gecos }}" - system: yes - password: "{{ gitea_password }}" - home: "{{ gitea_home_path }}" +- include: install.yaml + when: + - gitea_version is version(gitea_local_version, '>') -- name: "create {{ gitea_etc_path }}" - file: - path: "{{ gitea_etc_path }}" - owner: root - group: "{{ gitea_group }}" - mode: 0750 - state: directory +- include: configure.yaml -- name: configure - template: - src: app.ini.j2 - dest: "{{ gitea_config_path }}" - owner: root - group: "{{ gitea_group }}" - mode: 0640 - notify: - - restart gitea - -- name: "create {{ gitea_var_path }} tree" - file: - path: "{{ item }}" - owner: "{{ gitea_user }}" - group: "{{ gitea_group }}" - mode: 0750 - state: directory - with_items: "{{ gitea_var_tree }}" - -- name: "create {{ gitea_log_path }} path" - file: - path: "{{ gitea_log_path }}" - owner: "{{ gitea_user }}" - group: "{{ gitea_group }}" - mode: 0755 - state: directory - -- name: configure systemd unit - template: - src: gitea.service.j2 - dest: "{{ gitea_systemd_unit_path }}" - owner: root - group: root - mode: 0644 - notify: - - gitea daemon-reload - - restart gitea - when: ansible_service_mgr == 'systemd' - -- name: manage service - service: - name: "{{ gitea_service_name }}" - state: "{{ gitea_service_state }}" - enabled: "{{ gitea_service_enabled }}" - -- name: configure nginx - include: nginx.yaml - when: "'nginx' in ansible_play_role_names" +- include: nginx.yaml + when: '"nginx" in ansible_play_role_names' diff --git a/roles/gitea/tasks/nginx.yaml b/roles/gitea/tasks/nginx.yaml index 5ae2f86..282a1b0 100644 --- a/roles/gitea/tasks/nginx.yaml +++ b/roles/gitea/tasks/nginx.yaml @@ -1,3 +1,4 @@ +--- - name: configure nginx template: src: nginx.conf.j2 diff --git a/roles/gitea/tasks/pre.yaml b/roles/gitea/tasks/pre.yaml new file mode 100644 index 0000000..a36eead --- /dev/null +++ b/roles/gitea/tasks/pre.yaml @@ -0,0 +1,43 @@ +--- +- block: + - name: determine if installed + stat: + path: "{{ gitea_bin_path }}/gitea" + register: st + + - name: determine latest version + uri: + url: https://api.github.com/repos/go-gitea/gitea/releases/latest + return_content: true + body_format: json + register: _latest_version + until: _latest_version.status == 200 + retries: 3 + + - name: determine installed version + command: "{{ gitea_bin_path }}/gitea --version" + register: _installed_version_string + changed_when: false + when: st.stat.exists + + - name: set gitea facts + set_fact: + gitea_local_version: "{{ _installed_version_string.stdout | regex_search('^Gitea version ([\\d.]+)', '\\1') | first }}" + when: st.stat.exists + + - name: set gitea facts + set_fact: + gitea_local_version: "0" + when: not st.stat.exists + + - name: set gitea facts + set_fact: + gitea_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}" + + - name: download checksums + set_fact: + checksum_string: "{{ lookup('url', gitea_url + '.sha256') }}" + + - name: set gitea checksum + set_fact: + gitea_checksum: "sha256:{{ checksum_string.split(' ') | first }}" diff --git a/roles/gitea/vars/default.yaml b/roles/gitea/vars/default.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/gitea/vars/x86_64.yaml b/roles/gitea/vars/x86_64.yaml deleted file mode 100644 index eb9ad46..0000000 --- a/roles/gitea/vars/x86_64.yaml +++ /dev/null @@ -1,3 +0,0 @@ ---- -gitea_arch: amd64 -gitea_checksum: sha256:3faa3e97a621c3b9ecba7917fd870c07c3c6c88c8cc7c29ecbf2c7b9802d91b0