From 69ef04bf0c58a81d90add660bbfcb0ab6428e843 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 28 Dec 2023 00:28:30 +0100 Subject: [PATCH] implemented draf for gitlab role --- group_vars/all | 4 +++ roles/docker-gitlab/README.md | 35 +++++++++++++++++++ roles/docker-gitlab/handlers/main.yml | 8 +++++ roles/docker-gitlab/meta/main.yml | 2 ++ roles/docker-gitlab/tasks/main.yml | 13 +++++++ .../templates/docker-compose.yml.j2 | 32 +++++++++++++++++ roles/docker-gitlab/vars/main.yml | 7 ++++ .../templates/docker-compose.yml.j2 | 20 ++--------- roles/docker-listmonk/vars/main.yml | 13 +++---- servers.yml | 10 ++++++ templates/docker-postgres-service.yml.j2 | 19 ++++++++++ 11 files changed, 139 insertions(+), 24 deletions(-) create mode 100644 roles/docker-gitlab/README.md create mode 100644 roles/docker-gitlab/handlers/main.yml create mode 100644 roles/docker-gitlab/meta/main.yml create mode 100644 roles/docker-gitlab/tasks/main.yml create mode 100644 roles/docker-gitlab/templates/docker-compose.yml.j2 create mode 100644 roles/docker-gitlab/vars/main.yml create mode 100644 templates/docker-postgres-service.yml.j2 diff --git a/group_vars/all b/group_vars/all index 3ce11f52..a4e71f79 100644 --- a/group_vars/all +++ b/group_vars/all @@ -104,6 +104,7 @@ domain_elk: "elk.{{top_domain}}" domain_discourse: "forum.{{top_domain}}" domain_funkwhale: "music.{{top_domain}}" domain_gitea: "git.{{top_domain}}" +domain_gitlab: "gitlab.{{top_domain}}" domain_listmonk: "newsletter.{{top_domain}}" domain_mailu: "mail.{{top_domain}}" domain_mastodon: "microblog.{{top_domain}}" @@ -121,6 +122,9 @@ domain_attendize: "tickets.{{top_domain}}" domain_yourls: "s.{{top_domain}}" domain_openproject: "project.{{top_domain}}" +### Common Configurations +postgres_default_version: "16" + ### Docker Role Specific Parameters #### Pixelfed diff --git a/roles/docker-gitlab/README.md b/roles/docker-gitlab/README.md new file mode 100644 index 00000000..4f4fe67c --- /dev/null +++ b/roles/docker-gitlab/README.md @@ -0,0 +1,35 @@ +# Docker-GitLab Ansible Role README.md + +## Overview +This Ansible role is designed for setting up and managing a GitLab server running in a Docker container. It automates the process of installing GitLab, configuring its environment, and managing dependencies such as a PostgreSQL database and an Nginx reverse proxy. + +## Features +- **GitLab Installation**: Automatically deploys GitLab using Docker. +- **External PostgreSQL Database**: Configures GitLab to use an external PostgreSQL database. +- **Nginx Reverse Proxy Integration**: Includes tasks for setting up an Nginx reverse proxy for GitLab. +- **Customizable Configuration**: Variables and templates allow for easy customization. + +## Requirements +- Docker and Docker Compose installed on the target machine. +- Ansible for automation. + +## Role Variables +Variables are defined in `vars/main.yml`. Key variables include: +- `docker_compose_instance_directory`: Directory for Docker Compose instances. +- `database_instance`, `database_host`, `database_databasename`, `database_username`, `database_password`: Database configuration variables. +- `database_version`: PostgreSQL version, with a default fallback. + +## Handlers +- `recreate gitlab`: Restarts GitLab using Docker Compose when changes are detected. + +## Dependencies +- `nginx-docker-reverse-proxy`: A role for setting up an Nginx reverse proxy for GitLab. + +## Template Files +- `docker-compose.yml.j2`: Jinja2 template for the Docker Compose configuration. +- Additional templates for database and proxy configuration. + +## Usage +Include this role in your Ansible playbooks and specify the necessary variables. Run the playbook to deploy and configure GitLab in a Docker environment. + +For a detailed walkthrough and explanation of this role, refer to the conversation at [ChatGPT Session Transcript](https://chat.openai.com/share/1b0147bf-d4de-4790-b8ed-c332aa4e3ce3). \ No newline at end of file diff --git a/roles/docker-gitlab/handlers/main.yml b/roles/docker-gitlab/handlers/main.yml new file mode 100644 index 00000000..145a760f --- /dev/null +++ b/roles/docker-gitlab/handlers/main.yml @@ -0,0 +1,8 @@ +--- +- name: recreate gitlab + command: + cmd: docker-compose -p gitlab up -d --force-recreate + chdir: "{{docker_compose_instance_directory}}" + environment: + COMPOSE_HTTP_TIMEOUT: 600 + DOCKER_CLIENT_TIMEOUT: 600 diff --git a/roles/docker-gitlab/meta/main.yml b/roles/docker-gitlab/meta/main.yml new file mode 100644 index 00000000..d3606e63 --- /dev/null +++ b/roles/docker-gitlab/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- nginx-docker-reverse-proxy diff --git a/roles/docker-gitlab/tasks/main.yml b/roles/docker-gitlab/tasks/main.yml new file mode 100644 index 00000000..e4d53cec --- /dev/null +++ b/roles/docker-gitlab/tasks/main.yml @@ -0,0 +1,13 @@ +--- +- name: "include tasks nginx-docker-proxy-domain.yml" + include_tasks: nginx-docker-proxy-domain.yml + +- name: "create {{docker_compose_instance_directory}}" + file: + path: "{{docker_compose_instance_directory}}" + state: directory + mode: 0755 + +- name: add docker-compose.yml + template: src=docker-compose.yml.j2 dest={{docker_compose_instance_directory}}docker-compose.yml + notify: recreate gitlab diff --git a/roles/docker-gitlab/templates/docker-compose.yml.j2 b/roles/docker-gitlab/templates/docker-compose.yml.j2 new file mode 100644 index 00000000..af18907c --- /dev/null +++ b/roles/docker-gitlab/templates/docker-compose.yml.j2 @@ -0,0 +1,32 @@ +version: '3.6' +services: + web: + image: 'gitlab/gitlab-ee:latest' + restart: always + hostname: '{{domain}}' + environment: + GITLAB_OMNIBUS_CONFIG: | + external_url 'https://{{domain}}' + postgresql['enable'] = false + gitlab_rails['gitlab_shell_ssh_port'] = {{ssh_port}} + gitlab_rails['db_adapter'] = 'postgresql' + gitlab_rails['db_encoding'] = 'utf8' + gitlab_rails['db_host'] = '{{database_host}}' + gitlab_rails['db_port'] = 5432 + gitlab_rails['db_username'] = '{{database_username}}' + gitlab_rails['db_password'] = '{{database_password}}' + ports: + - '8929:8929' + ports: + - "127.0.0.1:{{http_port}}:80" + - "{{ssh_port}}:22" + volumes: + - 'config:/etc/gitlab' + - 'logs:/var/log/gitlab' + - 'data:/var/opt/gitlab' + shm_size: '256m' + +{% include 'templates/docker-postgres-service.yml.j2' %} + +volumes: + database: \ No newline at end of file diff --git a/roles/docker-gitlab/vars/main.yml b/roles/docker-gitlab/vars/main.yml new file mode 100644 index 00000000..7d748bad --- /dev/null +++ b/roles/docker-gitlab/vars/main.yml @@ -0,0 +1,7 @@ +docker_compose_instance_directory: "{{path_docker_compose_instances}}gitlab/" +database_instance: "gitlab" +database_host: "database" +database_databasename: "gitlabhq_production" +database_username: "gitlab" +database_password: "{{gitlab_database_password}}" +database_version: "{{ gitlab_database_version | default(postgres_default_version) }}" \ No newline at end of file diff --git a/roles/docker-listmonk/templates/docker-compose.yml.j2 b/roles/docker-listmonk/templates/docker-compose.yml.j2 index 5a605aef..025242c1 100644 --- a/roles/docker-listmonk/templates/docker-compose.yml.j2 +++ b/roles/docker-listmonk/templates/docker-compose.yml.j2 @@ -1,24 +1,8 @@ version: "3.7" services: - database: - image: postgres:13-alpine - ports: - - "9432:5432" - environment: - - POSTGRES_PASSWORD={{database_password}} - - POSTGRES_USER={{database_username}} - - POSTGRES_DB={{database_databasename}} - restart: unless-stopped - healthcheck: - test: ["CMD-SHELL", "pg_isready -U {{database_username}}"] - interval: 10s - timeout: 5s - retries: 6 - volumes: - - type: volume - source: database - target: /var/lib/postgresql/data + +{% include 'templates/docker-postgres-service.yml.j2' %} application: restart: unless-stopped diff --git a/roles/docker-listmonk/vars/main.yml b/roles/docker-listmonk/vars/main.yml index 673b56c4..16b51b40 100644 --- a/roles/docker-listmonk/vars/main.yml +++ b/roles/docker-listmonk/vars/main.yml @@ -1,6 +1,7 @@ -docker_compose_instance_directory: "{{path_docker_compose_instances}}listmonk/" -database_instance: "listmonk" -database_host: "database" -database_databasename: "{{database_instance}}" -database_username: "{{database_instance}}" -database_password: "{{listmonk_database_password}}" \ No newline at end of file +docker_compose_instance_directory: "{{path_docker_compose_instances}}listmonk/" +database_instance: "listmonk" +database_host: "database" +database_databasename: "{{database_instance}}" +database_username: "{{database_instance}}" +database_password: "{{listmonk_database_password}}" +database_version: "{{ listmonk_database_version | default(postgres_default_version) }}" \ No newline at end of file diff --git a/servers.yml b/servers.yml index 533d0b20..8a0f92ba 100644 --- a/servers.yml +++ b/servers.yml @@ -229,6 +229,16 @@ domain: "{{domain_openproject}}" http_port: 8023 +- name: setup gitlab hosts + hosts: gitlab + become: true + roles: + - role: docker-gitlab + vars: + domain: "{{domain_gitlab}}" + http_port: 8024 + ssh_port: 2202 + - name: setup akaunting hosts hosts: akaunting become: true diff --git a/templates/docker-postgres-service.yml.j2 b/templates/docker-postgres-service.yml.j2 new file mode 100644 index 00000000..2d53e1ad --- /dev/null +++ b/templates/docker-postgres-service.yml.j2 @@ -0,0 +1,19 @@ + + database: + image: postgres:{{database_version}}-alpine + ports: + - "9432:5432" + environment: + - POSTGRES_PASSWORD={{database_password}} + - POSTGRES_USER={{database_username}} + - POSTGRES_DB={{database_databasename}} + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "pg_isready -U {{database_username}}"] + interval: 10s + timeout: 5s + retries: 6 + volumes: + - type: volume + source: database + target: /var/lib/postgresql/data