-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer
executable file
·102 lines (75 loc) · 2.98 KB
/
deployer
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
#!/usr/bin/env python3
import yaml
import click
import os
import sys
import json
from jinja2 import Template
def read_config(filepath):
with open(filepath, 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
return data
@click.group()
def cli():
pass
def current_dir():
return os.path.dirname(os.path.abspath(__file__))
def get_default_config():
return current_dir() + '/config.yml'
def get_file_path(root, service):
return os.path.join(root, service)
def write2file(filepath, content):
with open(filepath, 'w') as f:
f.write(content)
def init_service(name, data):
root = get_file_path(current_dir(), name)
for filename in data.keys():
dest_filepath = get_file_path(root, filename)
template_filepath = dest_filepath + '.template'
print('Rendering %s' % template_filepath)
render_data = data.get(filename)
if os.path.exists(template_filepath):
with open(template_filepath) as f:
template = Template(f.read())
rendered = template.render(**render_data)
write2file(dest_filepath, rendered)
else:
print('No such template file: %s' % template_filepath)
sys.exit(1)
def clean_servie(name, data):
root = get_file_path(current_dir(), name)
for filename in data.keys():
dest_filepath = get_file_path(root, filename)
print("Remove %s" % dest_filepath)
os.remove(dest_filepath)
@click.command(help="Initialize compose services by the config file.")
@click.option('-c', '--config-file', default=get_default_config(), help='Config file for deployer, default value is config.yml')
@click.option('-d', '--debug', is_flag=True, help='More details.')
@click.option('-s', '--service', default='all', type=click.Choice(['all', 'core', 'auth', 'database']), help='Which service do you need to initialize? default value is all.')
def init(config_file, service, debug):
click.echo('Initialized the compose services')
if os.path.exists(config_file):
# Common + Services
config = read_config(config_file)
services = config.get('services')
for service in services.keys():
init_service(service, services.get(service))
if debug:
print(json.dumps(config, indent=4, sort_keys=True))
else:
print("No such config file: %s" % config_file)
@click.command(help="Clean all files which list in gitignore.")
@click.option('-c', '--config-file', default=get_default_config(), help='Config file for deployer, default value is config.yml')
def clean(config_file):
if os.path.exists(config_file):
# Common + Services
config = read_config(config_file)
services = config.get('services')
for service in services.keys():
clean_servie(service, services.get(service))
else:
print("No such config file: %s" % config_file)
cli.add_command(init)
cli.add_command(clean)
if __name__ == '__main__':
cli()