Rice University logo
 
Top blue bar image
A place to post stuff
 

Posts Tagged ‘red hat network satellite’


Python RHN Fun: Cleaning the Satellite

October 16th, 2012 by chwilk

Here’s a little script to delete any systems from your database that haven’t checked in for more than 90 days.

#!/usr/bin/python
# Removes systems not checking in within the last 90 days

import xmlrpclib
import getpass
import datetime

SATELLITE_URL = "http://YOUR-SATELLITE-HOSTNAME/rpc/api"
SATELLITE_LOGIN = "userid"
SATELLITE_PASSWORD = "password"

TARGET_ENTITLEMENT = ['monitoring_entitled']

if __name__ == "__main__":
    client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
    SATELLITE_LOGIN = str(raw_input("RHN Userid: ")).rstrip()
    SATELLITE_PASSWORD = getpass.getpass()

    key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
    result_array = client.system.listInactiveSystems(key,90)
    id_array = [profile['id'] for profile in result_array]
    client.system.deleteSystems(key, id_array)
    client.auth.logout(key)

Scripting RHN Re-activations

August 3rd, 2012 by chwilk

Here’s the situation: You’ve fixed something in your RHN Satellite activation key that should have been in there, or you’ve added functionality, but when you apply the change, you’d also like to apply those changes to machines that have already been registered with that activation key. Unfortunately, there’s no nice button to make that happen in the current version.

Hopefully you’ve been using system groups to keep track of machines deployed with your activation keys, because you’ll need to load that group into the system set manager and run a small python script one each machine as root. The idea is to re-register the machines to rhn, using each machine’s custom re-activation key to ensure that they reattach to their original system profile when you do so. Note: This will work if you’ve added something to the activation key, say a new config or software channel, but it won’t take away channels you want to remove.

Here’s the script:

#!/usr/bin/python
try:
    import xmlrpclib
    import sys
    import os
    import string

    system_id = "/etc/sysconfig/rhn/systemid"

    keys = sys.argv[1:]

    if os.path.exists(system_id):
        client =  xmlrpclib.Server("http://rhn.rice.edu/rpc/api")
        keys.append(client.system.obtain_reactivation_key(open(system_id).read()))
except Exception as inst:
    # xml rpc due to  a old/bad system id
    print type(inst)
    print inst.args
    print inst
    print "Could not get reactivation key, exiting"
    sys.exit(1)
else:
    command = "rhnreg_ks --force --activationkey=" + string.join(keys, ",")
    os.system(command)
    sys.exit(0)

Python Yum Fun 2: Kickstart List

May 29th, 2012 by chwilk

Need to create a Red Hat kickstart config to duplicate the installed packages off an existing system? Want to do this without having to create a giant file listing each RPM by hand?

Here’s a python script that will pull your installed packages list and prune it down by dependencies and yum groups, and give you a (hopefully) concise list of yum groups and RPMs that will work in a kickstart config file. The only complication is that you’ll need to ensure all your yum repositories are available at kickstart time, else you may run into some RPMs that are not available.

#!/usr/bin/env python
import yum

yb = yum.YumBase()
yb.doConfigSetup()
yb.doTsSetup()
yb.doRpmDBSetup()
    
# Grab Installed Package List (ipl)
ipl=set([pkg[0] for pkg in yb.rpmdb.pkglist])
grppkgs = set([])
# Grab Group List (gl)
gl=yb.doGroupLists()
# First index of gl is Installed Group List (igl)
igl=gl[0]

# Make sure we still are accounting for Core (sometimes packages get removed)
gotcore = False
for grp in igl:
    if grp.groupid == 'core': gotcore = True

if not gotcore:
    for grp in gl[1]:
        if grp.groupid == 'core':
           igl.append(grp)

# Find leaves of dependency tree
loners = ipl.copy()
for pkg in list(loners):
    deps= yb.findDeps(yb.returnInstalledPackagesByDep(pkg)).values()[0].keys()
    for dep in [d[0] for d in deps] :
        if dep in loners:
            loners.remove(pkg)
            break

# Print a kickstartable group list first
for grp in igl:
    for pkg in grp.mandatory_packages:
        if pkg in loners:
            loners.remove(pkg)
    for pkg in grp.default_packages:
        if pkg in loners:
            loners.remove(pkg)
    print '@'+grp.groupid

# Print rpms not pruned already
for pkg in loners:
   print pkg

Python Yum Fun 1: Kickstartable Grouplist

May 17th, 2012 by chwilk

Here’s a short python code to load up the local yum database, grab the list of yum groups, and print installed and available groups by short names (which can be used in kickstart scripts more easily than the output of yum grouplist.)

#!/usr/bin/env python
import yum
import sys

if __name__ == "__main__":
    yb = yum.YumBase()
    yb.doConfigSetup()
    yb.doTsSetup()
    yb.doRpmDBSetup()
    
    # Grab Group List (gl)
    gl=yb.doGroupLists()
    # First index of gl is Installed Group List (igl)
    igl=gl[0]
    # Second index of gl is Available Group List (agl)
    agl=gl[1]
    
    # Print a kickstartable group list
    print "Installed groups:\n"
    for grp in igl:
        print "@" + grp.groupid + ": '" + grp.name + "'"

    print "Available groups:\n"
    for grp in agl:
        print "@" + grp.groupid + ": '" + grp.name + "'"


Thanks to the for publishing a great starting point to yum python programming.