Migrated to ansible

This commit is contained in:
2024-04-02 00:59:42 +02:00
parent c53268e307
commit 0ccf6427f2
136 changed files with 5157 additions and 1407 deletions

View File

@@ -1,55 +1,89 @@
#!/bin/bash
# Helpers
get_aur_helper() {
if is_installed paru; then
echo "paru"
elif is_installed yay; then
echo "yay"
fi
# Vars
DOTFILES_DIR="$HOME/.dotfiles"
CONFIG_DIR="$HOME/.config/dotfiles"
REPO="https://gitea.keuledrive.de/Keule2/Dotfiles.git"
DIST=$(source /etc/os-release && echo $ID)
# Colors
COLOR_RESET='\033[0m'
RED='\033[0;31m'
LGREEN='\033[01;32m'
# Helper Functions
print_header() {
echo -e "${LGREEN}$1${COLOR_RESET}"
}
is_installed() {
if pacman -Qi $1 &> /dev/null; then
return 0
else
return 1
fi
cmd() {
local DOTFILES_LOG="./error.log"
if ! [[ -f $DOTFILES_LOG ]]; then
touch $DOTFILES_LOG
fi
if eval "$1" 1> /dev/null 2> $DOTFILES_LOG; then
return 0
fi
echo -e "${RED}"
cat $DOTFILES_LOG
echo -e "${COLOR_RESET}"
exit 1
}
is_pkg() {
if "$1" -Si "$2" &>/dev/null; then
return 0
else
return 1
fi
arch_install() {
# Install Ansible
if ! [ -x "$(command -v ansible)" ]; then
print_header "Installing ansible"
cmd "pacman -S ansible --noconfirm"
fi
# Install Git
if ! [ -x "$(command -v git)" ]; then
print_header "Installing git"
cmd "sudo pacman -S git --noconfirm"
fi
# Install PIP
if ! [ -x "$(command -v pip)" ]; then
print_header "Installing pip"
cmd "sudo pacman -S python-pip --noconfirm"
fi
# Install Watchdog
if [ -x "$(pip list | grep watchdog)" ]; then
print_header "Installing watchdog"
cmd "pip install --break-system-packages watchdog"
fi
}
is_aur_pkg() {
helper=$(get_aur_helper)
if [ -z $helper ]; then
return 1
fi
return $(is_pkg $helper $1)
ubuntu_install() {
# Install Ansible
if ! dpkg -s ansible >/dev/null 2>&1; then
print_header "Installing ansible"
cmd "sudo apt-get update"
cmd "sudo apt-get install -y software-properties-common"
cmd "sudo apt-add-repository -y ppa:ansible/ansible"
cmd "sudo apt-get update"
cmd "sudo apt-get install -y ansible"
cmd "sudo apt-get install python3-argcomplete"
cmd "sudo activate-global-python-argcomplete3"
fi
# Install Git
if ! dpkg -s git >/dev/null 2>&1; then
print_header "Installing git"
cmd "sudo apt-get install -y git"
fi
# TODO pip, watchdog
}
is_arch_pkg() {
return $(is_pkg pacman $1)
}
update_galaxy() {
local OS=$1
install() {
if is_installed "$1"; then
return 0
fi
if is_arch_pkg "$1"; then
sudo pacman -S $1
elif is_aur_pkg "$1"; then
sudo $(get_aur_helper) -S $1
else
echo "error: unknown package [$1]"
fi
print_header "Installing/Updating ansible-galaxy"
cmd "ansible-galaxy install -r $DOTFILES_DIR/requirements/default.yml"
if [ -f "$DOTFILES_DIR/requirements/$OS.yml" ]; then
cmd "ansible-galaxy install -r $DOTFILES_DIR/requirements/$OS.yml"
fi
}
# Banner
@@ -62,123 +96,44 @@ ________ __ .___ __ .__ .__
\/ \/ \/ \/ \/ \/
EOF
# Vars
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
clone_dir="$HOME/git/"
# Setup
case $DIST in
arch)
arch_install
;;
ubuntu)
ubuntu_install
;;
*)
echo "Unsupported OS"
exit 1
;;
esac
# Change working directory
cd $script_dir
echo "Working directory: $(pwd)"
# Dependencies
echo ""
echo "Installing dependencies..."
install git
install base-devel
echo "Dependencies installed!"
# AUR helper
if ! (is_installed paru || is_installed yay); then
echo ""
echo "Installing AUR helper..."
echo -e "AUR helper:\n1) paru\n2) yay"
read -p "Enter your preference (default=1): " helper
case $helper in
1) helper="paru" ;;
2) helper="yay" ;;
*) helper="paru" ;;
esac
if [ -d $clone_dir ]; then
rm -rf $clone_dir$helper
else
mkdir $clone_dir
fi
echo "Installing $helper..."
cd $clone_dir
git clone https://aur.archlinux.org/$helper.git
cd $helper
makepkg -si
cd $script_dir
echo "AUR helper installed!"
# Clone repository
if ! [[ -d "$DOTFILES_DIR" ]]; then
print_header "Cloning repo"
cmd "git clone $REPO $DOTFILES_DIR"
else
helper=$(get_aur_helper)
print_header "Updating repo"
cmd "git -C $DOTFILES_DIR pull"
fi
# Install packages
echo ""
echo "Installing packages..."
# Change Working Dir
cd "$DOTFILES_DIR"
for pkg_file in *.pkgs; do
[ -f "$pkg_file" ] || continue
echo "Found package file: $pkg_file"
read -p "Do you want to install the packages from this file(Y/n)?" inst
# Update Galaxy
update_galaxy $DIST
if [ "$inst" == "n" ]; then
continue
fi
while IFS= read -r pkg; do
if [[ $pkg = \#* ]] || [ -z "$pkg" ]; then
continue
fi
if is_installed $pkg; then
echo "info: package already installed, ignoring [$pkg]"
elif is_arch_pkg $pkg; then
arch_pkgs="${arch_pkgs} $pkg"
elif is_aur_pkg $pkg; then
aur_pkgs="${aur_pkgs} $pkg"
else
echo "error: unknown package [$pkg]"
fi
done < $pkg_file
done
echo ""
echo "Installing arch packages..."
if [ ! -z "$arch_pkgs" ]; then
sudo pacman -Sy $arch_pkgs
# Run playbook
if [[ -f "$CONFIG_DIR/vault-password.txt" ]]; then
if [[ -f "$CONFIG_DIR/values.yml" ]]; then
ansible-playbook --diff -v --ask-become-pass --extra-vars "@$CONFIG_DIR/values.yml" --vault-password-file "$CONFIG_DIR/vault-password.txt" "$DOTFILES_DIR/main.yml" "$@"
else
ansible-playbook --diff -v --ask-become-pass --vault-password-file "$CONFIG_DIR/vault-password.txt" "$DOTFILES_DIR/main.yml" "$@"
fi
elif [[ -f "$CONFIG_DIR/values.yml" ]]; then
ansible-playbook --diff -v --ask-become-pass --extra-vars "@$CONFIG_DIR/values.yml" "$DOTFILES_DIR/main.yml" "$@"
else
ansible-playbook --diff -v --ask-become-pass "$DOTFILES_DIR/main.yml" "$@"
fi
echo ""
echo "Installing AUR packages..."
if [ ! -z "$aur_pkgs" ]; then
$helper -Sy $aur_pkgs
fi
# Install minegrub
read -p "Install grub theme 'minegrub'(Y/n)?" inst
if [ "$inst" != "n" ]; then
git clone https://github.com/Lxtharia/minegrub-theme.git
cd ./minegrub-theme
sudo cp -ruv ./minegrub /boot/grub/themes/
sudo sed -i '/^\(#\)\?GRUB_THEME/ s~.*~GRUB_THEME=/boot/grub/themes/minegrub/theme.txt~' /etc/default/grub
sudo grub-mkconfig -o /boot/grub/grub.cfg
cd ..
fi
# TODO mic-indicator, nerd font
echo "Packages installed!"
# Apply default configs
if [ "$1" != "nc" ]; then
echo ""
echo "Copying configs..."
cp -r -f .config/. ~/.config/
cp -r -f home/. ~/
sudo cp -r -f etc/. /etc/
echo "Configs copied!"
fi
#TODO Set theme
#TODO Enable services
# cups (is_isnstalled $pkg -> ask? -> enable)
# bluetooth
# sddm
echo "Finished!"