Coming Up for Air

Yum Pseudo-Transactions

If you follow me on Twitter, you may have seen that I’ve been looking for a good media player. This long, painful process involved installing project Foo, along with its 87 dependencies, only to see that I didn’t like it, then running into the same thing with Bar and Baz. Now I have a ton of packages installed that I don’t need, which will irritate me as I think of all the wasted disk space. This morning, I decided to give Cinnamon a try. After seeing its long list of dependencies, I decided to tackle that problem and (the sadly named) tx_yum was born.

Before I get to the script, I’ll admit that when it comes to maintaining the RPM database, my skills are sorely lacking. Beyond installing and uninstalling, I can’t do much. :) This script, then, may not be optimal, but it was a fun, quick effort, so lay off! :P That said,


APP_NAME=`basename $0`

function usage() {
    echo "The arguments to use are:"
    echo "  -l : List all transactions"
    echo "  -r <title> : Rollback transaction <title>"
    echo "  -t <title> : Set a title for this transaction (default is first package name)"
    exit 1;

function list_txns() {
    ls $TXN_DIR | sed -e 's/\.txn$//'
    exit 0

while getopts hilr:t:y opt
    case "$opt" in
        h) usage ;;
        l) list_txns ;;
        t) TITLE=$OPTARG ;;
        r) ROLLBACK=true ; TITLE=$OPTARG ;;
        *) usage ;;

if [ "$TITLE" == "" ] ; then
    shift 2


if [ "$ROLLBACK" == "" ] ; then
    echo "Preparing transaction for $TITLE"

    sudo yum install -y $* 2>&1 | tee /tmp/tx_yum.log

    if [ $? == 0 ] ; then
        mkdir -p $TXN_DIR
        cat /tmp/tx_yum.log | grep "Installing :" | cut -f 5 -d " " > "$TXN_FILE"
        echo "Installation failed. No transaction recorded"
        exit -1

    rm /tmp/tx_yum.log
    if [ ! -e $TXN_FILE ] ; then
        echo "Transaction not found"
        exit -1
        echo "Rolling back transaction $TITLE"
        cat "$TXN_FILE" | xargs sudo yum remove -y
        rm "$TXN_FILE"

This script takes a parameters. With -t, the user can specify the name of the "transaction" that will be recorded. If this option is not specified, the name of the first package is used. Transactions can be "rolled back" using -r, which takes the name of the transacation to roll back, and transactions can be listed with -l.

For example:

$ tx_yum cinnamon
<lots of deleted output>
$ tx_yum -l
$ tx_yum -r cinnamon
Rolling back transaction cinnamon
$ tx_yum -l

That’s all there is to it. While I’m sure there are flaws and inefficiencies, it seems to work pretty well. If you have an improvement, feel free to hit the comments below. Either way, I hope you find it as helpful as I have so far. Now to find those orphaned RPMs…​ :)

tags: Linux


Sample quote

Quote source