Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
profile: production
offline: true
strict: true
skip_list:
# Role and group variables use UPPER_SNAKE. Third-party role variables stay
# lowercase because those roles require those names.
- var-naming
exclude_paths:
- .git
- .github
- terraform
mock_roles:
- devsec.hardening.os_hardening
- devsec.hardening.ssh_hardening
- devsec.hardening.nginx_hardening
- geerlingguy.ansible-role-swap
- geerlingguy.ansible-role-certbot
- monolithprojects.github_actions_runner
55 changes: 55 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:best-practices", ":semanticCommits", ":enablePreCommit"],
"labels": ["dependencies"],
"prConcurrentLimit": 5,
"packageRules": [
{
"description": "Group GitHub Actions updates into one pull request.",
"matchManagers": ["github-actions"],
"groupName": "github-actions",
"automerge": true
},
{
"description": "Group pre-commit hook updates into one pull request.",
"matchManagers": ["pre-commit"],
"groupName": "pre-commit",
"automerge": true
},
{
"description": "Install OpenTofu providers from the OpenTofu registry and update them together.",
"matchDatasources": ["terraform-provider"],
"registryUrls": ["https://registry.opentofu.org"],
"groupName": "opentofu providers",
"automerge": true
},
{
"description": "Group Ansible Galaxy roles and collections into one pull request.",
"matchManagers": ["ansible-galaxy"],
"groupName": "ansible galaxy",
"automerge": true
},
{
"description": "Group Ansible Python packages, including the ansible-lint hook extra.",
"matchPackageNames": ["ansible", "ansible-core", "ansible-vault"],
"groupName": "ansible packages",
"automerge": true
}
],
"customManagers": [
{
"customType": "regex",
"description": "OpenTofu CLI version installed in CI.",
"managerFilePatterns": ["/^\\.github/workflows/pre-commit\\.yml$/"],
"matchStrings": ["tofu_version:\\s*(?<currentValue>\\d+\\.\\d+\\.\\d+)"],
"depNameTemplate": "opentofu",
"packageNameTemplate": "opentofu/opentofu",
"datasourceTemplate": "github-releases",
"extractVersionTemplate": "^v(?<version>\\d+\\.\\d+\\.\\d+)$"
}
],
"vulnerabilityAlerts": {
"enabled": true,
"labels": ["dependencies", "security"]
}
}
58 changes: 58 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Linting

on:
pull_request:
branches: [main]
push:
branches: [main]
workflow_dispatch: {}

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pre-commit:
name: pre-commit
runs-on: ubuntu-24.04
timeout-minutes: 15
env:
TF_IN_AUTOMATION: "true"
TF_INPUT: "false"
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/.opentofu-plugin-cache
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Restore tool caches
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: |
~/.cache/pre-commit
~/.cache/ansible-syntax
${{ github.workspace }}/.opentofu-plugin-cache
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml', 'ansible/requirements.yaml', 'terraform/boundaries/core-infra/providers.tf', 'terraform/boundaries/github/providers.tf', 'terraform/modules/droplet/providers.tf') }}
restore-keys: |
${{ runner.os }}-pre-commit-

- name: Set up OpenTofu
uses: opentofu/setup-opentofu@a1320f892987e89d278cc92dc5adc984fb93aca4 # v2.0.2
with:
tofu_version: 1.13.1
tofu_wrapper: false

- name: Set up Python
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: "3.14"

- name: Prepare OpenTofu plugin cache
run: mkdir -p "$TF_PLUGIN_CACHE_DIR"

- name: Run pre-commit
uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
crash.log

# Exclude all .tfvars files, which are likely to contain sentitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
#
*.tfvars
Expand Down Expand Up @@ -43,3 +43,9 @@ terraform.rc
.idea/
.fleet/
.vscode/

# Ansible
ansible/.vault-pass

# Python
ansible/.venv/
13 changes: 13 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default: true

# Command examples include a shell prompt and no sample output.
MD014: false

# Tables and terraform-docs type signatures exceed 80 columns.
MD013: false

# terraform-docs emits HTML anchors and preformatted type blocks.
MD033: false

