-
Notifications
You must be signed in to change notification settings - Fork 54
/
nova_manage
93 lines (70 loc) · 2.25 KB
/
nova_manage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: nova_manage
short_description: Initialize OpenStack Compute (nova) database
description: Create the tables for the database backend used by nova
options:
action:
description:
- action to perform. Currently only dbsync is supported
required: true
requirements: [ nova ]
'''
EXAMPLES = '''
nova_manage: action=dbsync
'''
import subprocess
try:
from nova.db.sqlalchemy import migration
from nova import config
except ImportError:
nova_found = False
else:
nova_found = True
def load_config_file():
config.parse_args([])
def will_db_change():
""" Check if the database version will change after the sync.
"""
# Load the config file options
current_version = migration.db_version()
repository = migration._find_migrate_repo()
repo_version = repository.latest
return current_version != repo_version
def do_dbsync():
"""Do the dbsync. Returns (returncode, stdout, stderr)"""
# We call nova-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 = ['nova-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),
),
supports_check_mode=True
)
if not nova_found:
module.fail_json(msg="nova package could not be found")
action = module.params['action']
if action not in ['dbsync', 'db_sync']:
module.fail_json(msg="Only supported action is 'dbsync'")
load_config_file()
changed = will_db_change()
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="nova-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()