Passed
Branch master (07577f)
by Emmanuel
05:55
created

stakkr.proxy.Proxy.stop()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
# coding: utf-8
2 1
"""Manage public proxy to expose containers."""
3
4 1
import click
5 1
from docker.errors import DockerException
6 1
from stakkr import docker_actions as docker
7 1
from stakkr.file_utils import get_dir
8
9
10 1
class Proxy:
11
    """Main class that does actions asked by the cli."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
12
13 1
    def __init__(self, http_port: int = 80, https_port: int = 443, ct_name: str = 'proxy_stakkr'):
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...
14
        """Set the right values to start the proxy."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
15 1
        self.ports = {'http': http_port, 'https': https_port}
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
16 1
        self.ct_name = ct_name
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
17 1
        self.docker_client = docker.get_client()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
18
19 1
    def start(self, stakkr_network: str = None):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
20
        """Start stakkr proxy if stopped."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
21 1
        if docker.container_running(self.ct_name) is False:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
22 1
            print(click.style('[STARTING]', fg='green') + ' traefik')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
23 1
            self._start_container()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
24
25
        # Connect it to network if asked
26 1
        if stakkr_network is not None:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
27
            docker.add_container_to_network(self.ct_name, stakkr_network)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
28
29 1
    def stop(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
30
        """Stop stakkr proxy."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
31 1
        if docker.container_running(self.ct_name) is False:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
32 1
            return
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
33
34 1
        print(click.style('[STOPPING]', fg='green') + ' traefik')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
35 1
        proxy_ct = self.docker_client.containers.get(self.ct_name)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
36 1
        proxy_ct.stop()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
37
38 1
    def _start_container(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
39
        """Start proxy."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
40 1
        proxy_conf_dir = get_dir('static/proxy')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
41 1
        try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
42 1
            self.docker_client.images.pull('traefik:latest')
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
43 1
            self.docker_client.containers.run(
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
44
                'traefik:latest', remove=True, detach=True,
45
                hostname=self.ct_name, name=self.ct_name,
46
                volumes=[
47
                    '/var/run/docker.sock:/var/run/docker.sock',
48
                    '{}/traefik.toml:/etc/traefik/traefik.toml'.format(proxy_conf_dir),
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...
49
                    '{}/ssl:/etc/traefik/ssl'.format(proxy_conf_dir)],
50
                ports={80: self.ports['http'], 8080: 8080, 443: self.ports['https']})
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...
51
        except DockerException as error:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
52
            raise RuntimeError("Can't start proxy ...({})".format(error))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
53