applications.py 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2015, 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.

"""
17
This module contains code to validate application descriptions.
18 19 20
"""

import logging
21
import json
22

23 24 25
import jsonschema

from zoe_lib.exceptions import InvalidApplicationDescription, ZoeLibException
26
import zoe_lib.version
Daniele Venzano's avatar
Daniele Venzano committed
27
import zoe_lib.config
28 29 30 31

log = logging.getLogger(__name__)


32
def app_validate(data):
Daniele Venzano's avatar
Daniele Venzano committed
33 34 35
    """
    Validates an application description, making sure all required fields are present and of the correct type.
    If the description is not valid, an InvalidApplicationDescription exception is thrown.
36
    Uses a JSON schema definition.
Daniele Venzano's avatar
Daniele Venzano committed
37

38
    :param data: an open file descriptor containing JSON data
Daniele Venzano's avatar
Daniele Venzano committed
39 40
    :return: None if the application description is correct
    """
41

42
    schema = json.load(open('schemas/app_description_schema.json', 'r'))
43
    try:
44 45 46 47 48 49
        jsonschema.validate(data, schema)
    except jsonschema.ValidationError as e:
        raise InvalidApplicationDescription(str(e))
    except jsonschema.SchemaError:
        log.exception('BUG: invalid schema for application descriptions')
        raise ZoeLibException('BUG: invalid schema for application descriptions')
50

51 52 53
    # Start non-schema, semantic checks
    if data['version'] != zoe_lib.version.ZOE_APPLICATION_FORMAT_VERSION:
        raise InvalidApplicationDescription('Application description version mismatch (expected: {}, found: {}'.format(zoe_lib.version.ZOE_APPLICATION_FORMAT_VERSION, data['version']))
54 55

    found_monitor = False
56
    for service in data['services']:
57
        if service['monitor']:
58
            found_monitor = True
59

Daniele Venzano's avatar
Daniele Venzano committed
60
        service['resources']['memory']['max'] = zoe_lib.config.get_conf().max_memory_limit * (1024 ** 3)
61
        if service['resources']['memory']['min'] is not None and service['resources']['memory']['min'] > service['resources']['memory']['max']:
62
            raise InvalidApplicationDescription(msg='service {} tries to reserve more memory than the administrative limit'.format(service['name']))
63

64 65
        if service['resources']['cores']['min'] is None:
            service['resources']['cores']['min'] = 0.1
66

67 68
    if not found_monitor:
        raise InvalidApplicationDescription(msg="at least one process should have the monitor property set to true")