Appendix D Spacewalk XML/RPC API

Advanced users can use the Spacewalk XML/RPC API to create web interfaces or scripts to perform or automate tasks. More information about the API is available at https://swksvr_FQDN/rpc/api on a Spacewalk server.

For example, the following Python script get-channel-summaries uses the API to obtain a list of channels, the numbers of packages in each channel, and the number of systems that are subscribed to each channel.

#!/usr/bin/python
#
#  get-channel-summaries [--server URL <url>] [--username <user>] [--password <passwd>]

import getopt, struct, sys, xmlrpclib
from array import *

# Insert default values for the Spacewalk server API URL,
# Spacewalk admin user name, and Spacewalk admin password
url = "https://swksvr.mydom.com/rpc/api"
username = "swadmin"
password = "swadmin"

usage1 = "Usage: get-channel-summaries [--serverUrl <url>] \\\n"
usage2 = "         [--username <user>] [--password <passwd>]"

try:
    opts,args = getopt.getopt(sys.argv[1:],"s:u:p:",["serverUrl=","username=","password="])
except getopt.GetoptError as err:
    print(usage1+usage2)
    sys.exit(1)

for o,a in opts:
    if o in ("-s", "--serverUrl"):
        url = a
    elif o in ("-u", "--username"):
        username = a
    elif o in ("-p", "--password"):
        password = a
    else:
        assert False, "Unknown option"

# Connect to Spacewalk
client = xmlrpclib.Server(url,verbose=0)
session = client.auth.login(username,password)

# Get channel list
channels = client.channel.listAllChannels(session)

# Build channel arrays indexed by channel ID
channel_label = {}
channel_packages = {}
channel_systems = {}

for channel in channels:
    channel_label[channel['id']] =  channel['label']
    channel_packages[channel['id']] =  channel['packages']
    channel_systems[channel['id']] =  channel['systems']

# Print output header
fmt1 = '{0:<40s}{1:<10s}{2:<10s}'
print fmt1.format('Channel label','Packages','Systems')
print fmt1.format('-------------','--------','-------')

# Print channel label, package count, and system count -- sorted by label
fmt2 = '{0:<40s}{1:<10d}{2:<10d}'
for key,value in sorted(channel_label.iteritems(),key=lambda(k,v): (v,k)):
    id = int(key)
    print fmt2.format(value,channel_packages[id],channel_systems[id])

# Disconnect from Spacewalk
client.auth.logout(session)

The following is sample output from this command:

Channel label                           Packages  Systems   
-------------                           --------  -------   
oraclelinux6-x86_64                     6595      4         
oraclelinux6-x86_64-addons              230       4         
oraclelinux6-x86_64-mysql               204       0         
oraclelinux6-x86_64-playground          826       0         
oraclelinux6-x86_64-spacewalk20-client  43        0         
oraclelinux6-x86_64-spacewalk20-server  270       0         
oraclelinux6-x86_64-spacewalk22-client  30        4         
oraclelinux6-x86_64-spacewalk22-server  274       0         
oraclelinux6-x86_64-uek                 387       0         
oraclelinux6-x86_64-uek-r3              292       4         
oraclelinux6_u6_x86_64-patch            1332      4        

The next example script get-reposync-list displays the schedules for synchronizing repositories.

#!/usr/bin/python
#
#  get-reposync-list [--serverUrl <url>] [--username <user>] [--password <passwd>]

import getopt, struct, sys, xmlrpclib
from array import *

# Insert default values for the Spacewalk server API URL,
# Spacewalk admin user name, and Spacewalk admin password
url = "https://swksvr.mydom.com/rpc/api"
username = "swadmin"
password = "swadmin"

usage1 = "Usage: get-reposync-list [--serverUrl <url>] \\\n"
usage2 = "         [--username <user>] [--password <passwd>]"

try:
    opts,args = getopt.getopt(sys.argv[1:],"s:u:p:",["serverUrl=","username=","password="])
except getopt.GetoptError as err:
    print(usage1+usage2)
    sys.exit(1)

for o,a in opts:
    if o in ("-s", "--serverUrl"):
        url = a
    elif o in ("-u", "--username"):
        username = a
    elif o in ("-p", "--password"):
        password = a
    else:
        assert False, "Unknown option"

# Connect to Spacewalk
client = xmlrpclib.Server(url,verbose=0)
session = client.auth.login(username,password)

# Get channel list
channels = client.channel.listAllChannels(session)

# Build channel name array indexed by channel ID
channel_label = {}
channel_schedule = {}

for channel in channels:
    id = int(channel['id'])
    channel_label[id] =  channel['label']
    channel_schedule[id] = ''

# Get repository synchronization list
schedules = client.taskomatic.org.listActiveSchedulesByBunch(session,'repo-sync-bunch')

# Construct schedule array indexed by channel ID
for schedule in schedules:
    channel_schedule[int(schedule['data_map']['channel_id'])] = schedule['cron_expr']

# Print output header
fmt = '{0:<40s}{1:<40s}'
print fmt.format('Channel label','Schedule')
print fmt.format('-------------','--------')

# Print channel labels and repository synchronization schedule (if defined)
for key,value in sorted(channel_label.iteritems(),key=lambda(k,v):(v,k)):
    id = int(key)
    sched = channel_schedule[id]
    if (len(sched) > 0):
        print fmt.format(value,sched)
    else:
        print fmt.format(value,"Sync not scheduled")

# Disconnect from Spacewalk
client.auth.logout(session)

The following is sample output from this command:

Channel label                           Schedule                                
-------------                           --------                                
oraclelinux6-x86_64                     0 30 0 ? * *                     
oraclelinux6-x86_64-addons              0 30 2 ? * *                      
oraclelinux6-x86_64-mysql               0 30 4 ? * *                      
oraclelinux6-x86_64-playground          0 30 3 ? * *                      
oraclelinux6-x86_64-spacewalk20-client  0 0 5 ? * *                      
oraclelinux6-x86_64-spacewalk20-server  0 30 5 ? * *                      
oraclelinux6-x86_64-spacewalk22-client  0 0 2 ? * *                    
oraclelinux6-x86_64-spacewalk22-server  0 0 3 ? * *                      
oraclelinux6-x86_64-uek                 0 0 4 ? * *                      
oraclelinux6-x86_64-uek-r3              0 30 1 ? * *                      
oraclelinux6_u6_x86_64-patch            0 0 1 ? * *