Merge "Add a hook to help write json files"
This commit is contained in:
commit
01b1768740
@ -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'].
|
@ -0,0 +1 @@
|
||||
heat-config
|
@ -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
|
@ -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))
|
@ -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',
|
||||
|
62
tests/software_config/test_hook_json_file.py
Normal file
62
tests/software_config/test_hook_json_file.py
Normal file
@ -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())
|
Loading…
Reference in New Issue
Block a user