On 06/14/2010 12:14 PM, Tom Brown wrote:
> Hi
> 
> Does anyone have any examples of using the API to install package updates?
> 
> What i am trying to achieve is automatic installation of available
> updates without having to go via the web gui and without using a yum
> update.
> 
> Is that possible?
> 

Hello,

Yes, it definitely is.  I'll attach a python script we use here, but if
you want to write your own, a useful resource is the API docs (which can
be found at http://$your_spacewalk/rhn/apidoc/ .

Some notes on our script:
- it uses the local /etc/yum.conf "exclude" directive to mask packages
that will be pushed for updates
- inactive or locked systems will not have updates pushed out to them
- the script does its best to try to not schedule duplicate updates (it
looks at pending events and masks out packages that are currently queued)
- there's a commented-out block at the end that can apply errata, too-
although it does not currently respect the yum.conf excludes (*note*:
without this, updates /with/ errata will not be applied!)

Two bugs:
- clients will occasionally fail to install updates (although they will
be re-queued next run) for one of two reasons: the repodata isn't
written out yet that contains the new package information, or they use
the yum cache and again don't see the package.  In at least one of these
cases, I've seen a "success" returned- although, again, the package will
get re-queued the next run.
- there was a bug (rhbz 588901) in system.listLatestUpgradablePackages
that returned too many packages.  This should be fixed in sw1.1.

Also, as Tomas said, there is a checkbox in the webui as well, but that
only applies updates with errata.

Hope to help,

Josh
#!/usr/bin/python
import xmlrpclib
import getpass
from yum import config
import fnmatch

SATELLITE_URL = "http://localhost/rpc/api";
SATELLITE_LOGIN = raw_input("Username: ")
SATELLITE_PASSWORD = getpass.getpass("Password: ")

# get yum [main] excludes
startup = config.readStartupConfig('/etc/yum.conf', '/')
yumconf = config.YumConf()
yumconf.populate(startup._parser, 'main')
#print "Excluding packages found in local yum.conf: %s" % yumconf.exclude

# login to spacewalk
client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)

#inactive = client.system.listInactiveSystems(key)
#for mia in inactive:
#	print "Skipping inactive system %s" % mia['name']

systems = client.system.listActiveSystems(key)
for sys in systems:
	details = client.system.getDetails(key, sys['id'])
	if details['lock_status']:
		print "Skipping locked system %s" % sys['name']
		continue

	package_mask = {}
	events = client.system.listSystemEvents(key, sys['id'])
	for ev in events:
		# only look at Package Install actions
		if ev['action_type'] != 'Package Install':
			continue
		# if this Package Install has ever completed (successfully or otherwise), skip it
		if ev.has_key('result_msg'):
			continue

		# get the package list
		if ev.has_key('additional_info'):
			for info in ev['additional_info']:
				#print "%s - masking %s" % (sys['name'], info['detail'])
				package_mask[info['detail']] = 1

	packages = client.system.listLatestUpgradablePackages(key, sys['id'])
	upgrade = []
	for pack in packages:
		# go through yum excludes
		skip = False
		for excl in yumconf.exclude:
			if fnmatch.fnmatch(pack['name'], excl):
				#print "%s - skipping %s by %s" % (sys['name'], pack['name'], excl)
				skip = True
		if skip:
			continue

		# see if the package is already queued
		name = pack['name'] + "-" + pack['to_version'] + "-" + pack['to_release']
		if pack['to_epoch'] != '':
			try:
				unused_test = int(pack['to_epoch'])
				name += (":" + pack['to_epoch'])
			except:
				pass
		#print " - looking for %s" % name
		if package_mask.has_key(name):
			#print " - skipping already queued package %s" % name
			continue

		# mark for update!
		print "%s - updating to %s (%i)" % (sys['name'], name, pack['to_package_id'])
		upgrade.append(pack['to_package_id'])

	if len(upgrade) > 0:
		client.system.schedulePackageInstall(key, sys['id'], upgrade, xmlrpclib.DateTime(0))

#	### now find errata to apply ###
#	errata = client.system.getUnscheduledErrata(key, sys['id'])
##	erIds = []
##	for er in errata:
##		pkgs = client.errata.listPackages(key, er['advisory_name'])
##		for pkg in pkgs:
#	if len(errata) > 0:
#		erIds = map(lambda er: er['id'], errata)
#		print "%s - applying %i errata: %s" % (sys['name'], len(errata), erIds)
#		client.system.scheduleApplyErrata(key, sys['id'], erIds)

client.auth.logout(key)

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
Spacewalk-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/spacewalk-list

Reply via email to