forked from armadaplatform/armada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_create.py
70 lines (56 loc) · 2.99 KB
/
command_create.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
from __future__ import print_function
import os
import shutil
import tempfile
from armada_command.armada_utils import execute_local_command, ArmadaCommandException
from armada_command.dockyard import dockyard
def add_arguments(parser):
parser.add_argument('name',
help='Name of the created microservice.')
parser.add_argument('-b', '--base-template', default='python3',
help='Base microservice template. Possible choices: python, python3, node')
def _replace_in_file_content(file_path, old, new):
with open(file_path) as f:
content = f.read()
content = content.replace(old, new)
with open(file_path, 'w') as f:
f.write(content)
def _replace_in_path(path, old, new):
for subpath, dir_names, file_names in os.walk(path):
for file_name in file_names:
file_path = os.path.join(subpath, file_name)
_replace_in_file_content(file_path, old, new)
if old in file_name:
new_file_path = file_path.replace(old, new)
shutil.move(file_path, new_file_path)
def _get_template_name(base):
return "microservice_{base}_template".format(**locals())
def command_create(args):
base_template = _get_template_name(args.base_template)
service_name = args.name or base_template
destination_dir = os.path.join(os.getcwd(), service_name)
if os.path.exists(destination_dir):
raise ArmadaCommandException('Destination dir {destination_dir} already exists.'.format(**locals()))
command_list_code, command_list_out, command_list_err = execute_local_command('armada list armada -q | head -1')
if command_list_code != 0:
raise ArmadaCommandException('Could not get Armada container id:\n{command_list_err}'.format(**locals()))
path_to_base_template = os.path.join('/opt/templates', base_template)
armada_container_id = command_list_out.strip()
temp_dir = tempfile.mkdtemp()
try:
command_cp = 'docker cp {armada_container_id}:{path_to_base_template} {temp_dir}'.format(**locals())
command_cp_code, command_cp_out, command_cp_err = execute_local_command(command_cp)
if command_cp_code != 0:
raise ArmadaCommandException('Could not get microservice template:\n{command_cp_err}'.format(**locals()))
shutil.move(os.path.join(temp_dir, base_template), destination_dir)
if service_name != base_template:
upper_template = base_template.upper()
template_name_variable = '_{upper_template}_'.format(**locals())
_replace_in_path(destination_dir, template_name_variable, service_name)
dockyard_address = dockyard.get_dockyard_address()
template_dockyard_variable = '_DOCKYARD_ADDRESS_'
_replace_in_path(destination_dir, template_dockyard_variable, dockyard_address)
print('Service {service_name} has been created in {destination_dir} from {args.base_template} template.'
.format(**locals()))
finally:
shutil.rmtree(temp_dir)