rename machines to hosts

This commit is contained in:
Denis Lehmann 2021-12-08 11:33:33 +01:00
parent f3c923bca6
commit 6f04744a11
6 changed files with 27 additions and 27 deletions

View file

@ -4,30 +4,30 @@
After trying several approaches and switching between different setups, this method finally met all of my requirements:
- As simple as possible
- Multiple machines are managed in one repository
- Identical configurations for one program on several machines only need to be adjusted in one place
- Different configurations for one program on several machines are no problem
- Multiple hosts are managed in one repository
- Identical configurations for one program on several hosts only need to be adjusted in one place
- Different configurations for one program on several hosts are no problem
- Set everything up with one command
- Updating configurations shall require nothing more than =git pull=
This is a reference repository which contains a bash script (=dotlink=) and some example dotfiles for two machines (=host1= and =host2=).
This is a reference repository which contains a bash script (=dotlink=) and some example dotfiles for two hosts (=host1= and =host2=).
*Warning*: If you want to try this make a backup of your dotfiles!
The script doesn't overwrite existing files but you never know.
** Concept
The concept is based on symlinks and two directories, =common= and =machines=.
The concept is based on symlinks and two directories, =common= and =hosts=.
The =machines= directory contains subdirectories for all machines on which dotfiles are managed.
They need to match the /hostname/ of the machine (in this repository =host1= and =host2=) and mimic the corresponding =$HOME= directories.
The =hosts= directory contains subdirectories for all hosts on which dotfiles are managed.
They need to match the /hostname/ of the host (in this repository =host1= and =host2=) and mimic the corresponding =$HOME= directories.
The =common= directory contains configs which are present on multiple machines.
The =common= directory contains configs which are present on multiple hosts.
It doesn't follow any specific structure, you can choose what suits your setup.
Subdirectories with program names, followed by the configuration files (in this repository only =mpv=) make probably the most sense but its up to you.
The =common= directory should never contain symlinks.
Symlinks from the =machines= directory to the =common= directory make the configs available on multiple machines and they can be adjusted in one place (see =mpv= in this repository).
Symlinks from the =hosts= directory to the =common= directory make the configs available on multiple hosts and they can be adjusted in one place (see =mpv= in this repository).
Here is the tree output from this repository:
@ -37,7 +37,7 @@
│   └── mpv
│   ├── input.conf
│   └── mpv.conf
├── machines
├── hosts
│   ├── host1
│   │   ├── .config
│   │   │   └── mpv -> ../../../common/mpv/
@ -52,17 +52,17 @@
The *mpv* configuration is shared between hosts, =host1= has an *OfflineIMAP* configuration in his home directory and =host2= has a *beets* configuration in his =.config= directory.
Every file from every =machines/<hostname>= directory can then be linked to the corresponding path into the home directory of the machine.
Every file from every =hosts/<hostname>= directory can then be linked to the corresponding path into the home directory of the host.
This can be done by hand or with help of the =dotlink= script.
If you updated a configuration somewhere else just call =git pull= and thats it.
** Script usage
When the =dotlink= script is executed, all files from the =machines/<hostname>= directory, which matches the current /hostname/, are linked to their destination in the =$HOME= directory.
When the =dotlink= script is executed, all files from the =hosts/<hostname>= directory, which matches the current /hostname/, are linked to their destination in the =$HOME= directory.
Executing the script is only neccessary when new files were added which are not linked yet.
The script can be executed from everywhere, it is just important that it's stored next to a =machines= directory like in this repository.
The script can be executed from everywhere, it is just important that it's stored next to a =hosts= directory like in this repository.
#+begin_example
Usage: dotlink [OPTIONS]
@ -76,14 +76,14 @@
** Add configuration files for only one host
Add the files to =machines/<hostname>/<path_in_home>=.
If you want to keep all configuration files in the =common= directory and just use symlinks in the =machines= directory, you can follow the instructions below.
Add the files to =hosts/<hostname>/<path_in_home>=.
If you want to keep all configuration files in the =common= directory and just use symlinks in the =hosts= directory, you can follow the instructions below.
** Add configuration files for multiple hosts
It is important that the links from the =machines= directory to the =common= directory are relative.
Follow these steps to add new configurations for multiple machines:
It is important that the links from the =hosts= directory to the =common= directory are relative.
Follow these steps to add new configurations for multiple hosts:
1. Add the files somewhere to the =common= directory
2. Execute =ln -rs common/<config_or_directory> machines/<hostname>/<path_in_home>= for every machine on which the files should be present
3. Execute the =dotlink= script on every modified machine
2. Execute =ln -rs common/<config_or_directory> hosts/<hostname>/<path_in_home>= for every host on which the files should be present
3. Execute the =dotlink= script on every modified host

16
dotlink
View file

@ -58,13 +58,13 @@ done
# Get current dotfile directory for later linking
dotfiles="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Check if dotfiles for machine exist
if [ ! -d "$dotfiles/machines/$HOSTNAME" ]; then
error "No dotfiles for machine $text_bold$HOSTNAME$text_reset found, make sure the directory $text_bold$dotfiles/machines/$HOSTNAME$text_reset exists"
# Check if dotfiles for host exist
if [ ! -d "$dotfiles/hosts/$HOSTNAME" ]; then
error "No dotfiles for host $text_bold$HOSTNAME$text_reset found, make sure the directory $text_bold$dotfiles/hosts/$HOSTNAME$text_reset exists"
fi
# Get dotfiles for current machine
cd "$dotfiles/machines/$HOSTNAME"
# Get dotfiles for current host
cd "$dotfiles/hosts/$HOSTNAME"
files=( $(find -L -type f -printf '%P\n'))
if [ "$unlink" == true ]; then
@ -78,7 +78,7 @@ for file in "${files[@]}"; do
# Unlink files
if [ "$unlink" == true ]; then
if [ -L "$HOME/$file" ] && [ "$(readlink $HOME/$file)" == "$dotfiles/machines/$HOSTNAME/$file" ]; then
if [ -L "$HOME/$file" ] && [ "$(readlink $HOME/$file)" == "$dotfiles/hosts/$HOSTNAME/$file" ]; then
rm "$HOME/$file"
log "Unlinked $text_bold$HOME/$file$text_reset"
@ -96,7 +96,7 @@ for file in "${files[@]}"; do
# Check if target is a link
if [ -L "$HOME/$file" ]; then
if [ "$(readlink $HOME/$file)" != "$dotfiles/machines/$HOSTNAME/$file" ]; then
if [ "$(readlink $HOME/$file)" != "$dotfiles/hosts/$HOSTNAME/$file" ]; then
warning "$text_bold$HOME/$file$text_reset is a link but doesn't point to this repository, it will not be linked"
continue
fi
@ -114,7 +114,7 @@ for file in "${files[@]}"; do
mkdir -p "$(dirname $HOME/$file)"
# Link file
ln -s "$dotfiles/machines/$HOSTNAME/$file" "$HOME/$file"
ln -s "$dotfiles/hosts/$HOSTNAME/$file" "$HOME/$file"
log "Linked $text_bold$dotfiles/$file$text_reset to $text_bold$HOME/$file$text_reset"
fi
fi