From b46e272d7977c786720cbdbbba297e50f5653100 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Sun, 17 Mar 2024 14:44:43 -0400 Subject: [PATCH] updated get-my-ip --- bin/terraform.sh | 10 ++++----- pydeps.requirements | 3 ++- terraform/update-myip.py | 38 ++++++++++++++++++++++++++++++++++ terraform/var.auto.tfvars.json | 14 +++++++++++++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 terraform/update-myip.py create mode 100644 terraform/var.auto.tfvars.json diff --git a/bin/terraform.sh b/bin/terraform.sh index 670bdbc..ff7874d 100755 --- a/bin/terraform.sh +++ b/bin/terraform.sh @@ -2,7 +2,7 @@ NS=terraforms TF_VARS_SECRET=homelab-tfvars -TFVAR_FILE_NAME=var.tfvars +TFVAR_FILE_NAME=var.auto.tfvars.json LOCAL_TF_VARS=./terraform/$TFVAR_FILE_NAME save_tfvars() { @@ -10,26 +10,26 @@ save_tfvars() { kubectl -n $NS create secret generic $TF_VARS_SECRET \ --save-config \ --dry-run=client \ - --from-file=var.tfvars=$LOCAL_TF_VARS \ + --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\.tfvars']}" \ + -o jsonpath="{.data['var\.auto\.tfvars\.json']}" \ | base64 -d > $LOCAL_TF_VARS } plan() { pushd ./terraform || exit 1 - terraform plan -var-file=$TFVAR_FILE_NAME + terraform plan } apply() { pushd ./terraform || exit 1 save_tfvars - terraform apply -var-file=$TFVAR_FILE_NAME + terraform apply } init() { 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/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/var.auto.tfvars.json b/terraform/var.auto.tfvars.json new file mode 100644 index 0000000..2e0dbdc --- /dev/null +++ b/terraform/var.auto.tfvars.json @@ -0,0 +1,14 @@ +{ + "cloudflare_api_token": "XjfyyAfvMjNK9-93QJNOWtrM3Bad-NAR8RdaPssG", + "myip": "24.212.175.249", + "domain_mappings": { + "sosnovsky.ca": { + "*": {}, + "*.home": {}, + "home": {} + }, + "skyg.ca": { + "*": {} + } + } +} \ No newline at end of file