# Generated module docs start at a second-level heading.
MD041: false
71 changes: 71 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- id: end-of-file-fixer
- id: mixed-line-ending
args: [--fix=lf]
- id: check-merge-conflict
- id: check-case-conflict
- id: check-added-large-files
- id: check-symlinks
- id: check-yaml
- id: detect-private-key

- repo: https://github.com/google/yamlfmt
rev: v0.21.0
hooks:
- id: yamlfmt
types: [yaml]

- repo: https://github.com/adrienverge/yamllint
rev: v1.38.0
hooks:
- id: yamllint
args: [--strict]

- repo: https://github.com/rhysd/actionlint
rev: v1.7.12
hooks:
- id: actionlint

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.49.1
hooks:
- id: markdownlint

- repo: https://github.com/ansible/ansible-lint
rev: v26.10.1
hooks:
- id: ansible-lint
args: [--strict]
additional_dependencies:
- ansible~=14.5

- repo: local
hooks:
- id: tofu-fmt
name: tofu fmt
entry: tofu fmt -recursive
language: system
pass_filenames: false
types: [terraform]

- id: tofu-validate
name: tofu validate
entry: bash -c 'set -euo pipefail; for root in terraform/boundaries/core-infra terraform/boundaries/github; do (cd "$root" && tofu init -backend=false -input=false && tofu validate); done'
language: system
pass_filenames: false
types: [terraform]

- id: ansible-syntax
name: ansible syntax
language: python
language_version: python3.14
additional_dependencies:
- ansible~=14.5
entry: bash scripts/ansible-syntax-check.sh
pass_filenames: false
files: ^ansible/
5 changes: 5 additions & 0 deletions .yamlfmt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
line_ending: lf
formatter:
type: basic
include_document_start: false
retain_line_breaks: true
15 changes: 15 additions & 0 deletions .yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extends: default

rules:
braces:
max-spaces-inside: 1
comments:
min-spaces-from-content: 1
comments-indentation: disable
document-start: disable
line-length: disable
octal-values:
forbid-explicit-octal: true
forbid-implicit-octal: true
truthy:
check-keys: false
42 changes: 16 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
# Infrastructure

