feat: structure work

- split files/ into separate dirs
- rename scripts/ to script/, remove .sh extensions
- remove publish scripts, tf module, use github host
- remove unused install files
This commit is contained in:
Andrejus Kostarevas
2023-02-24 00:34:31 +00:00
parent f85a257b98
commit 50193eca51
44 changed files with 49 additions and 1088 deletions

95
script/_utils.sh Executable file
View File

@@ -0,0 +1,95 @@
# Utility functions for common tasks
# @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 clean {
sudo apt-get clean -qq
}
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
# * repository - apt repository
# * signingKey - gpg signing key url
# * components - apt components
function add_repository {
repository=$(jq -r ".repository" <<<"$1")
if ! grep -q "^deb .*${repository}" /etc/apt/sources.list; then
signingKey=$(jq -r ".signingKey" <<<"$1")
components=$(jq -r ".components" <<<"$1")
curl -fsSL $signingKey | sudo apt-key add -
source="deb [arch=$(dpkg --print-architecture)] ${repository} ${components}"
sudo add-apt-repository --yes "$source"
fi
}
# @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 $HOME/.config
target=$HOME/.config
;;
SSH)
mkdir $HOME/.ssh
target=$HOME/.ssh
;;
*) ;;
esac
echo "Stowing $ABS_DIR/files/$name to $target"
sudo stow --dir="$ABS_DIR/files" --target=$target $name
}

72
script/install Executable file
View File

@@ -0,0 +1,72 @@
#!/usr/bin/env bash
set -eo pipefail
#
# Script that installs system dependencies specified in a config,
# and runs all post-install scripts contained in a subdirectory.
#
TIME=${TIME:-$(date)}
UUID=${UUID:-$(uuidgen)}
HOST=${HOST:-$(hostname)}
NAME=$(basename "$0")
REL_DIR=$(dirname "$0")
ABS_DIR=$(readlink -f $REL_DIR/../) # Scripts are nested inside of /script
UTILS="${REL_DIR}/_utils.sh"
CONFIG="${REL_DIR}/install_config.json"
INSTALL_DIR="${REL_DIR}/install.d"
LOG_DIR="${ABS_DIR}/logs"
mkdir -p "$LOG_DIR"
LOG_TARGET=${LOG_TARGET:-"${LOG_DIR}/${UUID}.log"}
install() {
echo "Running $NAME at $TIME"
echo "Running as $USER on $HOST"
# 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)"
source $script
done
}
echo "install: Logging to $LOG_TARGET"
install 2>&1 | tee "$LOG_TARGET"

2
script/install.d/00-apt.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
cat /etc/os-release

9
script/install.d/01-ssh.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
ssh_target="$HOME/.ssh"
ssh_key="$ssh_target/id_ed25519"
ssh_pub="$ssh_key.pub"
if [ ! -f "$ssh_key" ]; then
ssh-keygen -t ed25519 -f "$ssh_key"
fi
cat "$ssh_pub"

