Eric Radman : a Journal

Repeatable Workspaces

In software engineering a repeatable configuration is critical to testing, but a repeatable environment is not a clean environment. A test that tries too hard to eliminate environmental differences may also be allowing the software to make assumptions that don't hold when deployed in the wild. Therefore a repeatable test harness allows the same software to be reliably tested in a variety of platforms and environments.

A similar, but not equivalent benefit exists for creating a workspace that can be re-instantiated. tmux gives you the capability to recreate the session you work in so that you can get back to work after a power outage or even after refreshing ssh-agent.

The Loop

Speaking of ssh-agent, the fist thing I want to do is assert that it's running

#!/bin/sh -x
# Start development environment in tmux

test -S ${SSH_AUTH_SOCK:-/dev} || { echo "ssh-agent not running"; exit 1; }

Next, spin up a new tmux session with some windows

session=main
editor=${EDITOR:-vim}

# start new session, disconnected
tmux new-session -s "$session" -d
for n in 0 1; do
window=$session:$n
case $n in
    0)
        tmux rename-window -t $window "saltm"
        ;;
    1)
        tmux new-window -t $window -n "salt-devel"
        ;;
esac
done

# Switch to first window and attach to session
tmux select-window -t $session:0
tmux -2 attach-session -t "$session"

I create a new window for each major project or major operation that I'm in working on. All of the tmux commands such as rename-window and split-window that work from a script provided you specify the session name and window number. Here I split virtically, ssh to a development checkout, and activate a Python virtual environment

    0)
      tmux rename-window -t $window "saltm"
      tmux send-keys -t $window "ssh ensaltm1" C-m
      tmux send-keys -t $window ". ~/local/saltenv/bin/activate" C-m
      tmux send-keys -t $window "cd ~/git/salt" C-m
      tmux split-window -t $window -h -p 50
      tmux send-keys -t $window "ssh dhmsaltm1" C-m
      tmux send-keys -t $window ". ~/local/saltenv/bin/activate" C-m
      tmux send-keys -t $window "cd ~/git/salt" C-m
      tmux select-pane -t $window.0
      ;

To assist with the testing of the script itself you may want to destroy the window after detaching

tmux -2 attach-session -t "$session"
# user exited, destroy
tmux kill-session -t "$session"

Last updated on August 09, 2017