diff --git a/.gitignore b/.gitignore index 970b053..05e7479 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,10 @@ devenv.local.nix .pre-commit-config.yaml # Terraform -*.tfvars +*.tfvars* .terraform -os/routers/openwrt-mesh/.env .DS_Store +.terraform.lock.hcl + +# router +os/routers/openwrt-mesh/.env diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..cad1a21 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "hashicorp.terraform" + ] +} \ No newline at end of file diff --git a/bin/terraform.sh b/bin/terraform.sh new file mode 100755 index 0000000..ff7874d --- /dev/null +++ b/bin/terraform.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +NS=terraforms +TF_VARS_SECRET=homelab-tfvars +TFVAR_FILE_NAME=var.auto.tfvars.json +LOCAL_TF_VARS=./terraform/$TFVAR_FILE_NAME + +save_tfvars() { + echo "$LOCAL_TF_VARS ... > kubectl -n $NS create secret/$TF_VARS_SECRET" + kubectl -n $NS create secret generic $TF_VARS_SECRET \ + --save-config \ + --dry-run=client \ + --from-file=var.auto.tfvars.json=$LOCAL_TF_VARS \ + -o yaml | kubectl apply -f - +} + +get_tfvars() { + echo "kubectl -n $NS get secret/$TF_VARS_SECRET ... > $LOCAL_TF_VARS" + kubectl -n $NS get secret/$TF_VARS_SECRET \ + -o jsonpath="{.data['var\.auto\.tfvars\.json']}" \ + | base64 -d > $LOCAL_TF_VARS +} + +plan() { + pushd ./terraform || exit 1 + terraform plan +} + +apply() { + pushd ./terraform || exit 1 + save_tfvars + terraform apply +} + +init() { + pushd ./terraform || exit 1 + get_tfvars + terraform init +} +help() { + echo "My Awesome Terraform Wrapper!" + echo "" + echo " a|apply) apply terraform in ./terraform folder" + echo " p|plan) plan terraform in ./terraform folder" + echo " i|init) download latest tfvars and init ./terraform folder" + echo " s|save-tfvars) save tfvars to k8s ns" + echo " g|get-tfvars) downloads tfvars from k8s ns" + echo " h|help) display this help" + echo "" +} +case $1 in + a|apply) + apply + ;; + p|plan) + plan + ;; + i|init) + init + ;; + s|save-tfvars) + save_tfvars + ;; + g|get-tfvars) + get_tfvars + ;; + h|help) + help + ;; + *) + echo "invalid input '$1'!" + help + ;; +esac \ No newline at end of file diff --git a/devenv.nix b/devenv.nix index 2b14344..9598da0 100644 --- a/devenv.nix +++ b/devenv.nix @@ -6,7 +6,8 @@ pkgs.kubectl pkgs.kubernetes-helm pkgs.argocd - # pkgs.terraform + pkgs.yq + pkgs.jq ]; languages.nix.enable = true; diff --git a/devenv.yaml b/devenv.yaml index 05972cf..6b9fb94 100644 --- a/devenv.yaml +++ b/devenv.yaml @@ -1,3 +1,4 @@ +allowUnfree: true inputs: nixpkgs: url: github:NixOS/nixpkgs/release-23.11 diff --git a/pydeps.requirements b/pydeps.requirements index dbfc709..8ef4243 100644 --- a/pydeps.requirements +++ b/pydeps.requirements @@ -1 +1,2 @@ -PyYAML \ No newline at end of file +PyYAML +httpx \ No newline at end of file diff --git a/shell.nix b/shell.nix deleted file mode 100644 index e69de29..0000000 diff --git a/terraform/dns.tf b/terraform/dns.tf new file mode 100644 index 0000000..6f2f4d0 --- /dev/null +++ b/terraform/dns.tf @@ -0,0 +1,34 @@ +data "cloudflare_zone" "this" { + for_each = var.domain_mappings + name = each.key +} + +locals { + records = flatten([ + for zone, records in var.domain_mappings : [ + for r, def in records : + { + "zone" : zone, + "record" : r, + "type" : def.type != null ? def.type : "A", + "ip" : def.ip != null ? def.ip : var.myip, + "zone_id" : data.cloudflare_zone.this[zone].id, + } + ] + ]) + current_date = formatdate("YYYY-MM-DD", timestamp()) + record_comment = "Updated by Terraform at ${local.current_date}" +} + +resource "cloudflare_record" "this" { + for_each = { + for d in local.records : + "${d.record}.${d.zone}" => d + } + zone_id = each.value.zone_id + name = each.value.record + value = each.value.ip + comment = local.record_comment + type = each.value.type + proxied = false +} \ No newline at end of file diff --git a/terraform/provider.tf b/terraform/provider.tf new file mode 100644 index 0000000..a9b45be --- /dev/null +++ b/terraform/provider.tf @@ -0,0 +1,19 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + version = "~> 4.0" + } + } +} + +provider "cloudflare" { + api_token = var.cloudflare_api_token +} + +terraform { + backend "kubernetes" { + secret_suffix = "homelab" + namespace = "terraforms" + } +} \ No newline at end of file diff --git a/terraform/update-myip.py b/terraform/update-myip.py new file mode 100644 index 0000000..a696a4e --- /dev/null +++ b/terraform/update-myip.py @@ -0,0 +1,38 @@ +import httpx +import json +import os +from pathlib import Path + +MYIP_URL = os.environ.get("MYIP_URL", "https://api.myip.com/") +MYIP_KEY = os.environ.get("MYIP_KEY", "myip") + +def get_auto_vars_file(): + if p := os.environ.get("AUTO_TFVARS", None): + return Path(p) + return Path(__file__).parent / "var.auto.tfvars.json" + +auto_tfvars = get_auto_vars_file() + +print("Starting ip-update with") +print(f" > auto_tfvars = {auto_tfvars}") +print(f" > MYIP_URL = {MYIP_URL}") +print(f" > MYIP_KEY = {MYIP_KEY}") + +resp = httpx.get(MYIP_URL) + +if resp.status_code != 200: + raise AssertionError(f"{MYIP_URL} returned {resp}") + +data = resp.json() +print(f"myip response = {data}") + +myip = data['ip'] + +if not auto_tfvars.exists(): + raise AssertionError(f"{auto_tfvars} does not exists!") + +auto_tfvars_data = json.loads(auto_tfvars.read_text()) +auto_tfvars_data[MYIP_KEY] = myip +auto_tfvars.write_text(json.dumps(auto_tfvars_data, indent=4)) + +print("DONE") \ No newline at end of file diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..009c2cc --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,14 @@ +variable "myip" {} +variable "cloudflare_api_token" {} +variable "main_domain" { + default = "sosnovsky.ca" +} +variable "secondary_domain" { + default = "skyg.ca" +} +variable "domain_mappings" { + type = map(map(object({ + ip = optional(string), + type = optional(string, "A"), + }))) +}