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