- hosts: localhost vars: meta: application_name: Forgejo playbook_version: 0.1 app_domain: "{{ uberspace_default_web_domain }}" app_db_name: "{{ uberspace_asteroid_name }}_forgejo" app_admin_username: "{{ uberspace_asteroid_name }}" app_admin_mailaddress: "{{ uberspace_mail_sysmail }}" app_port: 3000 _home: "/home/{{ uberspace_asteroid_name }}" _root: "{{ _home }}/opt/forgejo" _binary: "forgejo-{{ _version }}-linux-amd64" _release_url: "https://codeberg.org/forgejo/forgejo/releases/download/v{{ _version }}" _signing_key: EB114F5E6C0DC2BCDD183550A4B61A2DC5923710 tasks: # Required by `uberspace app install`; skipped during installation. - name: _Return resolved playbook vars debug: var: vars tags: [playbook-vars] - name: Validate installation options assert: that: - ansible_facts['architecture'] == 'x86_64' - app_domain is match('^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9]$') - app_db_name is match('^[a-zA-Z0-9_]+$') - app_db_name | length <= 64 - app_db_name.startswith(uberspace_asteroid_name + '_') - app_admin_username is match('^[a-zA-Z0-9]+([_.-][a-zA-Z0-9]+)*$') - app_admin_username | lower not in ['admin', 'root', 'api', 'user', 'org', 'assets', 'explore'] - app_admin_mailaddress is match('^[A-Za-z0-9_.+%-]+@[A-Za-z0-9.-]+$') - app_port | string is match('^[0-9]+$') - app_port | int >= 1024 - app_port | int <= 65535 - uberspace_mariadb_password | default('') | length > 0 fail_msg: "Use an account-prefixed database, a non-reserved admin username, an email address and a port from 1024 to 65535. The installer must supply your MariaDB password." - name: _Validate the database password for INI quoting assert: that: - "'`' not in uberspace_mariadb_password" - "'\n' not in uberspace_mariadb_password" - "'\r' not in uberspace_mariadb_password" no_log: true - name: Inspect installation and service paths stat: path: "{{ item }}" loop: - "{{ _root }}" - "{{ _home }}/.config/systemd/user/forgejo.service" register: existing - name: Refuse to overwrite an existing installation or service assert: that: not item.stat.exists fail_msg: "{{ item.item }} already exists. This playbook only supports fresh installations." loop: "{{ existing.results }}" loop_control: label: "{{ item.item }}" - name: Inspect the document root find: paths: "/var/www/virtual/{{ uberspace_asteroid_name }}/html" hidden: true file_type: any excludes: [nocontent.html] register: contents - name: Require an empty document root assert: that: contents.matched == 0 fail_msg: "Use a fresh Asteroid with an empty document root." - name: List existing databases command: argv: [mariadb, --batch, --skip-column-names, --execute=SHOW DATABASES] register: databases changed_when: false check_mode: false - name: Refuse to reuse an existing database assert: that: app_db_name not in databases.stdout_lines fail_msg: "Database {{ app_db_name }} already exists. Choose a new db_name." - name: Inspect existing web routes command: argv: [uberspace, web, backend, list, --json] register: routes changed_when: false check_mode: false - name: Refuse to replace another application's route assert: that: item.path != '/' or item.destination == 'APACHE' fail_msg: "{{ app_domain }}/ already serves another backend." loop: "{{ (routes.stdout | from_json) | selectattr('domain', 'equalto', app_domain) | list }}" loop_control: label: "{{ item.path }}" - name: Require an unused HTTP port wait_for: host: 127.0.0.1 port: "{{ app_port }}" state: stopped timeout: 1 when: not ansible_check_mode - name: List registered web domains command: argv: [uberspace, web, domain, list, --json] register: domains changed_when: false check_mode: false # Skip dependent mutations in check mode; preflight checks above still run. - name: Install Forgejo when: not ansible_check_mode block: - name: Find the latest stable Forgejo release uri: url: https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/latest return_content: true register: release - name: Require a stable release version assert: that: - not release.json.draft - not release.json.prerelease - release.json.tag_name is match('^v[0-9]+\.[0-9]+\.[0-9]+$') fail_msg: "Could not resolve a stable Forgejo release. No installation files have been created." - name: Use the resolved version throughout this installation set_fact: _version: "{{ release.json.tag_name | regex_replace('^v', '') }}" - name: Create private application directories file: path: "{{ item }}" state: directory mode: '0700' loop: - "{{ _root }}" - "{{ _root }}/custom/conf" - "{{ _root }}/data/forgejo-repositories" - "{{ _root }}/.gnupg" - "{{ _home }}/.config/systemd/user" - name: Download the binary and signature get_url: url: "{{ _release_url }}/{{ _binary }}{{ item }}" dest: "{{ _root }}/{{ _binary }}{{ item }}" mode: '0600' loop: ['', '.asc'] - name: Import the Forgejo release signing key into a dedicated keyring command: argv: [gpg, --batch, --homedir, "{{ _root }}/.gnupg", --keyserver, hkps://keys.openpgp.org, --recv-keys, "{{ _signing_key }}"] - name: Verify the binary signature before executing it command: argv: [gpg, --batch, --homedir, "{{ _root }}/.gnupg", --verify, "{{ _root }}/{{ _binary }}.asc", "{{ _root }}/{{ _binary }}"] changed_when: false - name: Make the verified binary executable file: path: "{{ _root }}/{{ _binary }}" mode: '0700' - name: Link the Forgejo binary file: src: "{{ _binary }}" dest: "{{ _root }}/forgejo" state: link - name: _Generate the configuration secret command: argv: ["{{ _root }}/forgejo", generate, secret, SECRET_KEY] register: secret no_log: true - name: _Write private Forgejo configuration copy: dest: "{{ _root }}/custom/conf/app.ini" mode: '0600' content: | RUN_USER = {{ uberspace_asteroid_name }} RUN_MODE = prod WORK_PATH = {{ _root }} [server] DOMAIN = {{ app_domain }} ROOT_URL = https://{{ app_domain }}/ HTTP_ADDR = 0.0.0.0 HTTP_PORT = {{ app_port }} SSH_DOMAIN = {{ app_domain }} SSH_USER = {{ uberspace_asteroid_name }} SSH_ALLOW_UNEXPECTED_AUTHORIZED_KEYS = true OFFLINE_MODE = true LFS_START_SERVER = true [database] DB_TYPE = mysql HOST = 127.0.0.1:3306 NAME = {{ app_db_name }} USER = {{ uberspace_asteroid_name }} PASSWD = `{{ uberspace_mariadb_password }}` [security] INSTALL_LOCK = true SECRET_KEY = {{ secret.stdout | trim }} [service] REQUIRE_SIGNIN_VIEW = true DISABLE_REGISTRATION = true SHOW_REGISTRATION_BUTTON = false REGISTER_EMAIL_CONFIRM = true DEFAULT_ORG_VISIBILITY = private DEFAULT_KEEP_EMAIL_PRIVATE = true NO_REPLY_ADDRESS = noreply.{{ uberspace_default_web_domain }} [mailer] ENABLED = true PROTOCOL = sendmail FROM = {{ uberspace_asteroid_name }}@uber.space [log] MODE = console LEVEL = Info no_log: true - name: Create the MariaDB database command: argv: [mariadb, "--execute=CREATE DATABASE `{{ app_db_name }}` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"] - name: Initialize the database command: argv: ["{{ _root }}/forgejo", migrate] chdir: "{{ _root }}" # Forgejo generates the password internally; never log it or put it in argv. - name: _Create the administrator with a random password command: argv: ["{{ _root }}/forgejo", admin, user, create, --username, "{{ app_admin_username }}", --email, "{{ app_admin_mailaddress }}", --admin, --random-password, --random-password-length, '32'] chdir: "{{ _root }}" no_log: true - name: Create the Forgejo service copy: dest: "{{ _home }}/.config/systemd/user/forgejo.service" mode: '0644' content: | [Unit] Description=Forgejo [Service] WorkingDirectory=%h/opt/forgejo ExecStart=%h/opt/forgejo/forgejo web Restart=on-failure RestartSec=5 UMask=0077 [Install] WantedBy=default.target - name: Start and enable Forgejo systemd: name: forgejo daemon_reload: true enabled: true state: started scope: user - name: Verify local Forgejo health uri: url: "http://127.0.0.1:{{ app_port }}/api/healthz" register: health until: health.status == 200 retries: 24 delay: 5 - name: Register the domain command: argv: [uberspace, web, domain, add, "{{ app_domain }}", --wait] when: app_domain not in (domains.stdout | from_json | map(attribute='name') | list) - name: Route the domain to Forgejo command: argv: [uberspace, web, backend, add, "{{ app_domain }}/", port, "{{ app_port | string }}", --force, --wait] - name: Verify public Forgejo health uri: url: "https://{{ app_domain }}/api/healthz" register: site until: site.status == 200 retries: 12 delay: 5 - name: _Report debug: msg: | Forgejo: https://{{ app_domain }} Installed version: {{ _version }} Username: {{ app_admin_username }} Use "Forgot password?" with {{ app_admin_mailaddress }} to set your password. Use a separate SSH key for Forgejo; keep your Uberspace shell login key. Updates are manual. Back up files and MariaDB before updating. Guide: https://u8lab.uberspace.de/guide_forgejo/