2016-11-02 22:47:42 +01:00
|
|
|
#!/usr/bin/env python
|
2013-05-21 21:40:26 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Spacewalk external inventory script
|
|
|
|
=================================
|
|
|
|
|
|
|
|
Ansible has a feature where instead of reading from /etc/ansible/hosts
|
|
|
|
as a text file, it can query external programs to obtain the list
|
|
|
|
of hosts, groups the hosts are in, and even variables to assign to each host.
|
|
|
|
|
|
|
|
To use this, copy this file over /etc/ansible/hosts and chmod +x the file.
|
|
|
|
This, more or less, allows you to keep one central database containing
|
|
|
|
info about all of your managed instances.
|
|
|
|
|
|
|
|
This script is dependent upon the spacealk-reports package being installed
|
|
|
|
on the same machine. It is basically a CSV-to-JSON converter from the
|
|
|
|
output of "spacewalk-report system-groups-systems|inventory".
|
|
|
|
|
2015-07-31 14:23:54 +02:00
|
|
|
Tested with Ansible 1.9.2 and spacewalk 2.3
|
2013-05-21 21:40:26 +02:00
|
|
|
"""
|
2017-01-28 00:20:31 +01:00
|
|
|
#
|
2013-05-21 21:40:26 +02:00
|
|
|
# Author:: Jon Miller <jonEbird@gmail.com>
|
|
|
|
# Copyright:: Copyright (c) 2013, Jon Miller
|
2017-01-28 00:20:31 +01:00
|
|
|
#
|
2015-07-31 14:23:54 +02:00
|
|
|
# Extended for support of multiple organizations and
|
|
|
|
# adding the "_meta" dictionary to --list output by
|
|
|
|
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2015
|
2014-09-03 12:49:26 +02:00
|
|
|
#
|
2013-05-21 21:40:26 +02:00
|
|
|
# This program 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 2 of the License, or (at
|
|
|
|
# your option) any later version.
|
2017-01-28 00:20:31 +01:00
|
|
|
#
|
2013-05-21 21:40:26 +02:00
|
|
|
# This program 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.
|
2017-01-28 00:20:31 +01:00
|
|
|
#
|
2013-05-21 21:40:26 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-28 00:20:31 +01:00
|
|
|
#
|
2013-05-21 21:40:26 +02:00
|
|
|
|
2015-08-28 08:18:13 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
2015-07-31 14:23:54 +02:00
|
|
|
import ConfigParser
|
2013-05-21 21:40:26 +02:00
|
|
|
|
2015-09-03 08:23:27 +02:00
|
|
|
from six import iteritems
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
2017-05-09 23:38:08 +02:00
|
|
|
base_dir = os.path.dirname(os.path.realpath(__file__))
|
2016-04-05 17:14:28 +02:00
|
|
|
default_ini_file = os.path.join(base_dir, "spacewalk.ini")
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
SW_REPORT = '/usr/bin/spacewalk-report'
|
|
|
|
CACHE_DIR = os.path.join(base_dir, ".spacewalk_reports")
|
2017-05-09 23:38:08 +02:00
|
|
|
CACHE_AGE = 300 # 5min
|
2016-04-05 17:14:28 +02:00
|
|
|
INI_FILE = os.path.expanduser(os.path.expandvars(os.environ.get("SPACEWALK_INI_PATH", default_ini_file)))
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
|
2017-01-27 22:04:59 +01:00
|
|
|
# Sanity check
|
2013-05-21 21:40:26 +02:00
|
|
|
if not os.path.exists(SW_REPORT):
|
2015-08-28 08:18:13 +02:00
|
|
|
print('Error: %s is required for operation.' % (SW_REPORT), file=sys.stderr)
|
2013-05-21 21:40:26 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Pre-startup work
|
|
|
|
if not os.path.exists(CACHE_DIR):
|
|
|
|
os.mkdir(CACHE_DIR)
|
2016-04-02 02:35:19 +02:00
|
|
|
os.chmod(CACHE_DIR, 0o2775)
|
2013-05-21 21:40:26 +02:00
|
|
|
|
|
|
|
# Helper functions
|
2017-05-09 23:38:08 +02:00
|
|
|
# ------------------------------
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
|
|
|
|
def spacewalk_report(name):
|
|
|
|
"""Yield a dictionary form of each CSV output produced by the specified
|
|
|
|
spacewalk-report
|
|
|
|
"""
|
|
|
|
cache_filename = os.path.join(CACHE_DIR, name)
|
|
|
|
if not os.path.exists(cache_filename) or \
|
|
|
|
(time.time() - os.stat(cache_filename).st_mtime) > CACHE_AGE:
|
|
|
|
# Update the cache
|
|
|
|
fh = open(cache_filename, 'w')
|
|
|
|
p = subprocess.Popen([SW_REPORT, name], stdout=fh)
|
|
|
|
p.wait()
|
|
|
|
fh.close()
|
|
|
|
|
|
|
|
lines = open(cache_filename, 'r').readlines()
|
|
|
|
keys = lines[0].strip().split(',')
|
2015-07-31 14:23:54 +02:00
|
|
|
# add 'spacewalk_' prefix to the keys
|
2017-05-09 23:38:08 +02:00
|
|
|
keys = ['spacewalk_' + key for key in keys]
|
2013-05-21 21:40:26 +02:00
|
|
|
for line in lines[1:]:
|
|
|
|
values = line.strip().split(',')
|
|
|
|
if len(keys) == len(values):
|
|
|
|
yield dict(zip(keys, values))
|
|
|
|
|
|
|
|
|
|
|
|
# Options
|
2017-05-09 23:38:08 +02:00
|
|
|
# ------------------------------
|
2013-05-21 21:40:26 +02:00
|
|
|
|
|
|
|
parser = OptionParser(usage="%prog [options] --list | --host <machine>")
|
|
|
|
parser.add_option('--list', default=False, dest="list", action="store_true",
|
|
|
|
help="Produce a JSON consumable grouping of servers for Ansible")
|
|
|
|
parser.add_option('--host', default=None, dest="host",
|
|
|
|
help="Generate additional host specific details for given host for Ansible")
|
|
|
|
parser.add_option('-H', '--human', dest="human",
|
|
|
|
default=False, action="store_true",
|
|
|
|
help="Produce a friendlier version of either server list or host detail")
|
2017-01-28 00:20:31 +01:00
|
|
|
parser.add_option('-o', '--org', default=None, dest="org_number",
|
2017-01-28 09:12:11 +01:00
|
|
|
help="Limit to spacewalk organization number")
|
2014-09-03 12:49:26 +02:00
|
|
|
parser.add_option('-p', default=False, dest="prefix_org_name", action="store_true",
|
2017-01-28 09:12:11 +01:00
|
|
|
help="Prefix the group name with the organization number")
|
2013-05-21 21:40:26 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
|
2015-07-31 14:23:54 +02:00
|
|
|
# read spacewalk.ini if present
|
2017-05-09 23:38:08 +02:00
|
|
|
# ------------------------------
|
2015-07-31 14:23:54 +02:00
|
|
|
if os.path.exists(INI_FILE):
|
|
|
|
config = ConfigParser.SafeConfigParser()
|
|
|
|
config.read(INI_FILE)
|
2017-05-09 23:38:08 +02:00
|
|
|
if config.has_option('spacewalk', 'cache_age'):
|
|
|
|
CACHE_AGE = config.get('spacewalk', 'cache_age')
|
|
|
|
if not options.org_number and config.has_option('spacewalk', 'org_number'):
|
|
|
|
options.org_number = config.get('spacewalk', 'org_number')
|
|
|
|
if not options.prefix_org_name and config.has_option('spacewalk', 'prefix_org_name'):
|
|
|
|
options.prefix_org_name = config.getboolean('spacewalk', 'prefix_org_name')
|
2015-07-31 14:23:54 +02:00
|
|
|
|
|
|
|
|
2014-09-03 12:49:26 +02:00
|
|
|
# Generate dictionary for mapping group_id to org_id
|
2017-05-09 23:38:08 +02:00
|
|
|
# ------------------------------
|
2014-09-03 12:49:26 +02:00
|
|
|
org_groups = {}
|
|
|
|
try:
|
2017-01-28 09:12:11 +01:00
|
|
|
for group in spacewalk_report('system-groups'):
|
|
|
|
org_groups[group['spacewalk_group_id']] = group['spacewalk_org_id']
|
2014-09-03 12:49:26 +02:00
|
|
|
|
2015-08-27 20:58:51 +02:00
|
|
|
except (OSError) as e:
|
2017-01-28 09:12:11 +01:00
|
|
|
print('Problem executing the command "%s system-groups": %s' %
|
2015-08-28 08:18:13 +02:00
|
|
|
(SW_REPORT, str(e)), file=sys.stderr)
|
2017-01-28 09:12:11 +01:00
|
|
|
sys.exit(2)
|
2014-09-03 12:49:26 +02:00
|
|
|
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
# List out the known server from Spacewalk
|
2017-05-09 23:38:08 +02:00
|
|
|
# ------------------------------
|
2013-05-21 21:40:26 +02:00
|
|
|
if options.list:
|
|
|
|
|
2015-07-31 14:23:54 +02:00
|
|
|
# to build the "_meta"-Group with hostvars first create dictionary for later use
|
|
|
|
host_vars = {}
|
|
|
|
try:
|
|
|
|
for item in spacewalk_report('inventory'):
|
2017-05-09 23:38:08 +02:00
|
|
|
host_vars[item['spacewalk_profile_name']] = dict((key, (value.split(';') if ';' in value else value)) for key, value in item.items())
|
2015-07-31 14:23:54 +02:00
|
|
|
|
2015-08-27 20:58:51 +02:00
|
|
|
except (OSError) as e:
|
2015-08-28 08:18:13 +02:00
|
|
|
print('Problem executing the command "%s inventory": %s' %
|
|
|
|
(SW_REPORT, str(e)), file=sys.stderr)
|
2015-07-31 14:23:54 +02:00
|
|
|
sys.exit(2)
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
groups = {}
|
2017-05-09 23:38:08 +02:00
|
|
|
meta = {"hostvars": {}}
|
2013-05-21 21:40:26 +02:00
|
|
|
try:
|
|
|
|
for system in spacewalk_report('system-groups-systems'):
|
2015-07-31 14:23:54 +02:00
|
|
|
# first get org_id of system
|
2017-05-09 23:38:08 +02:00
|
|
|
org_id = org_groups[system['spacewalk_group_id']]
|
2014-09-03 12:49:26 +02:00
|
|
|
|
2015-07-31 14:23:54 +02:00
|
|
|
# shall we add the org_id as prefix to the group name:
|
2014-09-03 12:49:26 +02:00
|
|
|
if options.prefix_org_name:
|
2015-07-31 14:23:54 +02:00
|
|
|
prefix = org_id + "-"
|
|
|
|
group_name = prefix + system['spacewalk_group_name']
|
|
|
|
else:
|
|
|
|
group_name = system['spacewalk_group_name']
|
|
|
|
|
|
|
|
# if we are limited to one organization:
|
|
|
|
if options.org_number:
|
|
|
|
if org_id == options.org_number:
|
|
|
|
if group_name not in groups:
|
|
|
|
groups[group_name] = set()
|
|
|
|
|
|
|
|
groups[group_name].add(system['spacewalk_server_name'])
|
2017-05-09 23:38:08 +02:00
|
|
|
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta["hostvars"]:
|
|
|
|
meta["hostvars"][system['spacewalk_server_name']] = host_vars[system['spacewalk_server_name']]
|
2015-07-31 14:23:54 +02:00
|
|
|
# or we list all groups and systems:
|
|
|
|
else:
|
|
|
|
if group_name not in groups:
|
|
|
|
groups[group_name] = set()
|
|
|
|
|
|
|
|
groups[group_name].add(system['spacewalk_server_name'])
|
2017-05-09 23:38:08 +02:00
|
|
|
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta["hostvars"]:
|
|
|
|
meta["hostvars"][system['spacewalk_server_name']] = host_vars[system['spacewalk_server_name']]
|
2013-05-21 21:40:26 +02:00
|
|
|
|
2015-08-27 20:58:51 +02:00
|
|
|
except (OSError) as e:
|
2015-08-28 08:18:13 +02:00
|
|
|
print('Problem executing the command "%s system-groups-systems": %s' %
|
|
|
|
(SW_REPORT, str(e)), file=sys.stderr)
|
2013-05-21 21:40:26 +02:00
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if options.human:
|
2015-09-03 08:23:27 +02:00
|
|
|
for group, systems in iteritems(groups):
|
2015-08-28 08:18:13 +02:00
|
|
|
print('[%s]\n%s\n' % (group, '\n'.join(systems)))
|
2013-05-21 21:40:26 +02:00
|
|
|
else:
|
2017-05-09 23:38:08 +02:00
|
|
|
final = dict([(k, list(s)) for k, s in iteritems(groups)])
|
2015-07-31 14:23:54 +02:00
|
|
|
final["_meta"] = meta
|
2017-05-09 23:38:08 +02:00
|
|
|
print(json.dumps(final))
|
|
|
|
# print(json.dumps(groups))
|
2013-05-21 21:40:26 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
# Return a details information concerning the spacewalk server
|
2017-05-09 23:38:08 +02:00
|
|
|
# ------------------------------
|
2013-05-21 21:40:26 +02:00
|
|
|
elif options.host:
|
|
|
|
|
|
|
|
host_details = {}
|
|
|
|
try:
|
|
|
|
for system in spacewalk_report('inventory'):
|
2015-07-31 14:23:54 +02:00
|
|
|
if system['spacewalk_hostname'] == options.host:
|
2013-05-21 21:40:26 +02:00
|
|
|
host_details = system
|
|
|
|
break
|
|
|
|
|
2015-08-27 20:58:51 +02:00
|
|
|
except (OSError) as e:
|
2015-08-28 08:18:13 +02:00
|
|
|
print('Problem executing the command "%s inventory": %s' %
|
|
|
|
(SW_REPORT, str(e)), file=sys.stderr)
|
2013-05-21 21:40:26 +02:00
|
|
|
sys.exit(2)
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
if options.human:
|
2015-08-28 08:18:13 +02:00
|
|
|
print('Host: %s' % options.host)
|
2015-09-03 08:23:27 +02:00
|
|
|
for k, v in iteritems(host_details):
|
2015-08-28 08:18:13 +02:00
|
|
|
print(' %s: %s' % (k, '\n '.join(v.split(';'))))
|
2013-05-21 21:40:26 +02:00
|
|
|
else:
|
2017-05-09 23:38:08 +02:00
|
|
|
print(json.dumps(dict((key, (value.split(';') if ';' in value else value)) for key, value in host_details.items())))
|
2013-05-21 21:40:26 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|