Passed
Pull Request — master (#10)
by
unknown
06:28
created

stakkr.proxy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 54
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Proxy.start() 0 9 3
A Proxy.__init__() 0 6 1
A Proxy._start_container() 0 15 2
A Proxy.stop() 0 8 2
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."""
12
13 1
    def __init__(self, http_port: int = 80, https_port: int = 443, ct_name: str = 'proxy_stakkr', version: str = 'latest'):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (123/100).

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

Loading history...
14
        """Set the right values to start the proxy."""
15 1
        self.ports = {'http': http_port, 'https': https_port}
16 1
        self.ct_name = ct_name
17 1
        self.docker_client = docker.get_client()
18 1
        self.version = version
19
20 1
    def start(self, stakkr_network: str = None):
21
        """Start stakkr proxy if stopped."""
22 1
        if docker.container_running(self.ct_name) is False:
23 1
            print(click.style('[STARTING]', fg='green') + ' traefik')
24 1
            self._start_container()
25
26
        # Connect it to network if asked
27 1
        if stakkr_network is not None:
28 1
            docker.add_container_to_network(self.ct_name, stakkr_network)
29
30 1
    def stop(self):
31
        """Stop stakkr proxy."""
32 1
        if docker.container_running(self.ct_name) is False:
33 1
            return
34
35 1
        print(click.style('[STOPPING]', fg='green') + ' traefik')
36 1
        proxy_ct = self.docker_client.containers.get(self.ct_name)
37 1
        proxy_ct.stop()
38
39 1
    def _start_container(self):
40
        """Start proxy."""
41 1
        proxy_conf_dir = get_dir('static/proxy')
42 1
        try:
43 1
            self.docker_client.images.pull('traefik:{}'.format(self.version))
44 1
            self.docker_client.containers.run(
45
                'traefik:{}'.format(self.version), remove=True, detach=True,
46
                hostname=self.ct_name, name=self.ct_name,
47
                volumes=[
48
                    '/var/run/docker.sock:/var/run/docker.sock',
49
                    '{}/traefik.toml:/etc/traefik/traefik.toml'.format(proxy_conf_dir),
50
                    '{}/ssl:/etc/traefik/ssl'.format(proxy_conf_dir)],
51
                ports={80: self.ports['http'], 8080: 8080, 443: self.ports['https']})
52
        except DockerException as error:
53
            raise RuntimeError("Can't start proxy ...({})".format(error))
54