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

stakkr.stakkr_compose._get_config()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
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, command):
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 1
def _add_services_from_plugins(project_dir: str, available_services: list):
42
    """Read plugin path and extract services in subdirectories services/."""
43 1
    from pkg_resources import iter_entry_points
44
45
    # Override services with plugins
46 1
    for entry in iter_entry_points('stakkr.plugins'):
47
        plugin_dir = str(entry).split('=')[0].strip()
48
        services_dir = project_dir + '/plugins/' + plugin_dir + '/services'
49
50
        conf_files = _get_services_from_dir(services_dir)
51
        for conf_file in conf_files:
52
            available_services[conf_file[:-4]] = services_dir + '/' + conf_file
53
54 1
    return available_services
55
56
57 1
def _add_local_services(project_dir: str, available_services: list):
58
    """Get services in the virtualenv services/ directory, so specific to that stakkr."""
59 1
    services_dir = project_dir + '/services/*/docker-compose'
60 1
    for service_dir in glob.glob(services_dir):
61 1
        conf_files = _get_services_from_dir(service_dir)
62 1
        for conf_file in conf_files:
63 1
            available_services[conf_file[:-4]] = service_dir + '/' + conf_file
64
65 1
    return available_services
66
67
68 1
def get_available_services(project_dir: str):
69
    """Get standard services bundled with stakkr."""
70 1
    services_dir = file_utils.get_dir('static') + '/services/'
71 1
    conf_files = _get_services_from_dir(services_dir)
72
73 1
    services = dict()
74 1
    for conf_file in conf_files:
75 1
        services[conf_file[:-4]] = services_dir + conf_file
76
77 1
    services = _add_services_from_plugins(project_dir, services)
78 1
    services = _add_local_services(project_dir, services)
79
80 1
    return services
81
82
83 1
def _get_base_command(config: dict):
84
    """Build the docker-compose file to be run as a command."""
85 1
    main_file = 'docker-compose.yml'
86
    # Set the network subnet ?
87 1
    if config['subnet'] != '':
88 1
        main_file = 'docker-compose.subnet.yml'
89 1
    cmd = ['docker-compose', '-f', file_utils.get_file('static', main_file)]
90
91
    # What to load
92 1
    activated_services = _get_enabled_services_files(
93
        config['project_dir'],
94
        [svc for svc, opts in config['services'].items() if opts['enabled'] is True])
95
    # Create the command
96 1
    services = []
97 1
    for service in activated_services:
98 1
        services.append('-f')
99 1
        services.append(service)
100
101 1
    return cmd + services + ['-p', config['project_name']]
102
103
104 1
def _get_config(config_file: str):
105
    """Read main stakkr.yml file."""
106 1
    config_reader = Config(config_file)
107 1
    config = config_reader.read()
108
109 1
    if config is False:
110 1
        config.display_errors()
111
        sys.exit(1)
112
113 1
    return config, config_reader.config_file
114
115
116 1
def _get_enabled_services_files(project_dir: str, configured_services: list):
117
    """Compile all available services : standard, plugins, local install."""
118 1
    available_services = get_available_services(project_dir)
119
120 1
    services_files = []
121 1
    for service in configured_services:
122 1
        if service not in available_services:
123
            msg = 'Error: service "{}" has no configuration file. '.format(service)
124
            msg += 'Check your config'
125
            click.secho(msg, fg='red')
126
            sys.exit(1)
127 1
        services_files.append(available_services[service])
128
129 1
    return services_files
130
131
132 1
def _get_gid(gid: int):
133 1
    if gid is not None:
134 1
        return str(gid)
135
136 1
    return '1000' if os.name == 'nt' else str(os.getgid())
137
138
139 1
def _get_services_from_dir(services_dir: str):
140 1
    if os.path.isdir(services_dir) is False:
141
        return []
142
143 1
    return [service for service in os.listdir(services_dir) if service.endswith('.yml')]
144
145
146 1
def _get_uid(uid: int):
147 1
    if uid is not None:
148 1
        return str(uid)
149
150 1
    return '1000' if os.name == 'nt' else str(os.getuid())
151
152
153 1
def _set_env_for_proxy(config: dict):
154
    """Define environment variables to be used in services yaml."""
155 1
    os.environ['PROXY_ENABLED'] = str(config['enabled'])
156 1
    os.environ['PROXY_DOMAIN'] = str(config['domain'])
157 1
    os.environ['PROXY_PORT'] = str(config['port'])
158
159
160 1
def _set_env_for_services(services: dict):
161 1
    for service, params in services.items():
162 1
        if params['enabled'] is False:
163 1
            continue
164
165 1
        for param, value in params.items():
166 1
            env_var = 'DOCKER_{}_{}'.format(service, param).upper()
167 1
            os.environ[env_var] = str(value)
168
169
170 1
def _set_env_from_config(config: dict):
171
    """Define environment variables to be used in services yaml."""
172 1
    os.environ['COMPOSE_BASE_DIR'] = config['project_dir']
173 1
    os.environ['COMPOSE_PROJECT_NAME'] = config['project_name']
174 1
    os.environ['DOCKER_UID'] = _get_uid(config['uid'])
175 1
    os.environ['DOCKER_GID'] = _get_gid(config['gid'])
176 1
    for parameter, value in config.items():
177 1
        if parameter == 'services':
178 1
            _set_env_for_services(value)
179 1
            continue
180
181 1
        os.environ['DOCKER_{}'.format(parameter.upper())] = str(value)
182
183
184 1
if __name__ == '__main__':
185
    cli()
186