17
script/install.d/02-fish.sh Executable file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env bash
fish --version
current_shell=$(grep "^$USER" /etc/passwd)
current_shell=${current_shell##*:}
fish_shell=$(command -v fish)
if [[ "$current_shell" != "$fish_shell" ]]; then
sudo usermod --shell "$fish_shell" "$USER"
fi
fisher_location="$XDG_CONFIG_HOME/fish/functions/fisher.fish"
if ! [ -f $fisher_location ]; then
fish -c "curl -sL https://git.io/fisher | source && fisher update"
fi
fish -c "fisher update"
fish -c "fisher --version"

View File

@@ -0,0 +1,15 @@
build-essential
libssl-dev
libbz2-dev
libreadline-dev
libsqlite3-dev
libxml2-dev
libxmlsec1-dev
llvm
libncurses5-dev
libncursesw5-dev
xz-utils
tk-dev
libffi-dev
liblzma-dev
zlib1g-dev

24
script/install.d/10-pyenv.sh Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
if ! bin_in_path "pyenv"; then
# see https://github.com/pyenv/pyenv/wiki/common-build-problems
pyenv_list_file="$INSTALL_DIR/10-pyenv-pkglist"
install_file "$pyenv_list_file"
# see https://github.com/pyenv/pyenv-installer
download_run \
"https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer" \
bash
fi
virtualenv_path="$(pyenv root)/plugins/pyenv-virtualenv"
if [ ! -d "$virtualenv_path" ]; then
git clone \
https://github.com/pyenv/pyenv-virtualenv.git \
$virtualenv_path
fi
eval "$(pyenv init --path)"
pyenv update
pyenv --version

16
script/install.d/11-python.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
if ! bin_in_path "pip3"; then
pyenv install 3.9.0
pyenv global 3.9.0
fi
pip install --upgrade pip
pip3 install --upgrade pip
python3 --version
pip3 --version
for dep in $(jq -r ".pip_dependencies[]" $CONFIG); do
pip3 install --upgrade $dep
done

22
script/install.d/12-node.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
nvm_version="v0.38.0"
if ! bin_in_path "nvm"; then
download_run "https://raw.githubusercontent.com/nvm-sh/nvm/${nvm_version}/install.sh" \
"bash"
fi
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"
nvm --version
nvm alias default lts/fermium
nvm install lts/fermium
nvm use lts/fermium
node --version
yarn --version
for dep in $(jq -r ".node_dependencies[]" $CONFIG); do
yarn global add $dep
yarn global upgrade $dep
done

13
script/install.d/20-docker.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
docker --version
readonly docker_group="docker"
if ! grep -q "$docker_group" /etc/group; then
echo "Adding docker group"
sudo groupadd "$docker_group"
fi
if ! groups "$USER" | grep -q "\b$docker_group\b"; then
echo "Adding user to docker group"
sudo usermod -aG docker "$USER"
fi

View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
clean

View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
screenfetch

107
script/install_config.json Normal file
View File

@@ -0,0 +1,107 @@
{
"apt_repositories": [
{
"signingKey": "https://apt.releases.hashicorp.com/gpg",
"repository": "https://apt.releases.hashicorp.com",
"components": "buster main"
},
{
"signingKey": "https://dl.yarnpkg.com/debian/pubkey.gpg",
"repository": "https://dl.yarnpkg.com/debian",
"components": "stable main"
},
{
"signingKey": "https://packages.cloud.google.com/apt/doc/apt-key.gpg",
"repository": "https://packages.cloud.google.com/apt",
"components": "cloud-sdk main"
},
{
"signingKey": "https://packages.cloud.google.com/apt/doc/apt-key.gpg",
"repository": "https://apt.kubernetes.io/",
"components": "kubernetes-xenial main"
},
{
"signingKey": "https://download.docker.com/linux/debian/gpg",
"repository": "https://download.docker.com/linux/debian",
"components": "buster stable"
},
{
"signingKey": "https://download.opensuse.org/repositories/shells:fish/Debian_10/Release.key",
"repository": "https://download.opensuse.org/repositories/shells:/fish/Debian_10/",
"components": "/"
},
{
"signingKey": "https://cli.github.com/packages/githubcli-archive-keyring.gpg",
"repository": "https://cli.github.com/packages",
"components": "stable main"
}
],
"apt_core_dependencies": [
"curl",
"gnupg",
"gnupg2"
],
"apt_dependencies": [
"apt-transport-https",
"ca-certificates",
"containerd.io",
"cowsay",
"default-jre",
"devscripts",
"docker-ce-cli",
"docker-ce",
"emacs",
"fd-find",
"figlet",
"fish",
"fonts-nanum",
"fortune-mod",
"fzf",
"gh",
"git",
"google-cloud-sdk",
"kubectl",
"lsb-release",
"make",
"net-tools",
"netcat",
"openssh-client",
"openssh-server",
"redis-tools",
"screenfetch",
"stow",
"terraform-ls",
"terraform",
"tmux",
"unzip",
"yarn"
],
"node_dependencies": [
"firebase-tools",
"neovim",
"typescript-language-server",
"typescript"
],
"pip_dependencies": [
"awscli",
"docker-compose",
"neovim",
"poetry",
"python-language-server[all]",
"pyvim"
],
"stow_packages": [
{
"name": "config",
"target": "CONFIG"
},
{
"name": "home",
"target": "HOME"
},
{
"name": "ssh",
"target": "SSH"
}
]
}

13
script/run Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail
tag=$(cat /proc/sys/kernel/random/uuid)
docker build . \
--build-arg UUID=$tag \
--tag dotfiles:$tag \
--target install
docker run \
-v "$(pwd)"/logs:/home/test-user/.dotfiles/logs \
dotfiles:$tag \
/bin/bash

34
script/setup Executable file
View File

@@ -0,0 +1,34 @@
#!/usr/bin/env bash
set -eo pipefail
#
# Script that checks out a compatible dotfiles repository
# and runs the installer to set up a new installation.
#
author=${GITHUB_AUTHOR:-andrejusk}
repository=${GITHUB_REPOSITORY:-dotfiles}
branch=${GITHUB_BRANCH:-master}
echo "Using repository $author/$repository at $branch"
setup_dir=${DOTFILES_DIR:-$HOME/.dotfiles}
# Prevent overwriting existing installation
mkdir -p $setup_dir
if [[ -z $(ls -A $setup_dir) ]]; then
echo "Setting up $setup_dir"
else
echo "Failed: Setup directory not empty $setup_dir"
exit 1
fi
# Download and untar repo
tmp_dir=$(mktemp -d)
tmp_dest="$tmp_dir/dotfiles.tar.gz"
wget "https://github.com/$author/$repository/archive/$branch.tar.gz" -qO $tmp_dest
tar -C $tmp_dir -zxf $tmp_dest
mv $tmp_dir/$repository-$branch/* $setup_dir
rm -rf $tmp_dir
# Run installer
$setup_dir/script/install

15
script/test Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
IMAGE=${IMAGE:-"andrejusk/dotfiles"}
tag=${TAG:-uuidgen}
docker build . \
--build-arg UUID=$tag \
--cache-from $IMAGE \
--tag $IMAGE:$tag \
--target test
docker run \
-v "$(pwd)"/logs:/home/test-user/.dotfiles/logs \
$IMAGE:$tag