update code to reflect s/cornfig/os-config-applier/

This commit is contained in:
Tim Miller 2013-01-30 19:48:27 -08:00
parent f5e9edca98
commit 6cd1768338
4 changed files with 24 additions and 24 deletions

View File

@ -9,7 +9,7 @@ from optparse import OptionParser
from pystache.context import KeyNotFoundError
from subprocess import Popen, PIPE
def install_cornfig(config_path, template_root, output_path, write):
def install_config(config_path, template_root, output_path, write):
config = read_config(config_path)
tree = build_tree( template_paths(template_root), config )
if write:
@ -36,9 +36,9 @@ def render_template(template, config):
try:
return render_moustache(open(template).read(), config)
except KeyNotFoundError as e:
raise CornfigException("key '%s' does not exist in metadata file '%s'" % (e.key, template))
raise ConfigException("key '%s' does not exist in metadata file '%s'" % (e.key, template))
except Exception as e:
raise CornfigException("could not render moustache template %s" % template)
raise ConfigException("could not render moustache template %s" % template)
def is_executable(path):
return os.path.isfile(path) and os.access(path, os.X_OK)
@ -51,14 +51,14 @@ def render_executable(path, config):
p = Popen([path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(json.dumps(config))
p.wait()
if p.returncode != 0: raise CornfigException("config script failed: %s\n\nwith output:\n\n%s" % (path, stdout + stderr))
if p.returncode != 0: raise ConfigException("config script failed: %s\n\nwith output:\n\n%s" % (path, stdout + stderr))
return stdout
def read_config(path):
try:
return json.loads(open(path).read())
except:
raise CornfigException("invalid metadata file: %s" % path)
raise ConfigException("invalid metadata file: %s" % path)
def template_paths(root):
res = []
@ -72,7 +72,7 @@ def strip_prefix(prefix, s):
return s[len(prefix):] if s.startswith(prefix) else s
def parse_opts():
parser = OptionParser(usage="cornfig -t TEMPLATE_ROOT [-m METADATA_FILE] [-o OUT_DIR]")
parser = OptionParser(usage="os-config-applier -t TEMPLATE_ROOT [-m METADATA_FILE] [-o OUT_DIR]")
parser.add_option('-t', '--templates', dest='template_root', help='path to template root directory')
parser.add_option('-o', '--output', dest='out_root', help='root directory for output (default: /)',
default='/')
@ -82,21 +82,21 @@ def parse_opts():
default=True, action='store_false')
(opts, args) = parser.parse_args()
if opts.template_root is None: raise CornfigException('missing option --templates')
if opts.template_root is None: raise ConfigException('missing option --templates')
if not os.access(opts.out_root, os.W_OK):
raise CornfigException("you don't have permission to write to '%s'" % opts.out_root)
raise ConfigException("you don't have permission to write to '%s'" % opts.out_root)
return opts
def main():
try:
opts = parse_opts()
install_cornfig(opts.metadata_path, opts.template_root, opts.out_root, opts.write)
install_config(opts.metadata_path, opts.template_root, opts.out_root, opts.write)
logger.info("success")
except CornfigException as e:
except ConfigException as e:
logger.error(e)
sys.exit(1)
class CornfigException(Exception):
class ConfigException(Exception):
pass
# logginig
@ -105,10 +105,10 @@ DATE_FORMAT = '%Y/%m/%d %I:%M:%S %p'
def add_handler(logger, handler):
handler.setFormatter(logging.Formatter(LOG_FORMAT, datefmt=DATE_FORMAT))
logger.addHandler(handler)
logger = logging.getLogger('cornfig')
logger = logging.getLogger('os-config-applier')
logger.setLevel(logging.INFO)
add_handler(logger, logging.StreamHandler(sys.stdout))
if os.geteuid() == 0: add_handler(logger, logging.FileHandler('/var/log/cornfig.log'))
if os.geteuid() == 0: add_handler(logger, logging.FileHandler('/var/log/os-config-applier.log'))
if __name__ == '__main__':
main()

View File

@ -4,19 +4,19 @@ except ImportError:
from distutils.core import setup
config = {
'name': 'cornfig',
'description': 'applies cornfiguration from cloud metadata.',
'name': 'os-config-applier',
'description': 'applies configuration from cloud metadata.',
'author': 'echohead',
'author_email': 'tim.miller.0@gmail.com',
'url': 'http://github.com/echohead/cornfig',
'url': 'http://github.com/tripleo/os-config-applier',
'version': '0.3',
'install_requires': ['nose'],
'packages': ['cornfig'],
'packages': ['os_config_applier'],
'scripts': [],
'install_requires': ['pystache', 'anyjson'],
# 'long_description': open('README.md').read(),
'entry_points': {
'console_scripts': ['cornfig = cornfig.cornfig:main']
'console_scripts': ['os-config-applier = os_config_applier.os_config_applier:main']
}
}

View File

@ -2,7 +2,7 @@ import json
import os
import tempfile
from nose.tools import *
from cornfig.cornfig import *
from os_config_applier.os_config_applier import *
# example template tree
TEMPLATES = os.path.join(os.path.dirname(__file__), 'templates')
@ -35,12 +35,12 @@ def template(relpath):
return os.path.join(TEMPLATES, relpath[1:])
def test_install_cornfig():
def test_install_config():
t = tempfile.NamedTemporaryFile()
t.write(json.dumps(CONFIG))
t.flush()
tmpdir = tempfile.mkdtemp()
install_cornfig(t.name, TEMPLATES, tmpdir, True)
install_config(t.name, TEMPLATES, tmpdir, True)
for path, contents in OUTPUT.items():
full_path = os.path.join(tmpdir, path[1:])
assert os.path.exists(full_path)
@ -52,7 +52,7 @@ def test_build_tree():
def test_render_template():
# execute executable files, moustache non-executables
assert render_template(template("/etc/glance/script.conf"), {"x": "abc"}) == "abc\n"
assert_raises(CornfigException, render_template, template("/etc/glance/script.conf"), {})
assert_raises(ConfigException, render_template, template("/etc/glance/script.conf"), {})
def test_render_moustache():
assert_equals( render_moustache("ab{{x.a}}cd", {"x": {"a": "123"}}), "ab123cd" )
@ -65,7 +65,7 @@ def test_render_executable():
params = {"x": "foo"}
assert render_executable(template("/etc/glance/script.conf"), params) == "foo\n"
@raises(CornfigException)
@raises(ConfigException)
def test_render_executable_failure():
render_executable(template("/etc/glance/script.conf"), {})
@ -80,7 +80,7 @@ def test_read_config():
t.flush()
assert_equals( read_config(t.name), d )
@raises(CornfigException)
@raises(ConfigException)
def test_read_config_bad_json():
with tempfile.NamedTemporaryFile() as t:
t.write("{{{{")