Merge branch 'stable' of github.com:asosnovsky/homelab into stable

This commit is contained in:
Ariel Sosnovsky committed 2024-03-20 16:31:37 -04:00
commit 65be56d299
11 files changed
+194 -4

No files matched your search

+5 -2
View File
@@ -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
+5
View File
@@ -0,0 +1,5 @@
{
"recommendations": [
"hashicorp.terraform"
]
}
+74
View File
@@ -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
+2 -1
View File
@@ -6,7 +6,8 @@
pkgs.kubectl
pkgs.kubernetes-helm
pkgs.argocd
# pkgs.terraform
pkgs.yq
pkgs.jq
];
languages.nix.enable = true;
+1
View File
@@ -1,3 +1,4 @@
allowUnfree: true
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/release-23.11
+2 -1
View File
@@ -1 +1,2 @@
PyYAML
PyYAML
httpx
View File
Whitespace-only changes.
+34
View File
@@ -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
}
+19
View File
@@ -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"
}
}
+38
View File
@@ -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")
+14
View File
@@ -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"),
})))
}