From 21757767d28d8a028de8a4d6c759bd3208564e5c Mon Sep 17 00:00:00 2001 From: Andrejus Date: Sat, 11 Jul 2020 20:59:48 +0100 Subject: [PATCH] Rewrite install script --- Makefile | 11 +++----- install.pl | 41 ++++++++++++++++++++++++++++++ install.sh | 53 --------------------------------------- install/00-apt.sh | 1 + install/01-bash.sh | 4 +-- install/02-fish.sh | 2 +- install/03-ssh.sh | 1 + install/04-git.sh | 1 + install/10-pyenv.sh | 1 + install/11-python.sh | 1 + install/12-poetry.sh | 1 + install/13-nvm.sh | 1 + install/14-yarn.sh | 1 + install/15-java.sh | 1 + install/16-vim.sh | 1 + install/17-elm.sh | 1 + install/30-docker.sh | 1 + install/32-firebase.sh | 1 + install/33-aws.sh | 1 + install/34-terraform.sh | 1 + install/98-apt-clean.sh | 1 + install/99-screenfetch.sh | 1 + install/utils.sh | 20 +++++++++------ setup.pl | 15 ++++++++--- 24 files changed, 88 insertions(+), 75 deletions(-) create mode 100755 install.pl delete mode 100755 install.sh diff --git a/Makefile b/Makefile index b452f6a..322adc1 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,11 @@ -SHELL := /bin/bash # ---------------------------------------------------------------------------- # -# Local commands +# Local commands # ---------------------------------------------------------------------------- # .PHONY: clean +# @arg $TARGET binary to install all: - # Install dotfiles - ./bootstrap.sh - -clean: - # Remove all temporary files and artefacts - rm -rf .dotlock tmp + ./install.pl # ---------------------------------------------------------------------------- # # Docker commands diff --git a/install.pl b/install.pl new file mode 100755 index 0000000..b453640 --- /dev/null +++ b/install.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Cwd; + +# Prevent running as root +if ($< == 0) { + print "Failed: Running as root. Please run as user, not sudo\n"; + exit (1); +} + +# Generate unique logfile +my $uuid = `uuidgen`; +chomp $uuid; +my $log_path = "$ENV{'HOME'}/.dotfiles.$uuid.log"; +print "Logs: $log_path\n"; + + +my $dir = getcwd; +my $install_dir = "$dir/install"; +print "Installing $install_dir\n"; + +# Read scripts to be installed +opendir(DIR, $install_dir) or die "Could not open $install_dir\n"; +my @files = readdir(DIR); +closedir(DIR); +@files = sort { lc($a) cmp lc($b) } @files; + +foreach my $file (@files) +{ + # Install all scripts by default + next if($file !~ /^\d{2}-.*\.sh$/); + + print "Running $file...\r"; + my $exit_status = system("/bin/bash -l $install_dir/$file >> $log_path 2>&1"); + if ($exit_status != 0) { + print "Failure in $file, see above and logs for more detail.\n"; + exit ($exit_status); + } +} diff --git a/install.sh b/install.sh deleted file mode 100755 index 932625d..0000000 --- a/install.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env bash -set -eo pipefail - -export install_dir="$dotfiles_dir/install" -source "$install_dir/utils.sh" -printf "\nInstalling ${C_CYAN}$REPOSITORY${C_NC}" -printf " as ${C_YELLOW}$USER${C_NC}\n" - -# Prevent running as root -if [[ $USER == root ]]; then - printf "Failed: ${C_RED}Running as root${C_NC}\n" - printf "Please run as user, not ${C_YELLOW}sudo${C_NC}\n" - exit 1 -fi - -# Prevent concurrent runs -readonly install_lock_file="$dotfiles_dir/.dotlock" -if [ -f "$install_lock_file" ]; then - printf "Failed: ${C_RED}Script already running${C_NC}\n" - printf "Please wait for script to exit or ${C_YELLOW}make clean${C_NC}\n" - exit 1 -fi -touch "$install_lock_file" - -# Install all scripts by default -if [ -z "$TARGET" ]; then - export TARGET="all" -fi - -if [ "$TARGET" == "all" ]; then - scripts=($install_dir/*.sh) -else - scripts=($install_dir/*-{bash,$TARGET}.sh) -fi -for script in "${scripts[@]}"; do - - script_name="$(basename $script .sh)" - IFS='-' read -a script_fields <<<"$script_name" - script_number=${script_fields[0]} - script_target=${script_fields[1]} - - printf "\nRunning #$script_number ${C_YELLOW}$script_target${C_NC}...\n${C_DGRAY}" - chmod +x "$script" - source "$script" | indent - printf "${C_NC}" - -# Clean up if fails -done || make clean - -printf "\nDone! Cleaning up...\n${C_DGRAY}" -make clean -printf "${C_NC}\n" -exit 0 diff --git a/install/00-apt.sh b/install/00-apt.sh index a671e2c..6898965 100755 --- a/install/00-apt.sh +++ b/install/00-apt.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" clean update diff --git a/install/01-bash.sh b/install/01-bash.sh index 0cad245..87fa916 100755 --- a/install/01-bash.sh +++ b/install/01-bash.sh @@ -1,13 +1,11 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" bash_source="$dotfiles_dir/bash" bash_target="$HOME" link_folder "$bash_source" "$bash_target" echo "bash dotfiles are linked" -source "$HOME/.bashrc" -echo "bashrc sourced" - sudo chmod -R 0644 /etc/update-motd.d/ bash --version diff --git a/install/02-fish.sh b/install/02-fish.sh index b8d27d2..e7d9bcd 100755 --- a/install/02-fish.sh +++ b/install/02-fish.sh @@ -1,11 +1,11 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" if not_installed "fish"; then echo "installing fish..." add_ppa "fish-shell/release-3" update install fish - fi echo "fish is installed" diff --git a/install/03-ssh.sh b/install/03-ssh.sh index 3a6a0f9..bd8395b 100755 --- a/install/03-ssh.sh +++ b/install/03-ssh.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # ssh key exists ssh_target="$HOME/.ssh" diff --git a/install/04-git.sh b/install/04-git.sh index 9b7c7a7..7bc3589 100755 --- a/install/04-git.sh +++ b/install/04-git.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # git dotfiles are symlinked git_source="$dotfiles_dir/git" diff --git a/install/10-pyenv.sh b/install/10-pyenv.sh index 30b288c..e26ed2a 100755 --- a/install/10-pyenv.sh +++ b/install/10-pyenv.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # pyenv is installed if not_installed "pyenv"; then diff --git a/install/11-python.sh b/install/11-python.sh index cd77f6b..f6a2cea 100755 --- a/install/11-python.sh +++ b/install/11-python.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # python3 and pip3 are installed if not_installed "pip3"; then diff --git a/install/12-poetry.sh b/install/12-poetry.sh index d8e11ab..35af53d 100755 --- a/install/12-poetry.sh +++ b/install/12-poetry.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # poetry is installed if not_installed "poetry"; then diff --git a/install/13-nvm.sh b/install/13-nvm.sh index c009308..e1cefa3 100755 --- a/install/13-nvm.sh +++ b/install/13-nvm.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # nvm is installed if not_installed "nvm"; then diff --git a/install/14-yarn.sh b/install/14-yarn.sh index e1c4efb..03d8a5c 100755 --- a/install/14-yarn.sh +++ b/install/14-yarn.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # yarn is installed if not_installed "yarn"; then diff --git a/install/15-java.sh b/install/15-java.sh index 38e1b5e..fd88936 100755 --- a/install/15-java.sh +++ b/install/15-java.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" if not_installed "java"; then echo "Installing java..." diff --git a/install/16-vim.sh b/install/16-vim.sh index a278ab2..619c9ae 100755 --- a/install/16-vim.sh +++ b/install/16-vim.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" vim_source="$dotfiles_dir/vim" vim_target="$XDG_CONFIG_HOME/nvim" diff --git a/install/17-elm.sh b/install/17-elm.sh index a22dc52..61f0253 100755 --- a/install/17-elm.sh +++ b/install/17-elm.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" if not_installed "elm"; then curl -L -o elm.gz https://github.com/elm/compiler/releases/download/0.19.1/binary-for-linux-64-bit.gz diff --git a/install/30-docker.sh b/install/30-docker.sh index 3d9fcfa..e8f9b5e 100755 --- a/install/30-docker.sh +++ b/install/30-docker.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" # docker is installed DOCKER_FOLDER="$HOME/.docker" diff --git a/install/32-firebase.sh b/install/32-firebase.sh index 752a326..d64f237 100755 --- a/install/32-firebase.sh +++ b/install/32-firebase.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" if not_installed "firebase"; then echo "Installing firebase..." diff --git a/install/33-aws.sh b/install/33-aws.sh index 90b1f0d..98675f8 100755 --- a/install/33-aws.sh +++ b/install/33-aws.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip -d "$dotfiles_dir/tmp" diff --git a/install/34-terraform.sh b/install/34-terraform.sh index 02aed45..5e2a808 100755 --- a/install/34-terraform.sh +++ b/install/34-terraform.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" if not_installed "terraform"; then echo "Installing terraform..." diff --git a/install/98-apt-clean.sh b/install/98-apt-clean.sh index 07c4dcd..3ebe748 100755 --- a/install/98-apt-clean.sh +++ b/install/98-apt-clean.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" echo "cleaning" clean diff --git a/install/99-screenfetch.sh b/install/99-screenfetch.sh index 32f9325..046e94b 100755 --- a/install/99-screenfetch.sh +++ b/install/99-screenfetch.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +source "$(dirname $0)/utils.sh" if not_installed "screenfetch"; then echo "Installing screenfetch..." diff --git a/install/utils.sh b/install/utils.sh index cae83f5..7fc69bd 100755 --- a/install/utils.sh +++ b/install/utils.sh @@ -1,4 +1,10 @@ #!/usr/bin/env bash +# ---------------------------------------------------------------------------- # +# Helper variables # +# ---------------------------------------------------------------------------- # +install_dir="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"; +dotfiles_dir="$(dirname "$install_dir")"; + # ---------------------------------------------------------------------------- # # Helper functions # ---------------------------------------------------------------------------- # @@ -8,28 +14,28 @@ clean() { } update() { - sudo apt-get update -qq + sudo apt-get update } # Non-interactive upgrade -# Skip if FAST_MODE is defined upgrade() { - [ "$FAST_MODE" != false ] && return DEBIAN_FRONTEND=noninteractive \ - sudo apt-get dist-upgrade -qq \ + sudo apt-get \ -o Dpkg::Options::="--force-confdef" \ - -o Dpkg::Options::="--force-confold" + -o Dpkg::Options::="--force-confold" \ + -y \ + dist-upgrade } # @arg $1 packages to install install() { - sudo apt-get install -qq $1 + sudo apt-get install $1 refresh } # @arg $1 package list file to install install_file() { - sudo apt-get install -fqq $(cat $1) + sudo apt-get install -f $(cat $1) refresh } diff --git a/setup.pl b/setup.pl index 585d964..0a42a30 100755 --- a/setup.pl +++ b/setup.pl @@ -4,6 +4,7 @@ use warnings; use Archive::Tar; +use File::Copy; use File::Temp (); use IPC::System::Simple qw(capture); use LWP::Simple; @@ -18,9 +19,12 @@ my $branch = $ENV{'DOTFILES_BRANCH'} // 'master'; print "Using repository $author/$repository at $branch\n"; # Installer filename -my $installer = $ENV{'DOTFILES_INSTALLER'} // 'install.sh'; +my $installer = $ENV{'DOTFILES_INSTALLER'} // 'install.pl'; print "Using installer $installer\n"; +# Install location +my $dotfiles_dir = $ENV{'DOTFILES_DIR'} // "$ENV{'HOME'}/.dotfiles"; + # Download repo my $repository_url = "https://github.com/$author/$repository/archive/$branch.tar.gz"; my $temp_handle = File::Temp->new(); @@ -36,8 +40,13 @@ $tar->read($repository_temp); $tar->setcwd($temp_dir); $tar->extract(); +# Move dotfiles out of temporary dir +my $temp_dotfiles_dir = "$temp_dir/$repository-$branch"; +print "Moving $temp_dotfiles_dir to $dotfiles_dir\n"; +move($temp_dotfiles_dir, $dotfiles_dir); + # Install repo -my $installer_path = "$temp_dir/$repository-$branch/$installer"; -print 'Running installer <' . $installer_path . ">\n"; +my $installer_path = "$dotfiles_dir/$installer"; +print 'Running installer ' . $installer_path . "\n"; my $output = capture([0,1,2], $^X, $installer_path); print $output;