Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
Steve McIntyre | 94ef65e | 2015-09-25 01:08:14 +0100 | [diff] [blame] | 3 | # Copyright 2014-2015 Linaro Limited |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 4 | # Author: Steve McIntyre <steve.mcintyre@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | # |
| 21 | # VLANd simple config parser |
| 22 | # |
| 23 | |
| 24 | import ConfigParser |
| 25 | import os, sys, re |
| 26 | |
| 27 | if __name__ == '__main__': |
| 28 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 29 | sys.path.insert(0, vlandpath) |
| 30 | sys.path.insert(0, "%s/.." % vlandpath) |
| 31 | |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 32 | from errors import ConfigError |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 33 | |
Steve McIntyre | c9e72e8 | 2015-07-14 15:25:08 +0100 | [diff] [blame] | 34 | def is_positive(text): |
Steve McIntyre | fa1beea | 2015-10-19 18:52:29 +0100 | [diff] [blame] | 35 | valid_true = ('1', 'y', 'yes', 't', 'true') |
| 36 | valid_false = ('0', 'n', 'no', 'f', 'false') |
| 37 | |
| 38 | if str(text) in valid_true or str(text).lower() in valid_true: |
Steve McIntyre | c9e72e8 | 2015-07-14 15:25:08 +0100 | [diff] [blame] | 39 | return True |
Steve McIntyre | fa1beea | 2015-10-19 18:52:29 +0100 | [diff] [blame] | 40 | elif str(text) in valid_false or str(text).lower() in valid_false: |
Steve McIntyre | c9e72e8 | 2015-07-14 15:25:08 +0100 | [diff] [blame] | 41 | return False |
| 42 | |
Steve McIntyre | c370980 | 2015-10-19 18:34:53 +0100 | [diff] [blame] | 43 | def is_valid_logging_level(text): |
| 44 | valid = ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG') |
| 45 | if text in valid: |
| 46 | return True |
| 47 | return False |
Steve McIntyre | 869ee5d | 2015-10-19 19:05:55 +0100 | [diff] [blame] | 48 | |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 49 | class DaemonConfigClass: |
| 50 | """ Simple container for stuff to make for nicer syntax """ |
| 51 | |
| 52 | def __repr__(self): |
| 53 | return "<DaemonConfig: port: %s>" % (self.port) |
| 54 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 55 | class DBConfigClass: |
| 56 | """ Simple container for stuff to make for nicer syntax """ |
| 57 | |
| 58 | def __repr__(self): |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 59 | return "<DBConfig: server: %s, port: %s, dbname: %s, username: %s, password: %s>" % (self.server, self.port, self.dbname, self.username, self.password) |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 60 | |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 61 | class LoggingConfigClass: |
| 62 | """ Simple container for stuff to make for nicer syntax """ |
| 63 | |
| 64 | def __repr__(self): |
| 65 | return "<LoggingConfig: level: %s, filename: %s>" % (self.level, self.filename) |
| 66 | |
Steve McIntyre | a866e0c | 2015-09-18 14:11:06 +0100 | [diff] [blame] | 67 | class VisualisationConfigClass: |
| 68 | """ Simple container for stuff to make for nicer syntax """ |
| 69 | def __repr__(self): |
| 70 | return "<VisualisationConfig: enabled: %s, port: %s>" % (self.enabled, self.port) |
| 71 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 72 | class SwitchConfigClass: |
| 73 | """ Simple container for stuff to make for nicer syntax """ |
| 74 | def __repr__(self): |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 75 | return "<SwitchConfig: name: %s, section: %s, driver: %s, username: %s, password: %s, enable_password: %s>" % (self.name, self.section, self.driver, self.username, self.password, self.enable_password) |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 76 | |
| 77 | class VlanConfig: |
| 78 | """VLANd config class""" |
| 79 | def __init__(self, filenames): |
Steve McIntyre | d773cac | 2014-12-10 23:27:07 +0000 | [diff] [blame] | 80 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 81 | config = ConfigParser.RawConfigParser({ |
| 82 | # Set default values |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 83 | 'dbname': None, |
Steve McIntyre | 7f359dd | 2015-10-08 14:28:56 +0100 | [diff] [blame] | 84 | 'debug': False, |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 85 | 'driver': None, |
Steve McIntyre | c9e72e8 | 2015-07-14 15:25:08 +0100 | [diff] [blame] | 86 | 'enable_password': None, |
Steve McIntyre | 7f359dd | 2015-10-08 14:28:56 +0100 | [diff] [blame] | 87 | 'enabled': False, |
| 88 | 'name': None, |
| 89 | 'password': None, |
| 90 | 'port': None, |
Steve McIntyre | b0aa460 | 2015-10-08 15:33:28 +0100 | [diff] [blame] | 91 | 'refresh': None, |
Steve McIntyre | 7f359dd | 2015-10-08 14:28:56 +0100 | [diff] [blame] | 92 | 'server': None, |
| 93 | 'username': None, |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 94 | }) |
| 95 | |
| 96 | config.read(filenames) |
| 97 | |
| 98 | # Parse out the config file |
| 99 | # Must have a [database] section |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 100 | # May have a [vland] section |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 101 | # May have a [logging] section |
| 102 | # May have multiple [switch 'foo'] sections |
| 103 | if not config.has_section('database'): |
| 104 | raise ConfigError('No database configuration section found') |
| 105 | |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 106 | # No DB-specific defaults to set |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 107 | self.database = DBConfigClass() |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 108 | |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 109 | # Set defaults logging details |
| 110 | self.logging = LoggingConfigClass() |
Steve McIntyre | c370980 | 2015-10-19 18:34:53 +0100 | [diff] [blame] | 111 | self.logging.level = 'CRITICAL' |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 112 | self.logging.filename = None |
| 113 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 114 | # Set default port number and VLAN tag |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 115 | self.vland = DaemonConfigClass() |
| 116 | self.vland.port = 3080 |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 117 | self.vland.default_vlan_tag = 1 |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 118 | |
Steve McIntyre | a866e0c | 2015-09-18 14:11:06 +0100 | [diff] [blame] | 119 | # Visualisation is disabled by default |
| 120 | self.visualisation = VisualisationConfigClass() |
| 121 | self.visualisation.port = 3081 |
| 122 | self.visualisation.enabled = False |
Steve McIntyre | cb25868 | 2015-09-18 14:25:06 +0100 | [diff] [blame] | 123 | |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 124 | # No switch-specific defaults to set |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 125 | self.switches = {} |
| 126 | |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 127 | sw_regex = re.compile(r'(switch)\ (.*)', flags=re.I) |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 128 | for section in config.sections(): |
| 129 | if section == 'database': |
| 130 | try: |
| 131 | self.database.server = config.get(section, 'server') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 132 | except ConfigParser.NoOptionError: |
| 133 | pass |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame] | 134 | except: |
| 135 | raise ConfigError('Invalid database configuration (server)') |
| 136 | |
| 137 | try: |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 138 | port = config.get(section, 'port') |
| 139 | if port is not None: |
| 140 | self.database.port = config.getint(section, 'port') |
| 141 | except ConfigParser.NoOptionError: |
| 142 | pass |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame] | 143 | except: |
| 144 | raise ConfigError('Invalid database configuration (port)') |
| 145 | |
| 146 | try: |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 147 | self.database.dbname = config.get(section, 'dbname') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 148 | except ConfigParser.NoOptionError: |
| 149 | pass |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame] | 150 | except: |
| 151 | raise ConfigError('Invalid database configuration (dbname)') |
| 152 | |
| 153 | try: |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 154 | self.database.username = config.get(section, 'username') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 155 | except ConfigParser.NoOptionError: |
| 156 | pass |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame] | 157 | except: |
| 158 | raise ConfigError('Invalid database configuration (username)') |
| 159 | |
| 160 | try: |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 161 | self.database.password = config.get(section, 'password') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 162 | except ConfigParser.NoOptionError: |
| 163 | pass |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 164 | except: |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame] | 165 | raise ConfigError('Invalid database configuration (password)') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 166 | |
| 167 | # Other database config options are optional, but these are not |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 168 | if self.database.dbname is None or self.database.username is None: |
| 169 | raise ConfigError('Database configuration section incomplete') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 170 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 171 | elif section == 'logging': |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 172 | try: |
| 173 | self.logging.level = config.get(section, 'level') |
| 174 | except ConfigParser.NoOptionError: |
| 175 | pass |
| 176 | except: |
| 177 | raise ConfigError('Invalid logging configuration (level)') |
Steve McIntyre | c370980 | 2015-10-19 18:34:53 +0100 | [diff] [blame] | 178 | self.logging.level = self.logging.level.upper() |
| 179 | if not is_valid_logging_level(self.logging.level): |
Steve McIntyre | 869ee5d | 2015-10-19 19:05:55 +0100 | [diff] [blame] | 180 | raise ConfigError('Invalid logging configuration (level)') |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 181 | |
| 182 | try: |
Steve McIntyre | 19e6b06 | 2015-02-12 08:05:08 +0000 | [diff] [blame] | 183 | self.logging.filename = config.get(section, 'filename') |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 184 | except ConfigParser.NoOptionError: |
| 185 | pass |
| 186 | except: |
| 187 | raise ConfigError('Invalid logging configuration (filename)') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 188 | |
| 189 | elif section == 'vland': |
| 190 | try: |
Steve McIntyre | d05265b | 2015-09-23 00:03:09 +0100 | [diff] [blame] | 191 | self.vland.port = config.getint(section, 'port') |
Steve McIntyre | a987506 | 2014-12-12 17:15:15 +0000 | [diff] [blame] | 192 | except ConfigParser.NoOptionError: |
| 193 | pass |
| 194 | except: |
| 195 | raise ConfigError('Invalid vland configuration (port)') |
| 196 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 197 | try: |
Steve McIntyre | c690698 | 2014-12-23 13:22:38 +0000 | [diff] [blame] | 198 | self.vland.default_vlan_tag = config.getint(section, 'default_vlan_tag') |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 199 | except ConfigParser.NoOptionError: |
| 200 | pass |
| 201 | except: |
| 202 | raise ConfigError('Invalid vland configuration (default_vlan_tag)') |
| 203 | |
Steve McIntyre | a866e0c | 2015-09-18 14:11:06 +0100 | [diff] [blame] | 204 | elif section == 'visualisation': |
| 205 | try: |
| 206 | self.visualisation.port = config.getint(section, 'port') |
| 207 | except ConfigParser.NoOptionError: |
| 208 | pass |
| 209 | except: |
| 210 | raise ConfigError('Invalid visualisation configuration (port)') |
| 211 | |
| 212 | try: |
| 213 | self.visualisation.enabled = config.get(section, 'enabled') |
| 214 | if not is_positive(self.visualisation.enabled): |
| 215 | self.visualisation.enabled = False |
| 216 | elif is_positive(self.visualisation.enabled): |
| 217 | self.visualisation.enabled = True |
| 218 | except ConfigParser.NoOptionError: |
| 219 | pass |
| 220 | except: |
| 221 | raise ConfigError('Invalid visualisation configuration (enabled)') |
| 222 | |
Steve McIntyre | b0aa460 | 2015-10-08 15:33:28 +0100 | [diff] [blame] | 223 | try: |
| 224 | self.visualisation.refresh = config.get(section, 'refresh') |
| 225 | if self.visualisation.refresh is not None: |
Steve McIntyre | c2308ff | 2015-10-27 12:07:17 +0000 | [diff] [blame] | 226 | if not is_positive(self.visualisation.refresh): |
| 227 | self.visualisation.refresh = None |
| 228 | else: |
| 229 | self.visualisation.refresh = int(self.visualisation.refresh) |
Steve McIntyre | b0aa460 | 2015-10-08 15:33:28 +0100 | [diff] [blame] | 230 | except ConfigParser.NoOptionError: |
| 231 | pass |
| 232 | except: |
| 233 | raise ConfigError('Invalid visualisation configuration (refresh)') |
| 234 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 235 | else: |
| 236 | match = sw_regex.match(section) |
| 237 | if match: |
| 238 | # Constraint: switch names must be unique! See if |
| 239 | # there's already a switch with this name |
| 240 | name = config.get(section, 'name') |
| 241 | for key in self.switches.keys(): |
| 242 | if name == key: |
| 243 | raise ConfigError('Found switches with the same name (%s)' % name) |
| 244 | self.switches[name] = SwitchConfigClass() |
| 245 | self.switches[name].name = name |
| 246 | self.switches[name].section = section |
| 247 | self.switches[name].driver = config.get(section, 'driver') |
| 248 | self.switches[name].username = config.get(section, 'username') |
| 249 | self.switches[name].password = config.get(section, 'password') |
| 250 | self.switches[name].enable_password = config.get(section, 'enable_password') |
Steve McIntyre | c9e72e8 | 2015-07-14 15:25:08 +0100 | [diff] [blame] | 251 | self.switches[name].debug = config.get(section, 'debug') |
| 252 | if not is_positive(self.switches[name].debug): |
| 253 | self.switches[name].debug = False |
| 254 | elif is_positive(self.switches[name].debug): |
| 255 | self.switches[name].debug = True |
| 256 | else: |
| 257 | raise ConfigError('Invalid vland configuration (switch "%s", debug "%s"' % (name, self.switches[name].debug)) |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 258 | else: |
| 259 | raise ConfigError('Unrecognised config section %s' % section) |
| 260 | |
Steve McIntyre | 4e9fee5 | 2015-10-19 18:54:23 +0100 | [diff] [blame] | 261 | # Generic checking for config values |
| 262 | if self.visualisation.enabled: |
| 263 | if self.visualisation.port == self.vland.port: |
Steve McIntyre | 869ee5d | 2015-10-19 19:05:55 +0100 | [diff] [blame] | 264 | raise ConfigError('Invalid configuration: VLANd and the visualisation service must use distinct port numbers') |
Steve McIntyre | 4e9fee5 | 2015-10-19 18:54:23 +0100 | [diff] [blame] | 265 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 266 | def __del__(self): |
| 267 | pass |
| 268 | |
| 269 | if __name__ == '__main__': |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 270 | c = VlanConfig(filenames=('./vland.cfg',)) |
| 271 | print c.database |
| 272 | print c.vland |
| 273 | for switch in c.switches: |
| 274 | print c.switches[switch] |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 275 | |