replace ansible with bootstrap script

This commit is contained in:
Fernando Schauenburg 2021-01-03 17:24:48 +01:00
parent 10cb54744c
commit b16ca6a626
30 changed files with 198 additions and 231 deletions

View file

@ -1,6 +0,0 @@
[defaults]
interpreter_python = auto_silent
inventory = ./inventory
nocows = True
roles_path = ./roles

198
bootstrap Executable file
View file

@ -0,0 +1,198 @@
#!/bin/sh
set -eu
DRY_RUN=false
DEFAULT_GIT_USER="Fernando Schauenburg"
DEFAULT_GIT_EMAIL="fernando@schauenburg.me"
XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$XDG_CONFIG_HOME}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$home/.local/share}"
DIRS=$(cat <<EOF
${HOME}/.local/bin
${HOME}/.local/etc/git
${HOME}/.ssh
${XDG_CONFIG_HOME}/alacritty
${XDG_CONFIG_HOME}/dircolors
${XDG_CONFIG_HOME}/git
${XDG_CONFIG_HOME}/mintty
${XDG_CONFIG_HOME}/python
${XDG_CONFIG_HOME}/readline
${XDG_CONFIG_HOME}/tmux
${XDG_CONFIG_HOME}/vim
${XDG_CONFIG_HOME}/vim/autoload
${XDG_DATA_HOME}/bash
${XDG_DATA_HOME}/bash-completion/completions
${XDG_DATA_HOME}/less
${XDG_DATA_HOME}/python
${XDG_DATA_HOME}/vim
${XDG_DATA_HOME}/vim/plugged
EOF
)
DOTFILES=$(cat <<EOF
files/bashrc ${HOME}/.bash_profile
files/bashrc ${HOME}/.bashrc
files/ssh_config ${HOME}/.ssh/config
files/alacritty.yml ${XDG_CONFIG_HOME}/alacritty/alacritty.yml
files/gitconfig ${XDG_CONFIG_HOME}/git/config
files/gitignore ${XDG_CONFIG_HOME}/git/ignore
files/inputrc ${XDG_CONFIG_HOME}/readline/inputrc
files/minttyrc ${XDG_CONFIG_HOME}/mintty/config
files/plug.vim ${XDG_CONFIG_HOME}/vim/autoload/plug.vim
files/python-startup.py ${XDG_CONFIG_HOME}/python/startup.py
files/tmux-colors.conf ${XDG_CONFIG_HOME}/tmux/tmux-colors.conf
files/tmux.conf ${XDG_CONFIG_HOME}/tmux/tmux.conf
files/vimrc ${XDG_CONFIG_HOME}/vim/vimrc
EOF
)
rst=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
heading() { printf '%s\n' "${blue}===== $1 ==========${rst}"; }
info() { printf '%s\n' "${green}${1}${rst}"; }
warn() { printf '%s\n' "${yellow}${1}${rst}"; }
error() { printf '%s\n' "${red}${1}${rst}"; }
directory_present() { # Make sure directory $1 exists.
if [ -d "$1" ]; then
info "OK: $1"
else
warn "creating $1"
$DRY_RUN || mkdir -p "$1"
fi
}
file_absent() { # Make sure file $1 does not exist.
if [ ! -f "$1" ]; then
info "OK: $1"
else
warn "removing $1"
$DRY_RUN || rm "$1"
fi
}
file_touch() { # Make sure file $1 exists (content is irrelevant).
if [ -f "$1" ]; then
info "OK: $1"
else
warn "creating $1"
$DRY_RUN || touch "$1"
fi
}
file_link() { # Make sure $1 is a link to $2.
target="$(realpath "$2")"
if [ "$(readlink "$1")" = "$target" ]; then
info "OK: $1"
else
warn "linking $1 -> $target"
$DRY_RUN || ln -sf "$target" "$1"
fi
}
file_content() { # Ensure $1 and $2 contents are equal, updating $1 if needed.
if diff "$1" "$2" >/dev/null 2>&1; then
info "OK: $1"
else
warn "overwriting $1 with $2:"
cat "$2"
$DRY_RUN || cp -f "$2" "$1"
fi
}
task_gather_user_info() {
heading "Gather user information"
printf 'git name [%s]: ' "$DEFAULT_GIT_USER"
read -r git_user
printf 'git e-mail [%s]: ' "$DEFAULT_GIT_EMAIL"
read -r git_email
}
task_remove_bash_profile() {
heading "Remove pre-installed bash profile"
for f in .bash_logout .profile; do
file_absent "$HOME/$f"
done
}
task_ensure_directories() {
heading "Ensure existence of directories used by dotfiles"
echo "$DIRS" | while read -r d; do directory_present "$d"; done
}
task_link_dotfiles() {
heading "Link dotfiles"
echo "$DOTFILES" | while read -r dest src; do
file_link "$src" "$dest"
done
}
task_deploy_templates() {
heading "Deploy templates"
temp_git="$(mktemp)"
cat >"$temp_git" <<EOF
# *************************************
# * DO NOT EDIT THIS FILE *
# *************************************
#
# This file was generated by the bootstrap script and any changes will be
# overwritten the next time the it is run. For local settings, use this
# instead:
#
# ~/.local/etc/git/config
#
EOF
git config -f "$temp_git" user.name "${git_user:-$DEFAULT_GIT_USER}"
git config -f "$temp_git" user.email "${git_email:-$DEFAULT_GIT_EMAIL}"
file_content "$HOME/.local/etc/git/config.host" "$temp_git"
}
task_link_local_commands() {
heading "Link local commands"
for cmd in bin/*; do
file_link "$HOME/.local/bin/$(basename "$cmd")" "$cmd"
done
}
task_link_dircolors() {
heading "Link dircolors"
for f in dircolors/*; do
file_link "$XDG_CONFIG_HOME/dircolors/$(basename "$f")" "$f"
done
}
task_make_logins_silent() {
heading "Make logins silent"
file_touch "$HOME/.hushlogin"
}
task_install_vim_plugins() {
heading "Install vim plugins"
if command -v vim >/dev/null 2>&1; then
warn "Installing vim plugins"
$DRY_RUN || vim -nes -u "$XDG_CONFIG_HOME/vim/vimrc" -c 'PlugInstall | qall!'
else
error "vim is not installed; skipping plugin installation..."
fi
}
main() {
task_gather_user_info
task_remove_bash_profile
task_ensure_directories
task_link_dotfiles
task_deploy_templates
task_link_local_commands
task_link_dircolors
task_make_logins_silent
task_install_vim_plugins
}
main

View file

@ -1,5 +0,0 @@
localhost
[all:vars]
ansible_connection=local

View file

@ -1,15 +0,0 @@
- name: Make myself at home :)
hosts: all
vars_prompt:
- name: git_user
private: no
default: Fernando Schauenburg
- name: git_email
private: no
default: fernando@schauenburg.me
roles:
- dotfiles
- packages

View file

@ -1,112 +0,0 @@
---
- name: remove pre-installed bash profile
file:
path: "{{ item }}"
state: absent
loop:
- ~/.bash_logout
- ~/.profile
- name: ensure existence of directories used by dotfiles
file:
path: "{{ item }}"
state: directory
loop:
- ~/.config/alacritty
- ~/.config/dircolors
- ~/.config/git
- ~/.config/mintty
- ~/.config/python
- ~/.config/readline
- ~/.config/tmux
- ~/.config/vim
- ~/.config/vim/autoload # vim plugin manager
- ~/.local/bin # local commands
- ~/.local/etc/git # local git config
- ~/.local/share/bash # for bash history
- ~/.local/share/bash-completion/completions # custom bash completions
- ~/.local/share/less # less history
- ~/.local/share/python # python history
- ~/.local/share/vim # viminfo
- ~/.local/share/vim/plugged # vim plugins
- ~/.ssh
- name: link dotfiles
file:
src: "{{ role_path }}/files/{{ item.src }}"
dest: "{{ item.dest }}"
state: link
force: yes
loop:
- { src: alacritty.yml, dest: ~/.config/alacritty/alacritty.yml }
- { src: bashrc, dest: ~/.bash_profile }
- { src: bashrc, dest: ~/.bashrc }
- { src: gitconfig, dest: ~/.config/git/config }
- { src: gitignore, dest: ~/.config/git/ignore }
- { src: inputrc, dest: ~/.config/readline/inputrc }
- { src: minttyrc, dest: ~/.config/mintty/config }
- { src: plug.vim, dest: ~/.config/vim/autoload/plug.vim }
- { src: python-startup.py, dest: ~/.config/python/startup.py }
- { src: ssh_config, dest: ~/.ssh/config }
- { src: tmux-colors.conf, dest: ~/.config/tmux/tmux-colors.conf }
- { src: tmux.conf, dest: ~/.config/tmux/tmux.conf }
- { src: vimrc, dest: ~/.config/vim/vimrc }
- name: deploy templates
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- src: gitconfig.host.j2
dest: ~/.local/etc/git/config.host
- name: link local commands
file:
src: "{{ item }}"
dest: ~/.local/bin/{{ item | basename }}
state: link
force: yes
with_fileglob:
- "{{ role_path }}/files/bin/*"
- name: link dircolors
file:
src: "{{ item }}"
dest: ~/.config/dircolors/{{ item | basename }}
state: link
force: yes
with_fileglob:
- "{{ role_path }}/files/dircolors/*"
- block:
- name: hushlogin exists?
stat:
path: ~/.hushlogin
register: hush
- name: create hushlogin
when: hush.stat.exists == false
file:
path: ~/.hushlogin
state: touch
- block:
- name: vim plugins installed?
find:
paths: ~/.local/share/vim/plugged
file_type: directory
register: vim_plugins
- name: install vim plugins
when: vim_plugins.matched|int == 0
shell: vim -nes -u ~/.config/vim/vimrc -c 'PlugInstall | qall!'
register: result_vim_plugins
failed_when: result_vim_plugins.rc != 0

View file

@ -1,19 +0,0 @@
# *************************************
# * DO NOT EDIT THIS FILE *
# *************************************
#
# This file was generated by Ansible and any changes will be overwritten the
# next time the playbook is run. For local settings, use this instead:
#
# ~/.local/etc/git/config
#
[user]
name = {{ git_user }}
email = {{ git_email }}
{% if ansible_os_family == 'Darwin' %}
[credential]
helper = osxkeychain
{% endif %}

View file

@ -1,74 +0,0 @@
---
- name: install apt packages
when: ansible_facts['os_family']|lower == "debian"
become: yes
apt:
state: latest
update_cache: yes
name:
- bash
- bash-completion
- curl
- exa
- exuberant-ctags
- ffmpeg
- git
- git-crypt
- git-lfs
- gpg
- htop
- nmap
- psmisc
- python3
- python3-pip
- shellcheck
- sqlite
- stow
- tmux
- tree
- vifm
- vim
- wget
- youtube-dl
- name: install homebrew packages
when: ansible_facts['os_family']|lower == "darwin"
homebrew:
state: latest
update_homebrew: yes
name:
- bash
- bash-completion
- coreutils
- ctags
- curl
- exa
- exiftool
- ffmpeg
- findutils
- git
- git-crypt
- git-lfs
- gnu-sed
- gpg
- grep
- gzip
- htop
- imagemagick
- jupyter
- make
- nmap
- pstree
- python3
- reattach-to-user-namespace
- shellcheck
- sqlite
- stow
- tmux
- tree
- vifm
- vim
- wget
- youtube-dl