Skip to content

Commit

Permalink
Merge pull request #80 from TechDufus/better_tasks
Browse files Browse the repository at this point in the history
Adding better task formatting to dotfiles.
  • Loading branch information
TechDufus authored Jan 11, 2024
2 parents e54c97b + d81a026 commit 026d636
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 73 deletions.
139 changes: 86 additions & 53 deletions bin/dotfiles
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#!/bin/bash
#color codes
RED='\033[1;31m'
YELLOW='\033[1;33m'
BLUE="\\033[38;5;27m"
SEA="\\033[38;5;49m"
GREEN='\033[1;32m'
CYAN='\033[1;36m'
NC='\033[0m'

#emoji codes
CHECK_MARK="${GREEN}\xE2\x9C\x94${NC}"
Expand All @@ -18,6 +10,66 @@ BOOK="${RED}\xF0\x9F\x93\x8B${NC}"
HOT="${ORANGE}\xF0\x9F\x94\xA5${NC}"
WARNING="${RED}\xF0\x9F\x9A\xA8${NC}"
RIGHT_ANGLE="${GREEN}\xE2\x88\x9F${NC}"
# color codes
RESTORE='\033[0m'
NC='\033[0m'
BLACK='\033[00;30m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
SEA="\\033[38;5;49m"
LIGHTGRAY='\033[00;37m'
LBLACK='\033[01;30m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
OVERWRITE='\e[1A\e[K'

DOTFILES_LOG="$HOME/.dotfiles.log"


# _header colorize the given argument with spacing
function _task {
# if _task is called while a task was set, complete the previous
if [[ $TASK != "" ]]; then
printf "${OVERWRITE}${LGREEN} [✓] ${LGREEN}${TASK}\n"
fi
# set new task title and print
TASK=$1
printf "${LBLACK} [ ] ${TASK} \n${LRED}"
}

# _cmd performs commands with error checking
function _cmd {
#create log if it doesn't exist
if ! [[ -f $DOTFILES_LOG ]]; then
touch $DOTFILES_LOG
fi
# empty conduro.log
> $DOTFILES_LOG
# hide stdout, on error we print and exit
if eval "$1" 1> /dev/null 2> $DOTFILES_LOG; then
return 0 # success
fi
# read error from log and add spacing
printf "${OVERWRITE}${LRED} [X] ${TASK}${LRED}\n"
while read line; do
printf " ${line}\n"
done < $DOTFILES_LOG
printf "\n"
# remove log file
rm $DOTFILES_LOG
# exit installation
exit 1
}


set -e

Expand All @@ -31,77 +83,58 @@ IS_FIRST_RUN="$HOME/.dotfiles_run"
# Install Ansible
# check lsb_release -si
if ! dpkg -s ansible >/dev/null 2>&1; then
echo -e "${ARROW} ${CYAN}Installing Ansible...${NC}"
echo -e " ${RIGHT_ANGLE} ${CYAN}Updating APT Repos${NC}"
sudo apt-get update 2>&1 > /dev/null
echo -e " ${RIGHT_ANGLE} ${CYAN}Installing dependency: ${YELLOW}software-properties-common${NC}"
sudo apt-get install -y software-properties-common 2>&1 > /dev/null
echo -e " ${RIGHT_ANGLE} ${CYAN}Adding Ansible PPA Repo${NC}"
sudo apt-add-repository -y ppa:ansible/ansible 2>&1 > /dev/null
echo -e " ${RIGHT_ANGLE} ${CYAN}Updating APT Repos${NC}"
sudo apt-get update 2>&1 > /dev/null
echo -e " ${RIGHT_ANGLE} ${CYAN}Installing Ansible${NC}"
sudo apt-get install -y ansible 2>&1 > /dev/null
echo -e "${ARROW} ${GREEN}Ansible installed!${NC}"
echo -e "${ARROW} ${CYAN}Installing ansible argument completion...${NC}"
sudo apt-get install python3-argcomplete 2>&1 > /dev/null
sudo activate-global-python-argcomplete3 2>&1 > /dev/null
_task "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

