Enables containerized overcloud deployments
[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
31 OPT_DEPLOY_SETTINGS = ['performance',
32                        'vsperf',
33                        'ceph_device',
34                        'yardstick',
35                        'dovetail',
36                        'odl_vpp_routing_node',
37                        'dvr',
38                        'odl_vpp_netvirt',
39                        'barometer',
40                        'calipso']
41
42 VALID_ROLES = ['Controller', 'Compute', 'ObjectStorage']
43 VALID_PERF_OPTS = ['kernel', 'nova', 'vpp', 'ovs']
44 VALID_DATAPLANES = ['ovs', 'ovs_dpdk', 'fdio']
45 REQ_PATCH_CRITERIA = ['change-id', 'project']
46 OPT_PATCH_CRITERIA = ['branch']
47
48
49 class DeploySettings(dict):
50     """
51     This class parses a APEX deploy settings yaml file into an object
52     """
53     def __init__(self, filename):
54         if isinstance(filename, str):
55             with open(filename, 'r') as deploy_settings_file:
56                 init_dict = yaml.safe_load(deploy_settings_file)
57         else:
58             # assume input is a dict to build from
59             init_dict = filename
60
61         super().__init__(init_dict)
62         self._validate_settings()
63
64     def _validate_settings(self):
65         """
66         Validates the deploy settings file provided
67
68         DeploySettingsException will be raised if validation fails.
69         """
70
71         if 'deploy_options' not in self:
72             raise DeploySettingsException("No deploy options provided in"
73                                           " deploy settings file")
74         if 'global_params' not in self:
75             raise DeploySettingsException("No global options provided in"
76                                           " deploy settings file")
77
78         deploy_options = self['deploy_options']
79         if not isinstance(deploy_options, dict):
80             raise DeploySettingsException("deploy_options should be a list")
81
82         if ('gluon' in self['deploy_options'] and
83            'vpn' in self['deploy_options']):
84                 if (self['deploy_options']['gluon'] is True and
85                    self['deploy_options']['vpn'] is False):
86                         raise DeploySettingsException(
87                             "Invalid deployment configuration: "
88                             "If gluon is enabled, "
89                             "vpn also needs to be enabled")
90
91         for setting, value in deploy_options.items():
92             if setting not in REQ_DEPLOY_SETTINGS + OPT_DEPLOY_SETTINGS:
93                 raise DeploySettingsException("Invalid deploy_option {} "
94                                               "specified".format(setting))
95             if setting == 'dataplane':
96                 if value not in VALID_DATAPLANES:
97                     planes = ' '.join(VALID_DATAPLANES)
98                     raise DeploySettingsException(
99                         "Invalid dataplane {} specified. Valid dataplanes:"
100                         " {}".format(value, planes))
101
102         for req_set in REQ_DEPLOY_SETTINGS:
103             if req_set not in deploy_options:
104                 if req_set == 'dataplane':
105                     self['deploy_options'][req_set] = 'ovs'
106                 elif req_set == 'ceph':
107                     self['deploy_options'][req_set] = True
108                 elif req_set == 'odl_version':
109                     self['deploy_options'][req_set] = \
110                         constants.DEFAULT_ODL_VERSION
111                 elif req_set == 'os_version':
112                     self['deploy_options'][req_set] = \
113                         constants.DEFAULT_OS_VERSION
114                 else:
115                     self['deploy_options'][req_set] = False
116             elif req_set == 'odl_version' and self['deploy_options'][
117                     'odl_version'] not in constants.VALID_ODL_VERSIONS:
118                 raise DeploySettingsException(
119                     "Invalid ODL version: {}".format(self[deploy_options][
120                         'odl_version']))
121             elif req_set == 'sriov':
122                 if self['deploy_options'][req_set] is True:
123                     raise DeploySettingsException(
124                         "Invalid SRIOV interface name: {}".format(
125                             self['deploy_options']['sriov']))
126
127         if self['deploy_options']['odl_version'] == 'oxygen':
128             self['deploy_options']['odl_version'] = 'master'
129
130         if 'performance' in deploy_options:
131             if not isinstance(deploy_options['performance'], dict):
132                 raise DeploySettingsException("Performance deploy_option"
133                                               "must be a dictionary.")
134             for role, role_perf_sets in deploy_options['performance'].items():
135                 if role not in VALID_ROLES:
136                     raise DeploySettingsException("Performance role {}"
137                                                   "is not valid, choose"
138                                                   "from {}".format(
139                                                       role,
140                                                       " ".join(VALID_ROLES)
141                                                   ))
142
143                 for key in role_perf_sets:
144                     if key not in VALID_PERF_OPTS:
145                         raise DeploySettingsException("Performance option {} "
146                                                       "is not valid, choose"
147                                                       "from {}".format(
148                                                           key,
149                                                           " ".join(
150                                                               VALID_PERF_OPTS)
151                                                       ))
152         # validate global params
153         if 'ha_enabled' not in self['global_params']:
154
155             raise DeploySettingsException('ha_enabled is missing in global '
156                                           'parameters of deploy settings file')
157         if 'patches' not in self['global_params']:
158             self['global_params']['patches'] = dict()
159         for node in ('undercloud', 'overcloud'):
160             if node not in self['global_params']['patches']:
161                 self['global_params']['patches'][node] = list()
162             else:
163                 patches = self['global_params']['patches'][node]
164                 assert isinstance(patches, list)
165                 for patch in patches:
166                     assert isinstance(patch, dict)
167                     # Assert all required criteria exists for each patch
168                     assert all(i in patch.keys() for i in REQ_PATCH_CRITERIA)
169                     patch_criteria = REQ_PATCH_CRITERIA + OPT_PATCH_CRITERIA
170                     # Assert all patch keys are valid criteria
171                     assert all(i in patch_criteria for i in patch.keys())
172
173     def _dump_performance(self):
174         """
175         Creates performance settings string for bash consumption.
176         Output will be in the form of a list that can be iterated over in
177         bash, with each string being the direct input to the performance
178         setting script in the form <role> <category> <key> <value> to
179         facilitate modification of the correct image.
180         """
181         bash_str = 'performance_options=(\n'
182         deploy_options = self['deploy_options']
183         for role, settings in deploy_options['performance'].items():
184             for category, options in settings.items():
185                 for key, value in options.items():
186                     bash_str += "\"{} {} {} {}\"\n".format(role,
187                                                            category,
188                                                            key,
189                                                            value)
190         bash_str += ')\n'
191         bash_str += '\n'
192         bash_str += 'performance_roles=(\n'
193         for role in self['deploy_options']['performance']:
194             bash_str += role + '\n'
195         bash_str += ')\n'
196         bash_str += '\n'
197
198         return bash_str
199
200     def _dump_deploy_options_array(self):
201         """
202         Creates deploy settings array in bash syntax.
203         """
204         bash_str = ''
205         for key, value in self['deploy_options'].items():
206             if not isinstance(value, bool):
207                 bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key,
208                                                                        value)
209             else:
210                 bash_str += "deploy_options_array[{}]={}\n".format(key,
211                                                                    value)
212         return bash_str
213
214
215 class DeploySettingsException(Exception):
216     def __init__(self, value):
217         self.value = value
218
219     def __str__(self):
220         return self.value