|
|
@ -16,12 +16,16 @@ 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
|
|
|
|
on the same machine. It is basically a CSV-to-JSON converter from the
|
|
|
|
output of "spacewalk-report system-groups-systems|inventory".
|
|
|
|
output of "spacewalk-report system-groups-systems|inventory".
|
|
|
|
|
|
|
|
|
|
|
|
Tested with Ansible 1.1
|
|
|
|
Tested with Ansible 1.9.2 and spacewalk 2.3
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Author:: Jon Miller <jonEbird@gmail.com>
|
|
|
|
# Author:: Jon Miller <jonEbird@gmail.com>
|
|
|
|
# Copyright:: Copyright (c) 2013, Jon Miller
|
|
|
|
# Copyright:: Copyright (c) 2013, Jon Miller
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
# Extended for support of multiple organizations and
|
|
|
|
|
|
|
|
# adding the "_meta" dictionary to --list output by
|
|
|
|
|
|
|
|
# Bernhard Lichtinger <bernhard.lichtinger@lrz.de> 2015
|
|
|
|
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or (at
|
|
|
@ -41,6 +45,7 @@ import os
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
from optparse import OptionParser
|
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import ConfigParser
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
import json
|
|
|
@ -51,8 +56,9 @@ base_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
SW_REPORT = '/usr/bin/spacewalk-report'
|
|
|
|
SW_REPORT = '/usr/bin/spacewalk-report'
|
|
|
|
CACHE_DIR = os.path.join(base_dir, ".spacewalk_reports")
|
|
|
|
CACHE_DIR = os.path.join(base_dir, ".spacewalk_reports")
|
|
|
|
CACHE_AGE = 300 # 5min
|
|
|
|
CACHE_AGE = 300 # 5min
|
|
|
|
|
|
|
|
INI_FILE = os.path.join(base_dir, "spacewalk.ini")
|
|
|
|
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
# Sanity check
|
|
|
|
if not os.path.exists(SW_REPORT):
|
|
|
|
if not os.path.exists(SW_REPORT):
|
|
|
|
print >> sys.stderr, 'Error: %s is required for operation.' % (SW_REPORT)
|
|
|
|
print >> sys.stderr, 'Error: %s is required for operation.' % (SW_REPORT)
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(1)
|
|
|
@ -80,6 +86,8 @@ def spacewalk_report(name):
|
|
|
|
|
|
|
|
|
|
|
|
lines = open(cache_filename, 'r').readlines()
|
|
|
|
lines = open(cache_filename, 'r').readlines()
|
|
|
|
keys = lines[0].strip().split(',')
|
|
|
|
keys = lines[0].strip().split(',')
|
|
|
|
|
|
|
|
# add 'spacewalk_' prefix to the keys
|
|
|
|
|
|
|
|
keys = [ 'spacewalk_' + key for key in keys ]
|
|
|
|
for line in lines[1:]:
|
|
|
|
for line in lines[1:]:
|
|
|
|
values = line.strip().split(',')
|
|
|
|
values = line.strip().split(',')
|
|
|
|
if len(keys) == len(values):
|
|
|
|
if len(keys) == len(values):
|
|
|
@ -97,20 +105,85 @@ parser.add_option('--host', default=None, dest="host",
|
|
|
|
parser.add_option('-H', '--human', dest="human",
|
|
|
|
parser.add_option('-H', '--human', dest="human",
|
|
|
|
default=False, action="store_true",
|
|
|
|
default=False, action="store_true",
|
|
|
|
help="Produce a friendlier version of either server list or host detail")
|
|
|
|
help="Produce a friendlier version of either server list or host detail")
|
|
|
|
|
|
|
|
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")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# read spacewalk.ini if present
|
|
|
|
|
|
|
|
#------------------------------
|
|
|
|
|
|
|
|
if os.path.exists(INI_FILE):
|
|
|
|
|
|
|
|
config = ConfigParser.SafeConfigParser()
|
|
|
|
|
|
|
|
config.read(INI_FILE)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Generate dictionary for mapping group_id to org_id
|
|
|
|
|
|
|
|
#------------------------------
|
|
|
|
|
|
|
|
org_groups = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
for group in spacewalk_report('system-groups'):
|
|
|
|
|
|
|
|
org_groups[group['spacewalk_group_id']] = group['spacewalk_org_id']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except (OSError), e:
|
|
|
|
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
|
|
|
|
|
|
|
|
(SW_REPORT, str(e))
|
|
|
|
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# List out the known server from Spacewalk
|
|
|
|
# List out the known server from Spacewalk
|
|
|
|
#------------------------------
|
|
|
|
#------------------------------
|
|
|
|
if options.list:
|
|
|
|
if options.list:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# to build the "_meta"-Group with hostvars first create dictionary for later use
|
|
|
|
|
|
|
|
host_vars = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
for item in spacewalk_report('inventory'):
|
|
|
|
|
|
|
|
host_vars[ item['spacewalk_profile_name'] ] = dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in item.items() )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except (OSError), e:
|
|
|
|
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
|
|
|
|
|
|
|
|
(SW_REPORT, str(e))
|
|
|
|
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
|
|
|
|
groups = {}
|
|
|
|
groups = {}
|
|
|
|
|
|
|
|
meta = { "hostvars" : {} }
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
for system in spacewalk_report('system-groups-systems'):
|
|
|
|
for system in spacewalk_report('system-groups-systems'):
|
|
|
|
if system['group_name'] not in groups:
|
|
|
|
# first get org_id of system
|
|
|
|
groups[system['group_name']] = set()
|
|
|
|
org_id = org_groups[ system['spacewalk_group_id'] ]
|
|
|
|
|
|
|
|
|
|
|
|
groups[system['group_name']].add(system['server_name'])
|
|
|
|
# shall we add the org_id as prefix to the group name:
|
|
|
|
|
|
|
|
if options.prefix_org_name:
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
|
|
|
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'] ]
|
|
|
|
|
|
|
|
# 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'])
|
|
|
|
|
|
|
|
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'] ]
|
|
|
|
|
|
|
|
|
|
|
|
except (OSError), e:
|
|
|
|
except (OSError), e:
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
|
|
|
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
|
|
|
@ -121,8 +194,10 @@ if options.list:
|
|
|
|
for group, systems in groups.iteritems():
|
|
|
|
for group, systems in groups.iteritems():
|
|
|
|
print '[%s]\n%s\n' % (group, '\n'.join(systems))
|
|
|
|
print '[%s]\n%s\n' % (group, '\n'.join(systems))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print json.dumps(dict([ (k, list(s)) for k, s in groups.iteritems() ]))
|
|
|
|
final = dict( [ (k, list(s)) for k, s in groups.iteritems() ] )
|
|
|
|
|
|
|
|
final["_meta"] = meta
|
|
|
|
|
|
|
|
print json.dumps( final )
|
|
|
|
|
|
|
|
#print json.dumps(groups)
|
|
|
|
sys.exit(0)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -133,7 +208,7 @@ elif options.host:
|
|
|
|
host_details = {}
|
|
|
|
host_details = {}
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
for system in spacewalk_report('inventory'):
|
|
|
|
for system in spacewalk_report('inventory'):
|
|
|
|
if system['hostname'] == options.host:
|
|
|
|
if system['spacewalk_hostname'] == options.host:
|
|
|
|
host_details = system
|
|
|
|
host_details = system
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
@ -147,8 +222,7 @@ elif options.host:
|
|
|
|
for k, v in host_details.iteritems():
|
|
|
|
for k, v in host_details.iteritems():
|
|
|
|
print ' %s: %s' % (k, '\n '.join(v.split(';')))
|
|
|
|
print ' %s: %s' % (k, '\n '.join(v.split(';')))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print json.dumps(host_details)
|
|
|
|
print json.dumps( dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in host_details.items() ) )
|
|
|
|
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|