integrate messaging into cdist

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
Nico Schottelius 2013-11-26 01:01:30 +01:00
parent 22a83d2c93
commit ac5fa7cd64
3 changed files with 109 additions and 10 deletions

View file

@ -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.

View file

@ -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):
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:

View file

@ -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 <http://www.gnu.org/licenses/>.
#
#
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)