From afed9e751c9eb70ea696436e07a535fc7c15783d Mon Sep 17 00:00:00 2001 From: Steven Hardy Date: Thu, 22 Jan 2015 12:17:44 +0000 Subject: Refactor noop to enable logging of disabled operations Currently we only output the config file contents in noop mode, but it's very useful to see what would be done in terms of taking interfaces up and down, and especially what will be removed when --cleanup mode is specified So this refactors the operations skipped by noop into the base-class so we can conveniently log what they would do when --noop is selected. Change-Id: Ia9cfa9a05b2df02c165a2ff992765ab63e55ae6b --- os_net_config/__init__.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'os_net_config/__init__.py') 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) -- cgit 1.2.3-korg