Passed
Push — master ( 13afb2...a9e5f8 )
by Emmanuel
05:18
created

stakkr.cli.stop()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 3
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# coding: utf-8
3 1
"""
4
CLI Entry Point.
5
6
From click, build stakkr.
7
Give all options to manage services to be launched, stopped, etc.
8
"""
9
10 1
import sys
11 1
import click
12 1
from click_plugins import with_plugins
13 1
from pkg_resources import iter_entry_points
14 1
from stakkr.docker_actions import get_running_containers_names
15
16
17 1
@with_plugins(iter_entry_points('stakkr.plugins'))
18 1
@click.group(help="""Main CLI Tool that easily create / maintain
19
a stack of services, for example for web development.
20
21
Read the configuration file and setup the required services by
22
linking and managing everything for you.""")
23 1
@click.version_option('4.0b1')
24 1
@click.option('--config', '-c', help='Change the configuration file')
25 1
@click.option('--debug/--no-debug', '-d', default=False)
26 1
@click.option('--verbose', '-v', is_flag=True)
27 1
@click.pass_context
28
def stakkr(ctx, config, debug, verbose):
29
    """Click group, set context and main object."""
30 1
    from stakkr.actions import StakkrActions
31
32 1
    ctx.obj['CONFIG'] = config
33 1
    ctx.obj['DEBUG'] = debug
34 1
    ctx.obj['VERBOSE'] = verbose
35 1
    ctx.obj['STAKKR'] = StakkrActions(ctx.obj)
36
37
38 1
@stakkr.command(help="""Enter a container to perform direct actions such as
39
install packages, run commands, etc.""")
40 1
@click.argument('container', required=True)
41 1
@click.option('--user', '-u', help="User's name. Valid choices : www-data or root")
42 1
@click.option('--tty/--no-tty', '-t/ ', is_flag=True, default=True, help="Use a TTY")
43 1
@click.pass_context
44 1
def console(ctx, container: str, user: str, tty: bool):
45
    """See command Help."""
46 1
    ctx.obj['STAKKR'].init_project()
47 1
    ctx.obj['CTS'] = get_running_containers_names(ctx.obj['STAKKR'].project_name)
48 1
    if len(ctx.obj['CTS']) is not 0:
49 1
        ct_choice = click.Choice(ctx.obj['CTS'])
50 1
        ct_choice.convert(container, None, ctx)
51
52 1
    ctx.obj['STAKKR'].console(container, _get_cmd_user(user, container), tty)
53
54
55 1
@stakkr.command(help="""Execute a command into a container.
56
57
Examples:\n
58
- ``stakkr -v exec mysql mysqldump -p'$MYSQL_ROOT_PASSWORD' mydb > /tmp/backup.sql``\n
59
- ``stakkr exec php php -v`` : Execute the php binary in the php container with option -v\n
60
- ``stakkr exec apache service apache2 restart``\n
61
""", name='exec', context_settings=dict(ignore_unknown_options=True, allow_interspersed_args=False))
62 1
@click.pass_context
63 1
@click.option('--user', '-u', help="User's name. Be careful, each container have its own users.")
64 1
@click.option('--tty/--no-tty', '-t/ ', is_flag=True, default=True, help="Use a TTY")
65 1
@click.argument('container', required=True)
66 1
@click.argument('command', required=True, nargs=-1, type=click.UNPROCESSED)
67 1
def exec_cmd(ctx, user: str, container: str, command: tuple, tty: bool):
68
    """See command Help."""
69 1
    ctx.obj['STAKKR'].init_project()
70 1
    ctx.obj['CTS'] = get_running_containers_names(ctx.obj['STAKKR'].project_name)
71 1
    if len(ctx.obj['CTS']) is not 0:
72 1
        click.Choice(ctx.obj['CTS']).convert(container, None, ctx)
73
74 1
    ctx.obj['STAKKR'].exec_cmd(container, _get_cmd_user(user, container), command, tty)
