/
inventory_handler.py
executable file
·111 lines (90 loc) · 3.21 KB
/
inventory_handler.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import os
import subprocess
from minio import Minio # type: ignore
def fetch_tfstate(workspace: str):
m_client = Minio(
"s3.isk01.sakurastorage.jp",
access_key=os.environ.get("TF_STATE_ACCESS_KEY"),
secret_key=os.environ.get("TF_STATE_SECRET_KEY"),
secure=True,
)
response = m_client.get_object(
"ictsc-drove",
f"env:/{workspace}/terraform.tfstate",
)
return json.loads(response.read())
def get_workspace():
cmd = "terraform workspace show"
working_dir = f"{os.path.dirname(__file__)}/../../terraform"
return (
subprocess.Popen(cmd, cwd=working_dir, stdout=subprocess.PIPE, shell=True)
.communicate()[0]
.decode("utf-8")
.strip()
)
def main():
inventory = {
"control_plane": {"hosts": []},
"worker_node": {"hosts": []},
"router": {"hosts": []},
"_meta": {"hostvars": {}},
}
workspace = get_workspace()
tfstate = fetch_tfstate(workspace)
# Uncomment below to see the tfstate object
#
# import pprint
# print(tfstate["outputs"])
router = 0
control_plane = 0
worker_node = 0
inventory_gp = {}
for output_key in tfstate["outputs"]:
match output_key:
case "k8s_control_plane_ip_address":
inventory_gp = inventory["control_plane"]
case "k8s_worker_node_ip_address":
inventory_gp = inventory["worker_node"]
case "k8s_router_ip_address":
inventory_gp = inventory["router"]
case _:
continue
for ip_address in tfstate["outputs"][output_key]["value"]:
# not handle private ip address
if ip_address[:11] == "192.168.100":
continue
inventory_gp["hosts"].append(ip_address)
match output_key:
case "k8s_router_ip_address":
inventory["_meta"]["hostvars"] = inventory["_meta"]["hostvars"] | {
ip_address: {"internal_ip": f"192.168.100.{str(router)}"}
}
router += 1
case "k8s_control_plane_ip_address":
inventory["_meta"]["hostvars"] = inventory["_meta"]["hostvars"] | {
ip_address: {
"internal_ip": f"192.168.100.1{str(control_plane)}"
}
}
control_plane += 1
case "k8s_worker_node_ip_address":
inventory["_meta"]["hostvars"] = inventory["_meta"]["hostvars"] | {
ip_address: {"internal_ip": f"192.168.100.2{str(worker_node)}"}
}
worker_node += 1
inventory["control_plane"]["vars"] = {
"VIP": tfstate["outputs"]["vip_address"]["value"]
}
inventory["router"]["vars"] = {
"bgp_address": tfstate["outputs"]["external_address_range"]["value"]
}
inventory["delegate_plane"] = {
"hosts": [inventory["control_plane"]["hosts"][0]],
"vars": {"workspace": workspace},
}
print(json.dumps(inventory))
if __name__ == "__main__":
main()