def apply(self, noop=False, cleanup=False): """Apply the network configuration. :param noop: A boolean which indicates whether this is a no-op. :returns: a dict of the format: filename/data which contains info for each file that was changed (or would be changed if in --noop mode). """ new_config = "" # write out bridges first. This ensures that an ifup -a # on reboot brings them up first for bridge_name, bridge_data in self.bridges.iteritems(): route_data = self.routes.get(bridge_name) bridge_data += (route_data or '') new_config += bridge_data for interface_name, iface_data in self.interfaces.iteritems(): route_data = self.routes.get(interface_name) iface_data += (route_data or '') new_config += iface_data if (utils.diff(_network_config_path(), new_config)): if noop: return {"/etc/network/interfaces": new_config} for interface in self.interfaces.keys(): logger.info('running ifdown on interface: %s' % interface) processutils.execute('/sbin/ifdown', interface, check_exit_code=False) for bridge in self.bridges.keys(): logger.info('running ifdown on bridge: %s' % bridge) processutils.execute('/sbin/ifdown', bridge, check_exit_code=False) logger.info('writing config file') utils.write_config(_network_config_path(), new_config) for bridge in self.bridges.keys(): logger.info('running ifup on bridge: %s' % bridge) processutils.execute('/sbin/ifup', bridge) for interface in self.interfaces.keys(): logger.info('running ifup on interface: %s' % interface) processutils.execute('/sbin/ifup', interface) else: logger.info('No interface changes are required.')
def apply(self): logger.info('applying network configs...') restart_interfaces = [] restart_bridges = [] update_files = {} for interface_name, iface_data in self.interfaces.iteritems(): route_data = self.routes.get(interface_name, '') if (utils.diff(ifcfg_config_path(interface_name), iface_data) or utils.diff(route_config_path(interface_name), route_data)): restart_interfaces.append(interface_name) update_files[ifcfg_config_path(interface_name)] = iface_data update_files[route_config_path(interface_name)] = route_data else: logger.info('No changes required for interface: %s' % interface_name) for bridge_name, bridge_data in self.bridges.iteritems(): route_data = self.routes.get(bridge_name, '') if (utils.diff(ifcfg_config_path(bridge_name), bridge_data) or utils.diff(route_config_path(bridge_name), route_data)): restart_bridges.append(bridge_name) update_files[bridge_config_path(bridge_name)] = bridge_data update_files[route_config_path(bridge_name)] = route_data else: logger.info('No changes required for bridge: %s' % bridge_name) for interface in restart_interfaces: logger.info('running ifdown on interface: %s' % interface) processutils.execute('/sbin/ifdown', interface, check_exit_code=False) for bridge in restart_bridges: logger.info('running ifdown on bridge: %s' % bridge) processutils.execute('/sbin/ifdown', bridge, check_exit_code=False) for location, data in update_files.iteritems(): logger.info('writing config file: %s' % location) utils.write_config(location, data) for bridge in restart_bridges: logger.info('running ifup on bridge: %s' % bridge) processutils.execute('/sbin/ifup', bridge) for interface in restart_interfaces: logger.info('running ifup on interface: %s' % interface) processutils.execute('/sbin/ifup', interface)
def apply(self): new_config = "" # write out bridges first. This ensures that an ifup -a # on reboot brings them up first for bridge_name, bridge_data in self.bridges.iteritems(): route_data = self.routes.get(bridge_name) bridge_data += (route_data or '') new_config += bridge_data for interface_name, iface_data in self.interfaces.iteritems(): route_data = self.routes.get(interface_name) iface_data += (route_data or '') new_config += iface_data if (utils.diff(_network_config_path(), new_config)): for interface in self.interfaces.keys(): logger.info('running ifdown on interface: %s' % interface) processutils.execute('/sbin/ifdown', interface, check_exit_code=False) for bridge in self.bridges.keys(): logger.info('running ifdown on bridge: %s' % bridge) processutils.execute('/sbin/ifdown', bridge, check_exit_code=False) logger.info('writing config file') utils.write_config(_network_config_path(), new_config) for bridge in self.bridges.keys(): logger.info('running ifup on bridge: %s' % bridge) processutils.execute('/sbin/ifup', bridge) for interface in self.interfaces.keys(): logger.info('running ifup on interface: %s' % interface) processutils.execute('/sbin/ifup', interface) else: logger.info('No interface changes are required.')
def apply(self, noop=False, cleanup=False): """Apply the network configuration. :param noop: A boolean which indicates whether this is a no-op. :param cleanup: A boolean which indicates whether any undefined (existing but not present in the object model) interface should be disabled and deleted. :returns: a dict of the format: filename/data which contains info for each file that was changed (or would be changed if in --noop mode). """ logger.info('applying network configs...') restart_interfaces = [] restart_bridges = [] update_files = {} all_file_names = [] for interface_name, iface_data in self.interface_data.iteritems(): route_data = self.route_data.get(interface_name, '') interface_path = ifcfg_config_path(interface_name) route_path = route_config_path(interface_name) all_file_names.append(interface_path) all_file_names.append(route_path) if (utils.diff(interface_path, iface_data) or utils.diff(route_path, route_data)): restart_interfaces.append(interface_name) restart_interfaces.extend(self.child_members(interface_name)) update_files[interface_path] = iface_data update_files[route_path] = route_data logger.info('No changes required for interface: %s' % interface_name) for bridge_name, bridge_data in self.bridge_data.iteritems(): route_data = self.route_data.get(bridge_name, '') bridge_path = bridge_config_path(bridge_name) bridge_route_path = route_config_path(bridge_name) all_file_names.append(bridge_path) all_file_names.append(bridge_route_path) if (utils.diff(bridge_path, bridge_data) or utils.diff(bridge_route_path, route_data)): restart_bridges.append(bridge_name) restart_interfaces.extend(self.child_members(bridge_name)) update_files[bridge_path] = bridge_data update_files[bridge_route_path] = route_data logger.info('No changes required for bridge: %s' % bridge_name) if noop: return update_files if cleanup: for ifcfg_file in glob.iglob(cleanup_pattern()): if ifcfg_file not in all_file_names: interface_name = ifcfg_file[len(cleanup_pattern()) - 1:] if interface_name != 'lo': logger.info('cleaning up interface: %s' % interface_name) processutils.execute('/sbin/ifdown', interface_name, check_exit_code=False) os.remove(ifcfg_file) for interface in restart_interfaces: logger.info('running ifdown on interface: %s' % interface) processutils.execute('/sbin/ifdown', interface, check_exit_code=False) for bridge in restart_bridges: logger.info('running ifdown on bridge: %s' % bridge) processutils.execute('/sbin/ifdown', bridge, check_exit_code=False) for location, data in update_files.iteritems(): logger.info('writing config file: %s' % location) utils.write_config(location, data) for bridge in restart_bridges: logger.info('running ifup on bridge: %s' % bridge) processutils.execute('/sbin/ifup', bridge) for interface in restart_interfaces: logger.info('running ifup on interface: %s' % interface) processutils.execute('/sbin/ifup', interface) return update_files