75
76
77 1
@stakkr.command(help="""`stakkr mysql` is a wrapper for the mysql binary
78
located in the mysql service.
79
80
You can run any mysql command as root, such as :\n
81
- ``stakkr mysql -e "CREATE DATABASE mydb"`` to create a DB from outside\n
82
- ``stakkr mysql`` to enter the mysql console\n
83
- ``cat myfile.sql | stakkr mysql --no-tty mydb`` to import a file from outside to mysql\n
84
85
For scripts, you must use the relative path.
86
""", context_settings=dict(ignore_unknown_options=True, allow_interspersed_args=False))
87 1
@click.pass_context
88 1
@click.option('--tty/--no-tty', '-t/ ', is_flag=True, default=True, help="Use a TTY")
89 1
@click.argument('command', nargs=-1, type=click.UNPROCESSED)
90 1
def mysql(ctx, tty: bool, command: tuple):
91
    """See command Help."""
92 1
    command = ('mysql', '-p$MYSQL_ROOT_PASSWORD') + command
93 1
    ctx.invoke(exec_cmd, user='root', container='mysql', command=command, tty=tty)
94
95
96 1
@stakkr.command(help='Required to be launched if you install a new plugin', name="refresh-plugins")
97 1
@click.pass_context
98
def refresh_plugins(ctx):
99
    """See command Help."""
100
    from stakkr.plugins import add_plugins
101
102
    print(click.style('Not implemented yet in v4', fg='red'))
103
    exit(1)
104
105
    print(click.style('Adding plugins from plugins/', fg='green'))
106
    plugins = add_plugins()
107
    if len(plugins) is 0:
108
        print(click.style('No plugin to add', fg='yellow'))
109
        exit(0)
110
111
    print()
112
    print(click.style('Plugins refreshed', fg='green'))
113
114
115 1
@stakkr.command(help="Restart all (or a single as CONTAINER) container(s)")
116 1
@click.argument('container', required=False)
117 1
@click.option('--pull', '-p', help="Force a pull of the latest images versions", is_flag=True)
118 1
@click.option('--recreate', '-r', help="Recreate all containers", is_flag=True)
119 1
@click.option('--proxy/--no-proxy', '-P', help="Restart the proxy", default=True)
120 1
@click.pass_context
121 1
def restart(ctx, container: str, pull: bool, recreate: bool, proxy: bool):
122
    """See command Help."""
123 1
    print(click.style('[RESTARTING]', fg='green') + ' your stakkr services')
124 1
    try:
125 1
        ctx.invoke(stop, container=container, proxy=proxy)
126 1
    except Exception:
127 1
        print()
128
129 1
    ctx.invoke(start, container=container, pull=pull, recreate=recreate, proxy=proxy)
130
131
132 1
@stakkr.command(help="List available services available for compose.ini (with info if the service is enabled)")
133 1
@click.pass_context
134
def services(ctx):
135
    """See command Help."""
136
    ctx.obj['STAKKR'].init_project()
137
138
    from stakkr.stakkr_compose import get_available_services
139
140
    print('Available services usable in stakkr.yml ', end='')
141
    print('({} = disabled) : '.format(click.style('✘', fg='red')))
142
143
    services = ctx.obj['STAKKR']._get_config()['services']
144
    enabled_services = [svc for svc, opts in services.items() if opts['enabled'] is True]
145
    available_services = get_available_services(ctx.obj['STAKKR'].project_dir)
146
    for available_service in sorted(list(available_services.keys())):
147
        sign = click.style('✘', fg='red')
148
        if available_service in enabled_services:
149
            version = services[available_service]['version']
150
            sign = click.style(str(version), fg='green')
151
152
        print('  - {} ({})'.format(available_service, sign))
153
154
155 1
@stakkr.command(help="Download a pack of services from github (see github) containing services")
156 1
@click.argument('package', required=True)
157 1
@click.pass_context
158 1
def services_add(ctx, package: str):
159
    """See command Help."""
160
    from stakkr.services import install
161
162
    project_dir = _get_project_dir(ctx.obj['CONFIG'])
163
    services_dir = '{}/services'.format(project_dir)
164
    success, message = install(services_dir, package)
165
    if success is False:
166
        click.echo(click.style(message, fg='red'))
167
        sys.exit(1)
168
169
    stdout_msg = 'Package "' + click.style(package, fg='green') + '" installed successfully'
170
    if message is not None:
171
        stdout_msg = click.style(message, fg='yellow')
