diff options
author | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2014-10-09 21:38:41 +0200 |
---|---|---|
committer | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2014-10-09 21:38:41 +0200 |
commit | 11926db23c2b3db40d7f3fabf7c7c5e99c03977a (patch) | |
tree | 7c04c410ef34f03f121eed7153f8c87dca550c5f | |
parent | fda249cad7a99b8ab6097ba0dac5385480c0834b (diff) | |
download | openstack-ansible-modules-11926db23c2b3db40d7f3fabf7c7c5e99c03977a.tar.gz |
Add the heat_manage module
Support only the dbsync command on icehouse for now.
-rw-r--r-- | heat_manage | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/heat_manage b/heat_manage new file mode 100644 index 0000000..2c5db87 --- /dev/null +++ b/heat_manage @@ -0,0 +1,101 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +DOCUMENTATION = ''' +--- +module: heat_manage +short_description: Initialize Orchestration (heat) database +description: Create the tables for the database backend used by heat +options: + action: + description: + - action to perform. Currently only dbysnc is supported + required: true + conf: + description: + - path to keystone config file. + required: false + default: /etc/heat/heat.conf +requirements: [ python-heatclient ] +author: Lorin Hochstein, Gauvain Pocentek +''' + +EXAMPLES = ''' +heat_manage: action=dbsync +''' + +import subprocess + +try: + import heat + from heat.db.sqlalchemy import migration + from migrate.versioning import api as versioning_api + from oslo.config import cfg +except ImportError: + heat_found = False +else: + heat_found = True + + +def will_db_change(conf): + """ Check if the database version will change after the sync. + + conf is the path to the heat config file + + """ + # Load the config file options + cfg.CONF(project='heat', default_config_files=[conf]) + current_version = migration.db_version() + + repo_path = os.path.join(os.path.dirname(heat.__file__), + 'db', 'sqlalchemy', 'migrate_repo') + repo_version = versioning_api.repository.Repository(repo_path).latest + return current_version != repo_version + + +def do_dbsync(): + """Do the dbsync. Returns (returncode, stdout, stderr)""" + # We call heat-manage db_sync on the shell rather than trying to + # do this in Python since we have no guarantees about changes to the + # internals. + args = ['heat-manage', 'db_sync'] + + call = subprocess.Popen(args, shell=False, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = call.communicate() + return (call.returncode, out, err) + + +def main(): + + module = AnsibleModule( + argument_spec=dict( + action=dict(required=True), + conf=dict(required=False, default="/etc/heat/heat.conf") + ), + supports_check_mode=True + ) + + if not heat_found: + module.fail_json(msg="python-heatclient could not be found") + + action = module.params['action'] + conf = module.params['conf'] + if action not in ['dbsync', 'db_sync']: + module.fail_json(msg="Only supported action is 'dbsync'") + + changed = will_db_change(conf) + if module.check_mode: + module.exit_json(changed=changed) + + (res, stdout, stderr) = do_dbsync() + + if res == 0: + module.exit_json(changed=changed, stdout=stdout, stderr=stderr) + else: + module.fail_json(msg="heat-manage returned non-zero value: %d" % res, + stdout=stdout, stderr=stderr) + +# this is magic, see lib/ansible/module_common.py +#<<INCLUDE_ANSIBLE_MODULE_COMMON>> +main() |