-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmodify_init_all.py
More file actions
executable file
·116 lines (97 loc) · 4.14 KB
/
modify_init_all.py
File metadata and controls
executable file
·116 lines (97 loc) · 4.14 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
#!/usr/bin/env python3
# Copyright (C) 2024-2026 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""This module will load incus.json file and use data to modify the init_all.py file."""
import json
import sys
def print_json(data):
"""Print JSON.
:param data: JSON representation
:type data: dict
"""
print(json.dumps(data, indent=4))
def load_json():
"""Load JSON from file into dict.
:return: dict
:rtype: dict
"""
with open('incus.json', encoding='utf-8') as json_file:
data = json.load(json_file)
return data
def save_to_init_all(key, value):
"""Append key=value to init_all.py.
:param key: key
:type key: str
:param value: value
:type value: str
"""
with open('init_all.py', 'a', encoding='utf-8') as file:
file.write(f'\n{key} = "{value}"')
def get_addresses(addresses_lst):
"""Get relevant addresses from the addresses section of a network from incus.json.
:param addresses_lst: addresses from a network from incus.json
:type addresses_lst: dict
:return: v4, v6 global, v6 link local
:rtype: tuple[str, str, str]
"""
ipv4_addr = None
ipv6_global = None
ipv6_link_local = None
for addr in addresses_lst:
if addr['family'] == 'inet':
ipv4_addr = addr['address']
if addr['family'] == 'inet6' and addr['scope'] == 'global':
ipv6_global = addr['address']
if addr['family'] == 'inet6' and addr['scope'] == 'link':
ipv6_link_local = addr['address']
# check if all are not None
if ipv4_addr is None or ipv6_global is None or ipv6_link_local is None:
print(f'ERROR: Some addresses are None: {ipv4_addr=}, {ipv6_global=}, {ipv6_link_local=}')
sys.exit(1)
return ipv4_addr, ipv6_global, ipv6_link_local
def main():
"""Entry point."""
data = load_json()
# this is temporary solution, we need to redesign address management if we want to have proper hub-and-spoke tests.
for device in data:
if device['name'] == 'kea-forge':
for key, value in device['state']["network"].items():
if key in ['lo', 'eth0']:
continue
ipv4, ipv6_global, ipv6_link_local = get_addresses(value['addresses'])
if key == 'eth1':
save_to_init_all("CLIENT_IPV6_ADDR_GLOBAL", ipv6_global)
save_to_init_all("CLNT4_ADDR", ipv4)
save_to_init_all("GIADDR4", ipv4)
save_to_init_all("CIADDR", ipv4)
else:
save_to_init_all(f"CLNT4_ADDR_{key[-1]}", ipv4)
continue
else:
for key, value in device['state']["network"].items():
# so far we ignore eth2
if key in ['lo', 'eth2']:
continue
ipv4, ipv6_global, ipv6_link_local = get_addresses(value['addresses'])
if key == 'eth0':
if device['name'] == 'kea-1':
save_to_init_all("MGMT_ADDRESS", ipv4)
else:
save_to_init_all(f"MGMT_ADDRESS_{device['name'][-1]}", ipv4)
continue
if device['name'] == 'kea-1':
save_to_init_all("SRV4_ADDR", ipv4)
save_to_init_all("DNS4_ADDR", ipv4)
save_to_init_all("DNS6_ADDR", ipv6_global)
save_to_init_all("SRV_IPV6_ADDR_GLOBAL", ipv6_global)
save_to_init_all("SRV_IPV6_ADDR_LINK_LOCAL", ipv6_link_local)
# this can be used in the future with different names
else:
save_to_init_all(f"SRV4_ADDR_{device['name'][-1]}", ipv4)
save_to_init_all(f"SRV_IPV6_ADDR_GLOBAL_{device['name'][-1]}", ipv6_global)
save_to_init_all(f"SRV_IPV6_ADDR_LINK_LOCAL_{device['name'][-1]}", ipv6_link_local)
if __name__ == '__main__':
main()