Andy Bavier | ddb7cfc | 2017-08-15 06:28:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Matteo Scandolo | 3896c47 | 2017-08-01 13:31:42 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 17 | import json |
| 18 | import subprocess |
Max Chu | 733504d | 2017-08-18 15:04:53 -0700 | [diff] [blame] | 19 | import sys |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 20 | |
| 21 | def dict_keys_dash_to_underscore(dashed): |
| 22 | underscored = dict((k.replace('-','_'),v) for k,v in dashed.items()) |
| 23 | return underscored |
| 24 | |
Andy Bavier | 88e198e | 2017-05-08 14:59:18 -0400 | [diff] [blame] | 25 | try: |
| 26 | juju_status_json = subprocess.check_output("juju status --format=json", shell=True) |
| 27 | juju_status = json.loads(juju_status_json) |
| 28 | except: |
| 29 | print json.dumps({ |
| 30 | "failed" : True, |
| 31 | "msg" : "'juju status' command failed" |
| 32 | }) |
| 33 | sys.exit(1) |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 34 | |
| 35 | juju_machines = {} |
| 36 | for index, data in juju_status['machines'].iteritems(): |
| 37 | data_underscore = dict_keys_dash_to_underscore(data) |
| 38 | juju_machines[data_underscore["dns_name"]] = data_underscore |
Zack Williams | f1fb094 | 2016-02-29 14:48:01 -0700 | [diff] [blame] | 39 | juju_machines[data_underscore["dns_name"]]["machine_id"] = index |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 40 | |
Zack Williams | 2de6f5e | 2016-03-15 09:57:52 -0700 | [diff] [blame] | 41 | juju_compute_nodes = {} |
Zack Williams | 439ddb9 | 2016-03-16 16:06:23 -0700 | [diff] [blame] | 42 | if 'nova-compute' in juju_status['services']: |
| 43 | for name, data in juju_status['services']['nova-compute']['units'].iteritems(): |
| 44 | juju_compute_nodes[data['public-address']] = data |
Zack Williams | 2de6f5e | 2016-03-15 09:57:52 -0700 | [diff] [blame] | 45 | |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 46 | print json.dumps({ |
| 47 | "changed": True, |
| 48 | "ansible_facts" : { |
Zack Williams | 2de6f5e | 2016-03-15 09:57:52 -0700 | [diff] [blame] | 49 | "juju_environment": juju_status['environment'], |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 50 | "juju_machines": juju_machines, |
Zack Williams | f1fb094 | 2016-02-29 14:48:01 -0700 | [diff] [blame] | 51 | "juju_services": juju_status['services'], |
Zack Williams | 2de6f5e | 2016-03-15 09:57:52 -0700 | [diff] [blame] | 52 | "juju_compute_nodes": juju_compute_nodes, |
Zack Williams | 2eb97f9 | 2016-02-29 09:37:36 -0700 | [diff] [blame] | 53 | }, |
| 54 | }) |
| 55 | |