Coming Up for Air

Yum Pseudo-Transactions

Wednesday, Sep 19, 2012 |

Yum Pseudo-Transactions

Jason Lee 2012-09-19

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,

#!/bin/bash

APP_NAME=`basename $0`
TXN_DIR="$HOME/.$APP_NAME"

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
do
    case "$opt" in
        h) usage ;;
        l) list_txns ;;
        t) TITLE=$OPTARG ;;
        r) ROLLBACK=true ; TITLE=$OPTARG ;;
        *) usage ;;
    esac
done

if [ "$TITLE" == "" ] ; then
    TITLE=$1
else
    shift 2
fi

TXN_FILE="$TXN_DIR/$TITLE.txn"

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"
    else
        echo "Installation failed. No transaction recorded"
        exit -1
    fi

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

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
cinnamon
$ 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... :)