From 762f3ef58c2c7de0ddebf827b75cc60bf060b407 Mon Sep 17 00:00:00 2001 From: bretello Date: Mon, 7 Dec 2020 19:45:06 +0100 Subject: [PATCH] install: use ansible - ansible/playbooks/setup.yml can be used to install all dotfiles - a test setup is included in the `dev` folder with arch and debian images --- README.md | 22 +++- ansible/ansible-packages.yml | 25 +++++ ansible/ansible.cfg | 5 + ansible/playbooks/setup.yml | 93 +++++++++++++++++ dev/Dockerfile.archlinux | 15 +++ dev/Dockerfile.debian | 16 +++ dev/Makefile | 25 +++++ dev/README.md | 27 +++++ dev/add_community_general.sh | 2 + dev/ansible-hosts | 2 + dev/entrypoint.sh | 5 + dev/id_ed25519.pub | 1 + install.sh | 189 ----------------------------------- 13 files changed, 235 insertions(+), 192 deletions(-) create mode 100644 ansible/ansible-packages.yml create mode 100644 ansible/ansible.cfg create mode 100644 ansible/playbooks/setup.yml create mode 100644 dev/Dockerfile.archlinux create mode 100644 dev/Dockerfile.debian create mode 100644 dev/Makefile create mode 100644 dev/README.md create mode 100644 dev/add_community_general.sh create mode 100644 dev/ansible-hosts create mode 100644 dev/entrypoint.sh create mode 100644 dev/id_ed25519.pub delete mode 100755 install.sh diff --git a/README.md b/README.md index c1265fe..4c37db9 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ A set of zsh dotfiles based on antibody and oh-my-zsh │ │     └── ANSI color escapes │ ├── functions.sh │ │     └── contains function definitions (sources `~/.dotfiles_functions`) -│ ├── install.sh -│ │     └── main install script +│ ├── ansible +│ │     └── ansible playboooks/config │ └── brethil_dotfile.sh: │       └── This file should be sourced by your `.zshrc` ├──────── antibody plugins ──────── @@ -55,8 +55,24 @@ A set of zsh dotfiles based on antibody and oh-my-zsh ## Installation +With ansible: + +```bash + [ bash dev/add_community.general.sh ] # if community.general is not installed + ansible-playbook -v -i [-e fix_annoyances=true] ansible/playbooks/setup.yml +``` + +[use this with a docker container](dev/README.md) + +Use `-e fix_annoyances=true` to fix some small annoyances (only run once) + +Manually (this only installs the zsh configuration): + +```bash git clone https://git.decapod.one/brethil/dotfiles ~/.dotfiles - cd .dotfiles && bash install.sh + echo 'DOTFILES=~/.dotfiles' >> ~/.zshrc + echo 'source $DOTFILES/brethil_dotfile.sh' +``` A self-update mechanism is included. It asks for confirmation to pull the latest changes from the git repo every two weeks. This also updates the git-sourced repositories installed diff --git a/ansible/ansible-packages.yml b/ansible/ansible-packages.yml new file mode 100644 index 0000000..197bcac --- /dev/null +++ b/ansible/ansible-packages.yml @@ -0,0 +1,25 @@ +packages: + - "vim" + - "curl" + - "git" + - "zsh" + - "grc" + - "ccze" + - "bmon" + - "mtr" + - "tmux" + - "tree" + - "byobu" + - "htop" + - "nmap" + - "bmon" +packages_debian: + - "pylint" + - "ipython3" + - "python3-pip" + - "python3-setuptools" + - "virtualenv" +packages_archlinux: + - "ipython" + - "python-pylint" + - "python-virtualenv" diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg new file mode 100644 index 0000000..d897379 --- /dev/null +++ b/ansible/ansible.cfg @@ -0,0 +1,5 @@ +[defaults] +forks = 10 +strategy=free +pipelining = true +internal_poll_interval = 0.001 diff --git a/ansible/playbooks/setup.yml b/ansible/playbooks/setup.yml new file mode 100644 index 0000000..6a52829 --- /dev/null +++ b/ansible/playbooks/setup.yml @@ -0,0 +1,93 @@ +--- +- hosts: all + become: true + vars_files: + - ../ansible-packages.yml + vars: + DOTFILES: "$HOME/.dotfiles" + + tasks: + - name: Install required system packages (debian) + apt: + install_recommends: no + update_cache: yes + pkg: "{{ packages + packages_debian }}" + when: ansible_facts['os_family'] == "Debian" + + - name: Install required system packages (debian) + pacman: + update_cache: yes + name: "{{ packages + packages_archlinux }}" + when: ansible_facts['os_family'] == "Archlinux" + + - name: dotfiles repo + git: + repo: https://git.decapod.one/brethil/dotfiles + dest: "{{ DOTFILES }}" + version: master + + - name: antibody + shell: | + set -o pipefail + curl -sfL https://git.io/antibody | sh -s - -b /usr/local/bin + antibody bundle "{{ DOTFILES }}/antibody_plugins.txt" + args: + executable: /bin/bash + creates: /usr/local/bin/antibody + + - name: zsh as default shell + user: + name: "{{ ansible_user }}" + shell: /usr/bin/zsh + + - name: zsh config + shell: | + echo "export DOTFILES={{ DOTFILES }}" >> "$HOME/.zshrc" + echo 'source $DOTFILES/brethil_dotfile.sh' >> "$HOME/.zshrc" + args: + creates: "~{{ ansible_user }}/.zshrc" + + - name: dotfiles symlink + file: + src: "{{ DOTFILES }}/{{ item.key }}" + dest: "{{ item.value }}" + state: link + force: yes + loop: "{{ files | dict2items }}" + vars: + files: + "ackrc": "~/.ackrc" + "vim/vimrc": "~/.vimrc" + "pdbrc.py": "~/.pdbrc.py" + # "ipython": "~/.ipython" # FIXME: ipython config is more complex + "ansible.cfg": "~/.ansible.cfg" + + - name: git config + community.general.git_config: + name: "include.path" + value: "{{ DOTFILES }}/gitconfig" + + - name: annoyances + shell: | + ## Fix scrolling in byobu + if [[ $(uname) == "Darwin" ]]; then + sed -i '' 's/set -g terminal-overrides/#set -g terminal-overrides/' /usr/share/byobu/profiles/tmux + else + sed -i 's/set -g terminal-overrides/#set -g terminal-overrides/' /usr/share/byobu/profiles/tmux + fi + sed 's|ls \\|#ls \\|' /etc/grc.zsh + args: + executable: /bin/bash + when: fix_annoyances is true + + - name: directories + file: + dest: "{{ item }}" + state: directory + mode: 0700 + loop: "{{ directories }}" + vars: + directories: + - "$HOME/bin" + - "$HOME/projects" + - "$HOME/git" diff --git a/dev/Dockerfile.archlinux b/dev/Dockerfile.archlinux new file mode 100644 index 0000000..998e427 --- /dev/null +++ b/dev/Dockerfile.archlinux @@ -0,0 +1,15 @@ +FROM archlinux:latest +ENV TERM=xterm-256color + +RUN pacman --noconfirm -Syu && pacman --noconfirm -Sy \ + python \ + openssh \ + && rm -rf /var/cache/pacman/* + +# uncomment to hardcode ssh_host keys in the image +# RUN ssh-keygen -A +COPY id_ed25519.pub /root/.ssh/authorized_keys + +WORKDIR /root/ +COPY entrypoint.sh / +CMD ["bash", "/entrypoint.sh"] diff --git a/dev/Dockerfile.debian b/dev/Dockerfile.debian new file mode 100644 index 0000000..179f1b6 --- /dev/null +++ b/dev/Dockerfile.debian @@ -0,0 +1,16 @@ +FROM debian:unstable +ENV TERM=xterm-256color + +RUN apt-get update && apt-get install -y --no-install-recommends \ + openssh-server \ + python python3 && \ + rm -rf /var/cache/apt/* /etc/ssh/ssh_host_* +# ssh host keys are generated by entrypoint, remove rm /etc/ssh/ssh_host* +# to hardcode ssh host keys into the image + +RUN mkdir /run/sshd +COPY id_ed25519.pub /root/.ssh/authorized_keys + +WORKDIR /root/ +COPY entrypoint.sh / +CMD ["bash", "/entrypoint.sh"] diff --git a/dev/Makefile b/dev/Makefile new file mode 100644 index 0000000..7378517 --- /dev/null +++ b/dev/Makefile @@ -0,0 +1,25 @@ +SHELL=/bin/bash + +all: list_targets + +list_targets: + echo "List of targets:" + @grep '^[^#[:space:]].*:' Makefile + +debian: clean build-debian run +arch archlinux: clean build-arch run + +build-debian: + docker build -t brethil/dotfiles:dev -f Dockerfile.debian . + +build-arch: + docker build -t brethil/dotfiles:dev -f Dockerfile.archlinux . + +run: + docker run -d --name=dotfiles-dev brethil/dotfiles:dev + echo "*** Started container, listening on ${BOLD}${WHITE}`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dotfiles-dev`:22${CLEAR}" + docker logs -f dotfiles-dev + +clean: + docker stop dotfiles-dev || echo "no containers to stop" + docker rm dotfiles-dev || echo "no containers to remove" diff --git a/dev/README.md b/dev/README.md new file mode 100644 index 0000000..198d4c3 --- /dev/null +++ b/dev/README.md @@ -0,0 +1,27 @@ +# dev + +This directory contains some useful dockerfiles/makefiles that can be used +to test the deployment ansible playbooks. + +```bash +make arch # this builds a docker image based on archlinux and runs it +make debian # this builds a docker image based on debian and runs it +``` + +## Workflow + +Start a builder in one shell: + +```bash +while true; do make; done +``` + +_check for the docker IP of the started container_. + +Test the playbook in another shell: + +```bash +ansible-playbook -v -i ./ansible-hosts playbooks/setup.yml +``` + +note: the ip address in `ansible-hosts` might have changed from `172.18.0.2`. diff --git a/dev/add_community_general.sh b/dev/add_community_general.sh new file mode 100644 index 0000000..c777359 --- /dev/null +++ b/dev/add_community_general.sh @@ -0,0 +1,2 @@ +#!/bin/bash +ansible-galaxy collection install community.general # for git_config diff --git a/dev/ansible-hosts b/dev/ansible-hosts new file mode 100644 index 0000000..22283ff --- /dev/null +++ b/dev/ansible-hosts @@ -0,0 +1,2 @@ +[dev] +172.18.0.2 ansible_user=root ansible_python_interpreter=python3 DOTFILES=~/.dotfiles diff --git a/dev/entrypoint.sh b/dev/entrypoint.sh new file mode 100644 index 0000000..08ea653 --- /dev/null +++ b/dev/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash +if [ ! -f /etc/ssh/ssh_host_ed25519_key.pub ]; then + ssh-keygen -A +fi +/usr/sbin/sshd -D -e diff --git a/dev/id_ed25519.pub b/dev/id_ed25519.pub new file mode 100644 index 0000000..f9a36c8 --- /dev/null +++ b/dev/id_ed25519.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILyIgoGSyuRjxvW+2SBWYJF2dMAVX+3d5h8TpKmIVpuk brethil@distruzione.org diff --git a/install.sh b/install.sh deleted file mode 100755 index 29e6400..0000000 --- a/install.sh +++ /dev/null @@ -1,189 +0,0 @@ -#!/bin/bash -## brethil's dotfiles installation script -## 20 June 2016 - -# Get the location for the dotfiles -DOTFILES=$PWD - -PACKAGES="vim zsh antibody git grc ccze bmon mtr tmux byobu htop" - -function setup_git { - # set up some git stuff - git config --global include.path $DOTFILES/gitconfig -} - -function install_vimrc { - # TODO: check if vim-plug is installed - if [ -f "$HOME/.vimrc" ]; then - mv "$HOME/.vimrc"{,.bak} && echo "Backed up old vimrc" - fi - ln -s "$DOTFILES/.vimrc" "$HOME/.vimrc" -} - -function setup_ackrc { - ln -s "$DOTFILES/ackrc" "$HOME/.ackrc" -} - -function setup_ipython { - python -m pip install pip ipython || python -m pip install --user pip ipython - ipython -c "1+1" # run ipython once so that we are sure that the profile directories exist - ln -s "$DOTFILES/ipython/profile_default/ipython_config.py" "$HOME/.ipython/profile_default/" - ln -s "$DOTFILES/ipython/profile_default/startup" "$HOME/.ipython/profile_default/startup" -} - -# Add an ssh config file with: -# - Connection multiplexer for faster multiple connections -# - Keep connections alive (avoid timeout disconnections) -function create_ssh_config { - echo "# Configuring ssh..." - - ssh_config="$HOME/.ssh/config" - - if [ ! -f $HOME/.ssh/id_rsa ]; then - echo "Creating ssh key (4096bit)..." - - echo "Enter ssh-key comment (leave empty for default: user@host)" - read comment - if [[ $comment ]]; then - ssh-keygen -t ed25519 -C "$comment" - else - ssh-keygen -t ed25519 - fi - # fix permissions - chmod 0700 "$HOME/.ssh" - fi - - if [[ -f $ssh_config ]]; then - until [[ $modifyssh == "y" || $modifyssh == "n" ]]; do - echo "Do you want to modify the existing ssh config? (New values will be appended) (y/n)" - read modifyssh - done - fi - - exec 3>&1 # save stdout file descriptor - - # add the ssh rc that symlinks the used SSH_AUTH_SOCK. This will be executed on every ssh login. - # The idea is that if the user is logging in using ssh -A, the symlink will point to the correct - # location of the ssh auth socket and the remote ssh agent will be used. - # SSH_AUTH_SOCK is declared in brethil_dotfile.sh - exec 1>>~/.ssh/rc - echo '#!/bin/bash' - echo 'if test "$SSH_AUTH_SOCK" ; then' - echo ' ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock' - echo 'fi ' - chmod 755 ~/.ssh/rc - - exec 1>>$ssh_config # redirect everything below to the $ssh_config file - if [[ "$modifyssh" == "n" ]]; then - echo "Did not modify $ssh_config." - exec 1>&3 # restore stdout - return - fi - echo "# brethil's dotfiles setup start" - echo 'TCPKeepAlive=yes' - echo 'ServerAliveCountMax=6' - echo - echo "## Uncomment to enable compression for all ssh sessions" - echo '#Compression=yes' - echo - echo '## Uncomment the following to enable ssh ControlMaster and ssh session persistence' - echo '#ControlMaster autoask # ask for confirmation before using a shared connection' - echo '#ControlMaster auto # do not ask for confirmation' - echo '#ControlPath ~/.ssh/%r@%h:%p' - echo '#ControlPersist yes' - echo - echo 'Host *' - echo ' ServerAliveInterval 300' - echo - echo '## Enable the following if you want to use the rmate textmate remote' - echo "#Host *" - echo "# RemoteForward 52698 localhost:52698" - echo - echo '## Enable the following if you want to use a reverse ssh tunnel to use mecp command on remote hosts' - echo "#Host *" - echo "# Remoteforward 2222 localhost:22" - echo - echo '# end of brethil dotfiles setup #' - - exec 1>&3 # restore stdout - echo ".ssh/ssh_config configured. Edit it to enable custom options:" - echo "- Compression" - echo "- Remote forwarding remote:2222->localhost:22 (revere tunnel to scp back to the original host using mecp)" - echo "# End of ssh config." -} - -function fix_annoyances { - ## Fix scrolling in byobu - if [[ $(uname) == "Darwin" ]]; then - sed -i '' 's/set -g terminal-overrides/#set -g terminal-overrides/' /usr/share/byobu/profiles/tmux - else - sed -i 's/set -g terminal-overrides/#set -g terminal-overrides/' /usr/share/byobu/profiles/tmux - fi - - # remove ls from the grc.zsh config: # TODO: fix this on MacOS - sed 's|ls \\|#ls \\|' /etc/grc.zsh - - # TODO: add iptables, docker to grc.zsh -} - - -# First setup -function brethil_dotfiles_setup { - antibody &>/dev/null || (echo "Please install antibody then continue." 1>&2 && echo "Other useful packages: $PACKAGES" && exit) - bin="$HOME/bin" - projects="$HOME/projects" - git="$HOME/git" - mkdir -p "$bin" "$projects" "$git" && echo "Created dirs $bin, $projects, $git" - - # prepare .zshrc - cp "$HOME/.zshrc"{,.pre-brethil-dotfiles} - exec 3>&1 # save stdout - exec 1>>"$HOME/.zshrc" - echo -e "\n\n# brethil's dotfiles:" - echo "export DOTFILES=$DOTFILES" - echo "source \$DOTFILES/brethil_dotfile.sh" - echo -e "# End of brethil's dotfiles\n\n" - exec 1>&3 # restore stdout - - antibody bundle "$DOTFILES/antibody_plugins.txt" - antibody update - - fix_annoyances - setup_vim - # Create ssh config - create_ssh_config - # git config - setup_git - # ipython profile - setup_ipython - # pdbpp rc - setup_pdbprc - # ackrc - setup_ackrc - - ZSH="$(antibody path robbyrussell/oh-my-zsh)" - ln -s "$DOTFILES/brethil.zsh-theme" "${ZSH}/themes/" # TODO: improve this - ln -s "$DOTFILES/brethil-minimal.zsh-theme" "${ZSH}/themes/" -} - - -function main(){ - set -e - set -o pipefail - # Setup - # brethil_dotfiles_setup - source "$DOTFILES/colors.sh" - - echo -e "$BOLD$GREEN Install complete!" - echo "" - echo "$WHITE Set \$DOTFILES to $DOTFILES" - echo -e "$RED Functions definitions:$WHITE \$DOTFILES/functions.sh\n\tyou can add your own functions in ~/.dotfiles_functions" - echo -e "$RED Aliases definitions:$WHITE \$DOTFILES/aliases.sh\n\tyou can add your own aliases in ~/.dotfiles_aliases)" - echo -e "$RED Colors definitions:$WHITE \$DOTFILES/colors.sh" - echo -e "$CLEAR" - echo "More zsh plugins can be added using antibody, adding them at \$DOTFILES/antibody_plugins.txt" - - echo "Type '. ~/.zshrc' or 'exec zsh -l' to source the new configuration." -} - -main