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