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,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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:

1
2
3
4
5
6
7
8
$ 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…​ :)

tags: Linux

Quotes

Sample quote

Quote source