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


Script Usage

./sw_listgroups.py 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: 2016-10-07
# Recent Changes:-header format/comment modifications
#                -Use spacecmd config file for credentials (ConfigParser)
####################################################################################
 
#### Import Modules #### 
# sys.argv: Command line arguments
from sys import argv
 
# ConfigParser: Use variables from external file
import ConfigParser
 
# os: Use bash environment variables
import os
 
# xmlrpclib: Allows for parsing of spacewalk APIs
import xmlrpclib
####################
 
#### Command Line Arguments #### 
# If there is not 1 argument, show usage and exit
if len(argv[1:]) != 1:
    print "==== Spacewalk: List Groups ===="
    print
    print "Description: Output a space separated list of Spacewalk groups a system is in."
    print
    print "--Usage--" 
    print "./sw_listgroups.py hostname"
    print
    print "--Other Requirements--"
    print "-> spacecmd config file setup."
    print
    exit(1)
 
# Set hostname from command line argument
system_name = argv[1]
################################
 
#### 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")
#########################
 
#### Main ####
 
# 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 = server.system.getId(key,system_name)
if not system_id:
    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[0]['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.1480561518.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)