|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
"""Setup post actions, used in main setup.py.""" |
|
3
|
|
|
|
|
4
|
|
|
import os |
|
5
|
|
|
import shutil |
|
6
|
|
|
import sys |
|
7
|
|
|
import click |
|
8
|
|
|
from yaml import safe_load, dump |
|
9
|
|
|
from stakkr import file_utils |
|
10
|
|
|
from stakkr.actions import StakkrActions |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
@click.command(help="""Initialize for the first time stakkr by copying |
|
14
|
|
|
templates and directory structure""") |
|
15
|
|
|
@click.option('--force', '-f', help="Force recreate directories structure", is_flag=True) |
|
16
|
|
|
@click.argument('recipe', required=False) |
|
17
|
|
|
def init(force: bool, recipe: str = None): |
|
18
|
|
|
"""CLI Entry point, when initializing stakkr manually.""" |
|
19
|
|
|
config_file = os.getcwd() + '/stakkr.yml' |
|
20
|
|
|
if os.path.isfile(config_file) and force is False: |
|
21
|
|
|
click.secho( |
|
22
|
|
|
'Config file (stakkr.yml) already present. Leaving.', fg='yellow') |
|
23
|
|
|
return |
|
24
|
|
|
|
|
25
|
|
|
click.secho('Create some required files / directories', fg='green') |
|
26
|
|
|
install_filetree(force) |
|
27
|
|
|
if recipe is not None: |
|
28
|
|
|
install_recipe(recipe.lower()) |
|
29
|
|
|
msg = "Recipe has been installed" |
|
30
|
|
|
else: |
|
31
|
|
|
msg = "Config (stakkr.yml) not present, don't forget to create it" |
|
32
|
|
|
|
|
33
|
|
|
click.secho(msg, fg='green') |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def install_filetree(force: bool = False): |
|
37
|
|
|
"""Create templates (directories and files).""" |
|
38
|
|
|
required_dirs = [ |
|
39
|
|
|
'conf/mysql-override', |
|
40
|
|
|
'conf/php-fpm-override', |
|
41
|
|
|
'conf/xhgui-override', |
|
42
|
|
|
'data', |
|
43
|
|
|
'home/www-data', |
|
44
|
|
|
'home/www-data/bin', |
|
45
|
|
|
'logs', |
|
46
|
|
|
'services', |
|
47
|
|
|
'www' |
|
48
|
|
|
] |
|
49
|
|
|
for required_dir in required_dirs: |
|
50
|
|
|
_create_dir(os.getcwd(), required_dir, force) |
|
51
|
|
|
|
|
52
|
|
|
required_tpls = [ |
|
53
|
|
|
# 'bash_completion', # How to do with a system wide installation ? |
|
54
|
|
|
'stakkr.yml.tpl', |
|
55
|
|
|
'conf/mysql-override/mysqld.cnf', |
|
56
|
|
|
'conf/php-fpm-override/example.conf', |
|
57
|
|
|
'conf/php-fpm-override/README', |
|
58
|
|
|
'conf/xhgui-override/config.php', |
|
59
|
|
|
'home/www-data/.bashrc' |
|
60
|
|
|
] |
|
61
|
|
|
for required_tpl in required_tpls: |
|
62
|
|
|
_copy_file(os.getcwd(), required_tpl, force) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def install_recipe(recipe_name: str): |
|
66
|
|
|
"""Install a recipe (do all tasks)""" |
|
67
|
|
|
# Get config |
|
68
|
|
|
recipe_config = _recipe_get_config(recipe_name) |
|
69
|
|
|
with open(recipe_config, 'r') as stream: |
|
70
|
|
|
recipe = safe_load(stream) |
|
71
|
|
|
|
|
72
|
|
|
# Install everything declared in the recipe |
|
73
|
|
|
click.secho('Installing services') |
|
74
|
|
|
_recipe_install_services(recipe['services']) |
|
75
|
|
|
|
|
76
|
|
|
click.secho('Creating config') |
|
77
|
|
|
_recipe_create_stakkr_config(recipe['config']) |
|
78
|
|
|
|
|
79
|
|
|
click.secho('Starting stakkr (can take a few minutes)') |
|
80
|
|
|
stakkr = _recipe_init_stakkr() |
|
81
|
|
|
stakkr.start(None, True, True, True) |
|
82
|
|
|
|
|
83
|
|
|
click.secho('Running commands') |
|
84
|
|
|
_recipe_run_commands(stakkr, recipe['commands']) |
|
85
|
|
|
_recipe_display_messages(stakkr, recipe['messages']) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
def _create_dir(project_dir: str, dir_name: str, force: bool): |
|
89
|
|
|
"""Create a directory from stakkr skel""" |
|
90
|
|
|
dir_name = project_dir + '/' + dir_name.lstrip('/') |
|
91
|
|
|
if os.path.isdir(dir_name) and force is False: |
|
92
|
|
|
return |
|
93
|
|
|
|
|
94
|
|
|
if not os.path.isdir(dir_name): |
|
95
|
|
|
os.makedirs(dir_name) |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
def _copy_file(project_dir: str, source_file: str, force: bool): |
|
99
|
|
|
"""Copy a file from a template to project dir""" |
|
100
|
|
|
full_path = file_utils.get_file('tpls', source_file) |
|
101
|
|
|
dest_file = project_dir + '/' + source_file |
|
102
|
|
|
if os.path.isfile(dest_file) and force is False: |
|
103
|
|
|
click.secho(' - {} exists, do not overwrite'.format(source_file)) |
|
104
|
|
|
return |
|
105
|
|
|
|
|
106
|
|
|
click.secho(' - {} written'.format(source_file)) |
|
107
|
|
|
try: |
|
108
|
|
|
shutil.copy(full_path, dest_file) |
|
109
|
|
|
except Exception: |
|
110
|
|
|
msg = "Error trying to copy {} .. check that the file is there ...\n" |
|
111
|
|
|
sys.stderr.write(msg.format(full_path)) |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
def _recipe_get_config(recipe: str): |
|
115
|
|
|
"""Get recipe""" |
|
116
|
|
|
if recipe is None: |
|
117
|
|
|
return '' |
|
118
|
|
|
|
|
119
|
|
|
recipe_config = file_utils.get_file( |
|
120
|
|
|
'static/recipes', '{}.yml'.format(recipe)) |
|
121
|
|
|
if os.path.isfile(recipe_config) is False: |
|
122
|
|
|
click.secho('"{}" recipe does not exist'.format(recipe), fg='red') |
|
123
|
|
|
sys.exit(1) |
|
124
|
|
|
|
|
125
|
|
|
return recipe_config |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
def _recipe_create_stakkr_config(config: dict): |
|
129
|
|
|
"""Build stakkr config from recipe""" |
|
130
|
|
|
with open('stakkr.yml', 'w') as outfile: |
|
131
|
|
|
dump(config, outfile, default_flow_style=False) |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
def _recipe_install_services(services: list): |
|
135
|
|
|
"""Install services defined in recipe""" |
|
136
|
|
|
from stakkr.services import install |
|
137
|
|
|
|
|
138
|
|
|
for service in services: |
|
139
|
|
|
success, message = install('services', service, service) |
|
140
|
|
|
if success is False: |
|
141
|
|
|
click.echo(click.style(' 😧 {}'.format(message), fg='red')) |
|
142
|
|
|
sys.exit(1) |
|
143
|
|
|
|
|
144
|
|
|
if message is not None: |
|
145
|
|
|
click.echo(click.style(' 😊 {}'.format(message), fg='yellow')) |
|
146
|
|
|
continue |
|
147
|
|
|
|
|
148
|
|
|
click.echo(click.style( |
|
149
|
|
|
' 😀 Package "{}" Installed'.format(service), fg='green')) |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
def _recipe_init_stakkr(): |
|
153
|
|
|
"""Initialize Stakkr""" |
|
154
|
|
|
return StakkrActions({ |
|
155
|
|
|
'CONFIG': '{}/stakkr.yml'.format(os.getcwd()), |
|
156
|
|
|
'VERBOSE': False, |
|
157
|
|
|
'DEBUG': False |
|
158
|
|
|
}) |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
def _recipe_run_commands(stakkr: StakkrActions, commands: dict): |
|
162
|
|
|
"""Run all commands defined in recipe""" |
|
163
|
|
|
for title, cmd in commands.items(): |
|
164
|
|
|
click.secho(' ↳ {}'.format(title)) |
|
165
|
|
|
user = cmd['user'] if 'user' in cmd else 'root' |
|
166
|
|
|
stakkr.exec_cmd(cmd['container'], user, cmd['args'], True) |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
def _recipe_display_messages(stakkr: StakkrActions, recipe_messages: list): |
|
170
|
|
|
"""Build messages to display after installing a recipe""" |
|
171
|
|
|
services_ports = stakkr.get_services_urls() |
|
172
|
|
|
click.secho('\nServices URLs :') |
|
173
|
|
|
click.secho(services_ports) |
|
174
|
|
|
|
|
175
|
|
|
if recipe_messages: |
|
176
|
|
|
click.secho('Recipe messages:', fg='green') |
|
177
|
|
|
|
|
178
|
|
|
for message in recipe_messages: |
|
179
|
|
|
click.secho(' - {}'.format(message)) |
|
180
|
|
|
|