Passed
Branch v4.0-dev (e005f1)
by Emmanuel
05:49
created

stakkr.cli._get_cmd_user()   A

Complexity

Conditions 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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