-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2-cloud.py
More file actions
executable file
·118 lines (104 loc) · 3.35 KB
/
Copy pathec2-cloud.py
File metadata and controls
executable file
·118 lines (104 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
import subprocess
import configparser
import sys
from os import path
from cloudflare import Cloudflare
config = configparser.ConfigParser()
configfile = path.expanduser("~/.cloudflare/my-script.ini")
config.read(configfile)
# config example
# [domain.com]
# zone_name = domain.com
# zone_id = (cloudflare zone id) (optional)
# subdomains = foo bar
# ipv6 = False
# token = (Cloudflare api token)
# prox-domains = @ bar ( any subdomains you want proxied )
def getdns(image):
describe_images_cmd = [
"aws", "ec2", "describe-images",
"--filters", f"Name=name,Values=*{image}*",
"--query", "Images[].ImageId",
"--output", "text"
]
try:
ami_ids = subprocess.check_output(describe_images_cmd, text=True).strip()
except subprocess.CalledProcessError as e:
print(f"Error running describe-images: {e}")
ami_ids = ""
if not ami_ids:
sys.exit("No matching images found!")
dname = subprocess.run(
[
"aws",
"ec2",
"describe-instances",
"--filters", "Name=instance-state-name,Values=running", f"Name=image-id,Values={ami_ids}",
"--query",
"Reservations[].Instances[].PublicDnsName",
"--output",
"text",
],
capture_output=True,
universal_newlines=True,
)
return dname.stdout.strip('"\n')
def main():
image = "openssh"
dname = getdns(image)
if dname is None or dname == "":
sys.exit("No instance found!")
for section in config.sections():
myconfig = config[section]
zone_name = myconfig["zone_name"]
try:
ttl = int(myconfig["ttl"])
except KeyError:
ttl = 1 # Fallback to auto
cf = Cloudflare(api_token=myconfig["token"])
try:
zone_id = myconfig["zone_id"]
except KeyError:
print("zone_id not specified, querying the api")
# params = {"name": zone_name}
search = cf.zones.list(name=zone_name)
zone_id = search.result[0].id
dns_records = [
{
"name": "keep." + zone_name,
"type": "CNAME",
"content": dname,
"ttl": ttl,
},
]
# if "@" in myconfig["prox-domains"]:
# dns_records[0]["proxied"] = True
# else:
dns_records[0]["proxied"] = False
def getrecord(name, ipv):
record = cf.dns.records.list(zone_id=zone_id, name=name, type=ipv)
return record
def getrecordid(name, ipv):
record = getrecord(name, ipv)
record = record.result[0]
return record.id
print("zone id: " + zone_id)
for mrecord in dns_records:
# print(ip4)
print("record: ", end="")
print(mrecord)
record_id = getrecordid(mrecord["name"], mrecord["type"])
print("record id: " + record_id)
r = cf.dns.records.update(
zone_id=zone_id,
dns_record_id=record_id,
name=mrecord["name"],
type=mrecord["type"],
content=mrecord["content"],
ttl=mrecord["ttl"],
)
print(r)
exit(0)
if __name__ == "__main__":
main()