Skip to content

Instantly share code, notes, and snippets.

@jonEbird
Created April 26, 2013 20:28
Show Gist options
  • Save jonEbird/5470238 to your computer and use it in GitHub Desktop.
Save jonEbird/5470238 to your computer and use it in GitHub Desktop.
Sample Python script for connecting to your RedHat Satellite or Spacewalk server. But also an example of a file that you can source from your shell to help set some useful variables for subsequent invocations of your Python script. Trying to combine two conveniences into one file. I had seen this before and got a refresher course from [rosettaco…
#!/bin/bash
# -*- python -*-
"true" '''\'
#
# Shell Code that helps you setup useful variables. Source this script.
#
BN=$(basename -- $0)
if [ "$BN" == "bash" -o "$BN" == "zsh" -o "$BN" == "sh" ]; then
if [ -z "${SPACEWALK_USER}" ]; then
read -p "What is your spacewalk user name: " -e -i "admin-prj" SPACEWALK_USER
fi
if [ -z "${SPACEWALK_PASS}" ]; then
read -p "What is your spacewalk ${SPACEWALK_USER} password: " -s -e SPACEWALK_PASS
echo
fi
if [ -z "${SERVER}" ]; then
read -p "What is your spacewalk server: " -e -i "localhost" SERVER
fi
export SPACEWALK_USER
export SPACEWALK_PASS
export SERVER
return
fi
"exec" "python" "$0" "$@"
exit 127
'''
import os, sys, getpass
import xmlrpclib
if __name__ == '__main__':
from optparse import OptionParser, OptionGroup
parser = OptionParser(usage="%prog [options]", version="%prog 1.0")
parser.add_option("-l", "--list", dest="list", action="store_true", default=False,
help="List the known kickstart labels")
parser.add_option("-g", "--get", dest="getks", metavar="LABEL",
help="Get the package contents for kickstart label")
parser.add_option("-p", "--put", dest="putks", metavar="LABEL",
help="Put the package contents for kickstart label")
parser.add_option("-f", "--file", dest="filepath", metavar="FILE",
help="File that contains the package list to upload")
# Spacewalk Options
group = OptionGroup(parser, "Spacewalk Options",
"Common authentication options")
group.add_option("--user", dest="user", default=os.getenv('SPACEWALK_USER'),
help="Defaults to ENV(SPACEWALK_USER)")
group.add_option("--password", dest="password", default=os.getenv('SPACEWALK_PASS'),
help="Defaults to ENV(SPACEWALK_PASS)", metavar="PASS")
group.add_option("--server", dest="server", default=os.getenv('SERVER', 'localhost'),
help="Defaults to ENV(SPACEWALK_SERVER) or localhost")
parser.add_option_group(group)
(options, args) = parser.parse_args()
SATELLITE_URL = "http://%s/rpc/api" % (options.server)
SATELLITE_LOGIN = options.user
SATELLITE_PASSWORD = options.password
# Connect and login to the spacewalk server
try:
client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
key_session = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
except Exception, e:
print 'Error: Problem authenticating to %s with the %s user: %s' % \
(SATELLITE_URL, SATELLITE_LOGIN, str(e))
sys.exit(1)
# Now onward to doing work
if options.list:
ksprofiles = client.kickstart.listKickstarts(key_session)
print 'Kickstart labels:\n %s' % '\n '.join([ ks['label'] for ks in ksprofiles ])
elif options.getks:
kslabel = options.getks
packages = client.kickstart.profile.software.getSoftwareList(key_session, kslabel)
print '# kickstart package list for profile: %s\n%s' % (kslabel, '\n'.join(packages))
elif options.putks:
kslabel = options.putks
contents = ''
if options.filepath:
src = 'file "%s"' % options.filepath
try:
fh = open(options.filepath)
contents = fh.read()
fh.close()
except (IOError, OSError), e:
print 'Error: Probably reading the contents from %s: %s' % (options.filepath, str(e))
sys.exit(2)
else:
src = 'stdin'
# assume contents from stdin
contents = sys.stdin.read()
#print 'Unimplemented at this point. Would have used this, though:\n%s' % (contents)
print 'Pushing content from %s to kickstart label %s.' % (src, kslabel)
newpackages = [ p for p in contents.split('\n') if p ] # no blank lines
client.kickstart.profile.software.setSoftwareList(key_session, kslabel, newpackages)
# Logout from spacewalk
client.auth.logout(key_session)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment