stakkr.proxy.Proxy.stop()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 8
ccs 3
cts 6
cp 0.5
crap 2.5
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 sys import stdout
6 1
from docker.errors import DockerException
7 1
from stakkr import docker_actions as docker
8 1
from stakkr.file_utils import get_dir
9
10
11 1
class Proxy:
12
    """Main class that does actions asked by the cli."""
13
14 1
    def __init__(self,
15
                 http_port: int = 80, https_port: int = 443,
16
                 ct_name: str = 'proxy_stakkr',
17
                 version: str = 'latest'):
18
        """Set the right values to start the proxy."""
19
20 1
        self.ports = {'http': http_port, 'https': https_port}
21 1
        self.ct_name = ct_name
22 1
        self.docker_client = docker.get_client()
23 1
        self.version = version
24
25 1
    def start(self, stakkr_network: str = None):
26
        """Start stakkr proxy if stopped."""
27 1
        if docker.container_running(self.ct_name) is False:
28 1
            msg = ' traefik (Dashboard: http://traefik.docker.localhost)\n'
29 1
            stdout.write(click.style('[STARTING]', fg='green') + msg)
30 1
            self._start_container()
31
32
        # Connect it to network if asked
33 1
        if stakkr_network is not None:
34 1
            docker.add_container_to_network(self.ct_name, stakkr_network)
35
36 1
    def stop(self):
37
        """Stop stakkr proxy."""
38 1
        if docker.container_running(self.ct_name) is False:
39 1
            return
40
41
        stdout.write(click.style('[STOPPING]', fg='green') + ' traefik\n')
42
        proxy_ct = self.docker_client.containers.get(self.ct_name)
43
        proxy_ct.stop()
44
45 1
    def _start_container(self):
46
        """Start proxy."""
47 1
        proxy_conf_dir = get_dir('static/proxy')
48 1
        try:
49 1
            self.docker_client.images.pull('traefik:{}'.format(self.version))
50 1
            self.docker_client.containers.run(
51
                'traefik:{}'.format(self.version), remove=True, detach=True,
52
                hostname=self.ct_name, name=self.ct_name,
53
                volumes=[
54
                    '/var/run/docker.sock:/var/run/docker.sock:ro',
55
                    '{}/traefik.yml:/etc/traefik/traefik.yml:ro'.format(proxy_conf_dir),
56
                    '{}/config.yml:/etc/traefik/config.yml'.format(proxy_conf_dir),
57
                    '{}/ssl:/etc/traefik/ssl'.format(proxy_conf_dir)],
58
                labels={'traefik.enable': 'true', 'traefik.http.routers.traefik': 'true'},
59
                ports={80: self.ports['http'], 443: self.ports['https']}
60
                )
61
        except DockerException as error:
62
            raise RuntimeError("Can't start proxy ...({})".format(error))
63