Version control for configuration files

Yesterday I wrote a post about setting up a Subversion server.
Subversion is a great tool for controlling versions of collections of files that are contained in a single root directory (an entire website, development of a software application, etc.). But it’s not the perfect tool for controlling the versions of single files.
So, after yesterday’s post, I now feel somewhat obliged to also document the system I use for controlling the versions of my configuration files.

Yes, I have all my configuration files under version control. I do this for 2 reasons:

  • I can easily go back to a previous version if changed settings break stuff
  • I can look up when and why I made certain changes

To ease this process, I wrote a wrapper around my text editor. Which in my case is vim, but could be any command line text editor; and maybe even graphical editors, I haven’t tested that.
You will find this Bash script below, but before you download and start using it, read these next few lines about it’s workings.

Make sure the RCS package is installed. If you can’t find it in your system’s package system, download it from the GNU website. With the last update in January 2015 it’s an old package; let’s hope it doesn’t die, because I haven’t been able to find an alternative.
Copy the script below, and save it as /usr/local/bin/vircs. It should be executable by everybody, but writable only by root:

# chmod 755 /usr/local/bin/vircs

From now on, the command you execute to edit configuration files is vircs, instead of vim, emacs, nano, or whatever you’ve been using; e.g.:

# vircs /etc/hosts

(Make sure the EDITOR environment variable is set to your favorite editor; vircs will default to vim if EDITOR is not set.)

This will create a file with the same name and suffix ‘,v‘ (‘comma-vee‘), which contains the version information; e.g. ‘/etc/hosts,v‘.

In some cases, this may be a problem. The configuration file for sudo/etc/sudoers — for example, contains the line

#includedir /etc/sudoers.d

which makes sudo include every file in the specified directory, including any files with a ‘,v’ suffix. But since sudo doesn’t recognize the RCS file format, it will then complain. Even though this is clearly a bug in Sudo, it is a reality we have to deal with.
In cases like this, create a sub-directory named ‘RCS‘ (yes, all capitals) before editing the file, and rcs will automatically store the version file in that directory.
If you’ve already created the version file, and then discover that it creates a problem (because a service won’t restart for example), create the sub-directory and move the version file into it. The service should now restart without complaining, and rcs will automatically find the version file when you re-edit the configuration file.
(BTW: cronie will even complain about this new subdirectory, which is even worse. But since it runs without any problems after having complained, we’ll just let it sob.)

The file you are editing is supposed to exist before you start editing, so if you want to check in a file that doesn’t exist yet, create it first:

$ touch ~/groceries.txt
$ vircs ~/groceries.txt

Obviously, vircs is not limited to configuration files, but can be used for all text-based files (text, HTML, JSON, etc.).

When you bring a file under version control (you edit the file for the first time using Vircs), the script will ask you for a short description of the file. This description will be displayed in the output of rlog. The size of this description is limited to 1 line; hitting [Enter] will mark the end of the description.

Each time you save a file, you are asked to add a short log message. This message is also displayed in the output of rlog, and it’s size is also limited to 1 line.

And that concludes what I wanted to say beforehand. Here’s the script.
Have fun!

(If the script is too wide for your screen, just click on it and then use your arrow keys to scroll right and left.)

#!/usr/bin/env bash

# This is vircs.
#
# Vircs is a simple wrapper around `rcs ci', `rcs co' and a text editor; this is
# probably the millionth of its kind.

# For more info, see
# https://www.ohreally.nl/2019/03/31/version-control-for-configuration-files/

################################################################################

# Copyright (c) 2018 Rob La Lau <rob@ohreally.nl>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

################################################################################

# Dependencies.
RCS=`which rcs`     || exit 1
CI="${RCS} ci"
CO="${RCS} co"
DIFF="${RCS} diff"
CLEAN="${RCS} clean"
CHGRP=`which chgrp` || exit 1
CHMOD=`which chmod` || exit 1
CHOWN=`which chown` || exit 1
FILE=`which file`   || exit 1
RM=`which rm`       || exit 1
STAT=`which stat`   || exit 1
[ -z "${EDITOR}" ] && {
    # Real men use vim. ;)
    echo "Environment variable EDITOR not set; we'll use vim."
    EDITOR=`which vim` || exit 1
}

# File to edit.
file=$@
[ -z "${file}" ] && {
    echo "Usage: $0 file"
    exit 1
}