# Check if python3 and pip is installed
if ! dpkg -s python3 >/dev/null 2>&1; then
echo -e "${ARROW} ${CYAN}Installing Python3...${NC}"
sudo apt-get install -y python3 2>&1 > /dev/null
echo -e "${ARROW} ${GREEN}Python3 installed!${NC}"
_task "Installing Python3"
_cmd "sudo apt-get install -y python3"
fi
if ! dpkg -s python3-pip >/dev/null 2>&1; then
echo -e "${ARROW} ${CYAN}Installing Python3 Pip...${NC}"
sudo apt-get install -y python3-pip 2>&1 > /dev/null
echo -e "${ARROW} ${GREEN}Python3 Pip installed!${NC}"
_task "Installing Python3 Pip"
_cmd "sudo apt-get install -y python3-pip"
fi
# Check if pip module watchdog is installed
if ! pip3 list | grep watchdog >/dev/null 2>&1; then
echo -e "${ARROW} ${CYAN}Installing Python3 Watchdog...${NC}"
pip3 install watchdog 2>&1 > /dev/null
echo -e "${ARROW} ${GREEN}Python3 Watchdog installed!${NC}"
_task "Installing Python3 Watchdog"
_cmd "pip3 install watchdog"
fi


# Generate SSH keys
if ! [[ -f "$SSH_DIR/authorized_keys" ]]; then
echo -e "${ARROW} ${CYAN}Generating SSH keys...${NC}"
mkdir -p "$SSH_DIR"

chmod 700 "$SSH_DIR"

ssh-keygen -b 4096 -t rsa -f "$SSH_DIR/id_rsa" -N "" -C "$USER@$HOSTNAME" 2>&1 > /dev/null

cat "$SSH_DIR/id_rsa.pub" >> "$SSH_DIR/authorized_keys"
_task "Generating SSH keys"
_cmd "mkdir -p $SSH_DIR"
_cmd "chmod 700 $SSH_DIR"
_cmd "ssh-keygen -b 4096 -t rsa -f $SSH_DIR/id_rsa -N '' -C $USER@$HOSTNAME"
_cmd "cat $SSH_DIR/id_rsa.pub >> $SSH_DIR/authorized_keys"
fi

# Clone repository
if ! [[ -d "$DOTFILES_DIR" ]]; then
echo -e "${ARROW} ${CYAN}Cloning repository: ${YELLOW}github.com/TechDufus/dotfiles${NC}"
git clone --quiet "https://github.com/TechDufus/dotfiles.git" "$DOTFILES_DIR" 2>&1 > /dev/null
_task "Cloning repository"
_cmd "git clone --quiet https://github.com/TechDufus/dotfiles.git $DOTFILES_DIR"
else
echo -e "${ARROW} ${CYAN}Updating repository: ${YELLOW}github.com/TechDufus/dotfiles${NC}"
git -C "$DOTFILES_DIR" pull --quiet > /dev/null
_task "Updating repository"
_cmd "git -C $DOTFILES_DIR pull --quiet"
fi

# Create path
pushd "$DOTFILES_DIR" 2>&1 > /dev/null
_task "Updating Galaxy"
_cmd "ansible-galaxy install -r requirements.yml"

# Update Galaxy
echo -e "${ARROW} ${CYAN}Updating Galaxy...${NC}"
ansible-galaxy install -r requirements.yml 2>&1 > /dev/null

# Run playbook
echo -e "${ARROW} ${CYAN}Running playbook...${NC}"
_task "Running playbook"
if [[ -f $VAULT_SECRET ]]; then
echo -e "${ARROW} ${CYAN}Using vault config file...${NC}"
ansible-playbook --vault-password-file $VAULT_SECRET "$DOTFILES_DIR/main.yml" "$@"
else
echo -e "${WARNING} ${CYAN}Vault config file not found...${NC}"
ansible-playbook "$DOTFILES_DIR/main.yml" "$@"
fi

Expand Down
44 changes: 44 additions & 0 deletions roles/bash/files/bash/tasks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

# generate a timestamped log file name
generate_log() {
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
RANDOM_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
echo "$TMP/bash._task.$TIMESTAMP-$RANDOM_STRING.log"
}


