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