updated get-my-ip

This commit is contained in:
ari committed 2024-03-17 14:44:43 -04:00
1 parent f01e86a1ac
commit b46e272d79
4 files changed
+59 -6

No files matched your search

+5 -5
View File
@@ -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() {
+2 -1
View File
@@ -1 +1,2 @@
PyYAML
PyYAML
httpx
+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 @@
{
"cloudflare_api_token": "XjfyyAfvMjNK9-93QJNOWtrM3Bad-NAR8RdaPssG",
"myip": "24.212.175.249",
"domain_mappings": {
"sosnovsky.ca": {
"*": {},
"*.home": {},
"home": {}
},
"skyg.ca": {
"*": {}
}
}
}