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