python_wiki:spacewalk_list_groups

This is an old revision of the document!


Spacewalk: List Groups

General Information

Uses Spacewalk APIs to login and print all of the subscribed groups for the given system name.

Checklist


Usage

./sw_listgroups.py -n HOSTNAME

The Code

sw_listgroups.py
#!/usr/bin/python
# Title: sw_listgroups.py
# Description: Output a space separated list of Spacewalk groups a system is in.
# Date: 2017-06-27
# Recent Changes:-updated arguments to use argparse
#                -check for variants of system name (lower,upper,fqdn of each)
####################################################################################
 
#=======================
# Import Modules
#=======================
# argparse: Command line arguments
import argparse
 
# ConfigParser: Use variables from external file
import ConfigParser
 
# os: Use bash environment variables
import os
 
# xmlrpclib: Allows for parsing of spacewalk APIs
import xmlrpclib
 
#=======================
# CUSTOMIZE HERE
#=======================
# Domain Name (include first '.')
domain_name=".example.com"
 
#=======================
# Get Script Arguments
#=======================
# Build argument parser information
parser = argparse.ArgumentParser(description="Output a space separated list of Spacewalk groups a system is in.")
parser.add_argument("-n","--name", help="System Name.", required=True)
args = vars(parser.parse_args())
 
system_name = args['name']
 
#=======================
# Config Settings
#=======================
# Read Spacecmd Config file - Path set from $HOME variable + expected spacecmd config file
config = ConfigParser.ConfigParser()
config.read(os.environ.get('HOME') + "/.spacecmd/config")
 
# Parse Spacecmd Config file - Set username, password, spacewalk server
spacewalk_login = config.get("spacecmd", "username")
spacewalk_password = config.get("spacecmd", "password")
spacewalk_server = config.get("spacecmd", "server")
#### End of Config Settings ####
 
#=====================================
# Functions; Main starts after
#=====================================
# Function: Get a system's id
def get_system_id(system_name):
 
  # Create all system name variants
  system_name_lower = system_name.strip(domain_name).lower()
  system_name_lower_fqdn = system_name_lower + domain_name
  system_name_upper = system_name.strip(domain_name).upper()
  system_name_upper_fqdn = system_name_upper + domain_name
 
  # Attempt to get the system's id for each
  system_id_lower = server.system.getId(key, system_name_lower)
  system_id_lower_fqdn = server.system.getId(key, system_name_lower_fqdn)
  system_id_upper = server.system.getId(key, system_name_upper)
  system_id_upper_fqdn = server.system.getId(key, system_name_upper_fqdn)
 
  # Check each for success
  if system_id_lower:
    system_id = system_id_lower[0]['id']
  elif system_id_lower_fqdn:
    system_id = system_id_lower_fqdn[0]['id']
  elif system_id_upper:
    system_id = system_id_upper[0]['id']
  elif system_id_upper_fqdn:
    system_id = system_id_upper_fqdn[0]['id']
  else:
    # System cannot be found
    system_id = None
 
  return system_id
 
#===================
# Main starts here
#===================
 
# Setup server info (rpc api endpoint)
spacewalk_url = "https://" + spacewalk_server + "/rpc/api"
server = xmlrpclib.Server(spacewalk_url)
 
# Attempt to login and get a session id
try:
    key = server.auth.login(spacewalk_login, spacewalk_password)
except:
    print "Spacewalk Login failed."
    exit(1)
 
# Get the system id and check to see if that system_name exists
system_id = get_system_id(system_name)
if system_id == None:
    server.auth.logout(key)
    print("--> Error: (" + system_name + ") not found in Spacewalk!")
    exit(1)
 
# Get all available groups for the system_name
available_groups = server.system.listGroups(key, system_id)
 
# Logout of Spacewalk server
server.auth.logout(key)
 
# Empty list to hold what groups the system_name is subscribed to
subscribed_groups = []
 
# Check available groups for subscribed = 1 (true), add to list
for flag in available_groups:
    if flag['subscribed'] == 1:
        subscribed_groups.append(flag['system_group_name'])
 
# A non-empty list evaluates to true
if subscribed_groups:
    for line in subscribed_groups:
        # Print each group with no newline (,)
        print line,
else:
    # We have an empty list; the system is not subscribed to any groups
    print("Node (" + system_name + ") is not in any groups.")
  • python_wiki/spacewalk_list_groups.1499484651.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)