Make util support more than 10 compute nodes
[apex.git] / apex / settings / deploy_settings.py
1 ##############################################################################
2 # Copyright (c) 2016 Michael Chapman (michapma@redhat.com) and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 import yaml
12
13 from apex.common import constants
14
15 REQ_DEPLOY_SETTINGS = ['sdn_controller',
16                        'odl_version',
17                        'tacker',
18                        'congress',
19                        'dataplane',
20                        'sfc',
21                        'vpn',
22                        'vpp',
23                        'ceph',
24                        'gluon',
25                        'rt_kvm',
26                        'os_version',
27                        'l2gw',
28                        'sriov',
29                        'containers',
30                        'ceph_device',
31                        'vim']
32
33 OPT_DEPLOY_SETTINGS = ['performance',
34                        'vsperf',
35                        'yardstick',
36                        'dovetail',
37                        'odl_vpp_routing_node',
38                        'dvr',
39                        'odl_vpp_netvirt',
40                        'barometer',
41                        'calipso']
42
43 VALID_ROLES = ['Controller', 'Compute', 'ObjectStorage']
44 VALID_PERF_OPTS = ['kernel', 'nova', 'vpp', 'ovs']
45 VALID_DATAPLANES = ['ovs', 'ovs_dpdk', 'fdio']
46 REQ_PATCH_CRITERIA = ['change-id', 'project']
47 OPT_PATCH_CRITERIA = ['branch']
48
49
50 class DeploySettings(dict):
51     """
52     This class parses a APEX deploy settings yaml file into an object
53     """
54     def __init__(self, filename):
55         if isinstance(filename, str):
56             with open(filename, 'r') as deploy_settings_file:
57                 init_dict = yaml.safe_load(deploy_settings_file)
58         else:
59             # assume input is a dict to build from
60             init_dict = filename
61
62         super().__init__(init_dict)
63         self._validate_settings()
64
65     def _validate_settings(self):
66         """
67         Validates the deploy settings file provided
68
69         DeploySettingsException will be raised if validation fails.
70         """
71
72         if 'deploy_options' not in self:
73             raise DeploySettingsException("No deploy options provided in"
74                                           " deploy settings file")
75         if 'global_params' not in self:
76             raise DeploySettingsException("No global options provided in"
77                                           " deploy settings file")
78
79         deploy_options = self['deploy_options']
80         if not isinstance(deploy_options, dict):
81             raise DeploySettingsException("deploy_options should be a list")
82
83         if ('gluon' in self['deploy_options'] and
84            'vpn' in self['deploy_options']):
85                 if (self['deploy_options']['gluon'] is True and
86                    self['deploy_options']['vpn'] is False):
87                         raise DeploySettingsException(
88                             "Invalid deployment configuration: "
89                             "If gluon is enabled, "
90                             "vpn also needs to be enabled")
91
92         for setting, value in deploy_options.items():
93             if setting not in REQ_DEPLOY_SETTINGS + OPT_DEPLOY_SETTINGS:
94                 raise DeploySettingsException("Invalid deploy_option {} "
95                                               "specified".format(setting))
96             if setting == 'dataplane':
97                 if value not in VALID_DATAPLANES:
98                     planes = ' '.join(VALID_DATAPLANES)
99                     raise DeploySettingsException(
100                         "Invalid dataplane {} specified. Valid dataplanes:"
101                         " {}".format(value, planes))
102
103         for req_set in REQ_DEPLOY_SETTINGS:
104             if req_set not in deploy_options:
105                 if req_set == 'dataplane':
106                     self['deploy_options'][req_set] = 'ovs'
107                 elif req_set == 'ceph':
108                     self['deploy_options'][req_set] = True
109                 elif req_set == 'ceph_device':
110                     self['deploy_options'][req_set] = '/dev/loop3'
111                 elif req_set == 'odl_version':
112                     self['deploy_options'][req_set] = \
113                         constants.DEFAULT_ODL_VERSION
114                 elif req_set == 'os_version':
115                     self['deploy_options'][req_set] = \
116                         constants.DEFAULT_OS_VERSION
117                 elif req_set == 'vim':
118                     self['deploy_options'][req_set] = 'openstack'
119                 else:
120                     self['deploy_options'][req_set] = False
121             elif req_set == 'odl_version' and self['deploy_options'][
122                     'odl_version'] not in constants.VALID_ODL_VERSIONS:
123                 raise DeploySettingsException(
124                     "Invalid ODL version: {}".format(self[deploy_options][
125                         'odl_version']))
126             elif req_set == 'sriov':
127                 if self['deploy_options'][req_set] is True:
128                     raise DeploySettingsException(
129                         "Invalid SRIOV interface name: {}".format(
130                             self['deploy_options']['sriov']))
131
132         if self['deploy_options']['odl_version'] == 'oxygen':
133             self['deploy_options']['odl_version'] = 'master'
134
135         if 'performance' in deploy_options:
136             if not isinstance(deploy_options['performance'], dict):
137                 raise DeploySettingsException("Performance deploy_option"
138                                               "must be a dictionary.")
139             for role, role_perf_sets in deploy_options['performance'].items():
140                 if role not in VALID_ROLES:
141                     raise DeploySettingsException("Performance role {}"
142                                                   "is not valid, choose"
143                                                   "from {}".format(
144                                                       role,
145                                                       " ".join(VALID_ROLES)
146                                                   ))
147
148                 for key in role_perf_sets:
149                     if key not in VALID_PERF_OPTS:
150                         raise DeploySettingsException("Performance option {} "
151                                                       "is not valid, choose"
152                                                       "from {}".format(
153                                                           key,
154                                                           " ".join(
155                                                               VALID_PERF_OPTS)
156                                                       ))
157         # validate global params
158         if 'ha_enabled' not in self['global_params']:
159
160             raise DeploySettingsException('ha_enabled is missing in global '
161                                           'parameters of deploy settings file')
162         if 'patches' not in self['global_params']:
163             self['global_params']['patches'] = dict()
164         for node in ('undercloud', 'overcloud'):
165             if node not in self['global_params']['patches']:
166                 self['global_params']['patches'][node] = list()
167             else:
168                 patches = self['global_params']['patches'][node]
169                 assert isinstance(patches, list)
170                 for patch in patches:
171                     assert isinstance(patch, dict)
172                     # Assert all required criteria exists for each patch
173                     assert all(i in patch.keys() for i in REQ_PATCH_CRITERIA)
174                     patch_criteria = REQ_PATCH_CRITERIA + OPT_PATCH_CRITERIA
175                     # Assert all patch keys are valid criteria
176                     assert all(i in patch_criteria for i in patch.keys())
177
178     def _dump_performance(self):
179         """
180         Creates performance settings string for bash consumption.
181         Output will be in the form of a list that can be iterated over in
182         bash, with each string being the direct input to the performance
183         setting script in the form <role> <category> <key> <value> to
184         facilitate modification of the correct image.
185         """
186         bash_str = 'performance_options=(\n'
187         deploy_options = self['deploy_options']
188         for role, settings in deploy_options['performance'].items():
189             for category, options in settings.items():
190                 for key, value in options.items():
191                     bash_str += "\"{} {} {} {}\"\n".format(role,
192                                                            category,
193                                                            key,
194                                                            value)
195         bash_str += ')\n'
196         bash_str += '\n'
197         bash_str += 'performance_roles=(\n'
198         for role in self['deploy_options']['performance']:
199             bash_str += role + '\n'
200         bash_str += ')\n'
201         bash_str += '\n'
202
203         return bash_str
204
205     def _dump_deploy_options_array(self):
206         """
207         Creates deploy settings array in bash syntax.
208         """
209         bash_str = ''
210         for key, value in self['deploy_options'].items():
211             if not isinstance(value, bool):
212                 bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key,
213                                                                        value)
214             else:
215                 bash_str += "deploy_options_array[{}]={}\n".format(key,
216                                                                    value)
217         return bash_str
218
219
220 class DeploySettingsException(Exception):
221     def __init__(self, value):
222         self.value = value
223
224     def __str__(self):
225         return self.value