summaryrefslogtreecommitdiff
path: root/posts/2014-01-04-setting-up-cgit-with-ansible.md
blob: edebf66a7a2a647ab378e56091feb0980736d163 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
I've [already write](file:///ansible-and-chef/) about [Ansible](http://www.ansibleworks.com/). I use it to manage and configure my servers (most of them are VMs running on my laptop, but the idea is the same). One of the server is used to store my personal git repositories, and I wanted to use [cgit](http://git.zx2c4.com/cgit/) as the front end for the public repositories instead of the old and slow [gitweb](https://git.wiki.kernel.org/index.php/Gitweb).

Since there's no package in Debian for cgit, I need to have an easy procedure to install it. I'll show how I do it with Ansible. This could be useful if you're learning about Ansible are you're looking for a simple use case.

Work directory
--------------

The work directory contains a bunch of files:

-   $workdir/hosts - local inventory with all the hosts, grouped by categories
-   $workdir/$hostname.yml - play book for a given host (more about this file later)
-   $workdir/roles/git - directory containing templates, tasks and handlers for installing cgit
-   $workdir/vars/$hostname.yml - contains all the variable needed to install cgit

> Replace $hostname with the name of the host you want to use for cgit.

Handlers
--------

In my case, cgit is hosted behind Nginx, so first, we need a handler to restart it after changing Nginx's configuration.

``` example
    # roles/git/handlers/main.yml
    - name: restart nginx
      service: name=nginx state=restarted
```

Roles
-----

Now we need to define our role for cgit. The idea is to install the required packages to be able to build cgit, to create the directories where we will store our repositories, and actually build cgit.

``` example
    # roles/git/tasks/main.yml
    - name: Set the directory for public repos
      file: path=/srv/git/public
            owner=www-data
            group=www-data
            mode=0770 recurse=yes
            state=directory

    - name: Set the directory for private repos
      file: path=/srv/git/private
            owner=www-data
            group=www-data
            mode=0770
            recurse=yes
            state=directory

    - name: Install necessities for cgit
      apt: pkg={{ item }} state=installed
      with_items:
        - build-essential
        - autoconf
        - automake
        - libtool
        - libfcgi-dev
        - libssl-dev
        - spawn-fcgi
        - highlight
        - fcgiwrap

    - name: Create cgit web directory
      file: path=/srv/www/{{ cgit_subdomain }}.{{ domain }}
            recurse=yes
            state=directory
            owner=www-data

    - name: Download cgit tarbal
      get_url: url=http://git.zx2c4.com/cgit/snapshot/cgit-0.9.2.zip
               dest=/tmp/cgit-0.9.2.zip
               force=no

    - name: Unzip cgit
      command: unzip -qo /tmp/cgit-0.9.2.zip -d /tmp

    - name: Configure cgit installation
      template: src=cgit.conf.j2 dest=/tmp/cgit-0.9.2/cgit.conf

    - name: Install cgit
      shell: make get-git && make && make install chdir=/tmp/cgit-0.9.2

    - name: Set permissions for cgit
      file: path=/srv/www/{{ cgit_subdomain }}.{{ domain }}
            owner=www-data
            state=directory
            recurse=yes

    - name: Configure the nginx HTTP server for cgit
      template: src=etc_nginx_sites-available_cgit.j2
                dest=/etc/nginx/sites-available/{{ cgit_subdomain }}.{{ domain }}
                group=www-data
                owner=www-data

    - name: Configure cgit
      template: src=etc_cgitrc.j2
                dest=/etc/cgitrc
                group=www-data
                owner=www-data

    - name: Enable cgit
      file: src=/etc/nginx/sites-available/{{ cgit_subdomain }}.{{ domain }}
            dest=/etc/nginx/sites-enabled/{{ cgit_subdomain }}.{{ domain }}
            state=link
            group=www-data
            owner=www-data
      notify: restart nginx

    - name: Backup git directory
      template: src=etc_cron.hourly_git-backup.j2
                dest=/etc/cron.hourly/git-backup
                mode=0755
```

Templates
---------

We need a bunch of templates to configure and build our tools. Let's start with **cgit.conf**.

``` bash
    # roles/git/templates/cgit.conf.j2

    CGIT_SCRIPT_PATH = /srv/www/{{ cgit_subdomain }}.{{ domain }}
```

This file is used when we build cgit to install it to a specific location.

The next template is to configure cgit.

``` example
    # roles/git/templates/etc_cgitrc.j2

    root-desc=Franck Cuny's projects
    virtual-root=/
    logo=/cgit.png
    css=/cgit.css
    scan-path=/srv/git/public
    remove-suffix=1
    clone-prefix=http://git.$hostname.net
```

This template is to configure nginx.

``` example
    # roles/git/templates/etc_nginx_sites-available_cgit.j2

    server {
        listen 80;
        server_name  "{{ cgit_subdomain}}.{{ domain }}";
        root /srv/www/{{ cgit_subdomain }}.{{ domain }};

        location / {
             try_files $uri @cgit;
        }

        location @cgit {
            index cgit.cgi;

            fastcgi_param  SCRIPT_FILENAME    $document_root/cgit.cgi;

            fastcgi_pass unix:/run/fcgiwrap.socket;
            fastcgi_param HTTP_HOST $server_name;
            fastcgi_param PATH_INFO $uri;
            fastcgi_param QUERY_INFO $uri;
            include "fastcgi_params";
        }

        error_log         /var/log/nginx/{{ cgit_subdomain }}.{{ domain }}-error.log;
        access_log        /var/log/nginx/{{ cgit_subdomain }}.{{ domain }}-access.log;
    }
```

Backing up on s3
----------------

I backup all my git repositories to a bucket on s3. In order to do that, you'll need either a new role or to update the current one by adding the following instructions.

``` example
    - name: Install s3cmd
      apt: pkg=s3cmd

    - name: Configure s3cmd
      sudo: false
      template:
        src="s3cfg.j2"
        dest="/root/.s3cfg"

    - name: Backup git directory
      template: src=etc_cron.hourly_git-backup.j2
                dest=/etc/cron.hourly/git-backup
                mode=0755
```

We need a template to configure our access to s3.

``` example
    [default]
    access_key = {{ aws_access_key }}
    secret_key = {{ aws_secret_key }}
    use_https = True
```

And another template for our cron job.

``` bash
    #!/bin/sh
    s3cmd sync -v /srv/git/ s3://$hostname-backup/git/ > /tmp/s3_backup_git.log 2>&1
```

Variables
---------

I have a file named **vars/$hostname.yml** that contains the

``` example
    domain: $hostname.net
    cgit_subdomain: git

    aws_access_key: access-key
    aws_secret_key: secret-key
```

Play time
---------

The content of the playbook

``` example
    - hosts: $hostname
      vars_files:
        - vars/$hostname.yml
      roles:
        - git
```

Now I can tell Ansible to run this playbook, and this will install cgit on my server: `ansible-playbook -i hosts lj.yml`.