This repository was archived by the owner on Feb 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathold_controller.py
More file actions
127 lines (104 loc) · 3.87 KB
/
Copy pathold_controller.py
File metadata and controls
127 lines (104 loc) · 3.87 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
import yaml
from ansible_runner import run
from filelock import Timeout, FileLock
states = ['requested', 'processing', 'active', 'terminate']
database = "hw4q2.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 i,ten in enumerate(data['tenants']):
if ten['state'] == states[0]:
data['tenants'][i]['state'] = states[1]
update_db(data)
print(f'Tenant requested: {ten["name"]}')
create_tenant(ten)
print(f'Tenant created: {ten["name"]}')
data['tenants'][i]['state'] = states[2]
update_db(data)
for j,vpc in enumerate(ten['vpcs']):
if vpc['state'] == states[0]:
data['tenants'][i]['vpcs'][j]['state'] = states[1]
update_db(data)
print(f'VPC requested: {vpc["name"]}')
create_vpc(vpc)
print(f'VPC created: {vpc["name"]}')
data['tenants'][i]['vpcs'][j]['state'] = states[2]
update_db(data)
for k,subnet in enumerate(vpc['subnets']):
if subnet['state'] == states[0]:
data['tenants'][i]['vpcs'][j]['subnets'][k]['state'] = states[1]
update_db(data)
print(f'Subnet requested: {subnet["name"]}')
create_subnet(subnet)
print(f'Subnet created: {subnet["name"]}')
data['tenants'][i]['vpcs'][j]['subnets'][k]['state'] = states[2]
update_db(data)
for k,vm in enumerate(vpc['vms']):
if vm['state'] == states[0]:
data['tenants'][i]['vpcs'][j]['vms'][k]['state'] = states[1]
update_db(data)
print(f'VM requested: {vm["name"]}')
create_vm(vm)
print(f'VM created: {vm["name"]}')
data['tenants'][i]['vpcs'][j]['vms'][k]['state'] = states[2]
update_db(data)
return
def create_tenant(tenant):
run_playbook('southbound/onboard-tenant.yaml', tenant)
return
def create_vpc(vpc):
run_playbook('southbound/create-namespace.yaml', vpc)
return
def create_subnet(subnet):
run_playbook('southbound/create-subnet.yaml', subnet)
return
def create_vm(vm):
run_playbook('southbound/create-vm.yaml', vm)
return
if __name__ == '__main__':
# vars = {
# 'vms' : [
# {
# "capacity": 12,
# "image_path": "/var/quickly/base/jammy-server-cloudimg-amd64-disk-kvm.img",
# "interface": [
# {
# "ipaddr": "192.168.131.2/24",
# "mac": "52:54:00:12:34:56",
# "name": "enp0s2",
# "subid": "1144"
# }
# ],
# "memory": 2048,
# "name": "vm11",
# "state": "requested/provisioning/running/stopped",
# "vcpu": 2,
# "vmid": "1155"
# }
# ]
# }
# run_playbook('test.yaml', vars)
# exit(0)
# lock.acquire()
with open(database, 'r') as f:
data = yaml.safe_load(f)
check_requests(data)
# lock.release()