Passed
Push — master ( 25f414...07577f )
by Emmanuel
06:23
created

stakkr.docker_actions.get_subnet()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.4218

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 6
ccs 1
cts 4
cp 0.25
crap 1.4218
rs 10
c 0
b 0
f 0
1
# coding: utf-8
2 1
"""Docker functions to get info about containers."""
3
4 1
from docker.errors import NotFound, NullResource
5 1
__st__ = {'cts_info': dict(), 'running_cts': 0}
6
7
8 1
def add_container_to_network(container: str, network: str):
9
    """Attach a container to a network."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
10 1
    if _container_in_network(container, network) is True:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
11 1
        return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
12
13 1
    docker_network = get_client().networks.get(network)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
14 1
    docker_network.connect(container)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
15
16 1
    return True
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
17
18
19 1
def block_ct_ports(service: str, ports: list, project_name: str) -> tuple:
20
    """Run iptables commands to block a list of port on a specific container."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
21
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
22
        container = get_client().containers.get(get_ct_item(service, 'id'))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
23
    except (LookupError, NullResource):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
24
        return False, '{} is not started, no port to block'.format(service)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
25
26
    _, iptables = container.exec_run(['which', 'iptables'])
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
27
    iptables = iptables.decode().strip()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
28
    if iptables == '':
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
29
        return True, "Can't block ports on {}, is iptables installed ?".format(service)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (87/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
30
31
    _allow_contact_subnet(project_name, container)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
32
33
    # Now for each port, add an iptable rule
34
    for port in ports:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
35
        rule = ['OUTPUT', '-p', 'tcp', '--dport', str(port), '-j', 'REJECT']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
36
        try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
37
            container.exec_run([iptables, '-D'] + rule)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
38
        finally:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
39
            container.exec_run([iptables, '-A'] + rule)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
40
41
    ports_list = ', '.join(map(str, ports))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
42
43
    return False, 'Blocked ports {} on container {}'.format(ports_list, service)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
44
45
46 1
def check_cts_are_running(project_name: str):
47
    """Throw an error if cts are not running."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
48
    get_running_containers(project_name)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
49
    if not __st__['running_cts']:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
50
        raise SystemError('Have you started stakkr with the start action ?')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
51
52
53 1
def container_running(container: str):
54
    """Return True if the container is running else False."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
55 1
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
56 1
        return get_api_client().inspect_container(container)['State']['Running']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
57 1
    except (NotFound, NullResource):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
58 1
        return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
59
60
61 1
def create_network(network: str):
62
    """Create a Network."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
63 1
    if network_exists(network):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
64 1
        return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
65
66 1
    return get_client().networks.create(network, driver='bridge').id
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
67
68
69 1
def get_api_client():
70
    """Return the API client or initialize it."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
71 1
    if 'api_client' not in __st__:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __st__ does not seem to be defined.
Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
72 1
        from docker import APIClient, utils
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
73 1
        params = utils.kwargs_from_env()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
74 1
        base_url = None if 'base_url' not in params else params['base_url']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
75 1
        tls = None if 'tls' not in params else params['tls']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
76
77 1
        __st__['api_client'] = APIClient(base_url=base_url, tls=tls)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
78
79 1
    return __st__['api_client']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
80
81
82 1
def get_client():
83
    """Return the client or initialize it."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
84 1
    if 'client' not in __st__:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __st__ does not seem to be defined.
Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
85 1
        from docker import client
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
86 1
        __st__['client'] = client.from_env()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
87
88 1
    return __st__['client']
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
89
90
91 1
def get_ct_item(compose_name: str, item_name: str):
92
    """Get a value from a container, such as name or IP."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
93
    if 'cts_info' not in __st__:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
94
        raise LookupError('Before getting an info from a ct, run check_cts_are_running()')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (90/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
95
96
    for _, ct_data in __st__['cts_info'].items():
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
97
        if ct_data['compose_name'] == compose_name:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
98
            return ct_data[item_name]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
99
100
    return ''
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
101
102
103 1
def get_ct_name(container: str):
104
    """Return the system name of a container, generated by docker-compose."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
105
    ct_name = get_ct_item(container, 'name')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
106
    if ct_name == '':
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
107
        raise LookupError('{} does not seem to be started ...'.format(container))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (81/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
108
109
    return ct_name
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
110
111
112 1
def get_network_name(project_name: str):
113
    """Find the full network name."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
114
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
115
        guessed_network_name = '{}_stakkr'.format(project_name).lower()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
116
        network = get_client().networks.get(guessed_network_name)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
117
    except NotFound:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
118
        raise RuntimeError("Couldn't identify network (check your project name)")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (81/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
119
120
    return network.name
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
121
122
123 1
def get_subnet(project_name: str):
124
    """Find the subnet of the current project."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
125
    network_name = get_network_name(project_name)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
126
    network_info = get_client().networks.get(network_name).attrs
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
127
128
    return network_info['IPAM']['Config'][0]['Subnet'].split('/')[0]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
129
130
131 1
def get_switch_ip():
132
    """Find the main docker daemon IP to add routes."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
133
    import socket
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
134
135
    cmd = r"""/bin/sh -c "ip addr show hvint0 | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'" """
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (91/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
136
    res = get_client().containers.run(
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
137
        'alpine', remove=True, tty=True, privileged=True,
138
        network_mode='host', pid_mode='host', command=cmd)
139
    ip_addr = res.strip().decode()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
140
141
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
142
        socket.inet_aton(ip_addr)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
143
        return ip_addr
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
144
    except socket.error:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
145
        raise ValueError('{} is not a valid ip, check docker is running')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
146
147
148 1
def get_running_containers(project_name: str) -> tuple:
149
    """Get the number of running containers and theirs details for the current stakkr instance."""
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (98/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
150 1
    from requests import exceptions
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
151
152 1
    filters = {
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
153
        'name': '{}_'.format(project_name),
154
        'status': 'running',
155
        'network': '{}_stakkr'.format(project_name).lower()}
156
157 1
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
158 1
        cts = get_client().containers.list(filters=filters)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
159
    except exceptions.ConnectionError:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
160
        raise exceptions.ConnectionError('Make sure docker is installed and running')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (85/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
161
162 1
    __st__['cts_info'] = dict()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
163 1
    for container in cts:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
164 1
        container_info = _extract_container_info(project_name, container.id)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
165 1
        __st__['cts_info'][container_info['name']] = container_info
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
166
167 1
    __st__['running_cts'] = len(cts)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
168
169 1
    return __st__['running_cts'], __st__['cts_info']
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __st__ does not seem to be defined.
Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
170
171
172 1
def get_running_containers_names(project_name: str) -> list:
173
    """Get a list of compose names of running containers for the current stakkr instance."""
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (92/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
174
    cts = get_running_containers(project_name)[1]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
175
176
    return sorted([ct_data['compose_name'] for docker_name, ct_data in cts.items()])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
177
178
179 1
def guess_shell(container: str) -> str:
180
    """By searching for binaries, guess what could be the primary shell available."""
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (85/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
181 1
    container = get_client().containers.get(container)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
182
183 1
    cmd = 'which -a bash sh'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
184 1
    _, shells = container.exec_run(cmd, stdout=True, stderr=False)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
185 1
    shells = shells.splitlines()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
186 1
    if b'/bin/bash' in shells:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
187 1
        return '/bin/bash'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
188
189 1
    if b'/bin/sh' in shells:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
190 1
        return '/bin/sh'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
191
192
    raise EnvironmentError('Could not find a shell for that container')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
193
194
195 1
def network_exists(network: str):
196
    """Return True if a network exists in docker, else False."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
197 1
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
198 1
        get_client().networks.get(network)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
199 1
        return True
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
200 1
    except NotFound:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
201 1
        return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
202
203
204 1
def _allow_contact_subnet(project_name: str, container: str) -> bool:
205
    _, iptables = container.exec_run(['which', 'iptables'])
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
206
    iptables = iptables.decode().strip()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
207
    if iptables == '':
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
208
        return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
209
210
    subnet = get_subnet(project_name) + '/24'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
211
    # Allow internal network
212
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
213
        container.exec_run([iptables, '-D', 'OUTPUT', '-d', subnet, '-j', 'ACCEPT'])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
214
    finally:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
215
        container.exec_run([iptables, '-A', 'OUTPUT', '-d', subnet, '-j', 'ACCEPT'])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
216
217
    return True
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
218
219 1
def _extract_container_info(project_name: str, ct_id: str):
220
    """Get a hash of info about a container : name, ports, image, ip ..."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
221 1
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
222 1
        ct_data = get_api_client().inspect_container(ct_id)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
223 1
    except NotFound:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
224 1
        return None
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
225
226 1
    cts_info = {
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
227
        'id': ct_id,
228
        'name': ct_data['Name'].lstrip('/'),
229
        'compose_name': ct_data['Config']['Labels']['com.docker.compose.service'],
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (82/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
230
        'ports': _extract_host_ports(ct_data),
231
        'image': ct_data['Config']['Image'],
232
        'traefik_host': _get_traefik_host(ct_data['Config']['Labels']),
233
        'ip': _get_ip_from_networks(project_name, ct_data['NetworkSettings']['Networks']),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (90/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
234
        'running': ct_data['State']['Running']
235
        }
236
237 1
    return cts_info
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
238
239
240 1
def _extract_host_ports(config: list):
241 1
    ports = []
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
242 1
    for _, host_ports in config['HostConfig']['PortBindings'].items():
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
243
        ports += [host_port['HostPort'] for host_port in host_ports]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
244
245 1
    return ports
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
246
247
248 1
def _get_ip_from_networks(project_name: str, networks: list):
249
    """Get the ip of a network."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
250 1
    network_settings = {}
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
251 1
    if '{}_stakkr'.format(project_name) in networks:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
252 1
        network_settings = networks['{}_stakkr'.format(project_name)]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
253
254 1
    return network_settings['IPAddress'] if 'IPAddress' in network_settings else ''
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (83/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
255
256
257 1
def _container_in_network(container: str, expected_network: str):
258
    """Return True if a container is in a network else false. Used by add_container_to_network."""
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (98/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
259 1
    try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
260 1
        ct_data = get_api_client().inspect_container(container)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
261 1
    except NotFound:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
262 1
        raise LookupError('Container {} does not seem to exist'.format(container))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (82/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
263
264 1
    for connected_network in ct_data['NetworkSettings']['Networks'].keys():
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
265 1
        if connected_network == expected_network:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
266 1
            return True
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
267
268 1
    return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
269
270
271 1
def _get_traefik_host(labels: list):
272 1
    if 'traefik.frontend.rule' not in labels:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
273 1
        return 'No traefik rule'
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
274
275 1
    rules = labels['traefik.frontend.rule'].split(':')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
276
277
    return rules[1]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
278