aboutsummaryrefslogtreecommitdiffstats
path: root/ansible/roles/git-mirrors/tasks/main.yml
blob: 2e86aa022d6e8c8befaf3327ae9ddbdd0d75f638 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
- name: Install cgit
  package:
    state: present
    name: cgit
  tags:
    - role::git-mirrors

- name: Install formatting dependencies for cgit
  package:
    state: present
    name:
      - python3-markdown
      - python3-docutils
      - python3-pygments
  tags:
    - role::git-mirrors

- name: Install moreutils for cron utilities
  package:
    state: present
    name:
      - moreutils
  tags:
    - role::git-mirrors

- name: Create mirroring user
  user:
    state: present
    system: true
    name: "{{ git_mirrors_user }}"
    home: "{{ git_mirrors_base_dir }}"
  tags:
    - role::git-mirrors

- name: Create mirror storage directory
  file:
    state: directory
    path: "{{ git_mirrors_base_dir }}/mirrored"
    owner: "{{ git_mirrors_user }}"
    group: "{{ git_mirrors_user }}"
    mode: "0755"
  tags:
    - role::git-mirrors

- name: Create organisation folders
  file:
    state: directory
    path: "{{ git_mirrors_base_dir }}/mirrored/{{ item.owner }}"
    owner: "{{ git_mirrors_user }}"
    group: "{{ git_mirrors_user }}"
    mode: "0755"
  with_items:
    - "{{ git_mirrors_mirrored_repositories }}"
  tags:
    - role::git-mirrors

# Unfortunately, the Ansible git module does not support the --mirror
# option to git clone, so for now we run the command ourselves if the
# directories could not be found.
- name: Clone repositories # noqa: command-instead-of-module
  become: true
  become_user: "{{ git_mirrors_user }}"
  command:
    argv:
      - "git"
      - "clone"
      - "--mirror"
      - "https://{{ item.domain | default('github.com') }}/{{ item.owner }}/{{ item.repo }}.git"
      - "{{ git_mirrors_base_dir }}/mirrored/{{ item.owner }}/{{ item.repo }}"
    creates: "{{ git_mirrors_base_dir }}/mirrored/{{ item.owner }}/{{ item.repo }}"
  with_items:
    - "{{ git_mirrors_mirrored_repositories }}"
  tags:
    - role::git-mirrors

- name: Set repository descriptions
  copy:
    content: "{{ item.description | default('A mirrored copy of the ' + item.owner + '/' + item.repo + ' repository.') }}"
    dest: "{{ git_mirrors_base_dir }}/mirrored/{{ item.owner }}/{{ item.repo }}/description"
    owner: "{{ git_mirrors_user }}"
    group: "{{ git_mirrors_user }}"
    mode: "0444"
  with_items:
    - "{{ git_mirrors_mirrored_repositories }}"
  tags:
    - role::git-mirrors

- name: Template cgitrc configuration file
  template:
    src: cgitrc.j2
    dest: /etc/cgitrc
    mode: "0444"
    owner: root
    group: root
  tags:
    - role::git-mirrors

- name: Install fcgiwrap for NGINX
  package:
    state: present
    name: fcgiwrap
  tags:
    - role::git-mirrors

- name: Enable fcgiwrap
  service:
    state: started
    enabled: true
    name: fcgiwrap
  tags:
    - role::git-mirrors

- name: Template NGINX configuration
  template:
    src: nginx-site.conf.j2
    dest: "/etc/nginx/sites-available/{{ git_mirrors_nginx_config_name }}"
    mode: "0444"
    owner: root
    group: root
  tags:
    - role::git-mirrors
  notify:
    - Reload the nginx service

- name: Enable the NGINX site
  file:
    src: "/etc/nginx/sites-available/{{ git_mirrors_nginx_config_name }}"
    dest: "/etc/nginx/sites-enabled/{{ git_mirrors_nginx_config_name }}"
    state: link
  tags:
    - role::git-mirrors
  notify:
    - Reload the nginx service

- name: Template mirror update script
  template:
    src: update-mirrors.sh.j2
    dest: "{{ git_mirrors_base_dir }}/update-mirrors.sh"
    mode: "0544"
    owner: "{{ git_mirrors_user }}"
    group: "{{ git_mirrors_user }}"
  tags:
    - role::git-mirrors

- name: Add cronjob for mirror updating
  cron:
    name: "Update the git mirrors published by cgit (git-mirrors role)"
    # Every 5 minutes
    minute: "*/5"
    job: "chronic {{ git_mirrors_base_dir }}/update-mirrors.sh"
    user: git-mirrors
    cron_file: "{{ git_mirrors_cron_file }}"
  tags:
    - role::git-mirrors

- name: Set cronjob failure email
  community.general.cronvar:
    name: MAILTO
    value: "{{ git_mirrors_error_email }}"
    cron_file: "{{ git_mirrors_cron_file }}"
  tags:
    - role::git-mirrors