summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-01-09 23:03:28 +0000
committerGerrit Code Review <review@openstack.org>2017-01-09 23:03:28 +0000
commit01b1768740f8ac6a83015876de0b5046c9874baa (patch)
treefdf716f7722fff1db65fca10fdc96616748e3bd3
parent2396e34af2ccb9a56299a39090ef5ddb9351a88d (diff)
parent08725114a811abd76374b74aac4b1da5103de335 (diff)
downloadheat-templates-01b1768740f8ac6a83015876de0b5046c9874baa.tar.gz
Merge "Add a hook to help write json files"
-rw-r--r--hot/software-config/elements/heat-config-json-file/README.rst19
-rw-r--r--hot/software-config/elements/heat-config-json-file/element-deps1
-rwxr-xr-xhot/software-config/elements/heat-config-json-file/install.d/50-heat-config-hook-json-file6
-rwxr-xr-xhot/software-config/elements/heat-config-json-file/install.d/hook-json-file.py52
-rw-r--r--tests/software_config/test_heat_config.py12
-rw-r--r--tests/software_config/test_hook_json_file.py62
6 files changed, 151 insertions, 1 deletions
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 0000000..33efa5f
--- /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 0000000..31d7aa5
--- /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 0000000..731727d
--- /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 0000000..57c9a79
--- /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 0392acf..9fb3f1b 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 = [
{
@@ -76,6 +76,11 @@ class HeatConfigTest(common.RunScriptTest):
'inputs': [],
'config': 'seven'
}, {
+ 'id': '8888',
+ 'group': 'json-file',
+ 'inputs': [],
+ 'config': 'eight'
+ }, {
'id': '9999',
'group': 'no-such-hook',
'inputs': [],
@@ -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 0000000..73a7d2f
--- /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())