From ac5fa7cd6461717e6f82fc10dece2b16405b38b8 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 26 Nov 2013 01:01:30 +0100 Subject: [PATCH] integrate messaging into cdist Signed-off-by: Nico Schottelius --- cdist/exec/local.py | 11 ++++- cdist/{core => }/message.py | 26 +++++++---- cdist/test/message/__init__.py | 82 ++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 10 deletions(-) rename cdist/{core => }/message.py (77%) create mode 100644 cdist/test/message/__init__.py diff --git a/cdist/exec/local.py b/cdist/exec/local.py index 3a3ac706..b0109a4e 100644 --- a/cdist/exec/local.py +++ b/cdist/exec/local.py @@ -44,9 +44,11 @@ class Local(object): exec_path=sys.argv[0], initial_manifest=None, base_path=None, - add_conf_dirs=None): + add_conf_dirs=None, + message_prefix=None): self.target_host = target_host + self.message_prefix=message_prefix # FIXME: stopped: create base that does not require moving later if base_path: @@ -92,6 +94,7 @@ class Local(object): self.conf_path = os.path.join(self.base_path, "conf") self.global_explorer_out_path = os.path.join(self.base_path, "explorer") self.object_path = os.path.join(self.base_path, "object") + self.messages_path = os.path.join(self.base_path, "messages") # Depending on conf_path self.global_explorer_path = os.path.join(self.conf_path, "explorer") @@ -163,6 +166,9 @@ class Local(object): # Export __target_host for use in __remote_{copy,exec} scripts env['__target_host'] = self.target_host + if self.message_prefix: + message = cdist.Message(self.message_prefix, self.messages_path) + try: if return_output: return subprocess.check_output(command, env=env).decode() @@ -172,6 +178,9 @@ class Local(object): raise cdist.Error("Command failed: " + " ".join(command)) except OSError as error: raise cdist.Error(" ".join(*args) + ": " + error.args[1]) + finally: + if self.message_prefix: + message.merge_messages() def run_script(self, script, env=None, return_output=False): """Run the given script with the given environment. diff --git a/cdist/core/message.py b/cdist/message.py similarity index 77% rename from cdist/core/message.py rename to cdist/message.py index 50f039f4..057c43d3 100644 --- a/cdist/core/message.py +++ b/cdist/message.py @@ -20,6 +20,7 @@ # import logging +import os import shutil import tempfile @@ -32,29 +33,36 @@ class Message(object): """Support messaging between types """ - def __init__(self, prefix, global_messages): + def __init__(self, prefix, messages): self.prefix = prefix - self.global_messages = global_messages + self.global_messages = messages - self.messages_in = tempfile.mkstemp(suffix='.cdist_message_in') - self.messages_out = tempfile.mkstemp(suffix='.cdist_message_out') + self.messages_in = tempfile.mkstemp(suffix='.cdist_message_in')[1] + self.messages_out = tempfile.mkstemp(suffix='.cdist_message_out')[1] + + self._copy_messages() - shutil.copyfile(self.global_messages, self.messages_in) @property - def env(self, env): + def env(self): env = {} env['__messages_in'] = self.messages_in env['__messages_out'] = self.messages_out return env + def _copy_messages(self): + """Copy global contents into our copy""" + shutil.copyfile(self.global_messages, self.messages_in) + def _cleanup(self): - os.remove(self.messages_in) - os.remove(self.messages_out) + if os.path.exists(self.messages_in): + os.remove(self.messages_in) + if os.path.exists(self.messages_out): + os.remove(self.messages_out) def _merge_messages(self): - with open(self.messages_in) as fd: + with open(self.messages_out) as fd: content = fd.readlines() with open(self.global_messages, 'a') as fd: diff --git a/cdist/test/message/__init__.py b/cdist/test/message/__init__.py new file mode 100644 index 00000000..653847f1 --- /dev/null +++ b/cdist/test/message/__init__.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +# +# 2013 Nico Schottelius (nico-cdist at schottelius.org) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import os +import tempfile + +from cdist import test +import cdist.message + +class MessageTestCase(test.CdistTestCase): + + def setUp(self): + self.prefix="cdist-test" + self.content = "A very short story" + self.tempfile = tempfile.mkstemp()[1] + self.message = cdist.message.Message(prefix=self.prefix, + messages=self.tempfile) + + def tearDown(self): + os.remove(self.tempfile) + self.message._cleanup() + + def test_env(self): + """ + Ensure environment is correct + """ + + env = self.message.env + + self.assertIn('__messages_in', env) + self.assertIn('__messages_out', env) + + + def test_copy_content(self): + """ + Ensure content copying is working + """ + + with open(self.tempfile, "w") as fd: + fd.write(self.content) + + self.message._copy_messages() + + with open(self.tempfile, "r") as fd: + testcontent = fd.read() + + self.assertEqual(self.content, testcontent) + + def test_message_merge_prefix(self): + """Ensure messages are merged and are prefixed""" + + expectedcontent = "%s:%s" % (self.prefix, self.content) + + out = self.message.env['__messages_out'] + + with open(out, "w") as fd: + fd.write(self.content) + + self.message._merge_messages() + + with open(self.tempfile, "r") as fd: + testcontent = fd.read() + + self.assertEqual(expectedcontent, testcontent)