Created
November 25, 2011 15:13
-
-
Save LadyNamedLaura/1393742 to your computer and use it in GitHub Desktop.
Script to convert yum repositories to smart channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
basearch=`rpmdb --showrc | awk '{ if($2=="_arch") print $3}'` | |
releasever=`rpm -q --qf "%{version}\n" --whatprovides redhat-release` | |
debug="false" | |
smart_channel="smart channel -y " | |
smart_mirror="smart mirror " | |
mirrors="false" | |
pre="false" | |
force="--show" | |
suffix="" | |
repos="" | |
repofile="/etc/yum.repos.d/*.repo" | |
usage() { | |
echo -n $2; | |
echo -e " | |
Usage: `basename $0` [OPTION]... [FILE]... | |
\`yum2smart' converts yum(8) repositories into smart(8) channels | |
uses .repo files refered to by FILE or /etc/yum.repos.d/*.repo when no FILE is given | |
Examples: | |
`basename $0` # Import all yum repos into smart if no corresponding channel is found | |
`basename $0` -cm # Clear all smart configuration and import the default channels and their mirrors | |
`basename $0` -a i386 -r 14 -s i386-f14 /etc/yum.repos.d/fedora.repo | |
# Import the default fedora channel for fedora 14 i386 as fedora-i386-f14 ... | |
Options: | |
-a, --arch=<arch> set \$basearch to <arch> (Default: $basearch) | |
-c, --clean clear out all mirrors and channels from smart first. | |
-d, --dry dry run, do not perform any actions. | |
-f, --force force, override existing channels. | |
-m, --mirrors[=<true|false>] wheter or not to import mirrors, if no argument is given mirrors get enabled | |
-r, --release=<release> set \$releasever to <release> (Default: $releasever) | |
-s, --suffix=<suffix> add <suffix> to the end of the channel id. | |
-v, --verbose be a little more verbose. | |
-h, --help display this help and exit" | |
exit $1; | |
} | |
import_mirrors() { | |
$debug " Importing mirrors ..." | |
echo $1 > $tmpdir/$channel-mirrors.smart | |
$debug " downloading mirrors from $2" | |
curl -L -s -o $tmpdir/$channel.mirrors "$2" | |
if grep "metalink" $tmpdir/$channel.mirrors >> /dev/null # metalink xml file : add mirrors with preference over 90 | |
then | |
$debug " parsing mirrors as \"metalink\" xml file" | |
sed -n 's!<url .* preference="\([0-9]*\)".*>\(.*\)repodata/repomd\.xml.*</url>!\1 \2 !p' $tmpdir/$channel.mirrors | \ | |
awk '{ if ($1>90) print " " $2}' | grep -e 'http://' -e 'ftp://' >> $tmpdir/$channel-mirrors.smart | |
else # plain text file | |
$debug " parsing mirrors as plain text file" | |
grep -v "#" $tmpdir/$channel.mirrors | grep -e 'http://' -e 'ftp://' | sed 's/^/ /' >> $tmpdir/$channel-mirrors.smart | |
fi | |
$smart_mirror --add "$tmpdir/$channel-mirrors.smart" >> /dev/null | |
$debug -e " `grep -c ' ' $tmpdir/$channel-mirrors.smart` mirrors imported " | |
} | |
clean() { | |
echo -e "\nClearing out current configuration" | |
for i in 5 4 3 2 1 | |
do | |
echo -e -n "\r press ctrl+C in the next $i secconds to abort"; | |
sleep 1; | |
done | |
echo -e "\r "; | |
$debug " Reading channels" | |
$debug " `smart channel --show | grep -c '^\[.*\]$'` channels read" | |
$debug " Removing channels" | |
$smart_channel -y --remove-all > /dev/null | |
$debug " `smart channel --show | grep -c '^\[.*\]$'` channels not deleted" | |
$debug | |
$debug " Reading mirrors" | |
smart mirror --show > $tmpdir/mirrors.smart | |
$debug " `grep -c ' ' $tmpdir/mirrors.smart` mirrors read" | |
$debug " Removing mirrors" | |
$smart_mirror --remove $tmpdir/mirrors.smart > /dev/null | |
$debug " `smart mirror --show | grep -c ' '` mirrors not deleted" | |
} | |
while getopts ":-:a:cdfF:mr:s:v" opt; do | |
if [ "$opt" = "-" ] | |
then | |
opt=${OPTARG%%=*} | |
fi | |
OPTARG=${OPTARG#*=} | |
case $opt in | |
a|arch) basearch=$OPTARG ;; | |
c|clean) pre="clean";; | |
d|dry) echo -e "Running dry! no actions will be taken" | |
smart_channel="false" | |
smart_mirror="false" ;; | |
f|force) force="--remove";; | |
m|mirrors) [ "$OPTARG" != "false" ] && mirrors="import_mirrors" ;; | |
r|release) releasever=$OPTARG ;; | |
s|suffix) suffix="-$OPTARG" ;; | |
v|verbose) debug="echo" ;; | |
h|help) usage 0;; | |
\?) usage 1 "Invalid option: -$OPTARG" >&2 ;; | |
:) usage 1 "Option -$OPTARG requires an argument." >&2 ;; | |
*) usage 1 "Invalid option: --$opt" >&2 ;; | |
esac | |
done | |
for i in $@ | |
do | |
case $i in | |
-*) continue ;; | |
*) repos="$repos $i" | |
unset repofile ;; | |
esac | |
done | |
which awk curl grep sed smart rpm > /dev/null 2>&1 || yum install awk curl sed grep smart rpm | |
which awk curl grep sed smart rpm > /dev/null 2>&1 || exit 1 | |
tmpdir=`mktemp --tmpdir -d yum2smart.XXXX` | |
$pre | |
if [ ! "$repofile" ] | |
then | |
repofile="$tmpdir/repos.yum" | |
$debug "Readin from $repos" | |
cat $repos > $repofile | |
fi | |
smart channel --show > $tmpdir/channels.smart | |
for channel in `sed -n 's!^\[\(.*\)\]$!\1!p' $repofile` | |
do | |
$debug -en "\nParsing yum repo file for \"$channel\"" | |
unset name baseurl mirrorlist enabled | |
eval `sed -n -e '/^\['$channel'\]$/,/^\[.*\]$/ {/^\[.*\]$/ d; s!^#baseurl!baseurl!; s!=\(.*\)$!="\1"!g; p}' $repofile` | |
echo -e "\nImporting \"$name\"" | |
if [ "$force" = "--remove" ] || ! grep "\[$channel$suffix\]" $tmpdir/channels.smart > /dev/null | |
then | |
$smart_channel $force $channel$suffix > /dev/null 2>&1 | |
[ "$enabled" = "1" ] && $smart_channel --add $channel$suffix type="rpm-md" name="$name" baseurl="$baseurl" disabled="No" > /dev/null | |
[ "$enabled" != "1" ] && $smart_channel --add $channel$suffix type="rpm-md" name="$name" baseurl="$baseurl" disabled="Yes" > /dev/null | |
else | |
echo " not adding \"$name\" as it seems to exists already use -f to override" | |
fi | |
[ "$mirrorlist" ] && $mirrors "$baseurl" "$mirrorlist" | |
done | |
rm -rf $tmpdir; | |
$debug -e "\nDone!\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment