a big hand for rfe

Here are some tools that I’ve put together that make a huge difference to a clunky rfe workflow. Bored already? Don’t worry, I’ve written the script already: tl;dr

Imagine power cycling a DICE server with redundant PSUs, using our lovely power bar control files. You don’t know where it’s installed, so you have to search for it. Thank goodness for the -xf script:

$ rfe -xf fpdu/myserver
s90.outlets:outlet 3 myserver on
s91.outlets:outlet 3 myserver on

Great. But now we have to open and edit each of these by hand:

$ rfe -xf fpdu/s90.outlets
outlet 1 thingy on
outlet 2 another on
outlet 3 myserver off⌶
outlet 4 [...]
…then…
$ rfe -xf fpdu/s91.outlets
outlet 1 thingy on
outlet 2 another on
outlet 3 myserver off⌶
outlet 4 [...]

and check the console, and again…

$ rfe -xf fpdu/s90.outlets
outlet 3 myserver off⌶
…and…
$ rfe -xf fpdu/s91.outlets
outlet 3 myserver off⌶
[...]

much easier to use my new rfeall script:

#!/bin/bash
# rfe edit all matching maps
search=$*; map=${search%/*}
maps=$(rfe -xf "$search" \
    |& awk -F ':' '/^rfe:/{exit;}{print $1;}' \
    | sort | uniq | xargs)
if [[ -n $maps ]]; then
    args=""
    for m in $maps; do args="${args}${map}/`filename ${m}` "; done
    rfe -S $args
else
    echo "No matches found for $search." >&2
    exit 1;
fi

which allows a far simpler edit:

$ rfeall fpdu/myserver

Oh. But we still have to edit two files, twice…


outlet 3 myserver off⌶
[...]
outlet 3 myserver off⌶
and relaunch, and…
outlet 3 myserver on⌶
[...]
outlet 3 myserver on⌶

So how much further can we take this?

Well, for a start I don’t want to have to navigate to line three every time. So let’s save the tedious effort of searching four times for the same thing. Turns out to be as easy as writing your own editor. Wait, what?

/usr/bin/vim -c 'call search("myserver")' $*

Maybe a slight exaggeration. But it needs to be a real, callable thing if you’re going to make it your $EDITOR, thus:

# rfe edit all matching maps; seek to match:
[...]
for m in $maps; do args="${args}${map}/${m} "; done
editr=/tmp/rfeall.$$
echo "/usr/bin/vim -c 'call search(\"$file\")' \$*" > $editr && chmod 700 $editr
EDITOR=$editr rfe -S $args
[...]

vim now opens at precisely the right location, give or take a word. But we still have to change those words by hand. Time for some vim scripting…

function! SwitchOn()
    s/o\(ff\|n\) *$/on/
endfunction
function! SwitchOff()
    s/o\(ff\|n\) *$/off/
endfunction
nmap   \O  :call SwitchOn()
nmap   \o  :call SwitchOff()

My vim’s not so sharp, so I have a nagging feeling there’s a nicer way to do this, but putting this into my .vimrc is enough to allow me to use two keys to turn the machine on the current line \On or \off.

So… you can now do your dual-PSU power cycle with approximately twenty keystrokes from the first time you hit <rtn>:


$ rfeall fpdu/myserver
\o :wq
\o :wq
…check your console, hit up, return…
\O :wq
\O :wq

all done. Since rfeall uses the same maps as rfe, it’s moderately useful to allow the rfe tab completion to work for the new script, too. In your ~/.bash_completion file (you have one, don’t you?):

complete -o nospace -F _rfe rfeall

Yes, I suppose could automate even the last step by setting ‘sed’ as my editor, but there’s a limit, I think – I trust myself little enough to turn off the right machine without delegating it to my dubious pattern-matching skills…

2 thoughts on “a big hand for rfe

Leave a Reply

Your email address will not be published. Required fields are marked *