service_instance.py 3.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright (c) 2017, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""When a service from the application description needs to be instantiated, it is transformed into a ServiceInstance, an internal representation of a generic container. This class is used to gather all the attributes that describe a container and to provide a clear interface with the backend."""

18
19
import collections

Daniele Venzano's avatar
Daniele Venzano committed
20
from zoe_lib.state import Service, Execution
21
22
23
from zoe_lib.config import get_conf
import zoe_master.backends.common

24
25
BackendPort = collections.namedtuple('BackendPort', ['number', 'proto'])

26
27
28
29
30
31

class ServiceInstance:
    """The ServiceInstance class, a Service that is going to be instantiated into a container."""
    def __init__(self, execution: Execution, service: Service, env_subst_dict):
        self.name = service.unique_name
        self.hostname = service.dns_name
32
        self.backend_host = service.backend_host
33

Daniele Venzano's avatar
Daniele Venzano committed
34
35
36
37
        if service.resource_reservation.memory.min is None:
            self.memory_limit = None
        else:
            self.memory_limit = service.resource_reservation.memory
38
39
            if self.memory_limit.max > get_conf().max_memory_limit * (1024 ** 3):
                self.memory_limit.max = get_conf().max_memory_limit * (1024 ** 3)
Daniele Venzano's avatar
Daniele Venzano committed
40
41
42
43
44

        if service.resource_reservation.cores.min is None:
            self.core_limit = None
        else:
            self.core_limit = service.resource_reservation.cores
45
46
            if self.core_limit.max > get_conf().max_core_limit:
                self.core_limit = get_conf().max_core_limit
47

48
49
        self.shm_size = service.resource_reservation.shm

50
51
52
53
54
        self.labels = {
            'zoe.execution.name': execution.name,
            'zoe.execution.id': str(execution.id),
            'zoe.service.name': service.name,
            'zoe.service.id': str(service.id),
55
            'zoe.owner': execution.owner.username,
56
            'zoe.deployment_name': get_conf().deployment_name,
57
58
            'zoe.type': 'service_{}'.format('essential' if service.essential else 'elastic'),
            'zoe.zapp_size': execution.size
59
60
61
62
63
64
65
66
        }
        if service.is_monitor:
            self.labels['zoe_monitor'] = 'true'
        else:
            self.labels['zoe_monitor'] = 'false'

        self.labels = zoe_master.backends.common.gen_labels(service, execution)
        self.environment = service.environment + zoe_master.backends.common.gen_environment(execution, service, env_subst_dict)
67
        self.volumes = zoe_master.backends.common.gen_volumes(service, execution)
68
69
70

        self.command = service.command

71
72
        self.work_dir = service.work_dir

73
74
        self.image_name = service.image_name

75
76
77
        self.ports = []
        for port in service.ports:
            self.ports.append(BackendPort(port.internal_number, port.protocol))
Daniele Venzano's avatar
Daniele Venzano committed
78
79
80
81
82

        if service.network is not None:
            self.network = service.network
        else:
            self.network = get_conf().overlay_network_name