# _header colorize the given argument with spacing
function _task {
# if _task is called while a task was set, complete the previous
if [[ $TASK != "" ]]; then
printf "${OVERWRITE}${LGREEN} [✓] ${LGREEN}${TASK}\n"
fi
# set new task title and print
TASK="$*"
printf "${LBLACK} [ ] ${TASK} \n${LRED}"
}

function _clear_task {
TASK=""
}

# _cmd performs commands with error checking
function _cmd {
LOG=$(generate_log)
# hide stdout, on error we print and exit
if eval "$1" 1> /dev/null 2> $LOG; then
rm $LOG
return 0 # success
fi
# read error from log and add spacing
printf "${OVERWRITE}${LRED} [X] ${TASK}${LRED}\n"
while read line; do
printf " ${line}\n"
done < $LOG
printf "\n"
rm $LOG
return 1
}


28 changes: 21 additions & 7 deletions roles/bash/files/bash/vars.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#!/usr/env/bin bash

#color codes
RED='\033[1;31m'
YELLOW='\033[1;33m'
BLUE="\\033[38;5;27m"
SEA="\\033[38;5;49m"
GREEN='\033[1;32m'
CYAN='\033[1;36m'
# color codes
RESTORE='\033[0m'
NC='\033[0m'
BLACK='\033[00;30m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
SEA="\\033[38;5;49m"
LIGHTGRAY='\033[00;37m'
LBLACK='\033[01;30m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
OVERWRITE='\e[1A\e[K'

#emoji codes
CHECK_MARK="${GREEN}\xE2\x9C\x94${NC}"
Expand All @@ -19,3 +32,4 @@ BOOK="${RED}\xF0\x9F\x93\x8B${NC}"
HOT="${ORANGE}\xF0\x9F\x94\xA5${NC}"
WARNING="${RED}\xF0\x9F\x9A\xA8${NC}"
RIGHT_ANGLE="${GREEN}\xE2\x88\x9F${NC}"

26 changes: 13 additions & 13 deletions roles/bash/files/themes/axin/axin.theme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ function k8s_info() {
fi
}

function rancher_info() {
# if the rancher command exists, use it to get the current context
# which will run 'rancher context current' and parse the data from this example output:
# Cluster:sandbox Project:dufus-test to look like [sandbox@dufus-test]
if [[ -x "$(command -v rancher)" ]]; then
local rancher_data="$(rancher context current 2> /dev/null | sed -E 's/Cluster:(\w+) Project:(\w+)/\1@\2/')"
if [[ "$rancher_data" != "" ]]; then
echo "[$rancher_data]"
fi
return
fi
}
# function rancher_info() {
# # if the rancher command exists, use it to get the current context
# # which will run 'rancher context current' and parse the data from this example output:
# # Cluster:sandbox Project:dufus-test to look like [sandbox@dufus-test]
# if [[ -x "$(command -v rancher)" ]]; then
# local rancher_data="$(rancher context current 2> /dev/null | sed -E 's/Cluster:(\w+) Project:(\w+)/\1@\2/')"
# if [[ "$rancher_data" != "" ]]; then
# echo "[$rancher_data]"
# fi
# return
# fi
# }

function _omb_theme_PROMPT_COMMAND() {
PS1="\[${BOLD}${MAGENTA}\]\u \[$WHITE\]@ \[$ORANGE\]\h \[$WHITE\]in \[$GREEN\]\w\[$WHITE\]\[$SCM_THEME_PROMPT_PREFIX\]$(clock_prompt)\[$PURPLE\]\$(scm_prompt_info)$YELLOW\$(k8s_info)$CYAN\$(rancher_info)$PURPLE\n\$ \[$RESET\]"
PS1="$(_clear_task)\[${BOLD}${MAGENTA}\]\u \[$WHITE\]@ \[$ORANGE\]\h \[$WHITE\]in \[$GREEN\]\w\[$WHITE\]\[$SCM_THEME_PROMPT_PREFIX\]$(clock_prompt)\[$PURPLE\]\$(scm_prompt_info)$YELLOW\$(k8s_info)$PURPLE\n\$ \[$RESET\]"
}
THEME_CLOCK_COLOR=${THEME_CLOCK_COLOR:-"${_omb_prompt_white}"}

Expand Down

0 comments on commit 026d636

Please sign in to comment.