Initial commit

This commit is contained in:
Paul Schaub 2018-09-15 20:52:17 +02:00
parent dfa10731da
commit 6691b62891
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 139 additions and 0 deletions

33
README.md Normal file
View file

@ -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`.

106
lincon Executable file
View file

@ -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 <add|deploy> <path/to/file>"
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 <add|deploy> <path/to/file>";
exit 0;
;;
esac