From 08725114a811abd76374b74aac4b1da5103de335 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Wed, 28 Dec 2016 21:51:40 -0500 Subject: [PATCH] Add a hook to help write json files This patch adds a new Heat software configuration hook that can be used to generate Json config files in a composable manner within Heat templates. This can be useful for everything from providing data to ad-hoc scripts from configuring kolla containers. Change-Id: I2b372ac2e291339e436202c9fe58a681ed6a743f --- .../elements/heat-config-json-file/README.rst | 19 ++++++ .../heat-config-json-file/element-deps | 1 + .../install.d/50-heat-config-hook-json-file | 6 ++ .../install.d/hook-json-file.py | 52 ++++++++++++++++ tests/software_config/test_heat_config.py | 12 +++- tests/software_config/test_hook_json_file.py | 62 +++++++++++++++++++ 6 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 hot/software-config/elements/heat-config-json-file/README.rst create mode 100644 hot/software-config/elements/heat-config-json-file/element-deps create mode 100755 hot/software-config/elements/heat-config-json-file/install.d/50-heat-config-hook-json-file create mode 100755 hot/software-config/elements/heat-config-json-file/install.d/hook-json-file.py create mode 100644 tests/software_config/test_hook_json_file.py diff --git a/hot/software-config/elements/heat-config-json-file/README.rst b/hot/software-config/elements/heat-config-json-file/README.rst new file mode 100644 index 00000000..33efa5fb --- /dev/null +++ b/hot/software-config/elements/heat-config-json-file/README.rst @@ -0,0 +1,19 @@ +A hook which helps write JSON files to disk for configuration or use +with ad-hoc scripts. The data files are written to the named file +location for each section listed under 'config'. + +Multiple JSON files can be written out in this manner. + +Example: + + JsonConfig: + type: OS::Heat::StructuredConfig + properties: + group: json-file + config: + /tmp/foo: + - bar + - bar2 + +This would write out a JSON files at + /tmp/foo containing a JSON representation of ['bar', 'bar2']. diff --git a/hot/software-config/elements/heat-config-json-file/element-deps b/hot/software-config/elements/heat-config-json-file/element-deps new file mode 100644 index 00000000..31d7aa57 --- /dev/null +++ b/hot/software-config/elements/heat-config-json-file/element-deps @@ -0,0 +1 @@ +heat-config diff --git a/hot/software-config/elements/heat-config-json-file/install.d/50-heat-config-hook-json-file b/hot/software-config/elements/heat-config-json-file/install.d/50-heat-config-hook-json-file new file mode 100755 index 00000000..731727d9 --- /dev/null +++ b/hot/software-config/elements/heat-config-json-file/install.d/50-heat-config-hook-json-file @@ -0,0 +1,6 @@ +#!/bin/bash +set -x + +SCRIPTDIR=$(dirname $0) + +install -D -g root -o root -m 0755 ${SCRIPTDIR}/hook-json-file.py /var/lib/heat-config/hooks/json-file diff --git a/hot/software-config/elements/heat-config-json-file/install.d/hook-json-file.py b/hot/software-config/elements/heat-config-json-file/install.d/hook-json-file.py new file mode 100755 index 00000000..57c9a792 --- /dev/null +++ b/hot/software-config/elements/heat-config-json-file/install.d/hook-json-file.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import json +import logging +import os +import sys + + +def prepare_dir(path): + if not os.path.isdir(path): + os.makedirs(path, 0o700) + + +def main(argv=sys.argv): + log = logging.getLogger('heat-config') + handler = logging.StreamHandler(sys.stderr) + handler.setFormatter( + logging.Formatter( + '[%(asctime)s] (%(name)s) [%(levelname)s] %(message)s')) + log.addHandler(handler) + log.setLevel('DEBUG') + + c = json.load(sys.stdin)['config'] + + for fname in c.keys(): + prepare_dir(os.path.dirname(fname)) + data = c.get(fname) + with open(fname, 'w') as json_data_file: + json.dump(data, json_data_file, indent=4, sort_keys=True) + + response = { + 'deploy_stdout': '', + 'deploy_stderr': '', + 'deploy_status_code': 0, + } + + json.dump(response, sys.stdout) + +if __name__ == '__main__': + sys.exit(main(sys.argv)) diff --git a/tests/software_config/test_heat_config.py b/tests/software_config/test_heat_config.py index 0392acf4..9fb3f1b4 100644 --- a/tests/software_config/test_heat_config.py +++ b/tests/software_config/test_heat_config.py @@ -26,7 +26,7 @@ from tests.software_config import common class HeatConfigTest(common.RunScriptTest): fake_hooks = ['cfn-init', 'chef', 'puppet', 'salt', 'script', - 'apply-config', 'hiera'] + 'apply-config', 'hiera', 'json-file'] data = [ { @@ -75,6 +75,11 @@ class HeatConfigTest(common.RunScriptTest): 'group': 'hiera', 'inputs': [], 'config': 'seven' + }, { + 'id': '8888', + 'group': 'json-file', + 'inputs': [], + 'config': 'eight' }, { 'id': '9999', 'group': 'no-such-hook', @@ -114,6 +119,11 @@ class HeatConfigTest(common.RunScriptTest): 'deploy_stderr': 'stderr', 'deploy_stdout': 'stdout' }, + 'json-file': { + 'deploy_status_code': '0', + 'deploy_stderr': 'stderr', + 'deploy_stdout': 'stdout' + }, 'apply-config': { 'deploy_status_code': '0', 'deploy_stderr': 'stderr', diff --git a/tests/software_config/test_hook_json_file.py b/tests/software_config/test_hook_json_file.py new file mode 100644 index 00000000..73a7d2f5 --- /dev/null +++ b/tests/software_config/test_hook_json_file.py @@ -0,0 +1,62 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import json +import logging +import os +import tempfile +import yaml + +from tests.software_config import common + +log = logging.getLogger('test_hook_json_file') + + +class HookKollaConfigTest(common.RunScriptTest): + + def setUp(self): + super(HookKollaConfigTest, self).setUp() + self.hook_path = self.relative_path( + __file__, + '../..', + 'hot/software-config/elements', + 'heat-config-json-file/install.d/hook-json-file.py') + + self.conf = tempfile.NamedTemporaryFile(mode='w', delete=False).name + os.unlink(self.conf) + + self.env = os.environ.copy() + self.data = { + 'id': 'test_json_file', + 'name': 'fake_resource_name', + 'group': 'json-file', + 'config': { + self.conf: { + 'command': 'foo' + } + } + } + + def test_hook(self): + + returncode, stdout, stderr = self.run_cmd( + [self.hook_path], self.env, json.dumps(self.data)) + + self.assertEqual(0, returncode, stderr) + ret = yaml.safe_load(stdout) + self.assertIsNotNone(ret['deploy_stderr']) + self.assertEqual('', ret['deploy_stdout']) + self.assertEqual(0, ret['deploy_status_code']) + + with open(os.path.join(self.conf)) as data: + self.assertEqual("{\n \"command\": \"foo\"\n}", data.read())