diff --git a/README.rst b/README.rst index 0451f50..3ab4925 100644 --- a/README.rst +++ b/README.rst @@ -64,11 +64,11 @@ example:: ~/my_templates$ tree . - └── etc - ├── keystone - │ └── keystone.conf - └── mysql - └── mysql.conf + +-- etc + +-- keystone + | +-- keystone.conf + +-- mysql + +-- mysql.conf An example tree can be found `here `_. diff --git a/os_apply_config/apply_config.py b/os_apply_config/apply_config.py index 271644a..0af7eed 100755 --- a/os_apply_config/apply_config.py +++ b/os_apply_config/apply_config.py @@ -103,6 +103,8 @@ def write_file(path, contents): d = os.path.dirname(path) os.path.exists(d) or os.makedirs(d) with tempfile.NamedTemporaryFile(dir=d, delete=False) as newfile: + if type(contents) == str: + contents = contents.encode('utf-8') newfile.write(contents) os.chmod(newfile.name, mode) os.chown(newfile.name, uid, gid) @@ -148,13 +150,13 @@ def render_executable(path, config): stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = p.communicate(json.dumps(config)) + stdout, stderr = p.communicate(json.dumps(config).encode('utf-8')) p.wait() if p.returncode != 0: raise exc.ConfigException( "config script failed: %s\n\nwith output:\n\n%s" % (path, stdout + stderr)) - return stdout + return stdout.decode('utf-8') def template_paths(root): diff --git a/os_apply_config/tests/templates/etc/glance/script.conf b/os_apply_config/tests/templates/etc/glance/script.conf index c1c4372..e873693 100755 --- a/os_apply_config/tests/templates/etc/glance/script.conf +++ b/os_apply_config/tests/templates/etc/glance/script.conf @@ -1,7 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function import json import sys params = json.loads(sys.stdin.read()) x = params["x"] if x is None: raise Exception("undefined: x") -print x +print(x) diff --git a/os_apply_config/tests/test_apply_config.py b/os_apply_config/tests/test_apply_config.py index 5bd716a..fe9c001 100644 --- a/os_apply_config/tests/test_apply_config.py +++ b/os_apply_config/tests/test_apply_config.py @@ -132,9 +132,10 @@ class TestRunOSConfigApplier(testtools.TestCase): def test_os_config_files(self): with tempfile.NamedTemporaryFile() as fake_os_config_files: with tempfile.NamedTemporaryFile() as fake_config: - fake_config.write(json.dumps(CONFIG)) + fake_config.write(json.dumps(CONFIG).encode('utf-8')) fake_config.flush() - fake_os_config_files.write(json.dumps([fake_config.name])) + fake_os_config_files.write( + json.dumps([fake_config.name]).encode('utf-8')) fake_os_config_files.flush() apply_config.main(['os-apply-config', '--key', 'database.url', @@ -242,7 +243,7 @@ class OSConfigApplierTestCase(testtools.TestCase): template("/etc/glance/script.conf"), {}) def test_template_paths(self): - expected = map(lambda p: (template(p), p), TEMPLATE_PATHS) + expected = list(map(lambda p: (template(p), p), TEMPLATE_PATHS)) actual = apply_config.template_paths(TEMPLATES) expected.sort(key=lambda tup: tup[1]) actual.sort(key=lambda tup: tup[1]) diff --git a/os_apply_config/value_types.py b/os_apply_config/value_types.py index 24fdef6..aa804c3 100644 --- a/os_apply_config/value_types.py +++ b/os_apply_config/value_types.py @@ -15,7 +15,7 @@ import re -from config_exception import ConfigException +from os_apply_config import config_exception TYPES = { "int": "^[0-9]+$", @@ -38,6 +38,7 @@ def ensure_type(string_value, type_name='default'): raise ValueError( "requested validation of unknown type: %s" % type_name) if not re.match(TYPES[type_name], string_value): - raise ConfigException("cannot interpret value '%s' as type %s" % ( + exception = config_exception.ConfigException + raise exception("cannot interpret value '%s' as type %s" % ( string_value, type_name)) return string_value