Gheek.net

December 16, 2010

Locally import RPMs to Spacewalk channel from CLI

Filed under: linux, shell scripts, spacewalk — lancevermilion @ 7:43 pm

Sometimes you don’t have a full version of oracle and thus it is not possible to hold a complete set of distro DVDs (updates, extras, base, epel, etc). So the solution I use is get a copy of the repo on my local disk and then push them into a channel. I wrote a script to do this in a prettier fashion since I normally don’t download directly to the distro tree.

Spacewalk is configured according to this website. The only exception is I have local repos for everything and I used spacewalk-client 1.2 instead of 1.0.
http://wiki.centos.org/HowTos/PackageManagement/Spacewalk

input file would look something like this.
osad-5.9.44-1.el5.noarch.rpm
python-ethtool-0.3-5.el5.i386.rpm
python-hwdata-1.2-1.el5.noarch.rpm
rhncfg-5.9.33-1.el5.noarch.rpm
rhncfg-actions-5.9.33-1.el5.noarch.rpm
rhncfg-client-5.9.33-1.el5.noarch.rpm
rhncfg-management-5.9.33-1.el5.noarch.rpm
rhn-check-1.2.15-1.el5.noarch.rpm
rhn-client-tools-1.2.15-1.el5.noarch.rpm
rhn-custom-info-5.4.3-1.el5.noarch.rpm
rhn-kickstart-5.4.5-1.el5.noarch.rpm
rhn-kickstart-common-5.4.5-1.el5.noarch.rpm
rhn-kickstart-virtualization-5.4.5-1.el5.noarch.rpm
rhnlib-2.5.28-1.el5.noarch.rpm
rhnmd-5.3.7-1.el5.noarch.rpm
rhnsd-4.9.7-1.el5.i386.rpm
rhn-setup-1.2.15-1.el5.noarch.rpm
rhn-setup-gnome-1.2.15-1.el5.noarch.rpm
rhn-virtualization-common-5.4.15-1.el5.noarch.rpm
rhn-virtualization-host-5.4.15-1.el5.noarch.rpm
spacewalk-backend-libs-1.2.74-1.el5.noarch.rpm
spacewalk-certs-tools-1.2.2-1.el5.noarch.rpm
spacewalk-koan-0.2.15-1.el5.noarch.rpm
yum-rhn-plugin-1.2.7-1.el5.noarch.rpm

#!/bin/bash

# variables
INPUTFILE="$1"
CHANNEL="$2"
DISTROTREE="$3"
CHOICE="$4"
SWUSER='spaceuser'
SWPASS='spacepass'
SWHOST='localhost'
SWPROTO='http'

if [ "$4" == '' ]; then
CHOICE='r'
fi

function VARIABLES ()
{
echo "#
# VARIABLES
#"
echo "Input file: \"${INPUTFILE}\""
echo "Spacewalk Channel: \"${CHANNEL}\""
if [ -d "${DISTROTREE}" ]; then
echo "Distro-tree directory: \"${DISTROTREE}\""
else
echo "FAIL: Distro-tree directory DOES NOT exist (${DISTROTREE})."
exit 1;
fi
}

function RPMCOPY ()
{
echo "#
# START RPM COPY
#"
echo "FOUND COPIED RPM"
for i in `cat ${INPUTFILE}`
do
FILE=`locate ${i} | grep "/${i}" | grep -v '${DISTROTREE}'`
if [ -f "${FILE}" ]; then
if [ -f "${DISTROTREE}/${i}" ]; then
#echo "File \"${i}\" is already in \"${DISTROTREE}\"."
echo "YES EXISTS ${i}"
else
cp $FILE ${DISTROTREE}/${i}
if [ -f "${DISTROTREE}/${i}" ]; then
echo "YES YES ${i}"
else
cp $FILE ${DISTROTREE}/${i}
echo "YES NO ${i}"
fi
fi
else
echo "NO NO ${i}"
fi
done

echo "#
# STOP RPM COPY
#"
}

function CHANNELCHECK ()
{
# Let the user know if the package is already in the channel
echo "
#
# START CHANNEL CHECK
#"
echo "IN_CHANNEL RPM"
for r in `rhnpush -l --channel=${CHANNEL} -u ${SWUSER} -p ${SWPASS} | awk '{print $1"-"$2"-"$3"."$5".rpm"}' | sed "s/\['//g" | sed "s/',//g" | sed "s/'//g"`
do
FIND=`find ${DISTROTREE} -name ${r}`
if [ "${FIND}" == '' ]; then
echo "NO ${r}"
else
echo "YES ${r}"
fi
done
echo "#
# STOP CHANNEL CHECK
#"
}

function PUSH2CHANNEL ()
{
echo -n "Are you sure? [y/N]: "
read ANS
case ${ANS} in
*y*) echo "Running: rhnpush --channel=${CHANNEL} --server=${SWPROTO}://${SWHOST}/APP --nosig --dir=${DISTROTREE} -v -u ${SWUSER} -p ${SWPASS}"
rhnpush --channel=${CHANNEL} --server=${SWPROTO}://${SWHOST}/APP --nosig --dir=${DISTROTREE} -v -u ${SWUSER} -p ${SWPASS}
cd ${DISTROTREE}
echo "Creating repo"
createrepo ${DISTROTREE}
;;
*) echo "Skipping push to Spacewalk."
echo "Exiting!!"
exit;
;;
esac
}

function CASE ()
{
case ${CHOICE} in
*[rR]*) RPMCOPY
echo -n "Do you wish to check if these RPM are in the Spacewalk Channel? [c/N]: "
read CHOICE
CASE;;
*[cC]*) CHANNELCHECK
echo -n "Do you wish to push these into a Spacewalk Channel? [p/N]: "
read CHOICE
CASE;;

*[pP]*) PUSH2CHANNEL;;
*[nN]*) echo "Exiting!!"
exit;;
esac
}

VARIABLES
CASE

Create a free website or blog at WordPress.com.