Reformat file
This commit is contained in:
@@ -65,6 +65,7 @@ def merge_config_file():
|
|||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def merge_defaults():
|
def merge_defaults():
|
||||||
global args
|
global args
|
||||||
default_args = {
|
default_args = {
|
||||||
@@ -96,36 +97,40 @@ if args["--zone"] is None:
|
|||||||
merge_defaults()
|
merge_defaults()
|
||||||
|
|
||||||
|
|
||||||
def get_addr(url,
|
def get_addr(
|
||||||
retry=int(args["--retry-attempts"]),
|
url, retry=int(args["--retry-attempts"]), delay=int(args["--retry-delay"])
|
||||||
delay=int(args["--retry-delay"])):
|
):
|
||||||
exception = None
|
exception = None
|
||||||
for i in range(retry):
|
for i in range(retry):
|
||||||
try:
|
try:
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
txt = urllib.request.urlopen(url).read()
|
txt = urllib.request.urlopen(url).read()
|
||||||
return txt.decode("utf-8")
|
return txt.decode("utf-8")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exception = e
|
exception = e
|
||||||
print(" connection failed, retrying in %d seconds..." % delay)
|
print(" connection failed, retrying in %d seconds..." % delay)
|
||||||
sleep(delay)
|
sleep(delay)
|
||||||
raise(exception)
|
raise (exception)
|
||||||
|
|
||||||
|
|
||||||
def get_all_records(zone):
|
def get_all_records(zone):
|
||||||
response = requests.get(url="https://dns.hetzner.com/api/v1/records",
|
response = requests.get(
|
||||||
|
url="https://dns.hetzner.com/api/v1/records",
|
||||||
params={"zone_id": zone["id"]},
|
params={"zone_id": zone["id"]},
|
||||||
headers={"Auth-API-Token": args["--token"]})
|
headers={"Auth-API-Token": args["--token"]},
|
||||||
|
)
|
||||||
return response.json()["records"]
|
return response.json()["records"]
|
||||||
|
|
||||||
|
|
||||||
def get_all_zones():
|
def get_all_zones():
|
||||||
response = requests.get(url="https://dns.hetzner.com/api/v1/zones",
|
response = requests.get(
|
||||||
headers={"Auth-API-Token": args["--token"]})
|
url="https://dns.hetzner.com/api/v1/zones",
|
||||||
|
headers={"Auth-API-Token": args["--token"]},
|
||||||
|
)
|
||||||
return response.json()["zones"]
|
return response.json()["zones"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def find_record(zone, name, kind="AAAA"):
|
def find_record(zone, name, kind="AAAA"):
|
||||||
all_records = get_all_records(zone)
|
all_records = get_all_records(zone)
|
||||||
for r in all_records:
|
for r in all_records:
|
||||||
@@ -139,7 +144,7 @@ def find_zone(name):
|
|||||||
for z in all_zones:
|
for z in all_zones:
|
||||||
if z["name"] == name:
|
if z["name"] == name:
|
||||||
return z
|
return z
|
||||||
raise(Exception("Zone not found: %s" % name))
|
raise (Exception("Zone not found: %s" % name))
|
||||||
|
|
||||||
|
|
||||||
def update_record(record):
|
def update_record(record):
|
||||||
@@ -149,7 +154,7 @@ def update_record(record):
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Auth-API-Token": args["--token"],
|
"Auth-API-Token": args["--token"],
|
||||||
},
|
},
|
||||||
data=json.dumps(record)
|
data=json.dumps(record),
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
@@ -161,7 +166,7 @@ def create_record(record):
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Auth-API-Token": args["--token"],
|
"Auth-API-Token": args["--token"],
|
||||||
},
|
},
|
||||||
data=json.dumps(record)
|
data=json.dumps(record),
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
@@ -189,20 +194,20 @@ def main():
|
|||||||
print(" %s" % addr)
|
print(" %s" % addr)
|
||||||
|
|
||||||
print("Finding existing %s record..." % kind)
|
print("Finding existing %s record..." % kind)
|
||||||
rec = find_record(zone=zone,
|
rec = find_record(zone=zone, kind=kind, name=args["--hostname"])
|
||||||
kind=kind,
|
|
||||||
name=args["--hostname"])
|
|
||||||
|
|
||||||
if rec is None:
|
if rec is None:
|
||||||
print(" not found")
|
print(" not found")
|
||||||
print("Creating new %s record..." % kind)
|
print("Creating new %s record..." % kind)
|
||||||
create_record({
|
create_record(
|
||||||
|
{
|
||||||
"value": addr,
|
"value": addr,
|
||||||
"type": kind,
|
"type": kind,
|
||||||
"name": args["--hostname"],
|
"name": args["--hostname"],
|
||||||
"zone_id": args["--zone"],
|
"zone_id": args["--zone"],
|
||||||
"ttl": args["--ttl"],
|
"ttl": args["--ttl"],
|
||||||
})
|
}
|
||||||
|
)
|
||||||
print(" done")
|
print(" done")
|
||||||
else:
|
else:
|
||||||
print(" found")
|
print(" found")
|
||||||
|
|||||||
Reference in New Issue
Block a user