Nanorcs: Ultrasimplistic Configuration File Revision Control

I present to you nanorcs, my ultrasimplistic configuration file revision control method. It’s nothing significant but at least it’s something to post about.

What is nanorcs? It’s a very simple bash script wrapper around the nano, the cute GNU version of pico and RCS, a basic but functional revision control software package. The script is used in place of a call to ‘nano’ when editing a file and it automates the tasks related to change management of configuration files including tasks such as check in/out, and prompting the user when a discrepancy between the current file version and the RCS version exist.

Basic Prerequisites

While few prerequisites exist for nanorcs, most of them should be quite simple to satisfy on any Linux system:

  • nano is installed
  • RCS is installed

Nanorcs also makes some assumptions regarding the path to certain executables: it assumes the required executables are in your path!. I know I could have made it “cleaner” by providing a variable in which to set the paths, but this script was 99% written for me, on my systems… where the RCS and nano commands are in my path… and besides, if you’re reading this you can probably figure out how to edit it to suit you anyways! This is just an example of how this might be done, not necessarily a working solution for your systems.

Functionality

Nanorcs does a few “nice” things:

  • The script checks to see if the file you are editing already exists in RCS and if not, does the initial check in before any changes are made.
  • The script will notify you if the local copy of the file you are editing is different from the latest revision in RCS. It then provides a mechanism to check in the local version to RCS so you do not clobber any changes made that may have not been commited to RCS. If you do not wish to commit the current changes, the script asks if you instead want to pull down the latest version from RCS and work with that one. Summed up, if the local version is different from RCS you have the option to clobber the local version or commit the local version to RCS.
  • After you are done editing the file with nano, you are asked if you wish to commit the changes. If you answer yes, the changes are commited and the script exits. If you do not wish to commit the edits to RCS, you are asked if you want to pull down the latest version from RCS again or if you want to leave the changes. Leaving the changes without committing them is bad form but there could be a good reason to do so (you realize after exiting the editor that you didn’t make all the changes you wanted or the changes were only slightly incorrect and it would be easier to update the current version than to pull down the last version from RCS and start over).

Using nanorcs

Using nanorcs is easy, but you always have to remember the key point that it must be invoked against a file that is in your current working directory! The basic calling convention is (assing the script is in your path!):

root@box:/etc/ssh
# nanorcs sshd_config

The Script

And finally, the script itself:

#!/bin/bash
#
# Name:     nanorcs
# Desc:     RCS wrapper around nano for editing config files
#
 
# Check if file exists in RCS
#
if [ ! -e $1,v ]; then
  echo "First time editing, checking file into RCS..."
  ci -l $1,v $1
else
  # Check if RCS version is different from local version
  rcsdiff $1,v $1 > /dev/null
  if [ "$?" -eq 1 ]; then
    echo -n "File $1 is not the same as RCS version, clobber it (y/n)? "
    read clobber
    if [ "$clobber" = "y" ]; then
      co -l $1,v $1
    else
      echo -n "Commit current version to RCS (y/n)? "
      read commitfirst
      if [ "$commitfirst" = "y" ]; then
        rcs -l $1,v
        ci -l $1,v $1
      else
        echo "Nothing left to do, exiting..."
        exit
      fi
    fi
  else
    co -l $1,v $1
  fi
fi
 
nano $1
 
echo -n "Commit changes to RCS (y/n)? "
read commitfinal
if [ "$commitfinal" = "y" ]; then
  echo "Committing changes..."
  ci -u $1,v $1
else
  rcsdiff $1,v $1 > /dev/null
  if [ "$?" ]; then
    echo -n "Do you want to revert to RCS version of $1 (y/n)? "
    read revert
    if [ "$revert" = "y" ]; then
      echo "Reverting to RCS version..."
      co -u $1,v $1
    else
      echo "Changes made have NOT been committed!"
    fi
  else
    echo "File unchanged from RCS version, BYE!"
  fi
fi
 
#
# END
#

That is all. Nanorcs is my ultra-simplistic solution to the minor hassle of managing configuration file revision. For more advanced users and admins, things like cfengine and pikt are certainly light years ahead of this in every way imaginable, but for those of you who don’t necessarily need or want a configuration engine, this type of solution seems to be suitable enough.

Leave a Reply

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