forked from ungleich-public/cdist
integrate messaging into cdist
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
parent
22a83d2c93
commit
ac5fa7cd64
3 changed files with 109 additions and 10 deletions
|
@ -44,9 +44,11 @@ class Local(object):
|
||||||
exec_path=sys.argv[0],
|
exec_path=sys.argv[0],
|
||||||
initial_manifest=None,
|
initial_manifest=None,
|
||||||
base_path=None,
|
base_path=None,
|
||||||
add_conf_dirs=None):
|
add_conf_dirs=None,
|
||||||
|
message_prefix=None):
|
||||||
|
|
||||||
self.target_host = target_host
|
self.target_host = target_host
|
||||||
|
self.message_prefix=message_prefix
|
||||||
|
|
||||||
# FIXME: stopped: create base that does not require moving later
|
# FIXME: stopped: create base that does not require moving later
|
||||||
if base_path:
|
if base_path:
|
||||||
|
@ -92,6 +94,7 @@ class Local(object):
|
||||||
self.conf_path = os.path.join(self.base_path, "conf")
|
self.conf_path = os.path.join(self.base_path, "conf")
|
||||||
self.global_explorer_out_path = os.path.join(self.base_path, "explorer")
|
self.global_explorer_out_path = os.path.join(self.base_path, "explorer")
|
||||||
self.object_path = os.path.join(self.base_path, "object")
|
self.object_path = os.path.join(self.base_path, "object")
|
||||||
|
self.messages_path = os.path.join(self.base_path, "messages")
|
||||||
|
|
||||||
# Depending on conf_path
|
# Depending on conf_path
|
||||||
self.global_explorer_path = os.path.join(self.conf_path, "explorer")
|
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
|
# Export __target_host for use in __remote_{copy,exec} scripts
|
||||||
env['__target_host'] = self.target_host
|
env['__target_host'] = self.target_host
|
||||||
|
|
||||||
|
if self.message_prefix:
|
||||||
|
message = cdist.Message(self.message_prefix, self.messages_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if return_output:
|
if return_output:
|
||||||
return subprocess.check_output(command, env=env).decode()
|
return subprocess.check_output(command, env=env).decode()
|
||||||
|
@ -172,6 +178,9 @@ class Local(object):
|
||||||
raise cdist.Error("Command failed: " + " ".join(command))
|
raise cdist.Error("Command failed: " + " ".join(command))
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|
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):
|
def run_script(self, script, env=None, return_output=False):
|
||||||
"""Run the given script with the given environment.
|
"""Run the given script with the given environment.
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
@ -32,29 +33,36 @@ class Message(object):
|
||||||
"""Support messaging between types
|
"""Support messaging between types
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, prefix, global_messages):
|
def __init__(self, prefix, messages):
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.global_messages = global_messages
|
self.global_messages = messages
|
||||||
|
|
||||||
self.messages_in = tempfile.mkstemp(suffix='.cdist_message_in')
|
self.messages_in = tempfile.mkstemp(suffix='.cdist_message_in')[1]
|
||||||
self.messages_out = tempfile.mkstemp(suffix='.cdist_message_out')
|
self.messages_out = tempfile.mkstemp(suffix='.cdist_message_out')[1]
|
||||||
|
|
||||||
|
self._copy_messages()
|
||||||
|
|
||||||
shutil.copyfile(self.global_messages, self.messages_in)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def env(self, env):
|
def env(self):
|
||||||
env = {}
|
env = {}
|
||||||
env['__messages_in'] = self.messages_in
|
env['__messages_in'] = self.messages_in
|
||||||
env['__messages_out'] = self.messages_out
|
env['__messages_out'] = self.messages_out
|
||||||
|
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
def _copy_messages(self):
|
||||||
|
"""Copy global contents into our copy"""
|
||||||
|
shutil.copyfile(self.global_messages, self.messages_in)
|
||||||
|
|
||||||
def _cleanup(self):
|
def _cleanup(self):
|
||||||
os.remove(self.messages_in)
|
if os.path.exists(self.messages_in):
|
||||||
os.remove(self.messages_out)
|
os.remove(self.messages_in)
|
||||||
|
if os.path.exists(self.messages_out):
|
||||||
|
os.remove(self.messages_out)
|
||||||
|
|
||||||
def _merge_messages(self):
|
def _merge_messages(self):
|
||||||
with open(self.messages_in) as fd:
|
with open(self.messages_out) as fd:
|
||||||
content = fd.readlines()
|
content = fd.readlines()
|
||||||
|
|
||||||
with open(self.global_messages, 'a') as fd:
|
with open(self.global_messages, 'a') as fd:
|
82
cdist/test/message/__init__.py
Normal file
82
cdist/test/message/__init__.py
Normal 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)
|
Loading…
Reference in a new issue