[review request] up2date_repo_autoconf -- configure system-specific repo automatically

Michael E Brown Michael_E_Brown at dell.com
Sun Apr 8 01:00:46 CDT 2007


All,
    I just released firmware-addon-dell 1.2.11. One of the executable
programs in this release is up2date_repo_autoconf. This executable and
its configuration file is only included in the RPMs built for RHEL3 and
RHEL4, as it is not necessary for RHEL5, nor any Fedora release.

    The purpose of this program is to automatically add entries to
/etc/sysconfig/rhn/sources for the Dell system-specific repositories.
To add the dell hardware-specific repositories, The command is run like
this:

    # up2date_repo_autoconf --add --osname=el4.\$basearch   \
        --url=http://linux.dell.com/repo/hardware/

To remove the repo entries, the command is run like this:

    # up2date_repo_autoconf --remove

    The program automatically creates a backup copy of the sources file
if it modifies the file, named sources.backup. There is a command line
option to skip this backup, if desired.

    The "url", and "osname" entries can be automatically pulled from the
default config file, /etc/sysconfig/rhn/dell-hardware.conf. This file
has the following format, and contains the following defaults:

    $ cat /etc/sysconfig/rhn/dell-hardware.conf
    [main]
    osname=el4.$basearch
    url=http://linux.dell.com/repo/hardware/

    If the dell-hardware.conf file is present and populated, then it is
not necessary to specify the 'url', nor 'osname' entries on the command
line, and you can simply run:

    # up2date_repo_autoconf [--add|--remove]

    The RHEL3 and RHEL4 RPMs have this configuration file automatically
populated with the (unofficial) dell hardware repository address.

    The source for up2date_repo_autoconf is included inline at the end
of this message. Please review this and let me know if you have any
comments or concerns, as I will be shortly releasing a dellhw-repository
RPM that will make use of this to automatically configure the
repositories for RHEL3/4.
--
Michael



#!/usr/bin/python
# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:

  #############################################################################
  #
  # Copyright (c) 2005 Dell Computer Corporation
  # Dual Licenced under GNU GPL and OSL
  #
  #############################################################################
"""up2date_repo_autoconf  {-a|-r} [-b|-n] [--url=<URL>] [--config=<FILE>]

usage:
    -h | --help         print this message
    -a | --add_config   add dell-hardware config lines
    -r | --remove_config remove all dell-hardware config lines
    -b | --backup       create backup files before modifying (default)
    -n | --no_backup    dont create backup files (not recommended)

The following args override settings in /etc/sysconfig/dell-hardware.conf:
    -o | --osname       set osname to use in url line
    -s | --sysid=       override sysid (for testing purposed, mostly)
    -u | --url=         set alternate url to use 
                        (mirror.pl and args automatically appended)

The following opts have reasonable defaults and are mainly used for testing:
         --config=      use alternative config file
                        default: /etc/sysconfig/rhn/dell-hardware.conf
         --rhn_sources= use alternative rhn sources file
                        default: /etc/sysconfig/rhn/sources
"""

from __future__ import generators

import ConfigParser
import getopt
import os
import shutil
import sys

version="1.2.10"

repoNameFirstPart = "dell-hardware-"
repoNameTemplate = "dell-hardware-ven_0x%04x_dev_0x%04x"
mirrorCgiTemplate="mirrors.pl?sys_ven_id=0x%04x&sys_dev_id=0x%04x&osname=%s&up2date_repo_autoconf=" + version
baseurlTemplate = "system.ven_0x%04x.dev_0x%04x/%s"

def checkLine(yum, repo, uri, venid, sysid, osname, url):
    needUpdate=0

    if yum == "yum":
        if uri != url + baseurlTemplate % (venid,sysid,osname):
            needUpdate=1

    if yum == "yum-mirror":
        if repo != repoNameTemplate % (venid, sysid):
            needUpdate=1

        fullurl= url + mirrorCgiTemplate % (venid,sysid,osname)
        if uri != fullurl:
            needUpdate=1

    return needUpdate

def backupFiles(fileName):
    backupName = os.path.basename(fileName) + ".backup"
    backupDir=os.path.dirname(fileName)
    backupFullPath=os.path.join(backupDir,backupName)
    shutil.copyfile(fileName, backupFullPath)

