付録D Spacewalk XML/RPC API
上級ユーザーの方は、Spacewalk XML/RPC APIを使用して、タスクを実行または自動化するWebインタフェースとスクリプトを作成できます。 このAPIの詳細については、Spacewalkサーバーのhttps://を参照してください。
swksvr_FQDN/rpc/api
たとえば、次のget-channel-summaries Pythonスクリプトでは、APIを使用してチャネルのリスト、各チャネルのパッケージ数、および各チャネルにサブスクライブされているシステムの数を取得します:
#!/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)
次に、このスクリプトを実行した場合の出力例を示します:
Channel label Packages Systems ------------- -------- ------- base-channel 0 0 epel6-channel-label 68 0 epel6-channel1-label 3 0 epel6-channel2-label 1 0 ol5-i386-channel-label 44 0 ol5-i386-test-label 34 0 ol5-x64-channel-label 44 0 ol6-i386-channel-label 45 0 ol6-x64-channel-label 45 1 ol7-channel-label 45 1 oraclelinux6-x86_64 0 0 oraclelinux6-x86_64-addons 0 0 oraclelinux6-x86_64-spacewalk22-client 0 0 oraclelinux6-x86_64-spacewalk22-server 0 0 oraclelinux6-x86_64-spacewalk24-client 0 0 oraclelinux6-x86_64-spacewalk24-server 0 0 oraclelinux6-x86_64-spacewalk26-client 0 0 oraclelinux6-x86_64-spacewalk26-server 0 0 oraclelinux6-x86_64-spacewalk27-server 0 0 uln-channel-label 173 0
次の例は、リポジトリの同期スケジュールを表示するget-reposync-listスクリプトを示しています。
#!/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)
次の例は、このコマンドの出力結果を示しています:
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-spacewalk24-client 0 0 5 ? * * oraclelinux6-x86_64-spacewalk24-server 0 30 5 ? * * oraclelinux6-x86_64-spacewalk26-client 0 0 2 ? * * oraclelinux6-x86_64-spacewalk26-server 0 0 3 ? * * oraclelinux6-x86_64-spacewalk27-server 0 0 4 ? * * oraclelinux6-x86_64-uek 0 0 5 ? * * oraclelinux6-x86_64-uek-r3 0 30 1 ? * * ...