From 84682c67b65e51be9168bb87d30d31bbd6d614a5 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 22 Oct 2026 23:28:49 +0300 Subject: feat: add cgit --- roles/cgit/handlers/main.yml | 4 +++ roles/cgit/tasks/main.yml | 52 +++++++++++++++++++++++++++++++++ roles/cgit/templates/cgitrc-instance.j2 | 15 ++++++++++ roles/cgit/templates/cgitrc.j2 | 29 ++++++++++++++++++ roles/cgit/templates/nginx.conf.j2 | 27 +++++++++++++++++ roles/cgit/templates/spawn-fcgi.cgit.j2 | 6 ++++ roles/cosmos/tasks/main.yml | 12 ++++++++ roles/cosmos/templates/nginx.conf.j2 | 45 ++++++++++++++++++++++++++++ roles/personal/tasks/main.yml | 19 ++++++++++++ roles/personal/templates/nginx.conf.j2 | 45 ++++++++++++++++++++++++++++ 10 files changed, 254 insertions(+) create mode 100644 roles/cgit/handlers/main.yml create mode 100644 roles/cgit/tasks/main.yml create mode 100644 roles/cgit/templates/cgitrc-instance.j2 create mode 100644 roles/cgit/templates/cgitrc.j2 create mode 100644 roles/cgit/templates/nginx.conf.j2 create mode 100644 roles/cgit/templates/spawn-fcgi.cgit.j2 (limited to 'roles') diff --git a/roles/cgit/handlers/main.yml b/roles/cgit/handlers/main.yml new file mode 100644 index 0000000..af9972a --- /dev/null +++ b/roles/cgit/handlers/main.yml @@ -0,0 +1,4 @@ +- name: "Restart spawn-fcgi.cgit" + ansible.builtin.service: + name: "spawn-fcgi.cgit" + state: restarted diff --git a/roles/cgit/tasks/main.yml b/roles/cgit/tasks/main.yml new file mode 100644 index 0000000..15516d4 --- /dev/null +++ b/roles/cgit/tasks/main.yml @@ -0,0 +1,52 @@ +--- +- block: + - name: Install CGit and FastCGI wrapper packages + community.general.apk: + name: + - cgit + - fcgiwrap + - spawn-fcgi + - py3-pygments + state: present + + - name: Ensure root CGit configuration exists + ansible.builtin.template: + src: cgitrc.j2 + dest: "/etc/cgitrc" + owner: root + group: root + mode: '0444' + + - name: Ensure {{ instance }} CGit configuration exists + ansible.builtin.template: + src: cgitrc-instance.j2 + dest: "/etc/cgitrc-{{ instance }}" + owner: root + group: root + mode: '0444' + + - name: Create OpenRC init script symlink + ansible.builtin.file: + src: /etc/init.d/spawn-fcgi + dest: "/etc/init.d/spawn-fcgi.cgit" + state: link + owner: root + group: root + + - name: Deploy spawn-fcgi configuration + ansible.builtin.template: + src: spawn-fcgi.cgit.j2 + dest: "/etc/conf.d/spawn-fcgi.cgit" + owner: root + group: root + mode: '0444' + notify: Restart spawn-fcgi.cgit + + - name: Ensure spawn-fcgi.git is started and enabled + ansible.builtin.service: + name: "spawn-fcgi.cgit" + state: started + enabled: true + + tags: + - cgit diff --git a/roles/cgit/templates/cgitrc-instance.j2 b/roles/cgit/templates/cgitrc-instance.j2 new file mode 100644 index 0000000..2e55b67 --- /dev/null +++ b/roles/cgit/templates/cgitrc-instance.j2 @@ -0,0 +1,15 @@ +{# -*- Conf -*- #} +# {{ ansible_managed }} + +include=/etc/cgitrc + +root-readme=/srv/http/{{ domain }}/about.html +root-title={{ root_title }} +root-desc={{ root_desc }} + +module-link=https://{{ domain }}/%s/tree/?id=%s +clone-url=https://{{ domain }}/$CGIT_REPO_URL ssh://git@{{ domain }}/$CGIT_REPO_URL + +scan-path={{ scan_path }} + +{{ extra }} diff --git a/roles/cgit/templates/cgitrc.j2 b/roles/cgit/templates/cgitrc.j2 new file mode 100644 index 0000000..9aee5f0 --- /dev/null +++ b/roles/cgit/templates/cgitrc.j2 @@ -0,0 +1,29 @@ +{# -*- Conf -*- #} +# {{ ansible_managed }} + +enable-log-filecount=1 +enable-log-linecount=1 +enable-remote-branches=1 +enable-subject-links=1 +enable-git-config=1 + +virtual-root=/ + +local-time=1 + +# logo= + +source-filter=/usr/local/lib/cgit/filters/syntax-highlighting.sh +email-filter=lua:/usr/local/lib/cgit/filters/email-libravatar.lua +about-filter=/usr/local/lib/cgit/filters/about-formatting.sh + +readme=master:README.org + +# repository-sort=name +side-by-side-diffs=1 + +snapshots=tar.gz + +strict-export=git-daemon-export-ok +remove-suffix=1 +section-from-path=1 diff --git a/roles/cgit/templates/nginx.conf.j2 b/roles/cgit/templates/nginx.conf.j2 new file mode 100644 index 0000000..2483471 --- /dev/null +++ b/roles/cgit/templates/nginx.conf.j2 @@ -0,0 +1,27 @@ +# -*- Nginx -*- +# {{ ansible_managed }} + +server { + listen 443 ssl; + listen [::]:443 ssl; + server_name {{ domain }}; + + ssl_certificate /etc/ssl/{{ domain | regex_replace('^git\\.', '') }}.fullchain.pem; + ssl_certificate_key /etc/ssl/private/{{ domain | regex_replace('^git\\.', '') }}.key; + + root /usr/share/webapps/cgit/; + try_files $uri @cgit_{{ instance }}; + + location / { + try_files $uri @cgit_{{ instance }}; + } + + location @cgit_{{ instance }} { + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME /usr/bin/cgit.cgi; + fastcgi_param PATH_INFO $uri; + fastcgi_param QUERY_STRING $args; + fastcgi_param HTTP_HOST $server_name; + fastcgi_pass 127.0.0.1:{{ port }}; + } +} diff --git a/roles/cgit/templates/spawn-fcgi.cgit.j2 b/roles/cgit/templates/spawn-fcgi.cgit.j2 new file mode 100644 index 0000000..8cbfced --- /dev/null +++ b/roles/cgit/templates/spawn-fcgi.cgit.j2 @@ -0,0 +1,6 @@ +# {{ ansible_managed }} +FCGI_SOCKET=/var/run/cgit.sock +FCGI_PROGRAM="/usr/bin/fcgiwrap -f" +FCGI_USER=git +FCGI_GROUP=git +FCGI_EXTRA_OPTIONS="-U nginx -G nginx" diff --git a/roles/cosmos/tasks/main.yml b/roles/cosmos/tasks/main.yml index c44505e..a50205c 100644 --- a/roles/cosmos/tasks/main.yml +++ b/roles/cosmos/tasks/main.yml @@ -18,5 +18,17 @@ validate: nginx-validate %s /etc/nginx/http.d/cosmos.conf notify: Reload nginx + - name: Provision cosmos CGit instance + ansible.builtin.include_role: + name: cgit + tags: + cgit + vars: + instance: "cosmos" + domain: "git.cosmos.party" + root_title: "Cosmos Party" + root_desc: "Here you can find all our git repositories" + scan_path: "/var/lib/git/repositories/" + tags: - cosmos diff --git a/roles/cosmos/templates/nginx.conf.j2 b/roles/cosmos/templates/nginx.conf.j2 index b9b6c67..98879ac 100644 --- a/roles/cosmos/templates/nginx.conf.j2 +++ b/roles/cosmos/templates/nginx.conf.j2 @@ -47,6 +47,51 @@ server { location / { try_files $uri /en$uri /en$uri/ =404; } } +server { + server_name git.cosmos.party; + + listen unix:/var/run/nginx/http.sock; + listen 443 ssl; + listen [::]:443 ssl; + listen 443 quic; + listen [::]:443 quic; + + http2 on; + http3 on; + + add_header Alt-Svc 'h3=":443"; ma=86400'; + + ssl_certificate /etc/ssl/cosmos.party.fullchain.pem; + ssl_certificate_key /etc/ssl/private/cosmos.party.key; + ssl_protocols TLSv1.2 TLSv1.3; + + location ~ /.+/(info/refs|git-upload-pack) { + include /etc/nginx/fastcgi_params; + fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; + fastcgi_param PATH_INFO $uri; + fastcgi_param GIT_PROJECT_ROOT /var/lib/git/repositories/; + fastcgi_pass unix:/var/run/cgit.sock-1; + } + + root /srv/http/git.cosmos.party/; + try_files $uri @cgit-static; + + location @cgit-static { + root /usr/share/webapps/cgit/; + try_files $uri @cgit; + } + + location @cgit { + include /etc/nginx/fastcgi_params; + fastcgi_param SCRIPT_FILENAME /usr/share/webapps/cgit/cgit.cgi; + fastcgi_param CGIT_CONFIG /etc/cgitrc-cosmos; + fastcgi_param PATH_INFO $uri; + fastcgi_param QUERY_STRING $args; + fastcgi_param HTTP_HOST $server_name; + fastcgi_pass unix:/var/run/cgit.sock-1; + } +} + server { server_name andr.cosmos.party; diff --git a/roles/personal/tasks/main.yml b/roles/personal/tasks/main.yml index 6a9b7c7..8491d2e 100644 --- a/roles/personal/tasks/main.yml +++ b/roles/personal/tasks/main.yml @@ -35,5 +35,24 @@ mode: '0440' notify: Reload nginx + - name: Provision personal CGit instance + ansible.builtin.include_role: + name: cgit + tags: + cgit + vars: + instance: "personal" + domain: "git.michalec.dev" + root_title: "Martin Michalec" + root_desc: "Here you can find all my git repositories" + scan_path: "/var/lib/git/repositories/cmmm/" + extra: | + section=cosmos + repo.url=cosmos/codebase + repo.name=codebase + repo.desc=The codebase + repo.owner=cmmm@cosmos.party + repo.path=/var/lib/git/repositories/codebase.git + tags: - personal diff --git a/roles/personal/templates/nginx.conf.j2 b/roles/personal/templates/nginx.conf.j2 index 6b2d05a..6f6cee8 100644 --- a/roles/personal/templates/nginx.conf.j2 +++ b/roles/personal/templates/nginx.conf.j2 @@ -83,3 +83,48 @@ server { include /etc/nginx/http.d/michalec.dev.d/*.conf; } + +server { + server_name git.michalec.dev; + + listen unix:/var/run/nginx/http.sock; + listen 443 ssl; + listen [::]:443 ssl; + listen 443 quic; + listen [::]:443 quic; + + http2 on; + http3 on; + + add_header Alt-Svc 'h3=":443"; ma=86400'; + + ssl_certificate /etc/ssl/michalec.dev.fullchain.pem; + ssl_certificate_key /etc/ssl/private/michalec.dev.key; + ssl_protocols TLSv1.2 TLSv1.3; + + root /srv/http/git.michalec.dev/; + try_files $uri @cgit-static; + + location ~ /.+/(info/refs|git-upload-pack) { + include /etc/nginx/fastcgi_params; + fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend; + fastcgi_param PATH_INFO $uri; + fastcgi_param GIT_PROJECT_ROOT /var/lib/git/repositories/cmmm/; + fastcgi_pass unix:/var/run/cgit.sock-1; + } + + location @cgit-static { + root /usr/share/webapps/cgit/; + try_files $uri @cgit; + } + + location @cgit { + include /etc/nginx/fastcgi_params; + fastcgi_param SCRIPT_FILENAME /usr/share/webapps/cgit/cgit.cgi; + fastcgi_param CGIT_CONFIG /etc/cgitrc-personal; + fastcgi_param PATH_INFO $uri; + fastcgi_param QUERY_STRING $args; + fastcgi_param HTTP_HOST $server_name; + fastcgi_pass unix:/var/run/cgit.sock-1; + } +} -- cgit v1.3.1