2013-05-21 21:40:26 +02:00
|
|
|
#!/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
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".
|
|
|
|
|
|
|
|
Tested with Ansible 1.1
|
|
|
|
"""
|
|
|
|
#
|
|
|
|
# Author:: Jon Miller <jonEbird@gmail.com>
|
|
|
|
# Copyright:: Copyright (c) 2013, Jon Miller
|
|
|
|
#
|
2014-09-03 12:49:26 +02:00
|
|
|
# Extended for support of multiple organizations by
|
|
|
|
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2014
|
|
|
|
#
|
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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
base_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
SW_REPORT = '/usr/bin/spacewalk-report'
|
|
|
|
CACHE_DIR = os.path.join(base_dir, ".spacewalk_reports")
|
|
|
|
CACHE_AGE = 300 # 5min
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
if not os.path.exists(SW_REPORT):
|
|
|
|
print >> sys.stderr, 'Error: %s is required for operation.' % (SW_REPORT)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Pre-startup work
|
|
|
|
if not os.path.exists(CACHE_DIR):
|
|
|
|
os.mkdir(CACHE_DIR)
|
2013-05-21 23:44:00 +02:00
|
|
|
os.chmod(CACHE_DIR, 2775)
|
2013-05-21 21:40:26 +02:00
|
|
|
|
|
|
|
# Helper functions
|
|
|
|
#------------------------------
|
|
|
|
|
|
|
|
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(',')
|
|
|
|
for line in lines[1:]:
|
|
|
|
values = line.strip().split(',')
|
|
|
|
if len(keys) == len(values):
|
|
|
|
yield dict(zip(keys, values))
|
|
|
|
|
|
|
|
|
|
|
|
# Options
|
|
|
|
#------------------------------
|
|
|
|
|
|
|
|
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")
|
2014-09-03 12:49:26 +02:00
|
|
|
parser.add_option('-o', '--org', default=None, dest="org_number",
|
|
|
|
help="Limit to spacewalk organization number")
|
|
|
|
parser.add_option('-p', default=False, dest="prefix_org_name", action="store_true",
|
|
|
|
help="Prefix the group name with the organization number")
|
2013-05-21 21:40:26 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
|
2014-09-03 12:49:26 +02:00
|
|
|
# Generate dictionary for mapping group_id to org_id
|
|
|
|
#------------------------------
|
|
|
|
org_groups = {}
|
|
|
|
try:
|
|
|
|
for group in spacewalk_report('system-groups'):
|
|
|
|
org_groups[group['group_id']] = group['org_id']
|
|
|
|
|
|
|
|
except (OSError), e:
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
|
|
|
|
(SW_REPORT, str(e))
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
|
2013-05-21 21:40:26 +02:00
|
|
|
# List out the known server from Spacewalk
|
|
|
|
#------------------------------
|
|
|
|
if options.list:
|
|
|
|
|
|
|
|
groups = {}
|
|
|
|
try:
|
|
|
|
for system in spacewalk_report('system-groups-systems'):
|
2014-09-03 12:49:26 +02:00
|
|
|
# first get org_id of system
|
|
|
|
org_id = org_groups[ system['group_id'] ]
|
|
|
|
|
|
|
|
# shall we add the org_id as prefix to the group name:
|
|
|
|
if options.prefix_org_name:
|
|
|
|
prefix = org_id + "-"
|
|
|
|
group_name = prefix + system['group_name']
|
|
|
|
else:
|
|
|
|
group_name = system['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['server_name'])
|
|
|
|
# or we list all groups and systems:
|
|
|
|
else:
|
|
|
|
if group_name not in groups:
|
|
|
|
groups[group_name] = set()
|
|
|
|
|
|
|
|
groups[group_name].add(system['server_name'])
|
2013-05-21 21:40:26 +02:00
|
|
|
|
|
|
|
except (OSError), e:
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
|
|
|
(SW_REPORT, str(e))
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if options.human:
|
|
|
|
for group, systems in groups.iteritems():
|
|
|
|
print '[%s]\n%s\n' % (group, '\n'.join(systems))
|
|
|
|
else:
|
|
|
|
print json.dumps(dict([ (k, list(s)) for k, s in groups.iteritems() ]))
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
# Return a details information concerning the spacewalk server
|
|
|
|
#------------------------------
|
|
|
|
elif options.host:
|
|
|
|
|
|
|
|
host_details = {}
|
|
|
|
try:
|
|
|
|
for system in spacewalk_report('inventory'):
|
|
|
|
if system['hostname'] == options.host:
|
|
|
|
host_details = system
|
|
|
|
break
|
|
|
|
|
|
|
|
except (OSError), e:
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
|
|
|
(SW_REPORT, str(e))
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if options.human:
|
|
|
|
print 'Host: %s' % options.host
|
|
|
|
for k, v in host_details.iteritems():
|
|
|
|
print ' %s: %s' % (k, '\n '.join(v.split(';')))
|
|
|
|
else:
|
|
|
|
print json.dumps(host_details)
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|