From 25cd4dba5e4e4d011d3049e28a4eb242440b1c77 Mon Sep 17 00:00:00 2001 From: Andrejus Date: Sun, 3 Mar 2024 21:06:52 +0000 Subject: [PATCH] feat: dir, install changes --- {files/config => dot-config}/.gitignore | 0 {files/ssh => dot-ssh}/config | 1 + files/home/.zshrc | 0 {files/home => home}/.bash_profile | 0 {files/home => home}/.gitconfig | 0 {files/home => home}/.profile | 0 home/.zshrc | 13 +++ script/_utils.sh | 104 ------------------ script/install | 80 +++++--------- script/install.d/00-os_info.sh | 0 script/install.d/01-ssh.sh | 0 script/install.d/02-brew.sh | 7 +- script/install.d/03-apt.sh | 21 ++++ .../{03-nerdfont.sh => 04-nerdfont.sh} | 10 +- script/install.d/04-zsh.sh | 14 --- script/install.d/05-zsh.sh | 21 ++++ script/install.d/06-stow.sh | 32 ++++++ script/install.d/10-pyenv.sh | 0 script/install.d/11-python.sh | 0 script/install.d/12-node.sh | 0 script/install.d/20-docker.sh | 0 script/install.d/98-clean.sh | 0 script/install.d/99-screenfetch.sh | 0 script/install_config.json | 30 +---- 24 files changed, 127 insertions(+), 206 deletions(-) rename {files/config => dot-config}/.gitignore (100%) rename {files/ssh => dot-ssh}/config (99%) delete mode 100644 files/home/.zshrc rename {files/home => home}/.bash_profile (100%) rename {files/home => home}/.gitconfig (100%) rename {files/home => home}/.profile (100%) create mode 100644 home/.zshrc delete mode 100755 script/_utils.sh mode change 100755 => 100644 script/install.d/00-os_info.sh mode change 100755 => 100644 script/install.d/01-ssh.sh mode change 100755 => 100644 script/install.d/02-brew.sh create mode 100644 script/install.d/03-apt.sh rename script/install.d/{03-nerdfont.sh => 04-nerdfont.sh} (60%) mode change 100755 => 100644 delete mode 100755 script/install.d/04-zsh.sh create mode 100644 script/install.d/05-zsh.sh create mode 100644 script/install.d/06-stow.sh mode change 100755 => 100644 script/install.d/10-pyenv.sh mode change 100755 => 100644 script/install.d/11-python.sh mode change 100755 => 100644 script/install.d/12-node.sh mode change 100755 => 100644 script/install.d/20-docker.sh mode change 100755 => 100644 script/install.d/98-clean.sh mode change 100755 => 100644 script/install.d/99-screenfetch.sh diff --git a/files/config/.gitignore b/dot-config/.gitignore similarity index 100% rename from files/config/.gitignore rename to dot-config/.gitignore diff --git a/files/ssh/config b/dot-ssh/config similarity index 99% rename from files/ssh/config rename to dot-ssh/config index a1241cb..5e421eb 100644 --- a/files/ssh/config +++ b/dot-ssh/config @@ -3,3 +3,4 @@ Host * UseKeychain yes AddKeysToAgent yes IdentityFile ~/.ssh/id_ed25519 + diff --git a/files/home/.zshrc b/files/home/.zshrc deleted file mode 100644 index e69de29..0000000 diff --git a/files/home/.bash_profile b/home/.bash_profile similarity index 100% rename from files/home/.bash_profile rename to home/.bash_profile diff --git a/files/home/.gitconfig b/home/.gitconfig similarity index 100% rename from files/home/.gitconfig rename to home/.gitconfig diff --git a/files/home/.profile b/home/.profile similarity index 100% rename from files/home/.profile rename to home/.profile diff --git a/home/.zshrc b/home/.zshrc new file mode 100644 index 0000000..ac94af3 --- /dev/null +++ b/home/.zshrc @@ -0,0 +1,13 @@ +source $HOME/.profile + + +export ZSH="$HOME/.oh-my-zsh" + +ZSH_THEME="robbyrussell" + +zstyle ':omz:update' frequency 13 + +plugins=(git) + +source $ZSH/oh-my-zsh.sh + diff --git a/script/_utils.sh b/script/_utils.sh deleted file mode 100755 index 4ec0b31..0000000 --- a/script/_utils.sh +++ /dev/null @@ -1,104 +0,0 @@ -# Utility functions for common tasks - -ARCH=$(dpkg --print-architecture) - -# @arg $1 URL to download -# @arg $2 Path to file -function download_file { - curl \ - --silent \ - --show-error \ - --location \ - --output $2 \ - $1 -} - -# @arg $1 URL to run -# @arg $2 binary to use -function download_run { - file=$(mktemp) - download_file $1 $file - cat $file | $2 -} - -# @arg $1 binary to test -function bin_in_path { - command -v $1 -} - -# @arg $1 apt package to test -function apt_installed { - dpkg --status $1 >/dev/null -} - -function update { - sudo apt-get update -qq -} - -# @arg $1 apt package to install if not present -function install { - if ! apt_installed $1; then - sudo apt-get install -qq $1 - fi -} - -# Add apt repository -# @arg $1 JSON object containing the following keys -# * key - entry key -# * repository - apt repository -# * signingKey - gpg signing key url -# * components - apt components -function add_repository { - key=$(jq -r ".key" <<<"$1") - echo "Updating apt repository ${key}..." - - signingKey=$(jq -r ".signingKey" <<<"$1") - repository=$(jq -r ".repository" <<<"$1") - components=$(jq -r ".components" <<<"$1") - - signingKeyPath="/etc/apt/keyrings/${key}.gpg" - sourcesListPath="/etc/apt/sources.list.d/${key}.list" - source="deb [signed-by=${signingKeyPath} arch=${ARCH}] ${repository} ${components}" - - sudo mkdir -p /etc/apt/keyrings - sudo mkdir -p /etc/apt/sources.list.d - - echo "$source" | sudo tee "$sourcesListPath" >/dev/null - wget -O- "$signingKey" | - gpg --dearmor | - sudo tee "$signingKeyPath" >/dev/null -} - -# @arg $1 package list file to install -function install_file { - sudo apt-get install -qqf $(cat $1) -} - -# @arg $1 JSON object containing the following keys -# * name - apt repository -# * target - gpg signing key url -function stow_package { - name=$(jq -r ".name" <<<"$1") - target=$(jq -r ".target" <<<"$1") - - case $target in - HOME) - rm -f $HOME/.bashrc - rm -f $HOME/.profile - target=$HOME - ;; - CONFIG) - mkdir -p $HOME/.config - target=$HOME/.config - ;; - SSH) - mkdir -p $HOME/.ssh - target=$HOME/.ssh - ;; - *) ;; - - esac - - echo "Stowing $ABS_DIR/files/$name to $target" - sudo stow --dir="$ABS_DIR/files" --target=$target $name -} diff --git a/script/install b/script/install index 86b033b..00bd066 100755 --- a/script/install +++ b/script/install @@ -2,69 +2,47 @@ set -eo pipefail # -------------------------------------------------------------------- -# Script that installs system dependencies specified in a config, -# and runs all post-install scripts contained in a subdirectory. +# Script to run all install scripts contained in install.d # -uuid=${UUID:-$(cat /proc/sys/kernel/random/uuid)} -dir=$(dirname "$0") -utils="${dir}/_utils.sh" -config="${dir}/install_config.json" -install_dir="${dir}/install.d" -log_dir="${dir}/logs" - -# Create log directory if it doesn't exist -# and the log path hasn't been overridden -if [[ -z "$LOG_TARGET" ]]; then - mkdir -p "$log_dir" +# Prevent running as root and check sudo access +if [[ $EUID -eq 0 ]]; then + echo "Failed: Running as sudo. Please run as user" + exit 1 +fi +sudo -v + +dir=$(dirname "$0") +install_dir="$dir/install.d" + +if [[ -z "$LOG_TARGET" ]]; then + timestamp=$(date +%Y-%m-%dT%H:%M:%S) + uuid=$( + uuidgen 2> /dev/null \ + || cat /proc/sys/kernel/random/uuid 2> /dev/null \ + || echo $RANDOM + ) + log_dir="$dir/logs" + mkdir -p "$log_dir" + log_target=${LOG_TARGET:-"$log_dir/$uuid.log"} +elif + log_target="$LOG_TARGET" fi -log_target=${LOG_TARGET:-"${log_dir}/${uuid}.log"} install() { echo "Running \"$(basename "$0")\" at \"$(date)\"" echo "Running as \"$(whoami)\" on \"$(hostname)\"" - # Prevent running as root - if [[ $EUID -eq 0 ]]; then - echo "Failed: Running as sudo. Please run as user" - exit 1 - fi - - # Load installer dependencies - source "$utils" - update - install jq - for dep in $(jq -r ".apt_core_dependencies[]" "$config"); do - install "$dep" - done - - # Add apt repositories - for i in $(jq ".apt_repositories | keys | .[]" "$config"); do - value=$(jq -r ".apt_repositories[$i]" "$config") - add_repository "$value" - done - update - - # Install apt dependencies - for dep in $(jq -r ".apt_dependencies[]" "$config"); do - install "$dep" - done - - # Install dotfiles on system and load them - figlet -c "Stowing..." - for i in $(jq ".stow_packages | keys | .[]" "$config"); do - value=$(jq -r ".stow_packages[$i]" "$config") - stow_package "$value" - done - source "$HOME/.profile" - - # Run custom installer scripts - figlet -c "Installing..." for script in $install_dir/*.sh; do - figlet -c "$(basename $script)" + script_name=$(basename $script) + printf "\n\n<<< $script_name:\n" source $script + printf "\n\n>>> $script_name\n" + unset script_name done } echo "install: Logging to \"$log_target\"" install 2>&1 | tee "$log_target" + +unset uuid dir install_dir log_dir log_target diff --git a/script/install.d/00-os_info.sh b/script/install.d/00-os_info.sh old mode 100755 new mode 100644 diff --git a/script/install.d/01-ssh.sh b/script/install.d/01-ssh.sh old mode 100755 new mode 100644 diff --git a/script/install.d/02-brew.sh b/script/install.d/02-brew.sh old mode 100755 new mode 100644 index 935a74a..526e297 --- a/script/install.d/02-brew.sh +++ b/script/install.d/02-brew.sh @@ -7,12 +7,11 @@ if [[ "$OSTYPE" == "darwin"* ]]; then export NONINTERACTIVE=1 - if ! bin_in_path brew; then - download_run https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh /bin/bash - else - brew update + if ! command -v brew &> /dev/null; then + bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" fi + brew update brew --version unset NONINTERACTIVE diff --git a/script/install.d/03-apt.sh b/script/install.d/03-apt.sh new file mode 100644 index 0000000..6141e69 --- /dev/null +++ b/script/install.d/03-apt.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +# ----------------------------------------------------------------------------- +# Description: +# (distros with apt only) Install core apt packages. +# + +if command -v apt-get &> /dev/null; then + apt_packages=( + curl + gnupg + gnupg2 + ) + + sudo apt-get update + sudo apt-get install -qq "${apt_packages[@]}" + sudo apt-get autoremove + sudo apt-get autoclean + + unset apt_packages +fi diff --git a/script/install.d/03-nerdfont.sh b/script/install.d/04-nerdfont.sh old mode 100755 new mode 100644 similarity index 60% rename from script/install.d/03-nerdfont.sh rename to script/install.d/04-nerdfont.sh index a7508a3..68d8453 --- a/script/install.d/03-nerdfont.sh +++ b/script/install.d/04-nerdfont.sh @@ -11,10 +11,12 @@ if [[ "$OSTYPE" == "darwin"* ]]; then font-fira-code-nerd-font ) - brew tap homebrew/cask-fonts - for font in "${fonts_list[@]}"; do - brew install --cask "$font" - done + if ! brew list "${fonts_list[@]}" &> /dev/null; then + brew tap homebrew/cask-fonts + for font in "${fonts_list[@]}"; do + brew install --cask "$font" + done + fi unset fonts_list fi diff --git a/script/install.d/04-zsh.sh b/script/install.d/04-zsh.sh deleted file mode 100755 index a8e41be..0000000 --- a/script/install.d/04-zsh.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -# ----------------------------------------------------------------------------- -# Description: -# Configure zsh shell. -# - -if ! bin_in_path zsh; then - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - install zsh - fi -fi - -zsh --version diff --git a/script/install.d/05-zsh.sh b/script/install.d/05-zsh.sh new file mode 100644 index 0000000..4b6f3f4 --- /dev/null +++ b/script/install.d/05-zsh.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +# ----------------------------------------------------------------------------- +# Description: +# Configure zsh shell. +# + +if ! command -v zsh &> /dev/null; then + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + sudo apt-get install -qq zsh + elif [[ "$OSTYPE" == "darwin"* ]]; then + brew install zsh + fi +fi + +zsh --version + +# check if oh-my-zsh is installed +if [[ ! -d "$HOME/.oh-my-zsh" ]]; then + bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" +fi diff --git a/script/install.d/06-stow.sh b/script/install.d/06-stow.sh new file mode 100644 index 0000000..70e535c --- /dev/null +++ b/script/install.d/06-stow.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# ----------------------------------------------------------------------------- +# Description: +# Install and run stow. +# + +if ! command -v stow &> /dev/null; then + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + sudo apt-get install -qq stow + elif [[ "$OSTYPE" == "darwin"* ]]; then + brew install stow + fi +fi + +stow --version + +root_dir=$(dirname "$(dirname "$(dirname "$(realpath "$0")")")") + +rm -f $HOME/.bash_profile +rm -f $HOME/.bashrc +rm -f $HOME/.gitconfig +rm -f $HOME/.profile +rm -f $HOME/.zshrc +rm -f $HOME/.ssh/config + +mkdir -p $HOME/.config +mkdir -p $HOME/.ssh + +sudo stow --dir="$root_dir" --target="$HOME" home +sudo stow --dir="$root_dir" --target="$HOME/.config" dot-config +sudo stow --dir="$root_dir" --target="$HOME/.ssh" dot-ssh diff --git a/script/install.d/10-pyenv.sh b/script/install.d/10-pyenv.sh old mode 100755 new mode 100644 diff --git a/script/install.d/11-python.sh b/script/install.d/11-python.sh old mode 100755 new mode 100644 diff --git a/script/install.d/12-node.sh b/script/install.d/12-node.sh old mode 100755 new mode 100644 diff --git a/script/install.d/20-docker.sh b/script/install.d/20-docker.sh old mode 100755 new mode 100644 diff --git a/script/install.d/98-clean.sh b/script/install.d/98-clean.sh old mode 100755 new mode 100644 diff --git a/script/install.d/99-screenfetch.sh b/script/install.d/99-screenfetch.sh old mode 100755 new mode 100644 diff --git a/script/install_config.json b/script/install_config.json index 4ce56d2..98311ea 100644 --- a/script/install_config.json +++ b/script/install_config.json @@ -30,12 +30,6 @@ "repository": "https://download.docker.com/linux/debian", "components": "bullseye stable" }, - { - "key": "fish", - "signingKey": "https://download.opensuse.org/repositories/shells:fish/Debian_10/Release.key", - "repository": "https://download.opensuse.org/repositories/shells:/fish/Debian_10/", - "components": "/" - }, { "key": "github", "signingKey": "https://cli.github.com/packages/githubcli-archive-keyring.gpg", @@ -43,11 +37,6 @@ "components": "stable main" } ], - "apt_core_dependencies": [ - "curl", - "gnupg", - "gnupg2" - ], "apt_dependencies": [ "apt-transport-https", "ca-certificates", @@ -60,7 +49,6 @@ "emacs", "fd-find", "figlet", - "fish", "fonts-nanum", "fortune-mod", "fzf", @@ -76,13 +64,11 @@ "openssh-server", "redis-tools", "screenfetch", - "stow", "terraform-ls", "terraform", "tmux", "unzip", - "yarn", - "zsh" + "yarn" ], "node_dependencies": [ "firebase-tools", @@ -97,19 +83,5 @@ "poetry", "python-language-server[all]", "pyvim" - ], - "stow_packages": [ - { - "name": "config", - "target": "CONFIG" - }, - { - "name": "home", - "target": "HOME" - }, - { - "name": "ssh", - "target": "SSH" - } ] }