-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_sites.py
93 lines (74 loc) · 4.01 KB
/
generate_sites.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
import yaml
import sys
import os
from configobj import ConfigObj
def deep_key_update(dict_to_update: dict, key_path: list, value) -> dict:
key = key_path.pop(0)
if len(key_path) == 0:
dict_to_update[key] = value
return dict_to_update
else:
return deep_key_update(dict_to_update[key], key_path, value)
template_yaml_directory = 'template_yaml'
k8_client = os.path.join('k8', 'client')
k8_server = os.path.join('k8', 'server')
k8_level_site = os.path.join('k8', 'level_sites')
level_sites = int(sys.argv[1])
level_sites += 1
# Read the Template YAML file
with open(os.path.join(template_yaml_directory, 'level_site_service_template.yaml'), 'r') as fd:
level_site_service = yaml.safe_load(fd)
with open(os.path.join(template_yaml_directory, 'level_site_deployment_template.yaml'), 'r') as fd:
level_site_deployment = yaml.safe_load(fd)
all_level_site_domains = []
ports = []
# Create a list of all level-site domains, used for client and server
for level in range(1, level_sites):
level_site = f"ppdt-level-site-{level:02}-service"
all_level_site_domains.append(level_site)
port_number = 9000 + level - 1
ports.append(str(port_number))
all_domains_env = {'name': 'LEVEL_SITE_DOMAINS', 'value': ','.join(all_level_site_domains)}
# Update all values for service - LEVEL-SITE
for level in range(1, level_sites):
# Update the name
level_site_service['metadata']['name'] = f'ppdt-level-site-{level:02}-service'
level_site_service['spec']['selector']['pod'] = f'ppdt-level-site-{level:02}-deploy'
# Create new file
current_level_site_service_file = f"level_site_{level:02}_service.yaml"
with open(os.path.join(k8_level_site, current_level_site_service_file), 'w') as fd:
yaml.dump(level_site_service, fd)
# Update all values for deployment - LEVEL-SITE
for level in range(1, level_sites):
# Update the name
current_level_site_deploy_name = f'ppdt-level-site-{level:02}-deploy'
level_site_deployment['metadata']['name'] = current_level_site_deploy_name
level_site_deployment['metadata']['labels']['app'] = current_level_site_deploy_name
level_site_deployment['spec']['selector']['matchLabels']['pod'] = current_level_site_deploy_name
level_site_deployment['spec']['template']['metadata']['labels']['pod'] = current_level_site_deploy_name
deep_key_update(level_site_deployment, ['spec', 'template', 'spec', 'containers', 0, 'name'],
current_level_site_deploy_name)
# Create a new file
current_level_site_deploy_file = f"level_site_{level:02}_deployment.yaml"
with open(os.path.join(k8_level_site, current_level_site_deploy_file), 'w') as fd:
yaml.dump(level_site_deployment, fd)
# -------------------------------Update Client Template--------------------------------------------
with open(os.path.join(template_yaml_directory, 'client_deployment_template.yaml'), 'r') as fd:
client_site_deployment = yaml.safe_load(fd)
# Remember, you are OVERWRITING THE 'LEVEL_SITE_DOMAINS' entry, which is at index 5 on the template
deep_key_update(client_site_deployment, ['spec', 'template', 'spec', 'containers', 0, 'env', 5],
all_domains_env)
with open(os.path.join(k8_client, 'client_deployment.yaml'), 'w') as fd:
yaml.dump(client_site_deployment, fd)
# -------------------------------Update Server Template-------------------------------------------
with open(os.path.join(template_yaml_directory, 'server_deployment_template.yaml'), 'r') as fd:
server_deployment = yaml.safe_load(fd)
# Remember, you are OVERWRITING THE 'LEVEL_SITE_DOMAINS' entry, which is at index 3 on the template
deep_key_update(server_deployment, ['spec', 'template', 'spec', 'containers', 0, 'env', 3],
all_domains_env)
with open(os.path.join(k8_server, 'server_deployment.yaml'), 'w') as fd:
yaml.dump(server_deployment, fd)
# -------------------------------Might as well Update Properties File-----------------------------
config = ConfigObj("config.properties")
config['level-site-ports'] = ','.join(ports)
config.write()