263 lines
9.2 KiB
Python
263 lines
9.2 KiB
Python
#!/usr/bin/env python
|
|
|
|
import json
|
|
import jinja2
|
|
import os
|
|
import argparse
|
|
import subprocess
|
|
|
|
|
|
|
|
def generate_global_configmap_yaml(json_data):
|
|
template_global_configmap_dir='template/'
|
|
global_configmap_dir='applications/'
|
|
template_global_configmaps = ['nginx.yaml','backend.yaml']
|
|
for global_configmap in template_global_configmaps:
|
|
template_global_config_file=f'{template_global_configmap_dir}{global_configmap}'
|
|
with open(template_global_config_file,'r') as file:
|
|
template = jinja2.Template(file.read())
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
)
|
|
global_configmap_file=f'{global_configmap_dir}{global_configmap}'
|
|
with open(global_configmap_file, 'w') as global_file:
|
|
global_file.write(rendered_yaml)
|
|
|
|
|
|
|
|
def generate_configmap_yaml(json_data, app):
|
|
template_global_configmap_dir='template/'
|
|
global_configmap_dir='applications/'
|
|
template_app_configmap_file=f'{template_global_configmap_dir}{app}''/configmap.yaml'
|
|
|
|
if not os.path.exists(template_app_configmap_file):
|
|
print(f"{template_app_configmap_file} does have configmap. exiting.")
|
|
return
|
|
|
|
app_configmap_path=f'applications/{app}'
|
|
if not os.path.isdir(app_configmap_path):
|
|
os.makedirs(app_configmap_path)
|
|
|
|
app_configmap_file=f'{app_configmap_path}''/configmap.yaml'
|
|
with open(template_app_configmap_file,'r') as template_app_configmap:
|
|
template = jinja2.Template(template_app_configmap.read())
|
|
superDomain = ["tmuyun-h5"]
|
|
if app in superDomain:
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
superDomain=json_data['superDomain'],
|
|
name=app
|
|
)
|
|
else:
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
domain=json_data['domain'],
|
|
name=app
|
|
)
|
|
with open(app_configmap_file,'w') as app_configmap:
|
|
app_configmap.write(rendered_yaml)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_deployment_yaml(json_data, app):
|
|
|
|
app_template = 'template/%s/deployment.yaml' % app
|
|
if os.path.exists(app_template):
|
|
pass
|
|
else:
|
|
print("%s 文件不存在" %app_template)
|
|
exit(1)
|
|
|
|
|
|
with open(app_template, 'r') as file:
|
|
template = jinja2.Template(file.read())
|
|
|
|
superApp=["tmuyun-h5"]
|
|
superGateway=["tmy-gateway"]
|
|
tssApp=["tss-api"]
|
|
|
|
for deployment in json_data['deployments']:
|
|
|
|
namespace=json_data['namespace'],
|
|
imageSecret=json_data['imageSecret'],
|
|
deployment_name=deployment['name'],
|
|
image=deployment['image'],
|
|
replicas=deployment['replicas'],
|
|
limits_cpu=deployment['resources']['limits']['cpu'],
|
|
limits_memory=deployment['resources']['limits']['memory'],
|
|
requests_cpu=deployment['resources']['requests']['cpu'],
|
|
requests_memory=deployment['resources']['requests']['memory'],
|
|
domain=json_data['domain'],
|
|
superDomain=json_data['superDomain'],
|
|
ingressClassName=json_data['ingressClassName']
|
|
|
|
|
|
if app != deployment['name']:
|
|
continue
|
|
else:
|
|
if app in superApp:
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
imageSecret=json_data['imageSecret'],
|
|
deployment_name=deployment['name'],
|
|
image=deployment['image'],
|
|
replicas=deployment['replicas'],
|
|
limits_cpu=deployment['resources']['limits']['cpu'],
|
|
limits_memory=deployment['resources']['limits']['memory'],
|
|
requests_cpu=deployment['resources']['requests']['cpu'],
|
|
requests_memory=deployment['resources']['requests']['memory'],
|
|
superDomain=json_data['superDomain'],
|
|
ingressClassName=json_data['ingressClassName']
|
|
)
|
|
elif app in superGateway:
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
imageSecret=json_data['imageSecret'],
|
|
deployment_name=deployment['name'],
|
|
image=deployment['image'],
|
|
replicas=deployment['replicas'],
|
|
limits_cpu=deployment['resources']['limits']['cpu'],
|
|
limits_memory=deployment['resources']['limits']['memory'],
|
|
requests_cpu=deployment['resources']['requests']['cpu'],
|
|
requests_memory=deployment['resources']['requests']['memory'],
|
|
superDomain=json_data['superDomain'],
|
|
domain=json_data['domain'],
|
|
ingressClassName=json_data['ingressClassName']
|
|
)
|
|
elif app in tssApp:
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
imageSecret=json_data['imageSecret'],
|
|
deployment_name=deployment['name'],
|
|
image=deployment['image'],
|
|
replicas=deployment['replicas'],
|
|
limits_cpu=deployment['resources']['limits']['cpu'],
|
|
limits_memory=deployment['resources']['limits']['memory'],
|
|
requests_cpu=deployment['resources']['requests']['cpu'],
|
|
requests_memory=deployment['resources']['requests']['memory'],
|
|
tssDomain=json_data['tssDomain'],
|
|
ingressClassName=json_data['ingressClassName']
|
|
)
|
|
else:
|
|
rendered_yaml = template.render(
|
|
namespace=json_data['namespace'],
|
|
imageSecret=json_data['imageSecret'],
|
|
deployment_name=deployment['name'],
|
|
image=deployment['image'],
|
|
replicas=deployment['replicas'],
|
|
limits_cpu=deployment['resources']['limits']['cpu'],
|
|
limits_memory=deployment['resources']['limits']['memory'],
|
|
requests_cpu=deployment['resources']['requests']['cpu'],
|
|
requests_memory=deployment['resources']['requests']['memory'],
|
|
domain=json_data['domain'],
|
|
ingressClassName=json_data['ingressClassName']
|
|
)
|
|
|
|
app_dir = 'applications/%s' %app
|
|
if not os.path.isdir(app_dir):
|
|
os.makedirs(app_dir)
|
|
print("%s 目录已创建" %app_dir)
|
|
|
|
app_deployment = app_dir+'/deployment.yaml'
|
|
|
|
with open(app_deployment, 'w') as file:
|
|
file.write(rendered_yaml)
|
|
print("Generated %s" %app_deployment)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def before_apply():
|
|
front_configmap='applications/nginx.yaml'
|
|
backend_config='applications/tmly.yaml'
|
|
|
|
cmd_front='kubectl apply -f %s' %front_configmap
|
|
cmd_backend='kubectl apply -f %s' %backend_config
|
|
subprocess.run(cmd_front,shell=True)
|
|
subprocess.run(cmd_backend,shell=True)
|
|
|
|
|
|
def delete_after():
|
|
front_configmap='applications/nginx.yaml'
|
|
backend_config='applications/tmly.yaml'
|
|
|
|
cmd_front='kubectl delete -f %s' %front_configmap
|
|
cmd_backend='kubectl delete -f %s' %backend_config
|
|
subprocess.run(cmd_front,shell=True)
|
|
subprocess.run(cmd_backend,shell=True)
|
|
|
|
def apply(app):
|
|
app_configmap='applications/%s/configmap.yaml'%app
|
|
app_deployment='applications/%s/deployment.yaml' %app
|
|
cmd_configmap='kubectl apply -f %s' %app_configmap
|
|
cmd_deployment = 'kubectl apply -f %s' %app_deployment
|
|
|
|
if os.path.exists(app_configmap):
|
|
subprocess.run(cmd_configmap,shell=True)
|
|
|
|
subprocess.run(cmd_deployment,shell=True)
|
|
|
|
|
|
|
|
def delete(app):
|
|
app_configmap='applications/%s/configmap.yaml'%app
|
|
app_deployment='applications/%s/deployment.yaml' %app
|
|
cmd_configmap='kubectl delete -f %s' %app_configmap
|
|
cmd_deployment = 'kubectl delete -f %s' %app_deployment
|
|
|
|
subprocess.run(cmd_deployment,shell=True)
|
|
|
|
if os.path.exists(app_configmap):
|
|
subprocess.run(cmd_configmap,shell=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open('global.json', 'r') as file:
|
|
json_data = json.load(file)
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="D")
|
|
parser.add_argument("action",type=str,help="config,build,apply,delete")
|
|
parser.add_argument("apps",type=str,help="app name or -")
|
|
args = parser.parse_args()
|
|
|
|
action = args.action
|
|
apps = args.apps
|
|
_app = []
|
|
|
|
# generate_global_configmap_yaml(json_data)
|
|
|
|
if apps == '-':
|
|
for app in json_data['deployments']:
|
|
_app.append(app['name'])
|
|
else:
|
|
_app.append(apps)
|
|
|
|
# generate configmap
|
|
if action == 'config':
|
|
generate_global_configmap_yaml(json_data)
|
|
for app in _app:
|
|
generate_configmap_yaml(json_data,app)
|
|
|
|
# generate deployment
|
|
if action == 'build':
|
|
for app in _app:
|
|
generate_deployment_yaml(json_data,app)
|
|
|
|
# apply configmap,deployment
|
|
if action == 'apply':
|
|
before_apply()
|
|
for app in _app:
|
|
apply(app)
|
|
|
|
# delete deployment,configmap
|
|
if action == 'delete':
|
|
for app in _app:
|
|
delete(app)
|
|
# delete_after()
|
|
|