This repository contains the Terraform resource definitions for the servers
operated for RethinkDB. This includes the download and update checker servers.
This repository manages RethinkDB servers on DigitalOcean. [OpenTofu](https://opentofu.org) creates the droplets. Ansible configures them. DNS stays with the DNS provider; this repository has no API access to it.

The DNS records are not managed with Terraform as we have no API access for the
DNS provider.
| Service | Host | Where it is defined |
| --- | --- | --- |
| Package repository (Nexus OSS) | `download.rethinkdb.com` | `terraform/boundaries/core-infra`, `ansible/playbooks/nexus-oss-repository.yml` |
| Update checker | `update.rethinkdb.com` | `terraform/boundaries/core-infra`, `ansible/playbooks/update-checker.yml` |
| Private GitHub Actions runners | `github_runner` inventory group | `terraform/boundaries/github`, `ansible/playbooks/private-github-runner.yml` |

## Getting started
`ansible/playbooks/initial-access.yml` creates the `ubuntu` login user and installs SSH keys before the service playbooks run.

### Prerequisites
Tokens and the Ansible Vault password stay on the operator machine. `*.tfvars` and `ansible/.vault-pass` are not committed.

- [Open Tofu](https://opentofu.org) cli
- [DigitalOcean](https://digitalocean.com) access
## Ansible

### Setup
Host configuration lives in [ansible/](ansible/README.md). Production secrets in that tree are Vault-encrypted. Install dependencies, place the Vault password, and run a playbook from the instructions there.

1. Create a new AWS profile in `~/.aws/credentials`, called `rethinkdb` with
the DigitalOcean spaces access key ID and Secret access key.
2. Create a new `secret.auto.tfvars` file to store your `digitalocean_token`.
3. Execute the commands below.
## Terraform

### Usage
Droplet definitions live in [terraform/](terraform/README.md) and are applied with OpenTofu. `boundaries/core-infra` holds the download and update servers. `boundaries/github` holds the private runners. Each boundary has its own state in DigitalOcean Spaces.

Initialize the terraform state and select your workspace:
## Checks

```bash
$ tofu init
```

See what plan would be executed:
Install [pre-commit](https://pre-commit.com) and enable the hooks:

```bash
$ tofu plan
pre-commit install
```

Check the final plan and apply the changes:

```bash
$ tofu apply
```
The hooks format Terraform, YAML, and Markdown, and they check YAML, GitHub Actions workflows, Ansible, and the OpenTofu configuration. OpenTofu checks run `tofu fmt -recursive`, then `tofu init -backend=false` and `tofu validate` in each boundary. Pull requests and pushes to `main` run the same hooks.
44 changes: 44 additions & 0 deletions ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Ansible

This directory contains Ansible deployment scripts for RethinkDB servers. The GitHub runner token stays in the `GITHUB_ACCESS_TOKEN` environment variable. When a file is Vault-encrypted, the password goes in `.vault-pass`, which is not committed.

## Getting Started

1. Create a Python 3.12 virtualenv and install Python requirements, `pip install -r requirements.txt`.
2. Install Ansible requirements, `ansible-galaxy install --force -r requirements.yaml`.
3. Execute `ansible-playbook playbooks/<playbook>.yml -l <limit>`, where `<playbook>` is the service-specific playbook, and `<limit>` is a specific host or group from the [inventory.ini](inventory.ini) file.

The first connection to a host stores its SSH key. A rebuilt droplet fails until the old key is removed with `ssh-keygen -R <host>`.

_**macOS notes: On macOS, add `export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES` to your `zshrc`, as ansible have a process forking bug: <https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#running-on-macos-as-a-control-node>. Also, make sure your `tar` command is `GNU tar`. On macOS, install it by executing `brew install gnu-tar`._

## Initial playbook runs

`playbooks/initial-access.yml` refuses to run without `--limit`. Pass `--limit all` only when every host should be updated. For DigitalOcean Droplets, use `ansible_user=root` until the `ubuntu` user exists. SSH keys for `ubuntu` are replaced with the keys published by the GitHub users in [group_vars/all.yml](group_vars/all.yml).

## Package upgrades

Playbooks install `unattended-upgrades` for security updates. A full package upgrade runs only when requested:

`ansible-playbook playbooks/<playbook>.yml -l <limit> --tags package-upgrade`

## Private GitHub runners

`playbooks/private-github-runner.yml` reads `GITHUB_ACCESS_TOKEN` from the environment. Add each runner to the `github_runner` group in [inventory.ini](inventory.ini). The runner version is pinned in the role defaults.

## Update checker

The update checker runs as the `update-checker` user under systemd. Read its logs with `journalctl -u rethinkdb-update`.

## Working with Ansible Vault

Before inlining any encrypted passwords, carefully consider whether it's truly necessary. Ansible Vault allows you to encrypt strings or files directly within Ansible without needing additional installations.

Always use the same Ansible Vault password for all encryptions. To encrypt a complete file, use `ansible-vault encrypt file.ext`. To view the contents later, use `ansible-vault view file.ext`.

Place the password in `.vault-pass` in this directory. Alternatively, provide the password interactively with `--ask-vault-pass`.

To encrypt a string for use as a variable, use `ansible-vault encrypt_string --name 'var_name' <secret>`. The resulting output can be used like any other variable.

To verify proper encryption, run:
`ansible localhost -m debug -a var="var_name" -e "@file_with_encrypted_var.yml"`.
16 changes: 16 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[defaults]
inventory = inventory.ini
remote_user = ubuntu
interpreter_python = auto_silent
forks = 25
timeout = 60
retry_files_enabled = false
vault_password_file = .vault-pass
callback_result_format = yaml

[ssh_connection]
pipelining = true
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=accept-new

[persistent_connection]
command_timeout = 60
3 changes: 3 additions & 0 deletions ansible/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COMMON_SSH_KEY_GITHUB_USERS:
- gabor-boros
- srh
8 changes: 8 additions & 0 deletions ansible/group_vars/package_repository.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NGINX_PROXY_PUBLIC_PORT: 443
NGINX_PROXY_INTERNAL_SCHEME: http
NGINX_PROXY_INTERNAL_PORT: 8081
NGINX_PROXY_USE_SSL: true

NGINX_PROXY_EXTRA_CONFIG: |
client_max_body_size 1G;
client_body_buffer_size 128k;
Loading
Loading