aboutsummaryrefslogtreecommitdiffstats
path: root/os_net_config/__init__.py
diff options
context:
space:
mode:
authorSteven Hardy <shardy@redhat.com>2015-01-22 12:17:44 +0000
committerSteven Hardy <shardy@redhat.com>2015-02-24 09:21:11 +0000
commitafed9e751c9eb70ea696436e07a535fc7c15783d (patch)
tree059f74155bf867153016d1edb431bb5fe11c20b4 /os_net_config/__init__.py
parentc86a76e13ea07005e31890695c220081b0ed816d (diff)
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
Diffstat (limited to 'os_net_config/__init__.py')
-rw-r--r--os_net_config/__init__.py32
1 files changed, 32 insertions, 0 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)