#####################################
### someone"s ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2025 by someone <someone@somenet.org>
#
---
- name: install gitolite3
  apt:
    pkg:
    - markdown
    - gitolite3
    state: present
    policy_rc_d: 101
  tags: "online"
  ignore_errors: "{{ignore_online_errors | bool}}"


- name: create git user
  user:
    name: "git"
    home: "/srv/gitolite"
    shell: "/bin/bash"
    system: yes
    state: present


- name: create gitolite homedir
  file:
    path: "/srv/gitolite"
    state: directory
    mode: 0710
    owner: "git"
    group: "git"


- name: create gitolite .ssh dir
  file:
    path: "/srv/gitolite/.ssh"
    state: directory
    mode: 0700
    owner: "git"
    group: "git"


- name: copy gitconfig
  copy:
    src: "{{item}}"
    dest: "/srv/gitolite/.gitconfig"
    mode: 0640
    owner: "git"
    group: "git"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/gitconfig"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/gitconfig"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/gitconfig"
    - "default/gitconfig"


- name: copy gitolite.rc
  copy:
    src: "{{item}}"
    dest: "/srv/gitolite/.gitolite.rc"
    mode: 0640
    owner: "git"
    group: "git"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/gitolite.rc"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/gitolite.rc"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/gitolite.rc"
    - "default/gitolite.rc"


- name: setup gitolite
  command: "gitolite setup -a gitolite"
  become: true
  become_user: "git"


- name: fix project.list permissions
  file:
    path: "/srv/gitolite/projects.list"
    state: file
    mode: 0640
    owner: "git"
    group: "git"


- name: fix repositories permissions
  file:
    path: "/srv/gitolite/repositories/"
    state: directory
    recurse: yes
    mode: "u=rwX,g=rX,o-rwx"
    owner: "git"
    group: "git"


- name: copy compact_gitolite_repos.sh
  copy:
    src: "{{item}}"
    dest: "/srv/gitolite/compact_gitolite_repos.sh"
    mode: 0750
    owner: "git"
    group: "git"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/compact_gitolite_repos.sh"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/compact_gitolite_repos.sh"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/compact_gitolite_repos.sh"
    - "default/compact_gitolite_repos.sh"


- name: copy compact_gitolite_repos.service to /etc/systemd/system/
  copy:
    src: "{{item}}"
    dest: "/etc/systemd/system/compact_gitolite_repos.service"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/compact_gitolite_repos.service"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/compact_gitolite_repos.service"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/compact_gitolite_repos.service"
    - "default/compact_gitolite_repos.service"


- name: copy compact_gitolite_repos.timer to /etc/systemd/system/
  copy:
    src: "{{item}}"
    dest: "/etc/systemd/system/compact_gitolite_repos.timer"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/compact_gitolite_repos.timer"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/compact_gitolite_repos.timer"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/compact_gitolite_repos.timer"
    - "default/compact_gitolite_repos.timer"


- name: enable and start compact_gitolite_repos.timer
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: compact_gitolite_repos.timer