172
173
    click.echo(stdout_msg)
174
    click.echo('Try ' + click.style('stakkr services', fg='green') + ' to see new available services')
175
176
177 1
@stakkr.command(help="Update all services packs in services/")
178 1
@click.pass_context
179
def services_update(ctx):
180
    """See command Help."""
181
    from stakkr.services import update_all
182
183
    project_dir = _get_project_dir(ctx.obj['CONFIG'])
184
    services_dir = '{}/services'.format(project_dir)
185
    update_all(services_dir)
186
    print(click.style('Packages updated', fg='green'))
187
188
189 1
@stakkr.command(help="Start all (or a single as CONTAINER) container(s) defined in compose.ini")
190 1
@click.argument('container', required=False)
191 1
@click.option('--pull', '-p', help="Force a pull of the latest images versions", is_flag=True)
192 1
@click.option('--recreate', '-r', help="Recreate all containers", is_flag=True)
193 1
@click.option('--proxy/--no-proxy', '-P', help="Start proxy", default=True)
194 1
@click.pass_context
195 1
def start(ctx, container: str, pull: bool, recreate: bool, proxy: bool):
196
    """See command Help."""
197 1
    print(click.style('[STARTING]', fg='green') + ' your stakkr services')
198
199 1
    ctx.obj['STAKKR'].start(container, pull, recreate, proxy)
200 1
    _show_status(ctx)
201
202
203 1
@stakkr.command(help="Display a list of running containers")
204 1
@click.pass_context
205
def status(ctx):
206
    """See command Help."""
207 1
    ctx.obj['STAKKR'].status()
208
209
210 1
@stakkr.command(help="Stop all (or a single as CONTAINER) container(s)")
211 1
@click.argument('container', required=False)
212 1
@click.option('--proxy/--no-proxy', '-P', help="Stop the proxy", default=True)
213 1
@click.pass_context
214 1
def stop(ctx, container: str, proxy: bool):
215
    """See command Help."""
216 1
    print(click.style('[STOPPING]', fg='yellow') + ' your stakkr services')
217 1
    ctx.obj['STAKKR'].stop(container, proxy)
218
219
220 1
def _get_cmd_user(user: str, container: str):
221 1
    users = {'apache': 'www-data', 'nginx': 'www-data', 'php': 'www-data'}
222
223 1
    cmd_user = 'root' if user is None else user
224 1
    if container in users and user is None:
225 1
        cmd_user = users[container]
226
227 1
    return cmd_user
228
229
230 1
def _show_status(ctx):
231 1
    services_ports = ctx.obj['STAKKR'].get_services_urls()
232 1
    if services_ports == '':
233 1
        print('\nServices Status:')
234 1
        ctx.invoke(status)
235 1
        return
236
237 1
    print('\nServices URLs :')
238 1
    print(services_ports)
239
240
241 1
def _get_project_dir(config: str):
242
    from os.path import abspath, dirname
243
244
    if config is not None:
245
        config = abspath(config)
246
        return dirname(config)
247
248
    from stakkr.file_utils import find_project_dir
249
    return find_project_dir()
250
251
252 1
def debug_mode():
253
    """Guess if we are in debug mode, useful to display runtime errors."""
254 1
    if '--debug' in sys.argv or '-d' in sys.argv:
255 1
        return True
256
257 1
    return False
258
259
260 1
def main():
261
    """Call the CLI Script."""
262 1
    try:
263 1
        stakkr(obj={})
264 1
    except Exception as error:
265 1
        msg = click.style(r""" ______ _____  _____   ____  _____
266
|  ____|  __ \|  __ \ / __ \|  __ \
267
| |__  | |__) | |__) | |  | | |__) |
268
|  __| |  _  /|  _  /| |  | |  _  /
269
| |____| | \ \| | \ \| |__| | | \ \
270
|______|_|  \_\_|  \_\\____/|_|  \_\
271
272
""", fg='yellow')
273 1
        msg += click.style('{}'.format(error), fg='red')
274 1
        print(msg + '\n', file=sys.stderr)
275
276 1
        if debug_mode() is True:
277 1
            raise error
278
279 1
        sys.exit(1)
280
281
282 1
if __name__ == '__main__':
283
    main()
284