summaryrefslogtreecommitdiffstats
path: root/deploy/config/schemas.py
blob: 0e013eb9b6df2dcac0c0a7d8c8b3c9b560265c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
##############################################################################
# Copyright (c) 2017 ZTE Corporation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import copy
from jsonschema import Draft4Validator, FormatChecker
import sys
import yaml


MIN_DAISY_DISK_SIZE = 50
# minimal size of root_lv_size is 102400 mega-bytes
MIN_NODE_DISK_SIZE = 110
MIN_CEPH_DISK_SIZE = 110

hosts_schema = {
    'type': 'array',
    'items': {
        'type': 'object',
        'properties': {
            'name': {'type': 'string', 'minLength': 1},
            'roles': {
                'type': 'array',
                'items': {
                    'type': 'string',
                    'enum': ['COMPUTER', 'CONTROLLER_LB', 'CONTROLLER_HA']
                }
            },
            'template': {'type': 'string', 'minLength': 1},
            'ipmi_ip': {'type': 'string', 'format': 'ip-address'},
            'ipmi_user': {'type': 'string'},
            'ipmi_pass': {'type': 'string'},
            'mac_addresses': {
                'type': 'array',
                'items': {
                    'type': 'string',
                    'pattern': '^([0-9a-fA-F]{2})((:[0-9a-fA-F]{2}){5})$'
                }
            }
        },
        'required': ['roles']
    }
}

disks_schema = {
    'type': 'object',
    'properties': {
        'daisy': {'type': 'integer', 'minimum': MIN_DAISY_DISK_SIZE},
        'controller': {'type': 'integer', 'minimum': MIN_NODE_DISK_SIZE},
        'compute': {'type': 'integer', 'minimum': MIN_NODE_DISK_SIZE},
        'ceph': {'type': 'integer', 'minimum': MIN_CEPH_DISK_SIZE}
    }
}

schema_mapping = {
    'adapter': {'type': 'string', 'enum': ['ipmi', 'libvirt']},
    'hosts': hosts_schema,
    'disks': disks_schema,
    'daisy_passwd': {'type': 'string'},
    'daisy_ip': {'type': 'string', 'format': 'ip-address'},
    'daisy_gateway': {'type': 'string', 'format': 'ip-address'},
    'ceph_disk_name': {'type': 'string'},
    'modules': {'type': ['array', 'null']}
}

deploy_schema = {
    'type': 'object',
    'properties': schema_mapping,
    'required': ['hosts', 'daisy_passwd', 'daisy_gateway']
}

deploy_schema_bm = copy.deepcopy(deploy_schema)
deploy_schema_bm['properties']['hosts']['items']['required'] = \
    ['roles', 'ipmi_ip', 'ipmi_user', 'ipmi_pass']


def _validate(data, schema):
    v = Draft4Validator(schema, format_checker=FormatChecker())
    errors = sorted(v.iter_errors(data), key=lambda e: e.path)
    return errors


def deploy_schema_validate(data):
    if data.get('adapter', 'libvirt') == 'ipmi':
        return _validate(data, deploy_schema_bm)
    else:
        return _validate(data, deploy_schema)


def _main():
    if 2 != len(sys.argv):
        sys.exit(1)
    try:
        data = yaml.safe_load(open(sys.argv[1], 'r'))
        errors = deploy_schema_validate(data)
    except Exception as e:
        errors = 'Exception occured: ' + str(e)
    if errors:
        sys.exit(errors)


if __name__ == '__main__':
    _main()