Move noop flag to provider base-class

Moving this flag makes it easier to implement additional functions
which work differently depending on noop mode being set, vs passing
it into every function like apply()

Change-Id: I796792aece3e40322523e910a3b87f7ab9a451dd
This commit is contained in:
Steven Hardy
2015-01-22 11:33:38 +00:00
parent a4ad189558
commit 70c235d30a
4 changed files with 20 additions and 16 deletions

View File

@@ -24,6 +24,9 @@ class NotImplemented(Exception):
class NetConfig(object): class NetConfig(object):
"""Configure network interfaces using the ifcfg format.""" """Configure network interfaces using the ifcfg format."""
def __init__(self, noop=False):
self.noop = noop
def add_object(self, obj): def add_object(self, obj):
"""Convenience method to add any type of object to the network config. """Convenience method to add any type of object to the network config.
See objects.py. See objects.py.
@@ -71,10 +74,9 @@ class NetConfig(object):
""" """
raise NotImplemented("add_bond is not implemented.") raise NotImplemented("add_bond is not implemented.")
def apply(self, noop=False, cleanup=False): def apply(self, cleanup=False):
"""Apply the network configuration. """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 :param cleanup: A boolean which indicates whether any undefined
(existing but not present in the object model) interfaces (existing but not present in the object model) interfaces
should be disabled and deleted. should be disabled and deleted.

View File

@@ -112,19 +112,19 @@ def main(argv=sys.argv):
provider = None provider = None
if opts.provider: if opts.provider:
if opts.provider == 'ifcfg': if opts.provider == 'ifcfg':
provider = impl_ifcfg.IfcfgNetConfig() provider = impl_ifcfg.IfcfgNetConfig(noop=opts.noop)
elif opts.provider == 'eni': elif opts.provider == 'eni':
provider = impl_eni.ENINetConfig() provider = impl_eni.ENINetConfig(noop=opts.noop)
elif opts.provider == 'iproute': elif opts.provider == 'iproute':
provider = impl_iproute.IPRouteNetConfig() provider = impl_iproute.IPRouteNetConfig(noop=opts.noop)
else: else:
logger.error('Invalid provider specified.') logger.error('Invalid provider specified.')
return 1 return 1
else: else:
if os.path.exists('/etc/sysconfig/network-scripts/'): if os.path.exists('/etc/sysconfig/network-scripts/'):
provider = impl_ifcfg.IfcfgNetConfig() provider = impl_ifcfg.IfcfgNetConfig(noop=opts.noop)
elif os.path.exists('/etc/network/'): elif os.path.exists('/etc/network/'):
provider = impl_eni.ENINetConfig() provider = impl_eni.ENINetConfig(noop=opts.noop)
else: else:
logger.error('Unable to set provider for this operating system.') logger.error('Unable to set provider for this operating system.')
return 1 return 1
@@ -161,7 +161,7 @@ def main(argv=sys.argv):
iface_json.update({'persist_mapping': persist_mapping}) iface_json.update({'persist_mapping': persist_mapping})
obj = objects.object_from_json(iface_json) obj = objects.object_from_json(iface_json)
provider.add_object(obj) provider.add_object(obj)
files_changed = provider.apply(noop=opts.noop, cleanup=opts.cleanup) files_changed = provider.apply(cleanup=opts.cleanup)
if opts.noop: if opts.noop:
for location, data in files_changed.iteritems(): for location, data in files_changed.iteritems():
print "File: %s\n" % location print "File: %s\n" % location

View File

@@ -39,7 +39,8 @@ class ENINetConfig(os_net_config.NetConfig):
/etc/network/interfaces format. /etc/network/interfaces format.
""" """
def __init__(self): def __init__(self, noop=False):
super(ENINetConfig, self).__init__(noop)
self.interfaces = {} self.interfaces = {}
self.routes = {} self.routes = {}
self.bridges = {} self.bridges = {}
@@ -188,13 +189,13 @@ class ENINetConfig(os_net_config.NetConfig):
self.routes[interface_name] = data self.routes[interface_name] = data
logger.debug('route data: %s' % self.routes[interface_name]) logger.debug('route data: %s' % self.routes[interface_name])
def apply(self, noop=False, cleanup=False): def apply(self, cleanup=False):
"""Apply the network configuration. """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 :returns: a dict of the format: filename/data which contains info
for each file that was changed (or would be changed if in --noop for each file that was changed (or would be changed if in --noop
mode). mode).
Note the noop mode is set via the constructor noop boolean
""" """
new_config = "" new_config = ""
@@ -211,7 +212,7 @@ class ENINetConfig(os_net_config.NetConfig):
new_config += iface_data new_config += iface_data
if (utils.diff(_network_config_path(), new_config)): if (utils.diff(_network_config_path(), new_config)):
if noop: if self.noop:
return {"/etc/network/interfaces": new_config} return {"/etc/network/interfaces": new_config}
for interface in self.interfaces.keys(): for interface in self.interfaces.keys():
logger.info('running ifdown on interface: %s' % interface) logger.info('running ifdown on interface: %s' % interface)

View File

@@ -49,7 +49,8 @@ def cleanup_pattern():
class IfcfgNetConfig(os_net_config.NetConfig): class IfcfgNetConfig(os_net_config.NetConfig):
"""Configure network interfaces using the ifcfg format.""" """Configure network interfaces using the ifcfg format."""
def __init__(self): def __init__(self, noop=False):
super(IfcfgNetConfig, self).__init__(noop)
self.interface_data = {} self.interface_data = {}
self.route_data = {} self.route_data = {}
self.bridge_data = {} self.bridge_data = {}
@@ -211,16 +212,16 @@ class IfcfgNetConfig(os_net_config.NetConfig):
if bond.routes: if bond.routes:
self._add_routes(bond.name, bond.routes) self._add_routes(bond.name, bond.routes)
def apply(self, noop=False, cleanup=False): def apply(self, cleanup=False):
"""Apply the network configuration. """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 :param cleanup: A boolean which indicates whether any undefined
(existing but not present in the object model) interface (existing but not present in the object model) interface
should be disabled and deleted. should be disabled and deleted.
:returns: a dict of the format: filename/data which contains info :returns: a dict of the format: filename/data which contains info
for each file that was changed (or would be changed if in --noop for each file that was changed (or would be changed if in --noop
mode). mode).
Note the noop mode is set via the constructor noop boolean
""" """
logger.info('applying network configs...') logger.info('applying network configs...')
restart_interfaces = [] restart_interfaces = []
@@ -257,7 +258,7 @@ class IfcfgNetConfig(os_net_config.NetConfig):
update_files[bridge_route_path] = route_data update_files[bridge_route_path] = route_data
logger.info('No changes required for bridge: %s' % bridge_name) logger.info('No changes required for bridge: %s' % bridge_name)
if noop: if self.noop:
return update_files return update_files
if cleanup: if cleanup: