Rewrite install script

This commit is contained in:
Andrejus
2020-07-11 20:59:48 +01:00
parent 0eb21776db
commit 21757767d2
24 changed files with 88 additions and 75 deletions

View File

@@ -1,16 +1,11 @@
SHELL := /bin/bash
# ---------------------------------------------------------------------------- #
# 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

41
install.pl Executable file
View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
clean
update

View File

@@ -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

View File

@@ -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"

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# ssh key exists
ssh_target="$HOME/.ssh"

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# git dotfiles are symlinked
git_source="$dotfiles_dir/git"

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# pyenv is installed
if not_installed "pyenv"; then

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# python3 and pip3 are installed
if not_installed "pip3"; then

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# poetry is installed
if not_installed "poetry"; then

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# nvm is installed
if not_installed "nvm"; then

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# yarn is installed
if not_installed "yarn"; then

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
if not_installed "java"; then
echo "Installing java..."

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
vim_source="$dotfiles_dir/vim"
vim_target="$XDG_CONFIG_HOME/nvim"

View File

@@ -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

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# docker is installed
DOCKER_FOLDER="$HOME/.docker"

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
if not_installed "firebase"; then
echo "Installing firebase..."

View File

@@ -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"

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
if not_installed "terraform"; then
echo "Installing terraform..."

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
echo "cleaning"
clean

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
if not_installed "screenfetch"; then
echo "Installing screenfetch..."

View File

@@ -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
}

View File

@@ -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;