From 6691b62891c2b0643f7f8d00e80b332b30b1fc97 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 15 Sep 2018 20:52:17 +0200 Subject: [PATCH] Initial commit --- README.md | 33 +++++++++++++++++ lincon | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 README.md create mode 100755 lincon diff --git a/README.md b/README.md new file mode 100644 index 0000000..9acaef8 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# Lincon - Link config files into a repository + +Lincon is a script that helps you to manage configuration files of linux machines using a versioning system (like git). + +Its two main functions are adding existing config files to your repository structure and deploying files from your repository to the system. + +Lincon does NOT handle versioning for you. You could use git for example. + +## Usage + +To get started, create your config repository and copy the lincon script into it. In the following examples we assume that your repo is located in `/home/user/configs`. + +Whenever you want to execute an operation with lincon, make sure that you are in your repositories root. + +In order to add an existing config file to your repo, just call + +```bash +$ ./lincon add /etc/someprogram/whatever.conf +``` + +Note that you always have to pass the absolute path to the script. + +lincon now moves the config file to `/home/user/configs/etc/someprogram/whatever.conf` and creates a hardlink at `/etc/someprogram/whatever.conf`. + +If you want to deploy a config file from your repo to the system, just call + +```bash +$ ./lincon deploy /etc/otherprogram/settings.conf +``` + +The command will create a hardlink at `/etc/otherprogram/settings.conf`, which points to the file in your repository (in this case `/home/user/configs/etc/otherprogram/settings.conf`. + +In case there is already a config file with the same name, the file will get backed up to `/etc/otherprogram/settings.conf.bak`. diff --git a/lincon b/lincon new file mode 100755 index 0000000..df47928 --- /dev/null +++ b/lincon @@ -0,0 +1,106 @@ +#!/bin/bash + +# Check if two hardlinks point to same file +aresame() +{ + [ "$(ls -i $1 | awk '{print $1;}')" == "$(ls -i $2 | awk '{print $1;}')" ] +} + +hardlink() +{ + ln $(readlink -f $1) $2; +} + +# Move $1 to $2 +pull() +{ + mkdir -p $(dirname $2); + mv $1 $2; +} + +# Pull $1 to $2 and link $2 into $1 +pulllink() +{ + pull $1 $2 + hardlink $2 $1 +} + +# Ask a question and return the answer +y() +{ + local yn; + read -p "$1" yn; + case "$yn" in + y|Y ) return 0;; + n|N ) return 1;; + * ) return $(y "$1");; + esac +} + +# Start of the script + +if (( $# != 2 )); then + echo "Usage: lincon " + exit 0 +fi + +OPERATION=$1 +ORIG=$2 +BAK=$ORIG.orig +TARGET=$(dirname "$0")$ORIG + +case $OPERATION in + + # Add a file to versioning + add) + # Error if original does not exist + if [ ! -e $ORIG ]; then + echo "Error: File $ORIG does not exist."; + exit 1; + fi + + # There is already a file in the repo + if [ -e $TARGET ]; then + # File is already linked + if aresame $ORIG $TARGET; then + echo "File $ORIG is already versioned."; + exit; + fi + # Repo contains different copy + echo "Versioned copy of $ORIG already exists."; + if ! y "Overwrite it (y/n)?"; then + exit; + fi + fi + pulllink $ORIG $TARGET + echo "File $ORIG versioned." + ;; + + # Link a file from repo + deploy) + # Check if versioned copy exists + if [ ! -e $TARGET ]; then + echo "Error: No versioned copy of $ORIG found."; + exit 1; + fi + + # Backup any original files + if [ -e $ORIG ]; then + if aresame $ORIG $TARGET; then + echo "File $ORIG is already versioned."; + exit; + else + echo "Backup original file to $BAK."; + mv $ORIG $BAK + fi + fi + # Link it, baby! + hardlink $TARGET $ORIG + echo "File $ORIG deployed." + ;; + *) + echo "Usage: repoconf "; + exit 0; + ;; +esac +