common.py 3.31 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.

"""The high-level interface that Zoe uses to talk to the configured container backend."""

18
from typing import Dict, List
19

hxquangnhat's avatar
hxquangnhat committed
20
from zoe_lib.config import get_conf
21
from zoe_lib.state import Service, Execution, VolumeDescription, VolumeDescriptionHostPath
22
from zoe_master.exceptions import ZoeStartExecutionFatalException
23 24
from zoe_master.workspace.filesystem import ZoeFSWorkspace

25

26
def gen_environment(execution: Execution, service: Service, env_subst_dict: Dict):
27 28 29 30 31 32 33 34 35 36 37 38
    """ Generate a dictionary containing the current cluster status (before the new container is spawned)

    This information is used to substitute template strings in the environment variables."""
    env_list = []
    for env_name, env_value in service.environment:
        try:
            env_value = env_value.format(**env_subst_dict)
        except KeyError:
            error_msg = "Unknown variable in environment expression '{}', known variables are: {}".format(env_value, list(env_subst_dict.keys()))
            service.set_error(error_msg)
            raise ZoeStartExecutionFatalException("Service {} has wrong environment expression")
        env_list.append((env_name, env_value))
39

qhoangxuan's avatar
qhoangxuan committed
40 41
    env_list.append(('EXECUTION_ID', str(execution.id)))
    env_list.append(('DEPLOY_NAME', get_conf().deployment_name))
42 43 44
    env_list.append(('ZOE_UID', execution.owner.fs_uid))
    env_list.append(('ZOE_GID', get_conf().fs_group_id))
    env_list.append(('ZOE_USER', execution.owner.username))
qhoangxuan's avatar
qhoangxuan committed
45
    env_list.append(('SERVICE_NAME', service.name))
46 47 48
    if get_conf().traefik_zk_ips is not None:
        for port in service.ports:
            env_list.append(('REVERSE_PROXY_PATH_{}'.format(port.internal_number), '/{}/{}'.format(get_conf().traefik_base_url, port.proxy_key())))
49

50
    wk_vol = ZoeFSWorkspace().get(execution.owner)
51
    env_list.append(('ZOE_WORKSPACE', wk_vol.mount_point))
52
    return env_list
53 54


55
def gen_volumes(service: Service, execution: Execution) -> List[VolumeDescription]:
56
    """Return the list of default volumes to be added to all containers."""
57 58
    vol_list = service.volumes

59
    wk_vol = ZoeFSWorkspace().get(execution.owner)
60 61 62

    vol_list.append(wk_vol)

63 64 65
    for volume_path, name in get_conf().additional_volumes:
        vol_list.append(VolumeDescriptionHostPath(path=volume_path, name=name, readonly=True))

66 67 68 69 70 71 72 73 74 75
    return vol_list


def gen_labels(service: Service, execution: Execution):
    """Generate container labels, useful for identifying containers in monitoring systems."""
    return {
        'zoe_execution_name': execution.name,
        'zoe_execution_id': str(execution.id),
        'zoe_service_name': service.name,
        'zoe_service_id': str(service.id),
76
        'zoe_owner': execution.owner.username,
77 78 79
        'zoe_deployment_name': get_conf().deployment_name,
        'zoe_type': 'app_service'
    }