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