# go through file and remove config lines for the hardware repo
def cleanConfig(fileName):
    fd=open(fileName,"r+")
    rhnSources = fd.readlines()
    fd.seek(0,0)
    fd.truncate()
    for line in rhnSources:
        if line.startswith("yum %s" % repoNameFirstPart): continue
        if line.startswith("yum-mirror %s" % repoNameFirstPart): continue
        fd.write(line)
    fd.close()

# append lines for the hardware repo to the end of the file
def doAddConfig(fileName, url, venid, sysid, osname):
    fd=open(fileName, "a")
    repoName = repoNameTemplate % (venid, sysid)
    fullurl= url + mirrorCgiTemplate % (venid,sysid,osname)
    fd.write( "yum %s %s\n" % (repoName, url + baseurlTemplate % (venid,sysid,osname)) )
    fd.write( "yum-mirror %s %s\n" % (repoName, fullurl) )
    fd.close()

def main():
    sysid = 0
    venid = 0x1028
    up2dateSources="/etc/sysconfig/rhn/sources"
    url=""
    addConfig=-1
    backup=1
    config="/etc/sysconfig/rhn/dell-hardware.conf"
    osname=""
    ini = ConfigParser.ConfigParser()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hbs:nru:o:a", ["help", "add_config", "backup", "sysid=", "config=", "remove_config", "url=", "rhn_sources=", "osname=", "no_backup"])
        for option, argument in opts:
            if option in ("-h", "--help"):
                print __doc__
                sys.exit(0)
            if option in ("-a", "--add_config"):
                addConfig=1
            if option in ("-s", "--sysid"):
                sysid=int(argument, 0)
            if option in ("-b", "--backup"):
                backup=1
            if option in ("-n", "--no_backup"):
                backup=0
            if option in ("-c", "--config"):
                config = None
                ini.read(argument)
            if option in ("--rhn_sources",):
                up2dateSources=argument
            if option in ("-r", "--remove_config"):
                addConfig=0
            if option in ("-o", "--osname"):
                osname=argument
            if option in ("-u", "--url"):
                url=argument

        if addConfig < 0:
            raise getopt.GetoptError("Must specify add_config or remove_config", "")

        if config is not None:
            ini.read(config)

        if not sysid and ini.has_option("main", "sysid"):
            sysid=ini.get("main","sysid")

        if not url and ini.has_option("main", "url"):
            url = ini.get("main", "url")  

        if not osname and ini.has_option("main", "osname"):
            osname = ini.get("main", "osname")  

        if addConfig==1 and not sysid:
            from biosHdr import getSystemId
            sysid = getSystemId()

        if addConfig==1 and not osname:
            raise getopt.GetoptError("must set OS name", "")

        if addConfig==1 and not url:
            raise getopt.GetoptError("must set url", "")

        # Read through the file to see what the current status is
        needUpdate=0
        foundYum=0
        foundYumMirror=0
        fd=open(up2dateSources,"r")
        while 1:
            line = fd.readline()
            if line=="": break
            if line.startswith("yum %s" % repoNameFirstPart):
                try:
                    (yum, repo, uri) = line.split(None, 2)
                except ValueError:
                    needUpdate=1
                    continue

                needUpdate = checkLine(yum, repo, uri, venid, sysid, osname, url)
                foundYum=foundYum+1

            if line.startswith("yum-mirror %s" % repoNameFirstPart):
                try:
                    (yum, repo, uri) = line.split(None, 2)
                except ValueError:
                    needUpdate=1
                    continue

                needUpdate = checkLine(yum, repo, uri, venid, sysid, osname, url)
                foundYumMirror=foundYumMirror+1
        fd.close()

        doClean=0
        doAdd=0
        if addConfig==1 and (needUpdate or foundYum!=1 or foundYumMirror!=1):
            doClean=1
            doAdd=1

        elif addConfig==0 and (foundYum or foundYumMirror):
            doClean=1

        if backup and (doClean or doAdd):
            backupFiles(up2dateSources)

        if doClean:
            cleanConfig(up2dateSources)

        if doAdd:
            doAddConfig(up2dateSources, url, venid, sysid, osname)


    except (getopt.GetoptError,), e:
        # print help information and exit:
        print __doc__
        print
        print e
        sys.exit(2)

    sys.exit(0)


if __name__ == "__main__":
    main()


More information about the firmware-tools-devel mailing list