-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (108 loc) · 3.89 KB
/
main.py
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
119
120
121
122
123
124
125
import boto3
import os
import ipaddress
import time
CCD_DIR = '/etc/openvpn/vpn.aceiot.cloud-ccd'
ROOT_DOMAIN = 'aceiot.cloud.'
HOSTS_ROOT = f'clients.{ROOT_DOMAIN}'
PROM_SRV_RECORD = "_prom-edge._tcp.aceiot.cloud."
r53 = boto3.client('route53')
# results = r53.get_hosted_zone(Id='aceiot.cloud.')
def get_hosted_zone_id(name, private=True):
results = r53.list_hosted_zones_by_name()
for result in results['HostedZones']:
if result['Name'] == name and result['Config']['PrivateZone'] is private:
return result['Id']
def get_records():
results = r53.list_hosted_zones_by_name()
for result in results['HostedZones']:
if result['Name'] == 'aceiot.cloud.' and result['Config']['PrivateZone'] is True:
HZ = r53.get_hosted_zone(Id=result['Id'])
return r53.list_resource_record_sets(HostedZoneId=HZ['HostedZone']['Id'])['ResourceRecordSets']
def get_edge_hosts():
ips = []
for root, subdirs, files in os.walk(CCD_DIR):
for path in files:
with open(os.path.join(root, path), 'r') as f:
if path != 'energy_data_test':
name_parts = path.split('-')
hostname = f"{name_parts[1].replace('_', '-')}.{name_parts[0]}"
for line in f:
segments = line.split()
if len(segments) > 2 and segments[0] == 'ifconfig-push':
ips.append((hostname, ipaddress.ip_address(segments[1])))
return ips
def get_edge_ips():
ips = []
for root, subdirs, files in os.walk(CCD_DIR):
for path in files:
with open(os.path.join(root, path), 'r') as f:
for line in f:
segments = line.split()
if len(segments) > 2 and segments[0] == 'ifconfig-push':
ips.append(ipaddress.ip_address(segments[1]))
return ips
def create_a_change_batch(hosts):
ChangeBatch = {
'Comment': "openvpn hosts update",
'Changes': []
}
for host, ip in hosts:
ChangeBatch['Changes'].append(
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': f"{host}.{HOSTS_ROOT}",
'Type': 'A',
'TTL': 300,
'ResourceRecords': [
{"Value": f"{ip}"}]
}
})
return ChangeBatch
def create_srv_record_set(hosts):
record_set = {
'Name': PROM_SRV_RECORD,
'Type': 'SRV',
'TTL': 300,
'ResourceRecords': []
}
for host, ip in hosts:
record_set['ResourceRecords'].append({
"Value": f"0 100 9100 {host}.clients.aceiot.cloud"
})
return record_set
def set_change_batch(change_batch, zone_id):
return r53.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch=change_batch)
def set_record_set(record_set, zone_id):
return r53.change_resource_record_sets(
HostedZoneId=zone_id,
ChangeBatch={
'Comment': "openvpn update",
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': record_set
}
]
})
if __name__ == '__main__':
zid = get_hosted_zone_id(ROOT_DOMAIN, private=True)
while True:
try:
hosts = get_edge_hosts()
records = create_srv_record_set(hosts)
results = set_record_set(records, zid)
print(results)
changes = create_a_change_batch(hosts)
results = set_change_batch(changes, zid)
print(results)
waiter = r53.get_waiter('resource_record_sets_changed')
waiter.wait(Id=results['ChangeInfo']['Id'])
print('Successful update')
except Exception as e:
raise e
pass
time.sleep(300)