diff options
Diffstat (limited to 'os_net_config')
-rw-r--r-- | os_net_config/__init__.py | 32 | ||||
-rw-r--r-- | os_net_config/impl_eni.py | 29 | ||||
-rw-r--r-- | os_net_config/impl_ifcfg.py | 37 |
3 files changed, 58 insertions, 40 deletions
diff --git a/os_net_config/__init__.py b/os_net_config/__init__.py index 3b01b71..8d241af 100644 --- a/os_net_config/__init__.py +++ b/os_net_config/__init__.py @@ -14,7 +14,16 @@ # License for the specific language governing permissions and limitations # under the License. +import logging +import os + +from oslo_concurrency import processutils + from os_net_config import objects +from os_net_config import utils + + +logger = logging.getLogger(__name__) class NotImplemented(Exception): @@ -26,6 +35,7 @@ class NetConfig(object): def __init__(self, noop=False): self.noop = noop + self.log_prefix = "NOOP: " if noop else "" def add_object(self, obj): """Convenience method to add any type of object to the network config. @@ -85,3 +95,25 @@ class NetConfig(object): mode). """ raise NotImplemented("apply is not implemented.") + + def execute(self, msg, cmd, *args, **kwargs): + """Print a message and run a command. + + Print a message and run a command with processutils + in noop mode, this just prints a message. + """ + logger.info('%s%s' % (self.log_prefix, msg)) + if not self.noop: + processutils.execute(cmd, *args, **kwargs) + + def write_config(self, filename, data, msg=None): + msg = msg or "Writing config %s" % filename + logger.info('%s%s' % (self.log_prefix, msg)) + if not self.noop: + utils.write_config(filename, data) + + def remove_config(self, filename, msg=None): + msg = msg or "Removing config %s" % filename + logger.info('%s%s' % (self.log_prefix, msg)) + if not self.noop: + os.remove(filename) diff --git a/os_net_config/impl_eni.py b/os_net_config/impl_eni.py index 93aa7db..880c4e4 100644 --- a/os_net_config/impl_eni.py +++ b/os_net_config/impl_eni.py @@ -21,8 +21,6 @@ import os_net_config from os_net_config import objects from os_net_config import utils -from oslo_concurrency import processutils - logger = logging.getLogger(__name__) @@ -212,27 +210,26 @@ class ENINetConfig(os_net_config.NetConfig): new_config += iface_data if (utils.diff(_network_config_path(), new_config)): - if self.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) + msg = 'running ifdown on interface: %s' % interface + self.execute(msg, '/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) + msg = 'running ifdown on bridge: %s' % bridge + self.execute(msg, '/sbin/ifdown', bridge, + check_exit_code=False) - logger.info('writing config file') - utils.write_config(_network_config_path(), new_config) + self.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) + msg = 'running ifup on bridge: %s' % bridge + self.execute(msg, '/sbin/ifup', bridge) for interface in self.interfaces.keys(): - logger.info('running ifup on interface: %s' % interface) - processutils.execute('/sbin/ifup', interface) + msg = 'running ifup on interface: %s' % interface + self.execute(msg, '/sbin/ifup', interface) else: logger.info('No interface changes are required.') + + return {"/etc/network/interfaces": new_config} diff --git a/os_net_config/impl_ifcfg.py b/os_net_config/impl_ifcfg.py index 0c23630..56a0e47 100644 --- a/os_net_config/impl_ifcfg.py +++ b/os_net_config/impl_ifcfg.py @@ -16,16 +16,12 @@ import glob import logging -import os import os_net_config from os_net_config import objects from os_net_config import utils -from oslo_concurrency import processutils - - logger = logging.getLogger(__name__) @@ -258,40 +254,33 @@ class IfcfgNetConfig(os_net_config.NetConfig): update_files[bridge_route_path] = route_data logger.info('No changes required for bridge: %s' % bridge_name) - if self.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) + msg = 'cleaning up interface: %s' % interface_name + self.execute(msg, '/sbin/ifdown', interface_name, + check_exit_code=False) + self.remove_config(ifcfg_file) for interface in restart_interfaces: - logger.info('running ifdown on interface: %s' % interface) - processutils.execute('/sbin/ifdown', interface, - check_exit_code=False) + msg = 'running ifdown on interface: %s' % interface + self.execute(msg, '/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) + msg = 'running ifdown on bridge: %s' % bridge + self.execute(msg, '/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) + self.write_config(location, data) for bridge in restart_bridges: - logger.info('running ifup on bridge: %s' % bridge) - processutils.execute('/sbin/ifup', bridge) + msg = 'running ifup on bridge: %s' % bridge + self.execute(msg, '/sbin/ifup', bridge) for interface in restart_interfaces: - logger.info('running ifup on interface: %s' % interface) - processutils.execute('/sbin/ifup', interface) + msg = 'running ifup on interface: %s' % interface + self.execute(msg, '/sbin/ifup', interface) return update_files |