c0594056f58404eebd978cbb47e7f0f88537aaf3
[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 utils
14 from apex.common import constants
15
16 REQ_DEPLOY_SETTINGS = ['sdn_controller',
17                        'odl_version',
18                        'tacker',
19                        'congress',
20                        'dataplane',
21                        'sfc',
22                        'vpn',
23                        'vpp',
24                        'ceph',
25                        'gluon',
26                        'rt_kvm']
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 VALID_ODL_VERSIONS = ['carbon', 'nitrogen', 'oxygen', 'master']
43
44
45 class DeploySettings(dict):
46     """
47     This class parses a APEX deploy settings yaml file into an object
48     """
49     def __init__(self, filename):
50         if isinstance(filename, str):
51             with open(filename, 'r') as deploy_settings_file:
52                 init_dict = yaml.safe_load(deploy_settings_file)
53         else:
54             # assume input is a dict to build from
55             init_dict = filename
56
57         super().__init__(init_dict)
58         self._validate_settings()
59
60     def _validate_settings(self):
61         """
62         Validates the deploy settings file provided
63
64         DeploySettingsException will be raised if validation fails.
65         """
66
67         if 'deploy_options' not in self:
68             raise DeploySettingsException("No deploy options provided in"
69                                           " deploy settings file")
70         if 'global_params' not in self:
71             raise DeploySettingsException("No global options provided in"
72                                           " deploy settings file")
73
74         deploy_options = self['deploy_options']
75         if not isinstance(deploy_options, dict):
76             raise DeploySettingsException("deploy_options should be a list")
77
78         if ('gluon' in self['deploy_options'] and
79            'vpn' in self['deploy_options']):
80                 if (self['deploy_options']['gluon'] is True and
81                    self['deploy_options']['vpn'] is False):
82                         raise DeploySettingsException(
83                             "Invalid deployment configuration: "
84                             "If gluon is enabled, "
85                             "vpn also needs to be enabled")
86
87         for setting, value in deploy_options.items():
88             if setting not in REQ_DEPLOY_SETTINGS + OPT_DEPLOY_SETTINGS:
89                 raise DeploySettingsException("Invalid deploy_option {} "
90                                               "specified".format(setting))
91             if setting == 'dataplane':
92                 if value not in VALID_DATAPLANES:
93                     planes = ' '.join(VALID_DATAPLANES)
94                     raise DeploySettingsException(
95                         "Invalid dataplane {} specified. Valid dataplanes:"
96                         " {}".format(value, planes))
97
98         for req_set in REQ_DEPLOY_SETTINGS:
99             if req_set not in deploy_options:
100                 if req_set == 'dataplane':
101                     self['deploy_options'][req_set] = 'ovs'
102                 elif req_set == 'ceph':
103                     self['deploy_options'][req_set] = True
104                 elif req_set == 'odl_version':
105                     self['deploy_options'][req_set] = \
106                         constants.DEFAULT_ODL_VERSION
107                 else:
108                     self['deploy_options'][req_set] = False
109             elif req_set == 'odl_version' and self['deploy_options'][
110                     'odl_version'] not in VALID_ODL_VERSIONS:
111                 raise DeploySettingsException(
112                     "Invalid ODL version: {}".format(self[deploy_options][
113                         'odl_version']))
114
115         if self['deploy_options']['odl_version'] == 'oxygen':
116             self['deploy_options']['odl_version'] = 'master'
117
118         if 'performance' in deploy_options:
119             if not isinstance(deploy_options['performance'], dict):
120                 raise DeploySettingsException("Performance deploy_option"
121                                               "must be a dictionary.")
122             for role, role_perf_sets in deploy_options['performance'].items():
123                 if role not in VALID_ROLES:
124                     raise DeploySettingsException("Performance role {}"
125                                                   "is not valid, choose"
126                                                   "from {}".format(
127                                                       role,
128                                                       " ".join(VALID_ROLES)
129                                                   ))
130
131                 for key in role_perf_sets:
132                     if key not in VALID_PERF_OPTS:
133                         raise DeploySettingsException("Performance option {} "
134                                                       "is not valid, choose"
135                                                       "from {}".format(
136                                                           key,
137                                                           " ".join(
138                                                               VALID_PERF_OPTS)
139                                                       ))
140
141     def _dump_performance(self):
142         """
143         Creates performance settings string for bash consumption.
144
145         Output will be in the form of a list that can be iterated over in
146         bash, with each string being the direct input to the performance
147         setting script in the form <role> <category> <key> <value> to
148         facilitate modification of the correct image.
149         """
150         bash_str = 'performance_options=(\n'
151         deploy_options = self['deploy_options']
152         for role, settings in deploy_options['performance'].items():
153             for category, options in settings.items():
154                 for key, value in options.items():
155                     bash_str += "\"{} {} {} {}\"\n".format(role,
156                                                            category,
157                                                            key,
158                                                            value)
159         bash_str += ')\n'
160         bash_str += '\n'
161         bash_str += 'performance_roles=(\n'
162         for role in self['deploy_options']['performance']:
163             bash_str += role + '\n'
164         bash_str += ')\n'
165         bash_str += '\n'
166
167         return bash_str
168
169     def _dump_deploy_options_array(self):
170         """
171         Creates deploy settings array in bash syntax.
172         """
173         bash_str = ''
174         for key, value in self['deploy_options'].items():
175             if not isinstance(value, bool):
176                 bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key,
177                                                                        value)
178             else:
179                 bash_str += "deploy_options_array[{}]={}\n".format(key,
180                                                                    value)
181         return bash_str
182
183
184 class DeploySettingsException(Exception):
185     def __init__(self, value):
186         self.value = value
187
188     def __str__(self):
189         return self.value