Passed
Push — master ( ebb62a...a9f909 )
by Emmanuel
06:31
created

stakkr.stakkr_compose.cli()   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 2
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 1
rs 9.75
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# coding: utf-8
3 2
"""
4
CLI Main Entry Point.
5
6
Wraps docker-compose and build it from what has been taken from config.
7
"""
8
9 2
import glob
10 2
import os
11 2
import subprocess
12 2
import sys
13 2
import click
14 2
from stakkr import file_utils
15 2
from stakkr.configreader import Config
16
17
18 2
@click.command(help="Wrapper for docker-compose",
19
               context_settings=dict(ignore_unknown_options=True))
20 2
@click.option('--config-file', '-c', help="Set stakkr config file location (default stakkr.yml)")
21 2
@click.argument('command', nargs=-1, type=click.UNPROCESSED)
22 2
def cli(config_file: str = None, command: tuple = ()):
23
    """Command line entry point."""
24 1
    config, config_file = _get_config(config_file)
25
26
    # Set main config and services as env variables
27 1
    _set_env_from_config(config)
28
29
    # Register proxy env parameters
30 1
    _set_env_for_proxy(config['proxy'])
31
32
    # set the base command
33 1
    base_cmd = _get_base_command(config)
34
35 1
    msg = click.style('[VERBOSE] ', fg='green')
36 1
    msg += 'Compose command: ' + ' '.join(base_cmd + list(command))
37 1
    click.echo(msg, err=True)
38 1
    subprocess.call(base_cmd + list(command))
39
40
41 2
def _add_local_services(project_dir: str, available_services: list):
42
    """Get services in the virtualenv services/ directory, so specific to that stakkr."""
43 2
    services_dir = project_dir + '/services/*/docker-compose'
44 2
    for service_dir in glob.glob(services_dir):
45 2
        conf_files = _get_services_from_dir(service_dir)
46 2
        for conf_file in conf_files:
47 2
            available_services[conf_file[:-4]] = service_dir + '/' + conf_file
48
49 2
    return available_services
50
51
52 2
def get_available_services(project_dir: str):
53
    """Get standard services bundled with stakkr."""
54 2
    services_dir = file_utils.get_dir('static') + '/services/'
55 2
    conf_files = _get_services_from_dir(services_dir)
56
57 2
    services = dict()
58 2
    for conf_file in conf_files:
59 2
        services[conf_file[:-4]] = services_dir + conf_file
60
61 2
    services = _add_local_services(project_dir, services)
62
63 2
    return services
64
65
66 2
def _get_base_command(config: dict):
67
    """Build the docker-compose file to be run as a command."""
68 1
    main_file = 'docker-compose.yml'
69
    # Set the network subnet ?
70 1
    if config['subnet'] != '':
71 1
        main_file = 'docker-compose.subnet.yml'
72 1
    cmd = ['docker-compose', '-f', file_utils.get_file('static', main_file)]
73
74
    # What to load
75 1
    activated_services = _get_enabled_services_files(
76
        config['project_dir'],
77
        [svc for svc, opts in config['services'].items() if opts['enabled'] is True])
78
    # Create the command
79 1
    services = []
80 1
    for service in activated_services:
81 1
        services.append('-f')
82 1
        services.append(service)
83
84 1
    return cmd + services + ['-p', config['project_name']]
85
86
87 2
def _get_config(config_file: str):
88
    """Read main stakkr.yml file."""
89 1
    config_reader = Config(config_file)
90 1
    config = config_reader.read()
91
92 1
    if config is False:
93 1
        config.display_errors()
94
        sys.exit(1)
95
96 1
    return config, config_reader.config_file
97
98
99 2
def _get_enabled_services_files(project_dir: str, configured_services: list):
100
    """Compile all available services : standard and local install."""
101 1
    available_services = get_available_services(project_dir)
102
103 1
    services_files = []
104 1
    for service in configured_services:
105 1
        if service not in available_services:
106
            msg = 'Error: service "{}" has no configuration file. '.format(service)
107
            msg += 'Check your config'
108
            click.secho(msg, fg='red')
109
            sys.exit(1)
110 1
        services_files.append(available_services[service])
111
112 1
    return services_files
113
114
115 2
def _get_gid(gid: int):
116 2
    if gid is not None:
117 1
        return str(gid)
118
119 2
    return '1000' if os.name == 'nt' else str(os.getgid())
120
121
122 2
def _get_services_from_dir(services_dir: str):
123 2
    if os.path.isdir(services_dir) is False:
124
        return []
125
126 2
    return [service for service in os.listdir(services_dir) if service.endswith('.yml')]
127
128
129 2
def _get_uid(uid: int):
130 2
    if uid is not None:
131 1
        return str(uid)
132
133 2
    return '1000' if os.name == 'nt' else str(os.getuid())
134
135
136 2
def _set_env_for_proxy(config: dict):
137
    """Define environment variables to be used in services yaml."""
138 1
    os.environ['PROXY_ENABLED'] = str(config['enabled'])
139 1
    os.environ['PROXY_DOMAIN'] = str(config['domain'])
140
141
142 2
def _set_env_for_services(services: dict):
143 1
    for service, params in services.items():
144 1
        if params['enabled'] is False:
145 1
            continue
146
147 1
        for param, value in params.items():
148 1
            env_var = 'DOCKER_{}_{}'.format(service, param).upper()
149 1
            os.environ[env_var] = str(value)
150
151
152 2
def _set_env_from_config(config: dict):
153
    """Define environment variables to be used in services yaml."""
154 1
    os.environ['COMPOSE_BASE_DIR'] = config['project_dir']
155 1
    os.environ['COMPOSE_PROJECT_NAME'] = config['project_name']
156 1
    for parameter, value in config.items():
157 1
        if parameter == 'services':
158 1
            _set_env_for_services(value)
159 1
            continue
160
161 1
        os.environ['DOCKER_{}'.format(parameter.upper())] = str(value)
162
163
    # Do that at the end, else the value in config will overwrite the possible
164
    # default value
165 1
    os.environ['DOCKER_UID'] = _get_uid(config['uid'])
166 1
    os.environ['DOCKER_GID'] = _get_gid(config['gid'])
167
168
169 2
if __name__ == '__main__':
170
    cli()
171