#!/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" if [[ -z $BRANCH ]]; then export BRANCH="main" fi LINUX_HOSTS_DIR=/etc/ansible LINUX_HOSTS_FILE="$LINUX_HOSTS_DIR/hosts" FREEBSD_HOSTS_DIR="/usr/local/etc/ansible" FREEBSD_HOSTS_FILE="$FREEBSD_HOSTS_DIR/hosts" HOSTS_DIR="$LINUX_HOSTS_DIR" HOSTS_FILE="$LINUX_HOSTS_FILE" config_dir="/usr/local/etc/hyperling-scm" general_config="$config_dir/general.ini" workstation_config="$config_dir/workstation.ini" server_config="$config_dir/server.ini" branch="" ## Functions ## # Accepts 1 parameter, it is used as the exit status. function usage { cat <<- EOF $PROG [-l] [-b branch_name] [-g] [-w] [-s] [-f] [-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. -f : Display this system's facts. -h : Display this help text. EOF exit $1 } ## Parameter Parsing ## while getopts ":lb:gwsfh" arg; do case $arg in l) local="Y" && echo "Running $LOCAL as the playbook." ;; b) branch="$OPTARG" && echo "Using branch $branch instead of $BRANCH." ;; g) create_general="Y" && echo "Generating test '$general_config'." ;; w) create_workstation="Y" && echo "Generating test '$workstation_config'." ;; s) create_server="Y" && echo "Generating test '$server_config'." ;; f) show_facts="Y" && echo "Displaying ansible facts then exiting." ;; 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 [[ "$branch" == "" ]]; then branch="$BRANCH" if [[ -z "$local" ]]; then echo "Running against default branch '$branch'." else echo "Set branch to '$branch'." fi 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 elif [[ $os == *FreeBSD* ]]; then sudo pkg install -y -g py*-ansible git HOSTS_DIR="$FREEBSD_HOSTS_DIR" HOSTS_FILE="$FREEBSD_HOSTS_FILE" elif [[ $os == *Arch* || $os == *Manjaro* || $os == *Artix* ]]; then sudo pacman -Sy --noconfirm ansible git elif [[ $os == *Darwin* ]]; then bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install ansible git HOSTS_DIR="" HOSTS_FILE="" elif [[ $os == *Fedora* ]]; then sudo dnf install -y ansible git python3-libselinux elif [[ $os == *openSUSE* ]]; then sudo zypper install -y ansible git elif [[ $os == *NixOS* ]]; then $DIR/files/scripts/nixos.sh -b $branch else echo -e "ERROR: OS not recognized." echo -e "$os" exit 1 fi fi echo "Installed!" if [[ -n $HOSTS_DIR && -n $HOSTS_FILE ]]; then echo "Ensuring hosts file exists..." if [[ ! -e $HOSTS_FILE ]]; then sudo mkdir -pv $HOSTS_DIR sudo sh -c "echo 'localhost ansible_connection=local' > $HOSTS_FILE" fi echo "$HOSTS_FILE: '`cat $HOSTS_FILE`'" echo "Hosts file created successfully!" fi if [[ "$show_facts" == "Y" ]]; then echo "Showing Ansible Facts" ansible localhost -m setup --connection=local exit 0 fi #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" || -n "$create_workstation" || -n "$create_server" ]] then sudo mkdir -pv "$config_dir" MARK1="BEGIN" MARK2="END" fi if [[ -n "$create_general" && ! -f "$general_config" ]]; then function print_general_contents { cat <<- EOF ; $MARK1 MANAGED BY ANSIBLE | Generic Config ; $MARK2 MANAGED BY ANSIBLE | Generic Config ; TEST DATA enable=true user=test user_desc=Test branch=$branch ; TEST DATA EOF } print_general_contents | sudo tee "$general_config" ls -lh "$general_config" fi if [[ -n "$create_workstation" && ! -f "$workstation_config" ]]; then function print_workstation_contents { cat <<- EOF ; $MARK1 MANAGED BY ANSIBLE | Workstation Config ; $MARK2 MANAGED BY ANSIBLE | Workstation Config ; TEST DATA enable=true coding=true editing=false gaming=false ; TEST DATA EOF } print_workstation_contents | sudo tee "$workstation_config" ls -lh "$workstation_config" fi if [[ -n "$create_server" && ! -f "$server_config" ]]; then function print_server_contents { cat <<- EOF ; $MARK1 MANAGED BY ANSIBLE | Server Config ; $MARK2 MANAGED BY ANSIBLE | Server Config ; TEST DATA enable=true ; TEST DATA EOF } print_server_contents | sudo tee "$server_config" ls -lh "$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