From f1f147024c41eef7eb8019d49db0e217e27309e2 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sat, 28 Dec 2024 14:17:21 +1100 Subject: [PATCH] feat: initial setup - manage nomad jobs - create makefile - create gitignore - manage terragrunt environments - add build jobs --- .gitea/workflows/build.yaml | 36 +++++++++++++++++ .gitignore | 6 +++ Makefile | 33 +++++++++++++++ environments/au-syd1/terragrunt.hcl | 27 +++++++++++++ environments/root.hcl | 47 ++++++++++++++++++++++ jobs/testapp1.hcl | 62 +++++++++++++++++++++++++++++ jobs/testapp2.hcl | 62 +++++++++++++++++++++++++++++ policies/anonymous.hcl | 24 +++++++++++ shared/modules.tf | 31 +++++++++++++++ shared/variables.tf | 14 +++++++ 10 files changed, 342 insertions(+) create mode 100644 .gitea/workflows/build.yaml create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 environments/au-syd1/terragrunt.hcl create mode 100644 environments/root.hcl create mode 100644 jobs/testapp1.hcl create mode 100644 jobs/testapp2.hcl create mode 100644 policies/anonymous.hcl create mode 100644 shared/modules.tf create mode 100644 shared/variables.tf diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml new file mode 100644 index 0000000..4d41b1d --- /dev/null +++ b/.gitea/workflows/build.yaml @@ -0,0 +1,36 @@ +name: Build + +on: + pull_request: + +jobs: + build: + runs-on: almalinux-8 + container: + image: git.query.consul/unkin/almalinux8-runnerdnd:latest + options: --privileged + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install Terraform/Terragrunt + run: | + dnf install terraform terragrunt jq -y + + - name: Run Terraform Plan + env: + VAULT_ROLEID: ${{ secrets.TERRAFORM_NOMAD_VAULT_ROLEID }} + run: | + make plan + + - name: Show Plans + run: | + find /workspace -type f -name "*.plan" + + #- name: Upload Artifacts + # uses: actions/upload-artifact@v3 + # with: + # name: plans + # path: /workspace/unkin/rpmbuilder/dist/*/*.rpm + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f6856c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.terraform +.terraform.lock.hcl +environments/*/*.tf +plans +.venv +env diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..19d118e --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +SHELL := /bin/bash +ENVIRONMENT ?= au-syd1 +ENV_DIR = environments/$(ENVIRONMENT) +PLAN_DIR = plans +PLAN_FILE = ../../$(PLAN_DIR)/$(ENVIRONMENT).plan + +.PHONY: clean init plan apply + +define vault_env + @export VAULT_ADDR="https://vault.service.consul:8200" && \ + export VAULT_TOKEN=$$(vault write -field=token auth/approle/login role_id=$$VAULT_ROLEID) && \ + export $$(vault read -format=json kv/data/service/terraform/nomad | jq -r '.data.data | to_entries[] | "\(.key)=\(.value)"') +endef + +clean: + @echo "Cleaning Terraform files..." + find environments -type f -name '*.tf' -exec rm -f "{}" \; && \ + find environments -type f -name '.terraform.lock.hcl' -exec rm -f "{}" \; && \ + find environments -type d -name '.terraform' -exec rm -rf "{}" \; && \ + rm -rf plans + +init: + terragrunt --terragrunt-working-dir $(ENV_DIR) init + +plan: init + @mkdir -p $(PLAN_DIR) + $(call vault_env) + env | grep CONSUL + terragrunt --terragrunt-working-dir $(ENV_DIR) plan -out=$(PLAN_FILE) + +apply: + $(call vault_env) + terragrunt --terragrunt-working-dir $(ENV_DIR) apply $(PLAN_FILE) diff --git a/environments/au-syd1/terragrunt.hcl b/environments/au-syd1/terragrunt.hcl new file mode 100644 index 0000000..8c1b5e7 --- /dev/null +++ b/environments/au-syd1/terragrunt.hcl @@ -0,0 +1,27 @@ +include "root" { + path = find_in_parent_folders("root.hcl") +} + +inputs = { + job_files = [ + "testapp1", + "testapp2", + ] + policy_files = [] +} + +generate "shared_modules" { + if_exists = "overwrite" + path = "modules.tf" + + # Dynamically include the shared/modules.tf content + contents = file("../../shared/modules.tf") +} + +generate "shared_variables" { + if_exists = "overwrite" + path = "variables.tf" + + # Dynamically include the shared/variables.tf content + contents = file("../../shared/variables.tf") +} diff --git a/environments/root.hcl b/environments/root.hcl new file mode 100644 index 0000000..444457c --- /dev/null +++ b/environments/root.hcl @@ -0,0 +1,47 @@ +locals { + vault_addr = "https://vault.service.consul:8200" + nomad_addr = "https://nomad.service.consul:4646" +} + +generate "backend" { + path = "backend.tf" + if_exists = "overwrite_terragrunt" + contents = <