#!/usr/bin/env bash # Script to initialize a system into Ansible collection. ## Global Variables ## DIR="$(dirname -- "${BASH_SOURCE[0]}")" PROG="$(basename -- "${BASH_SOURCE[0]}")" LOCAL=$DIR/local.yml URL="https://git.hyperling.com/me/env-ansible" BRANCH="main" ## Functions ## # Accepts 1 parameter, it is used as the exit status. function usage { cat <<- EOF $PROG [-l] [-b branch_name] [-g] [-w] [-s] [-h] Program to initialize synchronization with Hyperling's Ansible configuration. $URL Parameters: -l : Run the local playbook associated with this $PROG. This is helpful for development or just saving bandwidth. It also provides prettier colors than the plaintext from ansible-pull. ;) -b branch_name : Download and run a specific branch. Default is $BRANCH. -g : Enable the General config with test contents. -w : Enable the Workstation config with test contents. -s : Enable the Server config with test contents. -h : Display this help text EOF exit $1 } ## Parameter Parsing ## while getopts ":lb:gwsh" arg; do case $arg in l) local="Y" && echo "Running $LOCAL as the playbook." ;; b) branch="$OPTARG" && echo "Using branch $branch instead of $BRANCH." ;; l) create_general="Y" && echo "Creating basic '$general_config'." ;; l) create_workstation="Y" && echo "Creating basic '$workstation_config'." ;; l) create_server="Y" && echo "Creating basic '$server_config'." ;; h) usage ;; *) echo "ERROR: Parameter $OPTARG was not recognized." && usage 1 ;; esac done # Alert on historic usage of `setup.sh BRANCH`. if [[ ! -z $1 && $1 != "-"* ]]; then echo "ERROR: '$1' is not a valid option, please check your parameters and try again." usage 1 fi if [[ -z "$local" && "$branch" == "" ]]; then echo "Using default branch $BRANCH." branch="$BRANCH" fi if [[ -n "$create_general" && -f "$general_config" ]]; then echo "WARNING: General configuration already exists, will not overwrite." ls -lh "$general" fi if [[ -n "$create_workstation" && -f "$workstation_config" ]]; then echo "WARNING: Workstation configuration already exists, will not overwrite." ls -lh "$workstation_config" fi if [[ -n "$create_server" && -f "$server_config" ]]; then echo "WARNING: Server configuration already exists, will not overwrite." ls -lh "$server_config" fi ## Main ## os="$(cat /etc/os-release)" if [[ ! -f /.dockerenv ]]; then # If we are not in a Docker container, also check what the kernel says. os="$os $(uname -a)" # Docker containers use the host kernel which gives an incorrect reading. fi echo "Making sure all necessary packages are installed..." if [[ `which ansible > /dev/null; echo $?` != 0 ]]; then if [[ $os == *Debian* || $os == *Ubuntu* || $os == *"Pop!_OS"* || $os == *Mint* || $os == *Parrot* ]]; then sudo apt update sudo apt install -y ansible git <<< N sudo mkdir -p /etc/ansible sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' elif [[ $os == *FreeBSD* ]]; then sudo pkg install -y py38-ansible git sudo mkdir -p /usr/local/etc/ansible sudo sh -c 'echo "localhost ansible_connection=local" > /usr/local/etc/ansible/hosts' elif [[ $os == *Arch* || $os == *Manjaro* || $os == *Artix* ]]; then sudo pacman -Sy --noconfirm ansible git sudo mkdir -p /etc/ansible sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' elif [[ $os == *Darwin* ]]; then bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" echo "TESTING - EXIT!" exit 0 brew install ansible git elif [[ $os == *Fedora* ]]; then sudo dnf install -y ansible git python3-libselinux sudo mkdir -p /etc/ansible sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' elif [[ $os == *openSUSE* ]]; then sudo zypper install -y ansible git sudo mkdir -p /etc/ansible sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' elif [[ $os == *NixOS* ]]; then $DIR/files/scripts/nixos.sh -b $branch sudo mkdir -p /etc/ansible sudo sh -c 'echo "localhost ansible_connection=local" > /etc/ansible/hosts' else echo -e "ERROR: OS not detected." echo -e "$os" exit 1 fi fi echo "Installed!" #echo "Adding Ansible Collections..." #ansible-galaxy collection install community.general #echo "Added!" # Create basic layouts if configs do not exist and are requested. if [[ -n "$create_general" && ! -f "$general_config" ]]; then function print_general_contents { cat <<- EOF enable=true user=test user_desc=Test branch=$branch EOF } print_general_contents > "$general_config" fi if [[ -n "$create_workstation" && ! -f "$workstation_config" ]]; then function print_workstation_contents { cat <<- EOF enable=true coding=true editing=false gaming=false EOF } print_workstation_contents > "$workstation_config" fi if [[ -n "$create_server" && ! -f "$server_config" ]]; then function print_server_contents { cat <<- EOF enable=true EOF } print_server_contents > "$server_config" fi echo "Provisioning Ansible..." if [[ $local == "Y" ]]; then sudo ansible-playbook $LOCAL else sudo ansible-pull -U $URL.git --checkout $branch fi echo "Provisioned!" echo "Mounting all drives..." mount -a echo "Mounted!" echo "Don't forget to set any new users' passwords!" ## Finish ## echo "We're done!" exit 0