From 0d87c48e9c1fdb63852785c84e9b8405c6472c5a Mon Sep 17 00:00:00 2001 From: Andrejus Date: Mon, 2 Mar 2020 21:14:55 +0000 Subject: [PATCH] cleanup intsall fix attempts add pyenv path after install use https repository install apt-utils set FAST_MODE in bootstrap.sh keep truthy FAST_MODE variable source bashrc after linking clean up errors further install fixes further cleanup fix fisher install chmod +x install remove source bashrc from make --- Dockerfile | 17 +++++------ Makefile | 29 ++++++++++++------- bash/.bashrc | 12 +++++--- bootstrap.sh | 33 +++++++++++++++------- fish/config.fish | 5 ++-- install.sh | 5 ++-- install/00-apt.sh | 13 +++++---- install/01-bash.sh | 4 +-- install/02-fish.sh | 36 +++++++++++++++++------- install/03-ssh.sh | 8 +++--- install/04-git.sh | 4 +-- install/10-pyenv-pkglist | 13 +++++++++ install/10-pyenv.sh | 16 +++-------- install/11-python.sh | 1 - install/12-poetry.sh | 0 install/20-docker.sh | 9 ++++-- install/21-keybase.sh | 0 install/99-apt-clean.sh | 5 ++-- install/999-screenfetch.sh | 0 test.sh | 9 ------ .python-version => tests/.python-version | 0 tests/Makefile | 11 ++++++++ utils.sh | 30 +++++++++++++++++--- 23 files changed, 165 insertions(+), 95 deletions(-) mode change 100644 => 100755 bootstrap.sh mode change 100644 => 100755 install/00-apt.sh mode change 100644 => 100755 install/01-bash.sh mode change 100644 => 100755 install/02-fish.sh mode change 100644 => 100755 install/03-ssh.sh mode change 100644 => 100755 install/04-git.sh create mode 100644 install/10-pyenv-pkglist mode change 100644 => 100755 install/10-pyenv.sh mode change 100644 => 100755 install/11-python.sh mode change 100644 => 100755 install/12-poetry.sh mode change 100644 => 100755 install/20-docker.sh mode change 100644 => 100755 install/21-keybase.sh mode change 100644 => 100755 install/99-apt-clean.sh mode change 100644 => 100755 install/999-screenfetch.sh delete mode 100644 test.sh rename .python-version => tests/.python-version (100%) create mode 100644 tests/Makefile diff --git a/Dockerfile b/Dockerfile index 167ae2e..74fbbd3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,20 +4,20 @@ from ubuntu:bionic # (usually) Cached steps # ---------------------------------------------------------------------------- # -# Set variables up, FAST_MODE to prevent git and upgrade +# Set FAST_MODE to prevent git and upgrade ENV FAST_MODE="true" ENV WORKSPACE="$HOME/workspace" # Install sudo for compatibility, git since it is skipped, make # Triple quiet mode (-qq implies -y, pipe to /dev/null) -RUN apt-get -qq update && \ - apt-get -qq install sudo git make \ - < /dev/null > /dev/null +RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections +RUN apt-get -y update \ + && apt-get -y install sudo git make # Create user with sudo priviledge ENV USER="test-user" -RUN useradd --create-home -m $USER && \ - adduser $USER sudo +RUN useradd --create-home -m $USER \ + && adduser $USER sudo RUN echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers # ---------------------------------------------------------------------------- # @@ -28,9 +28,6 @@ RUN echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers ADD --chown=test-user . "$WORKSPACE/dotfiles" WORKDIR "$WORKSPACE/dotfiles" -# Allow execution -RUN chmod +x ./bootstrap.sh - # ---------------------------------------------------------------------------- # # Install steps # ---------------------------------------------------------------------------- # @@ -44,4 +41,4 @@ RUN make install # ---------------------------------------------------------------------------- # # Run tests -RUN make test || true +CMD ["make", "test"] diff --git a/Makefile b/Makefile index 7c5f042..152bb41 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,28 @@ -.PHONY: clean +# dotfiles Makefile +# ---------------------------------------------------------------------------- # +# Local target commands (warning: affects local environment) +# ---------------------------------------------------------------------------- # +.PHONY: install clean + +# Install dotfiles locally +install: SHELL:=/bin/bash +install: + chmod +x ./bootstrap.sh + ./bootstrap.sh + +# Clean up after install clean: rm -f .dotlock -.PHONY: install -install: - bash bootstrap.sh +# ---------------------------------------------------------------------------- # +# Docker commands +# ---------------------------------------------------------------------------- # +.PHONY: build run -.PHONY: test -test: - bash test.sh - -.PHONY: build +# Build and tag latest docker image build: docker build . -t dotfiles:latest -.PHONY: run +# Run latest docker container run: docker run -it dotfiles:latest /bin/bash diff --git a/bash/.bashrc b/bash/.bashrc index fd3e09d..0f350c1 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -1,7 +1,11 @@ +# .local +export PATH="$HOME/.local/bin:$PATH" + # pyenv -export PATH="$HOME/.pyenv/bin:$PATH" -eval "$(pyenv init -)" -eval "$(pyenv virtualenv-init -)" +PYENV_ROOT="$HOME/.pyenv" +export PATH="$PYENV_ROOT/bin:$PATH" +export PATH="$PYENV_ROOT/shims:$PATH" # poetry -export PATH="$HOME/.poetry/bin:$PATH" +POETRY_ROOT="$HOME/.poetry" +export PATH="$POETRY_ROOT/bin:$PATH" diff --git a/bootstrap.sh b/bootstrap.sh old mode 100644 new mode 100755 index c2e4d67..06db354 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -8,13 +8,19 @@ # # Usage: # -# i. Run in new bash shell. +# i. Run script. # # $ bash bootstrap.sh # $ bash /path/to/bootstrap.sh # $ wget path.to/bootstrap.sh -qO - | bash # -# ii. Source into existing bash shell. +# ii. Run explicitly in a bash shell. +# +# $ bash bootstrap.sh +# $ bash /path/to/bootstrap.sh +# $ wget path.to/bootstrap.sh -qO - | bash +# +# iii. Source into existing shell. # # $ source bootstrap.sh # $ source /path/to/bootstrap.sh @@ -33,7 +39,7 @@ # @default "install.sh" # # $FAST_MODE - whether to skip git (and speed up install steps) -# @defualt unset +# @defualt unset, i.e. false # # set -o pipefail @@ -41,26 +47,31 @@ echo "setting up..." # Variables: $REPOSITORY if [ -z "$REPOSITORY" ]; then - REPOSITORY="andrejusk/dotfiles" + export REPOSITORY="andrejusk/dotfiles" fi -readonly repository_url="git@github.com:$REPOSITORY.git" +export repository_url="https://github.com/$REPOSITORY.git" echo "using repository: $repository_url" # Variables: $WORKSPACE if [ -z "$WORKSPACE" ]; then - WORKSPACE="$HOME/workspace" + export WORKSPACE="$HOME/workspace" fi -readonly dotfiles_dir="$WORKSPACE/dotfiles" +export dotfiles_dir="$WORKSPACE/dotfiles" echo "using dir: $dotfiles_dir" # Variables: $INSTALLER -if [ -z "$INSTALLER" ]; then INSTALLER="install.sh"; fi -readonly installer="$dotfiles_dir/$INSTALLER" +if [ -z "$INSTALLER" ]; then + export INSTALLER="install.sh"; +fi +export installer="$dotfiles_dir/$INSTALLER" echo "using installer: $installer" # Pull latest git if not skipped if [ -z "$FAST_MODE" ]; then + # Set to falsy variable + export FAST_MODE=false + # Ensure git is installed if ! [ -x "$(command -v git)" ]; then echo "installing git..." @@ -81,6 +92,8 @@ if [ -z "$FAST_MODE" ]; then fi # Install dotfiles +echo "installing..." cd "$dotfiles_dir" -source "$installer" +chmod +x "$installer" +"$installer" echo "done!" diff --git a/fish/config.fish b/fish/config.fish index 16e5907..7bae98d 100644 --- a/fish/config.fish +++ b/fish/config.fish @@ -1,7 +1,6 @@ # Pyenv -export PATH="$HOME/.pyenv/bin:$PATH" -eval (pyenv init -) -eval (pyenv virtualenv-init -) +setenv PYENV_ROOT "$HOME/.pyenv" +setenv PATH "$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH" # Poetry set -gx PATH $HOME/.poetry/bin $PATH diff --git a/install.sh b/install.sh index 87dacaf..29a24c9 100755 --- a/install.sh +++ b/install.sh @@ -24,7 +24,7 @@ fi touch "$install_lock_file" # Requires clean # Run all install scripts -readonly install_dir="$dotfiles_dir/install" +export install_dir="$dotfiles_dir/install" readonly script_filter="$install_dir/*.sh" # Don't escape to unwrap glob for script in $script_filter; do @@ -36,7 +36,8 @@ for script in $script_filter; do printf "\nRunning ${C_YELLOW}$script_name${C_NC}...\n${C_DGRAY}" # Run and indent output - source "$script" | indent + chmod +x "$script" + source "$HOME/.bashrc" && "$script" | indent printf "${C_NC}" # Clean up if fails diff --git a/install/00-apt.sh b/install/00-apt.sh old mode 100644 new mode 100755 index 0a39079..4b8547e --- a/install/00-apt.sh +++ b/install/00-apt.sh @@ -5,15 +5,16 @@ # Install list of packages in ./00-apt-pkglist # -# apt update, upgrade if not fast +# pre clean +clean + +# apt update, upgrade update -if [ -z "$FAST_MODE" ]; then - upgrade -fi +upgrade # Package installs -readonly package_list_file="$install_dir/00-apt-pkglist" -install_file $package_list_file +package_list_file="$install_dir/00-apt-pkglist" +install_file "$package_list_file" # Log version cat /etc/os-release diff --git a/install/01-bash.sh b/install/01-bash.sh old mode 100644 new mode 100755 index b9dd090..eb716c4 --- a/install/01-bash.sh +++ b/install/01-bash.sh @@ -5,7 +5,7 @@ # # 1. bash dotfiles are symlinked -readonly bash_source="$dotfiles_dir/bash" -readonly bash_target="$HOME" +bash_source="$dotfiles_dir/bash" +bash_target="$HOME" link_folder "$bash_source" "$bash_target" printf "bash dotfiles are linked\n" diff --git a/install/02-fish.sh b/install/02-fish.sh old mode 100644 new mode 100755 index 8d47f09..42666a6 --- a/install/02-fish.sh +++ b/install/02-fish.sh @@ -4,6 +4,7 @@ # 1. fish shell is installed # 2. fish shell is default login shell # 3. fish dotfiles are symlinked +# 4. fisher is installed # # 1. fish shell is installed @@ -12,24 +13,22 @@ if not_installed "fish"; then printf "Installing fish...\n" # Add fish repository - app_ppa "fish-shell/release-3" + add_ppa "fish-shell/release-3" update # Install fish install fish - # Install fisher - set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config - curl https://git.io/fisher --create-dirs -sLo $XDG_CONFIG_HOME/fish/functions/fisher.fish - fish -c fisher - fi printf "fish is installed\n" fish --version # 2. fish shell is default login shell -readonly fish_path="$(which fish)" -if [ "$SHELL" != fish_path ]; then +current_shell="$(getent passwd $USER | cut -d: -f7)" +fish_path="$(which fish)" +if [ "$current_shell" != "$fish_path" ]; then + + printf "setting fish as default, current shell was $current_shell\n" # Update default login shell sudo chsh -s "$fish_path" "$USER" @@ -39,7 +38,24 @@ fi printf "fish is default login shell\n" # 3. fish dotfiles are symlinked -readonly fish_source="$dotfiles_dir/fish" -readonly fish_target="$HOME/.config/fish" +fish_source="$dotfiles_dir/fish" +fish_target="$HOME/.config/fish" link_folder "$fish_source" "$fish_target" printf "fish dotfiles linked\n" + +# 4. fisher is installed +XDG_CONFIG_HOME="$HOME/.config" +fisher_location="$XDG_CONFIG_HOME/fish/functions/fisher.fish" +if ! [ -f "$fisher_location" ]; then + + printf "Installing fisher...\n" + + # Install fisher + curl https://git.io/fisher --create-dirs -sLo "$fisher_location" + +fi +printf "fisher is installed, updating...\n" +fish -c "fisher" +fish -c "fisher --version" + +export XDG_CONFIG_HOME diff --git a/install/03-ssh.sh b/install/03-ssh.sh old mode 100644 new mode 100755 index 4ebb187..9932025 --- a/install/03-ssh.sh +++ b/install/03-ssh.sh @@ -6,9 +6,9 @@ # # 1. ssh key exists -readonly ssh_target="$HOME/.ssh" -readonly ssh_key="$ssh_target/id_rsa" -readonly ssh_pub="$ssh_key.pub" +ssh_target="$HOME/.ssh" +ssh_key="$ssh_target/id_rsa" +ssh_pub="$ssh_key.pub" if [ ! -f "$ssh_key" ]; then printf "generating ssh key...\n" ssh-keygen -t rsa -b 4096 -f "$ssh_key" @@ -16,7 +16,7 @@ fi printf "ssh key exists\n" # 2. ssh dotfiles are symlinked -readonly ssh_source="$dotfiles_dir/ssh" +ssh_source="$dotfiles_dir/ssh" link_folder "$ssh_source" "$ssh_target" printf "ssh dotfiles are symlinked\n" cat "$ssh_pub" diff --git a/install/04-git.sh b/install/04-git.sh old mode 100644 new mode 100755 index 117a067..5d64ac8 --- a/install/04-git.sh +++ b/install/04-git.sh @@ -5,8 +5,8 @@ # # 1. git dotfiles are symlinked -readonly git_source="$dotfiles_dir/git" -readonly git_target="$HOME" +git_source="$dotfiles_dir/git" +git_target="$HOME" link_folder "$git_source" "$git_target" printf "git dotfiles linked\n" git --version diff --git a/install/10-pyenv-pkglist b/install/10-pyenv-pkglist new file mode 100644 index 0000000..22850e6 --- /dev/null +++ b/install/10-pyenv-pkglist @@ -0,0 +1,13 @@ +build-essential +libssl-dev +libbz2-dev +libreadline-dev +libsqlite3-dev +llvm +libncurses5-dev +libncursesw5-dev +xz-utils +tk-dev +libffi-dev +liblzma-dev +python-openssl diff --git a/install/10-pyenv.sh b/install/10-pyenv.sh old mode 100644 new mode 100755 index 6a7ba49..2d4465c --- a/install/10-pyenv.sh +++ b/install/10-pyenv.sh @@ -5,30 +5,22 @@ # # 1. pyenv is installed +export PYENV_ROOT="$HOME/.pyenv" if not_installed "pyenv"; then printf "Installing pyenv...\n" # Install pyenv prerequisites # see https://github.com/pyenv/pyenv/wiki/common-build-problems - install make build-essential libssl-dev zlib1g-dev libbz2-dev \ - libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \ - xz-utils tk-dev libffi-dev liblzma-dev python-openssl git + pyenv_list_file="$install_dir/10-pyenv-pkglist" + install_file "$pyenv_list_file" - # Add to install path - add_path "$HOME/.pyenv/bin" - # Install pyenv # see https://github.com/pyenv/pyenv-installer run "https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer" \ "bash" - refresh - - eval "$(pyenv init -)" - eval "$(pyenv virtualenv-init -)" fi printf "pyenv is installed, upgrading...\n" -readonly pyenv_root=$(pyenv root) -git --git-dir="$pyenv_root" pull +git --git-dir="$PYENV_ROOT/.git" pull pyenv --version diff --git a/install/11-python.sh b/install/11-python.sh old mode 100644 new mode 100755 index 41c9dd4..bc4024e --- a/install/11-python.sh +++ b/install/11-python.sh @@ -11,7 +11,6 @@ if not_installed "pip3"; then pyenv install 3.7.0 pyenv global 3.7.0 - pyenv local 3.7.0 fi printf "python3 and pip3 are installed, upgrading...\n" diff --git a/install/12-poetry.sh b/install/12-poetry.sh old mode 100644 new mode 100755 diff --git a/install/20-docker.sh b/install/20-docker.sh old mode 100644 new mode 100755 index f1b5551..6e2655c --- a/install/20-docker.sh +++ b/install/20-docker.sh @@ -8,9 +8,13 @@ # # 1. docker is installed +DOCKER_FOLDER="$HOME/.docker" if not_installed "docker"; then printf "Installing docker...\n" + + # Create folder + mkdir -p "$DOCKER_FOLDER" # Requirements install apt-transport-https ca-certificates curl gnupg-agent \ @@ -28,8 +32,8 @@ if not_installed "docker"; then install docker-ce # Chown - sudo chown "$USER":"$USER" "$HOME/.docker" -R - sudo chmod g+rwx "$HOME/.docker" -R + sudo chown "$USER":"$USER" "$DOCKER_FOLDER" -R + sudo chmod g+rwx "$DOCKER_FOLDER" -R fi printf "docker is installed\n" @@ -43,7 +47,6 @@ if not_installed "docker-compose"; then # Docker-compose pip3 install --user docker-compose - fi printf "docker-compose is installed, upgrading\n" pip3 install --upgrade docker-compose diff --git a/install/21-keybase.sh b/install/21-keybase.sh old mode 100644 new mode 100755 diff --git a/install/99-apt-clean.sh b/install/99-apt-clean.sh old mode 100644 new mode 100755 index 10af6e3..2a5db67 --- a/install/99-apt-clean.sh +++ b/install/99-apt-clean.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash -# Clean up -sudo apt-get -y autoremove -sudo apt-get -y autoclean +# post clean +clean diff --git a/install/999-screenfetch.sh b/install/999-screenfetch.sh old mode 100644 new mode 100755 diff --git a/test.sh b/test.sh deleted file mode 100644 index 0092d91..0000000 --- a/test.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -# -# Invokes all test scripts. -# -set -euo pipefail - -cd ./tests -poetry install -poetry run pytest diff --git a/.python-version b/tests/.python-version similarity index 100% rename from .python-version rename to tests/.python-version diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..0ab6e6a --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,11 @@ +# dotfiles tests Makefile +# ---------------------------------------------------------------------------- # +# Test commands +# ---------------------------------------------------------------------------- # +.PHONY: test + +# Import .bashrc and run test suite +test: SHELL:=/bin/bash +test: + poetry install + poetry run pytest diff --git a/utils.sh b/utils.sh index 85c1c97..32254e6 100644 --- a/utils.sh +++ b/utils.sh @@ -3,12 +3,22 @@ # Alias commands and utilities. # +# ---------------------------------------------------------------------------- # +# Helper functions +# ---------------------------------------------------------------------------- # + +clean() { + sudo apt-get clean +} + update() { sudo apt-get update -qq } # Non-interactive upgrade +# Skip if FAST_MODE is defined upgrade() { + [ "$FAST_MODE" != false ] && return DEBIAN_FRONTEND=noninteractive \ sudo apt-get dist-upgrade -qq \ -o Dpkg::Options::="--force-confdef" \ @@ -23,18 +33,20 @@ install() { # @arg $1 package list file to install install_file() { - sudo apt-get install -qq $(cat $1) + sudo apt-get install -fqq $(cat $1) refresh } # @arg $1 repository to add -app_ppa() { +add_ppa() { sudo add-apt-repository -y ppa:$1 } # @arg $1 url to add add_key() { - curl -fsSL $1 | sudo apt-key add - + APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=true \ + curl -fsSL $1 \ + | sudo apt-key add - } # @arg $1 URL to run @@ -71,7 +83,17 @@ add_path() { refresh } -# Colors +# ---------------------------------------------------------------------------- # +# Function exports +# ---------------------------------------------------------------------------- # + +export -f clean update upgrade install install_file add_ppa add_key run \ + link_folder indent not_installed refresh add_path + +# ---------------------------------------------------------------------------- # +# Shell colours +# ---------------------------------------------------------------------------- # + C_BLACK='\033[0;30m' C_DGRAY='\033[1;30m' C_RED='\033[0;31m'