Passed
Push — master ( a9f909...a5fef7 )
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 4
CRAP Score 1.2963

Importance

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