# File exists?
[ ! -e "${file}" ] && {
    echo "File ${file} does not exist. Please create it before continuing."
    echo "If an RCS file exists, you can (re)create the original by executing"
    echo " ${CO} -u ${file},v"
    echo "Remember to set the correct permissions using 'chown', 'chgrp' and 'chmod'."
    exit 1
}

# Bail out if file ends in ',v'.
[ "${file##*,}" = "v" ] && {
    echo "You specified the RCS file. Please specify the working file."
    echo "If you do not have a working file, you can (re)create it by executing"
    echo " ${CO} -u ${file}"
    echo "Remember to set the correct permissions using 'chown', 'chgrp' and 'chmod'."
    exit 1
}

# Acceptable mime type?
mimetype=`"${FILE}" --brief --dereference --mime-type "${file}"`
[ "${mimetype%%/*}" != "text" -a "${mimetype}" != "inode/x-empty" ] && {
    echo "File ${file} is of type ${mimetype} and cannot be edited by $0."
    exit 1
}

# RCS ignores file owner, group and permissions.
# We don't, as this could break services or applications.
user=`"${STAT}" -c %u "${file}"`
group=`"${STAT}" -c %g "${file}"`
rights=`"${STAT}" -c %a "${file}"`

# Name of author, for log messages.
if [ -n "${SUDO_USER}" ]; then
    # Using `sudo'.
    author="-w${SUDO_USER}"
elif [ "${USER}" = "root" -a -n "${MAIL}" ]; then
    # When using `su', some of the user's environment is retained (but not when using `su -' !).
    # Obviously, we're making a dangerous assumption here (but for me it works).
    author="-w${MAIL##*/}"
fi

# Is file under version control?
# If so, are there any differences?
diff=`${DIFF} "${file}" 2> /dev/null`
retval=$?
# rcsdiff may have 1 of 3 return values:
# 0 : no differences found
# 1 : differences found
# 2 : rcs file or working file not found
if [ ${retval} -eq 2 ]; then
    # Initial checkin.
    echo "File ${file} is not yet under version control."
    echo "Let's check it in right away."
    echo "Please give a short (1 line) description of the file."
    echo "E.g.:"
    echo " Apache config for virtual host www.example.com"
    echo "or"
    echo " Postfix mail aliases for host `hostname`"
    echo "(This is not the log message!)"
    read -p "Description: " descr
    ${CI} -i -u ${author} -t-"${descr}" -m"Initial checkin" ${file}
    "${CHOWN}" "${user}" "${file}"
    "${CHGRP}" "${group}" "${file}"
    "${CHMOD}" "${rights}" "${file}"
elif [ ${retval} -eq 1 ]; then
    echo "There is a difference between ${file} and the last version in it's RCS file."
    echo "Please verify and correct this before continuing."
    echo
    echo "The approximate steps for correction would be (adjust to system and taste):"
    echo " mv '${file}' '${file}.changed'"
    echo " ${CO} -l '${file}'"
    echo " diff '${file}' '${file}.changed'"
    echo " # incorporate changes to be kept into '${file}'"
    echo " ${CI} -u ${author} -m'Checking in old changes.' '${file}'"
    echo " rm '${file}.changed'"
    echo "(Do not simply copy the above, but make sure you understand what it does.)"
    echo
    echo "After correction, make sure the file attributes for the working file are as follows:"
    echo "User: ${user}"
    echo "Group: ${group}"
    echo "Rights: ${rights}"
    exit 1
fi

# We can now safely delete the original (to keep co from complaining).
"${RM}" -f "${file}" || exit 1

# Checkout and lock.
${CO} -l "${file}"

# Edit.
"${EDITOR}" "${file}"

# Compare.
diff=`${DIFF} -q "${file}"`

if [ -n "${diff}" ]; then
    # Working file has changed.
    # Checkin (and checkout again, unlocked).
    echo "Please give a short (1 line) log message for this change."
    read -p "Log message: " msg
    ${CI} -u ${author} -m"${msg}" "${file}"
    echo "New version of ${file} saved."
else
    # Working file has not changed.
    # Remove unchanged locked working file, and checkout unlocked.
    ${CLEAN} -u "${file}"
    ${CO} -u "${file}"
    echo "Changes to ${file} cancelled."
fi

# Correct file attributes.
"${CHOWN}" "${user}" "${file}"
"${CHGRP}" "${group}" "${file}"
"${CHMOD}" "${rights}" "${file}"

# Done

To make full use of the potential of RCS, have a look at it’s documentation.

Dit bericht is geplaatst in Nerd stuff. Bookmark de permalink.

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

Deze website gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.