Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import gobject |
| 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 7 | import os |
| 8 | import os.path as path |
Adriana Kobylak | 256be78 | 2016-08-24 15:43:16 -0500 | [diff] [blame] | 9 | import sys |
Lei YU | 1d8b7cd | 2016-12-21 11:01:28 +0800 | [diff] [blame] | 10 | from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 11 | from IPy import IP |
Adriana Kobylak | 256be78 | 2016-08-24 15:43:16 -0500 | [diff] [blame] | 12 | |
Brad Bishop | 966fcd5 | 2016-09-29 09:22:32 -0400 | [diff] [blame] | 13 | settings_file_path = os.path.join( |
| 14 | sys.prefix, |
| 15 | 'share', |
| 16 | 'phosphor-settings') |
Adriana Kobylak | 256be78 | 2016-08-24 15:43:16 -0500 | [diff] [blame] | 17 | sys.path.insert(1, settings_file_path) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 18 | import settings_file as s |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 19 | import re |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 20 | import obmc.mapper |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 21 | |
| 22 | DBUS_NAME = 'org.openbmc.settings.Host' |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 23 | CONTROL_INTF = 'org.openbmc.Settings' |
| 24 | |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 25 | def walk_nest(d, keys =()): |
| 26 | """Arrange dictionary keys and values. |
| 27 | |
| 28 | Walk the dictionary and establish every possible path |
| 29 | returned to and processed by 'create_object' below |
| 30 | """ |
| 31 | if isinstance(d, dict): |
| 32 | for k, v in d.iteritems(): |
| 33 | for rv in walk_nest(v, keys + (k, )): |
| 34 | yield rv |
| 35 | else: |
| 36 | yield keys, d |
| 37 | |
| 38 | def create_object(settings): |
| 39 | """Create and format objects. |
| 40 | |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 41 | Parse dictionary file and return all objects and settings |
| 42 | in the following format: {obj_name {settings}} |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 43 | """ |
Sergey Solomin | f68b864 | 2016-11-14 16:26:17 -0600 | [diff] [blame] | 44 | bus = get_dbus() |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 45 | mapper = obmc.mapper.Mapper(bus) |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 46 | allobjects = {} |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 47 | queries = {} |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 48 | for compound_key, val in walk_nest(settings): |
| 49 | obj_name = compound_key[0].lower() |
| 50 | obj_name = obj_name.replace(".","/") |
| 51 | obj_name = "/" + obj_name + "0" |
| 52 | |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 53 | for i in compound_key[2:len(compound_key)-2]: |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 54 | obj_name = obj_name + "/" + i |
| 55 | |
| 56 | setting = compound_key[len(compound_key) - 2] |
| 57 | attribute = compound_key[len(compound_key) - 1] |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 58 | if settings.get(compound_key[0], {}).get('query', {}): |
| 59 | q = queries.setdefault(obj_name, {}) |
| 60 | s = q.setdefault( |
| 61 | setting, {'name': None, 'type': None, 'default': None}) |
| 62 | else: |
| 63 | o = allobjects.setdefault(obj_name, {}) |
| 64 | s = o.setdefault( |
| 65 | setting, {'name': None, 'type': None, 'default': None}) |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 66 | s[attribute] = val |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 67 | for settings in queries.itervalues(): |
| 68 | for setting in settings.itervalues(): |
| 69 | if setting['type'] is not 'instance_query': |
| 70 | continue |
Lei YU | 1d8b7cd | 2016-12-21 11:01:28 +0800 | [diff] [blame] | 71 | paths = mapper.get_subtree_paths(setting['subtree'], 0, |
| 72 | retries=10, interval=0.1) |
Matt Spinler | 371dd02 | 2016-12-15 12:48:04 -0600 | [diff] [blame] | 73 | |
| 74 | if setting['keyregex'] == 'host': |
| 75 | # Always create at least one host object. |
| 76 | paths = set(paths + ['/org/openbmc/control/host0']) |
| 77 | |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 78 | for path in paths: |
| 79 | m = re.search(setting['matchregex'], path) |
| 80 | if not m: |
| 81 | continue |
| 82 | allobjects.setdefault( |
| 83 | "/org/openbmc/settings/" + m.group(1), settings) |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 84 | return allobjects |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 85 | |
Lei YU | 1d8b7cd | 2016-12-21 11:01:28 +0800 | [diff] [blame] | 86 | class HostSettingsObject(DbusProperties, DbusObjectManager): |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 87 | def __init__(self, bus, name, settings, path): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 88 | super(HostSettingsObject, self).__init__( |
| 89 | conn=bus, |
| 90 | object_path=name, |
| 91 | validator=self.input_validator) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 92 | self.bus = bus |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 93 | self.path = path |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 94 | self.name = name |
| 95 | self.settings = settings |
Yi Li | 5ebab48 | 2016-11-22 16:34:18 +0800 | [diff] [blame] | 96 | self.fname = name[name.rfind("/")+1:] + '-' |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 97 | |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 98 | # Needed to ignore the validation on default networkconfig values as |
| 99 | # opposed to user giving the same. |
| 100 | self.adminmode = True |
| 101 | |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 102 | if not os.path.exists(path): |
| 103 | os.mkdir(path) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 104 | |
| 105 | # Listen to changes in the property values and sync them to the BMC |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 106 | bus.add_signal_receiver( |
| 107 | self.settings_signal_handler, |
| 108 | dbus_interface="org.freedesktop.DBus.Properties", |
| 109 | signal_name="PropertiesChanged", |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 110 | path=name) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 111 | |
| 112 | # Create the dbus properties |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 113 | for setting in settings.itervalues(): |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 114 | if setting['type'] is 'instance_query': |
| 115 | continue |
| 116 | self.set_settings_property( |
Yi Li | 5ebab48 | 2016-11-22 16:34:18 +0800 | [diff] [blame] | 117 | setting['name'], setting['type'], setting['default'], |
| 118 | self.fname) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 119 | # Done with consuming factory settings. |
| 120 | self.adminmode = False |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 121 | |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 122 | def get_bmc_value(self, name, fname): |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 123 | try: |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 124 | with open(path.join(self.path, fname + name), 'r') as f: |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 125 | return f.read() |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 126 | except (IOError): |
| 127 | pass |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 128 | return None |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 129 | |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 130 | # Create dbus properties based on bmc value. |
| 131 | # This will be either a value previously set, |
| 132 | # or the default file value if the BMC value |
| 133 | # does not exist. |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 134 | def set_settings_property(self, attr_name, attr_type, value, fname): |
Patrick Williams | 3b8d055 | 2017-04-06 14:14:15 -0500 | [diff] [blame^] | 135 | default = value |
| 136 | |
| 137 | # Read from file. |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 138 | bmcv = self.get_bmc_value(attr_name, fname) |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 139 | if bmcv: |
| 140 | value = bmcv |
Patrick Williams | 3b8d055 | 2017-04-06 14:14:15 -0500 | [diff] [blame^] | 141 | |
| 142 | # Perform type mapping. |
| 143 | type_map = {"i": int, "s": str, "b": bool}[attr_type] |
| 144 | real_value = type_map(value) |
| 145 | real_default = type_map(default) |
| 146 | |
| 147 | try: |
| 148 | self.Set(DBUS_NAME, attr_name, real_value) |
| 149 | except ValueError: |
| 150 | print("Persistent value for {} is invalid: {}{} had '{}', " |
| 151 | "using '{}'.".format(attr_name, fname, attr_name, |
| 152 | value, default)) |
| 153 | self.Set(DBUS_NAME, attr_name, real_default) |
| 154 | self.set_system_settings(attr_name, real_default, fname) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 155 | |
| 156 | # Save the settings to the BMC. This will write the settings value in |
| 157 | # individual files named by the property name to the BMC. |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 158 | def set_system_settings(self, name, value, fname): |
| 159 | bmcv = self.get_bmc_value(name, fname) |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 160 | if bmcv != value: |
Sergey Solomin | 62b55f3 | 2016-10-13 10:40:27 -0500 | [diff] [blame] | 161 | filepath = path.join(self.path, fname + name) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 162 | with open(filepath, 'w') as f: |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 163 | f.write(str(value)) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 164 | |
| 165 | # Signal handler for when one ore more settings properties were updated. |
| 166 | # This will sync the changes to the BMC. |
Brad Bishop | 2a9fe66 | 2016-08-31 12:37:35 -0400 | [diff] [blame] | 167 | def settings_signal_handler( |
Yi Li | 5ebab48 | 2016-11-22 16:34:18 +0800 | [diff] [blame] | 168 | self, interface_name, changed_properties, invalidated_properties): |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 169 | for name, value in changed_properties.items(): |
Yi Li | 5ebab48 | 2016-11-22 16:34:18 +0800 | [diff] [blame] | 170 | self.set_system_settings(name, value, self.fname) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 171 | |
| 172 | # Placeholder signal. Needed to register the settings interface. |
Adriana Kobylak | 41a925e | 2016-01-28 16:44:27 -0600 | [diff] [blame] | 173 | @dbus.service.signal(DBUS_NAME, signature='s') |
| 174 | def SettingsUpdated(self, sname): |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 175 | pass |
| 176 | |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 177 | def validate_regex(self, regex, value): |
| 178 | if not re.compile(regex).search(value): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 179 | raise ValueError("Invalid input. Data does not satisfy regex") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 180 | |
| 181 | def validate_range(self, min, max, value): |
| 182 | if value not in range(min, max): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 183 | raise ValueError("Invalid input. Data not in allowed range") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 184 | |
Vishwanatha Subbanna | eb1bea8 | 2017-01-12 16:42:44 +0530 | [diff] [blame] | 185 | def validate_list(self, lst, value): |
| 186 | if value not in map(lambda val: val, lst): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 187 | raise ValueError("Invalid input. Data not in allowed values") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 188 | |
| 189 | # validate host network configuration |
| 190 | # need "ipaddress=,prefix=,gateway=,mac=,addr_type=" |
| 191 | # Must be able to handle any order |
| 192 | def validate_net_config(self, value): |
| 193 | if self.adminmode: |
| 194 | return |
| 195 | |
| 196 | # Need all of these to be given by the user. |
| 197 | user_config = [] |
| 198 | all_config = ['ipaddress', 'prefix', 'gateway', 'mac', 'addr_type'] |
| 199 | |
| 200 | # This has a hard data format mentioned above so no blanks allowed. |
| 201 | if value.count(" ") or value.count("=") != 5: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 202 | raise ValueError("Invalid Network Data. No white spaces allowed") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 203 | |
| 204 | config = value.split(',') |
| 205 | for key_value in config: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 206 | key, value = key_value.split('=') |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 207 | if not key or not value: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 208 | raise ValueError("Invalid key or Data") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 209 | |
| 210 | # Add the current key seen so we can compare at the end to see |
| 211 | # if all values have been given |
| 212 | user_config.append(key.lower()) |
| 213 | |
| 214 | if key.lower() == 'ipaddress' or key.lower() == 'gateway': |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 215 | IP(value) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 216 | |
| 217 | elif key.lower() == 'mac': |
| 218 | regex = '([a-fA-F0-9]{2}[:|\-]?){6}' |
| 219 | self.validate_regex(regex, value) |
| 220 | |
| 221 | elif key.lower() == 'prefix': |
| 222 | self.validate_range(0, 33, int(value)) |
| 223 | |
| 224 | elif key.lower() == 'addr_type': |
| 225 | allowed = ["STATIC", "DYNAMIC"] |
Vishwanatha Subbanna | eb1bea8 | 2017-01-12 16:42:44 +0530 | [diff] [blame] | 226 | self.validate_list(allowed, value) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 227 | |
| 228 | # Did user pass everything ?? |
| 229 | if set(all_config) - set(user_config): |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 230 | raise ValueError( |
| 231 | "Invalid Network Data. All information is mandatory") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 232 | |
| 233 | # Validate to see if the changes are in order |
| 234 | def input_validator(self, iface, proprty, value): |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 235 | # User entered key is not present |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 236 | shk = None |
| 237 | for attr in self.settings.itervalues(): |
| 238 | if attr['name'] == proprty: |
| 239 | shk = attr |
| 240 | |
| 241 | if shk is None: |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 242 | raise KeyError("Invalid Property") |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 243 | |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 244 | validation = shk.get('validation', None) |
| 245 | |
| 246 | if validation == 'list': |
Vishwanatha Subbanna | eb1bea8 | 2017-01-12 16:42:44 +0530 | [diff] [blame] | 247 | self.validate_list(shk['allowed'], value) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 248 | |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 249 | elif validation == 'range': |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 250 | self.validate_range(shk['min'], shk['max']+1, value) |
| 251 | |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 252 | elif validation == 'regex': |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 253 | self.validate_regex(shk['regex'], value) |
| 254 | |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 255 | elif validation == 'custom': |
Brad Bishop | c1e5e9f | 2016-09-29 09:40:01 -0400 | [diff] [blame] | 256 | getattr(self, shk['method'])(value) |
Vishwanatha Subbanna | 5b090c6 | 2016-09-21 15:49:26 +0530 | [diff] [blame] | 257 | |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 258 | if __name__ == '__main__': |
| 259 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 260 | |
Brad Bishop | 5ef7fe6 | 2016-05-17 08:39:17 -0400 | [diff] [blame] | 261 | bus = get_dbus() |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 262 | allobjects = create_object(s.SETTINGS) |
| 263 | lastobject = None |
| 264 | objs = [] |
| 265 | for o, settings in allobjects.iteritems(): |
| 266 | objs.append(HostSettingsObject(bus, o, settings, "/var/lib/obmc/")) |
| 267 | objs[-1].unmask_signals() |
Sergey Solomin | 4a2433f | 2016-10-03 10:22:57 -0500 | [diff] [blame] | 268 | mainloop = gobject.MainLoop() |
Brad Bishop | 2cbef3d | 2016-08-31 12:40:13 -0400 | [diff] [blame] | 269 | name = dbus.service.BusName(DBUS_NAME, bus) |
Adriana Kobylak | 4c60e5e | 2016-01-10 15:22:45 -0600 | [diff] [blame] | 270 | print "Running HostSettingsService" |
| 271 | mainloop.run() |
Brad Bishop | 31c42f0 | 2016-09-29 09:35:32 -0400 | [diff] [blame] | 272 | |
| 273 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |