From 2da33853506c694684ced99675d6914053246b2a Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 11 Aug 2014 15:59:51 -0400 Subject: Use --noop instead of --mock. Updates the apply() function for each NetConfig object so that it now accepts noop instead. Also, the updated files are not returned (always instead of conditionally if mock was used). --- os_net_config/__init__.py | 9 ++++++++- os_net_config/cli.py | 17 +++++++++-------- os_net_config/impl_eni.py | 13 ++++++++++--- os_net_config/impl_ifcfg.py | 23 +++++++++++++---------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/os_net_config/__init__.py b/os_net_config/__init__.py index 6c6de9b..a3ea278 100644 --- a/os_net_config/__init__.py +++ b/os_net_config/__init__.py @@ -56,5 +56,12 @@ class NetConfig(object): def addBond(self, bond): raise NotImplemented("addBond is not implemented.") - def apply(self): + def apply(self, noop=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). + """ raise NotImplemented("apply is not implemented.") diff --git a/os_net_config/cli.py b/os_net_config/cli.py index 89ba3d0..0efc399 100644 --- a/os_net_config/cli.py +++ b/os_net_config/cli.py @@ -58,8 +58,8 @@ def parse_opts(argv): parser.add_argument('--version', action='version', version=os_net_config.__version__) parser.add_argument( - '-m', '--mock', - dest="mock", + '--noop', + dest="noop", action='store_true', help="Return the configuration commands, without applying them.", required=False) @@ -122,12 +122,13 @@ def main(argv=sys.argv): for iface_json in iface_array: obj = objects.object_from_json(iface_json) provider.addObject(obj) - if opts.mock: - res = provider.apply(mock=True) - print res - else: - provider.apply() - return 0 + files_changed = provider.apply(noop=opts.noop) + if opts.noop: + for location, data in files_changed.iteritems(): + print "File: %s\n" % location + print data + print "----" + return 0 if __name__ == '__main__': diff --git a/os_net_config/impl_eni.py b/os_net_config/impl_eni.py index 0df8b15..c32fc82 100644 --- a/os_net_config/impl_eni.py +++ b/os_net_config/impl_eni.py @@ -159,7 +159,14 @@ class ENINetConfig(os_net_config.NetConfig): self.routes[interface_name] = data logger.debug('route data: %s' % self.routes[interface_name]) - def apply(self, mock=False): + def apply(self, noop=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 @@ -175,8 +182,8 @@ class ENINetConfig(os_net_config.NetConfig): new_config += iface_data if (utils.diff(_network_config_path(), new_config)): - if mock: - return 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, diff --git a/os_net_config/impl_ifcfg.py b/os_net_config/impl_ifcfg.py index 6b920ae..87cf3d5 100644 --- a/os_net_config/impl_ifcfg.py +++ b/os_net_config/impl_ifcfg.py @@ -162,9 +162,15 @@ class IfcfgNetConfig(os_net_config.NetConfig): if bond.routes: self._addRoutes(bond.name, bond.routes) - def apply(self, mock=False): - if not mock: - logger.info('applying network configs...') + def apply(self, noop=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). + """ + logger.info('applying network configs...') restart_interfaces = [] restart_bridges = [] update_files = {} @@ -176,7 +182,6 @@ class IfcfgNetConfig(os_net_config.NetConfig): restart_interfaces.append(interface_name) update_files[ifcfg_config_path(interface_name)] = iface_data update_files[route_config_path(interface_name)] = route_data - elif not mock: logger.info('No changes required for interface: %s' % interface_name) @@ -187,14 +192,10 @@ class IfcfgNetConfig(os_net_config.NetConfig): restart_bridges.append(bridge_name) update_files[bridge_config_path(bridge_name)] = bridge_data update_files[route_config_path(bridge_name)] = route_data - elif not mock: logger.info('No changes required for bridge: %s' % bridge_name) - if mock: - mock_str = "" - for location, data in update_files.iteritems(): - mock_str += "%s:\n%s" % (location, data) - return mock_str + if noop: + return update_files for interface in restart_interfaces: logger.info('running ifdown on interface: %s' % interface) @@ -217,3 +218,5 @@ class IfcfgNetConfig(os_net_config.NetConfig): for interface in restart_interfaces: logger.info('running ifup on interface: %s' % interface) processutils.execute('/sbin/ifup', interface) + + return update_files -- cgit 1.2.3-korg