This repository was archived by the owner on Feb 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
161 lines (131 loc) · 6.03 KB
/
Copy pathcontroller.py
File metadata and controls
161 lines (131 loc) · 6.03 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import yaml
from ansible_runner import run
from filelock import Timeout, FileLock
from southbound import deploy_container, deploy_cdn, deploy_logserver, deploy_routes
import os
from pprint import pprint as pp
# print(f"Controller: {os.getcwd()}")
# print(deploy_container.main())
# exit(0)
states = ['requested', 'processing', 'active', 'terminate']
database = "test_master_db.yaml"
# lock_path = f"{database}.lock"
# lock = FileLock(lock_path, timeout=1)
def run_playbook(filename, vars:dict):
# Define the custom variables
# vars = {'web_server': 'example.com', 'port': '8080'}
# Define the Ansible Runner configuration
config = {
'private_data_dir': './',
'playbook': filename,
'extravars': vars
}
# Run the Ansible playbook using Ansible Runner
result = run(**config)
# Print the output
# print(result.stdout.read())
return result
def update_db(data):
with open(database, 'w') as f:
yaml.dump(data, f, sort_keys=False)
return
def check_requests(data):
for tenantkey,tenantval in data['tenants'].items():
if tenantval['state'] == states[0]:
data['tenants'][tenantkey]['state'] = states[1]
update_db(data)
print(f'Tenant requested: {tenantval["name"]}')
create_tenant(tenantkey)
#####
#create a logging server and a transit vpc
if 'logging' in tenantval.keys() and tenantval['logging'] == 'True':
create_logserver(tenantkey)
print(f'Tenant created: {tenantval["name"]}')
data['tenants'][tenantkey]['state'] = states[2]
update_db(data)
for vpckey, vpcval in (tenantval['vpcs']).items():
if vpcval['state'] == states[0]:
data['tenants'][tenantkey]['vpcs'][vpckey]['state'] = states[1]
update_db(data)
print(f'VPC requested: {vpcval["name"]}')
create_vpc(vpckey, vpcval)
#######
#connect vpc with transit if requested
if 'logging' in tenantval.keys() and tenantval['logging'] == 'True':
deploy_routes.main(vpckey, tenantkey, vpcval['tip'], vpcval['tipremote'])
print(f'VPC created: {vpcval["name"]}')
data['tenants'][tenantkey]['vpcs'][vpckey]['state'] = states[2]
update_db(data)
if 'subnets' in vpcval.keys():
for subkey,subval in (vpcval['subnets']).items():
if subval['state'] == states[0]:
data['tenants'][tenantkey]['vpcs'][vpckey]['subnets'][subkey]['state'] = states[1]
update_db(data)
print(f'Subnet requested: {subval["name"]}')
create_subnet(subkey, subval)
print(f'Subnet created: {subval["name"]}')
data['tenants'][tenantkey]['vpcs'][vpckey]['subnets'][subkey]['state'] = states[2]
update_db(data)
if 'vms' in vpcval.keys():
for vmk, vmv in (vpcval['vms']).items():
if vmv['state'] == states[0]:
data['tenants'][tenantkey]['vpcs'][vpckey]['vms'][vmk]['state'] = states[1]
update_db(data)
print(f'VM requested: {vmv["name"]}')
create_vm(vmk, vmv)
print(f'VM created: {vmv["name"]}')
data['tenants'][tenantkey]['vpcs'][vpckey]['vms'][vmk]['state'] = states[2]
update_db(data)
if 'containers' in vpcval.keys():
for conkey, conval in (vpcval['containers']).items():
if conval['state'] == states[0]:
data['tenants'][tenantkey]['vpcs'][vpckey]['containers'][conkey]['state'] = states[1]
update_db(data)
print(f'Container requested: {conval["name"]}')
create_container(conkey, conval)
print(f'Container created: {conval["name"]}')
data['tenants'][tenantkey]['vpcs'][vpckey]['containers'][conkey]['state'] = states[2]
update_db(data)
if 'cdn' in vpcval.keys():
cdn = vpcval['cdn']
if cdn['state'] == states[0]:
data['tenants'][tenantkey]['vpcs'][vpckey]['cdn']['state'] = states[1]
update_db(data)
print(f'CDN requested with origin: {cdn["origin"]}:{cdn["originport"]}')
# data['tenants'][tenantkey]
if 'logging' in tenantval.keys() and tenantval['logging'] == 'True':
# create cdnwith logging
create_cdn(cdn, vpckey, 'True')
else:
#create cdn without logging
create_cdn(cdn, vpckey, None)
print(f'CDN deployed with local: {cdn["primaryip"]}')
data['tenants'][tenantkey]['vpcs'][vpckey]['cdn']['state'] = states[2]
update_db(data)
return
def create_tenant(tenant):
run_playbook('southbound/onboard-tenant.yaml', {'id':tenant})
return
def create_vpc(vpcid, vpc):
run_playbook('southbound/create-namespace.yaml', {'vpcid':vpcid, **vpc})
return
def create_subnet(subid, subnet):
run_playbook('southbound/create-subnet.yaml', {'subid':subid, **subnet})
return
def create_vm(vmid, vm):
run_playbook('southbound/create-vm.yaml', vm.update({'vmid':vmid}))
return
def create_container(conname,con):
deploy_container.main(conname,con)
return
def create_cdn(cdn, vpckey, logip):
deploy_cdn.main(cdn, vpckey, logip)
return
def create_logserver(tenantkey):
deploy_logserver.main(tenantkey)
return
if __name__ == '__main__':
with open(database, 'r') as f:
data = yaml.safe_load(f)
check_requests(data)
# lock.release()