2013-08-22 15:22:18 +02:00
|
|
|
#!/usr/bin/env python
|
2013-08-08 23:26:24 +02:00
|
|
|
|
|
|
|
# (c) 2013, Michael Scherer <misc@zarb.org>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible,
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
inventory: openshift
|
|
|
|
short_description: Openshift gears external inventory script
|
|
|
|
description:
|
|
|
|
- Generates inventory of Openshift gears using the REST interface
|
2014-05-03 18:40:05 +02:00
|
|
|
- this permit to reuse playbook to setup an Openshift gear
|
2013-08-08 23:26:24 +02:00
|
|
|
version_added: None
|
|
|
|
author: Michael Scherer
|
|
|
|
'''
|
|
|
|
|
2018-08-10 18:13:29 +02:00
|
|
|
import json
|
2013-08-08 23:26:24 +02:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import StringIO
|
|
|
|
|
2015-09-15 16:58:52 +02:00
|
|
|
from ansible.module_utils.urls import open_url
|
2019-04-09 19:17:59 +02:00
|
|
|
from ansible.module_utils.six.moves import configparser as ConfigParser
|
2015-09-15 16:58:52 +02:00
|
|
|
|
2013-08-08 23:26:24 +02:00
|
|
|
configparser = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_from_rhc_config(variable):
|
|
|
|
global configparser
|
|
|
|
CONF_FILE = os.path.expanduser('~/.openshift/express.conf')
|
|
|
|
if os.path.exists(CONF_FILE):
|
|
|
|
if not configparser:
|
|
|
|
ini_str = '[root]\n' + open(CONF_FILE, 'r').read()
|
|
|
|
configparser = ConfigParser.SafeConfigParser()
|
|
|
|
configparser.readfp(StringIO.StringIO(ini_str))
|
|
|
|
try:
|
|
|
|
return configparser.get('root', variable)
|
|
|
|
except ConfigParser.NoOptionError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_config(env_var, config_var):
|
|
|
|
result = os.getenv(env_var)
|
|
|
|
if not result:
|
|
|
|
result = get_from_rhc_config(config_var)
|
|
|
|
if not result:
|
2016-08-23 17:09:23 +02:00
|
|
|
sys.exit("failed=True msg='missing %s'" % env_var)
|
2013-08-08 23:26:24 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2015-09-15 16:58:52 +02:00
|
|
|
def get_json_from_api(url, username, password):
|
|
|
|
headers = {'Accept': 'application/json; version=1.5'}
|
|
|
|
response = open_url(url, headers=headers, url_username=username, url_password=password)
|
2013-08-08 23:26:24 +02:00
|
|
|
return json.loads(response.read())['data']
|
|
|
|
|
|
|
|
|
|
|
|
username = get_config('ANSIBLE_OPENSHIFT_USERNAME', 'default_rhlogin')
|
|
|
|
password = get_config('ANSIBLE_OPENSHIFT_PASSWORD', 'password')
|
|
|
|
broker_url = 'https://%s/broker/rest/' % get_config('ANSIBLE_OPENSHIFT_BROKER', 'libra_server')
|
|
|
|
|
|
|
|
|
2015-09-15 16:58:52 +02:00
|
|
|
response = get_json_from_api(broker_url + '/domains', username, password)
|
2013-08-08 23:26:24 +02:00
|
|
|
|
|
|
|
response = get_json_from_api("%s/domains/%s/applications" %
|
2015-09-15 16:58:52 +02:00
|
|
|
(broker_url, response[0]['id']), username, password)
|
2013-08-08 23:26:24 +02:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
for app in response:
|
|
|
|
|
|
|
|
# ssh://520311404832ce3e570000ff@blog-johndoe.example.org
|
|
|
|
(user, host) = app['ssh_url'][6:].split('@')
|
|
|
|
app_name = host.split('-')[0]
|
|
|
|
|
|
|
|
result[app_name] = {}
|
|
|
|
result[app_name]['hosts'] = []
|
|
|
|
result[app_name]['hosts'].append(host)
|
|
|
|
result[app_name]['vars'] = {}
|
|
|
|
result[app_name]['vars']['ansible_ssh_user'] = user
|
|
|
|
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1] == '--list':
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps(result))
|
2013-08-08 23:26:24 +02:00
|
|
|
elif len(sys.argv) == 3 and sys.argv[1] == '--host':
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps({}))
|
2013-08-08 23:26:24 +02:00
|
|
|
else:
|
2015-08-28 08:18:13 +02:00
|
|
|
print("Need an argument, either --list or --host <host>")
|