diff --git a/autodeploy.sh b/autodeploy.sh index 5165186..f5eb8e8 100755 --- a/autodeploy.sh +++ b/autodeploy.sh @@ -11,6 +11,7 @@ TICK="[$GREEN+$ENDCOLOR] " TICK_MOVE="[$GREEN~>$ENDCOLOR]" TICK_BACKUP="[$GREEN<~~$ENDCOLOR] " TICK_INPUT="[$YELLOW!$ENDCOLOR] " +TICK_ERROR="[$RED!$ENDCOLOR] " TAB="--" CONFIG_PATH=~/.config/autodeploy HOST_CONFIG_PATH=~/.config/autodeploy/$(hostname)_config/ @@ -18,11 +19,44 @@ BACKUP_DIR=$HOST_CONFIG_PATH"backup/" FILE_LOG=$HOST_CONFIG_PATH$(hostname)_files.log remote_repo="http://iroh.int/Graham/ConfigFiles.git" user=$(hostname) -usage() { echo "Usage: $0 [-s <45|90>] [-p ]" 1>&2; exit 1; } # Copy and pasted, need to update +selected_config=$HOST_CONFIG_PATH + +usage() { + +echo $GREEN"-------------------------------------------------------------------"$ENDCOLOR +echo $GREEN"*** $BLUE AutoDeploy - A pure bash configuration management tool$GREEN ***"$ENDCOLOR +echo $GREEN"-------------------------------------------------------------------"$ENDCOLOR +echo " +$BLUE Usage: + autodeploy -h + autodeploy -e [apps|config|files] + +$BLUE Options: + $GREEN -h$BLUE Show this [h]elp screen. + $GREEN-l$BLUE [L]ist available configuration files + $GREEN-p$BLUE [P]ush files to remote repository + $GREEN-g$BLUE [G]et files from the remote repository + $GREEN-c$BLUE [C]ollect configuration files on the local file system and prepare them for a remote push (-p) + $GREEN-f$BLUE Re-run [f]irst time setup + $GREEN-a$BLUE [I]nstalls applications found in autodeploy_apps.conf + $GREEN-b$BLUE [B]acks up files defined in autodeploy_files.conf + $GREEN-m$BLUE [M]oves dotfiles defined in autodeploy_files.conf to their correct locations on the local machine + $GREEN-e $BLUE[E]dits autodeploy's configuration files + + --moored Moored (anchored) mine. + --drifting Drifting mine. + " + exit 0 + +} + clear first_setup(){ +# This function is run if the the directory ~/.config/autodeploy is not detected +# It handles creating the configuration directory, establishing the git repository for configuration files, and initializing +# the ~/.config/autodeploy/ as a git repository echo $TICK$GREEN"Running first time setup"$ENDCOLOR echo $TICK$GREEN"Creating Configuration Files in $BLUE~/.config/autodeploy/ "$ENDCOLOR mkdir -p ~/.config/autodeploy/ @@ -31,24 +65,20 @@ first_setup(){ echo $TICK$GREEN"For example: $BLUE"https://github.com/grahamhelton/DotFiles""$ENDCOLOR echo -n $TICK_INPUT$GREEN"Enter Remote Repository URL: $YELLOW" - read remote_repo - echo $TICK$GREEN"Setting Remote Repository to: $YELLOW$remote_repo " + read remote_repo + echo $NOCOLOR$TICK$GREEN"Setting Remote Repository to: $YELLOW$remote_repo " - #. ~/.config/autodeploy/global_config.conf + #. ~/.config/autodeploy/autodeploy_config.conf cd $CONFIG_PATH - git init > /dev/null 2>&1; - git remote add origin $remote_repo > /dev/null 2>&1; - git checkout -b main> /dev/null 2>&1; + git init #> /dev/null 2>&1; + git remote add origin $remote_repo #> /dev/null 2>&1; + git checkout -b main #> /dev/null 2>&1; } -# Handle command line options. Not sure why this isn't working inside a function - get_posture(){ -# This function is used to determine what kind of system the script is being deployed on. It will check for the following items: -# 1. Internet connectivity -# 2. Checks for dependencies -# 3. +# This function is used to determine if the current system has the required dependencies. Currently it only checks for apt and git + # Change to wget -q --spider $remote_repo then check for return code with $? if ping -c 1 8.8.8.8 > /dev/null 2>&1; then echo $TICK"Internet Connectivity Detected"$ENDCOLOR @@ -68,67 +98,102 @@ get_posture(){ } -install_apt(){ +install_apps(){ +# This function is responsible for installing any applications(using the apt package manager) defined in ~/.config/autodeploy/autodeploy_apps.conf echo $TICK$BLUE"Please input SUDO password"$ENDCOLOR echo $TICK$GREEN"Running apt update and apt upgrade..."$ENDCOLOR ; sudo apt update > /dev/null 2>&1 && sudo apt upgrade -y > /dev/null 2>&1 - echo $TICK$GREEN"Installing applications from $CONFIG_PATH/global_apps.conf"$ENDCOLOR && temp_output=$(xargs sudo apt install -y < $CONFIG_PATH/global_apps.conf) - echo $temp_output + #echo "$install" + echo $TICK$GREEN"Installing applications from $BLUE$CONFIG_PATH/autodeploy_apps.conf"$ENDCOLOR + grep -v '^#' $CONFIG_PATH/autodeploy_apps.conf | while read -r line; do + echo $TICK$GREEN"Installing $BLUE$line"$ENDCOLOR + sudo apt install $line -y > /dev/null 2>&1 #| grep -A 1 "NEW packages" | grep -v "NEW packages" + done + echo $TICK$GREEN"Applications installed."$ENDCOLOR } + list_configs(){ + echo $TICK$GREEN"Listing configs found in $BLUE$CONFIG_PATH"$ENDCOLOR echo -n $BLUE ls $CONFIG_PATH | grep "_config$" } -stage_files(){ - git -C $CONFIG_PATH pull origin main --allow-unrelated-histories +collect_files(){ + git -C $CONFIG_PATH pull origin main --allow-unrelated-histories #> /dev/null 2>&1 # Grab files from around the system and move them to $CONFIG_PATH - echo $TICK$GREEN"Staging Files!"$ENDCOLOR + echo $TICK$GREEN"Moving files"$ENDCOLOR if test -d $HOST_CONFIG_PATH;then - echo $TICK$GREEN"Config file already found in $BLUE$CONFIG_PATH/$(hostname)_config"$ENDCOLOR + echo $TICK$GREEN"Creating files in$BLUE$CONFIG_PATH/$(hostname)_config"$ENDCOLOR else mkdir -p $HOST_CONFIG_PATH - echo $TICK$GREEN"Created config file in $BLUE$CONFIG_PATH/$(hostname)_config"$ENDCOLOR + echo $TICK$GREEN"Creating files in$BLUE$CONFIG_PATH/$(hostname)_config"$ENDCOLOR fi - # Copy each line in $CONFIG_PATH/global_dotFiles.conf to $HOST_CONFIG_PATH + # Copy each line in $CONFIG_PATH/autodeploy_files.conf to $HOST_CONFIG_PATH cd $HOME while read line; do cp -rvf --parents $line $HOST_CONFIG_PATH | grep "^'" | awk '{print $1}' | sed "s/'//g" | sed 's@'"$HOME"'@$HOME@' >> $HOST_CONFIG_PATH/$(hostname)_files.log # Going to need to add sorting somewhere in here because this log will keep growing - echo $TICK_MOVE$GREEN"Copied $BLUE$line$GREEN to $BLUE$HOST_CONFIG_PATH"$ENDCOLOR - done < $CONFIG_PATH/global_dotFiles.conf + echo $TICK_MOVE$GREEN" Copied $BLUE$line$GREEN to $BLUE$HOST_CONFIG_PATH"$ENDCOLOR + done < $HOST_CONFIG_PATH/autodeploy_files.conf | grep -v "^#" cd $CONFIG_PATH - echo $TICK$GREEN"Configuration files saved to $BLUE$HOST_CONFIG_PATH$GREEN, ready to push!"$ENDCOLOR + echo $TICK$GREEN"Configuration files saved to $BLUE$HOST_CONFIG_PATH$GREEN. Files can be pushed to $BLUE$remote_repo$GREEN with$BLUE autodeploy -p$GREEN"$ENDCOLOR } edit_files() { + if [ $OPTARG = "apps" ];then + "${EDITOR:-vi}" $CONFIG_PATH/autodeploy_apps.conf + elif [ $OPTARG = "config" ];then + "${EDITOR:-vi}" $CONFIG_PATH/autodeploy_config.conf + elif [ $OPTARG = "files" ];then + "${EDITOR:-vi}" $CONFIG_PATH/autodeploy_files.conf + else + usage + fi +} +move_dev(){ + if [ -z "$2" ];then + echo $TICK$GREEN"No arguments supplied, using configuration in $BLUE$HOST_CONFIG_PATH"$ENDCOLOR + collect_files + else + select_config "$@" + fi + -"${EDITOR:-vi}" $CONFIG_PATH/$OPTARG +} +select_config(){ + echo $TICK_ERROR$YELLOW"Please specify the name of the config file you wish to use"$ENDCOLOR + selected_config=$2 + if test -d "$CONFIG_PATH/$selected_config";then + echo $TICK$GREEN"$selected_config selected" + distribute_files + else + list_configs + fi } check_git(){ - # Check if global_dotFiles.conf is in the current repo - if ! test -f "$CONFIG_PATH/global_dotFiles.conf";then + # Check if autodeploy_files.conf is in the current repo + if ! test -f "$CONFIG_PATH/autodeploy_files.conf";then echo $TICK$GREEN"Config file not found, generating base configuration..."$ENDCOLOR - echo "config_name=$(hostname)" > $CONFIG_PATH/global_config.conf - # Add $remote_repo to global_config - echo "$remote_repo" >> $CONFIG_PATH/global_config.conf - # Add default applications to global_apps - echo "curl\nneovim\nzsh" > $CONFIG_PATH/global_apps.conf - # Add default dot files to global_dotFiles.conf - echo ".tmux.conf" > $CONFIG_PATH/global_dotFiles.conf + echo "config_name=$(hostname)" > $CONFIG_PATH/autodeploy_config.conf + # Add $remote_repo to autodeploy_config + echo "$remote_repo" >> $CONFIG_PATH/autodeploy_config.conf + # Add default applications to autodeploy_apps + echo "curl\nneovim\nzsh" > $CONFIG_PATH/autodeploy_apps.conf + # Add default dot files to autodeploy_files.conf + echo ".tmux.conf" > $CONFIG_PATH/autodeploy_files.conf fi } get_files(){ # Push files to $remote_repo cd $CONFIG_PATH - git -C $CONFIG_PATH pull origin main --allow-unrelated-histories > /dev/null 2>&1; # Figure out how to check if repo exists + git -C $CONFIG_PATH pull origin main --allow-unrelated-histories # > /dev/null 2>&1; # Figure out how to check if repo exists check_git echo $TICK$GREEN"Pull complete"$ENDCOLOR - # This has an error during first time setup. If global files are already in the $CONFIG_PATH, the pull will fail because they'll be overwritten + # This has an error during first time setup. If autodeploy files are already in the $CONFIG_PATH, the pull will fail because they'll be overwritten } @@ -150,13 +215,19 @@ backup_old(){ while read line; do echo $TICK_BACKUP$GREEN"Backing up $BLUE$line$GREEN to $BLUE$BACKUP_DIR" cp -rf $HOME/$line $BACKUP_DIR > /dev/null 2>&1; - done < $CONFIG_PATH/global_dotFiles.conf # Fix + done < $CONFIG_PATH/autodeploy_files.conf # Fix +} + +new_client(){ +get_files +collect_files +install_apps } distribute_files(){ - # Places files defined in global_dotFiles in correct folders + # Places files defined in autodeploy_file.conf in correct folders backup_old - cd $HOST_CONFIG_PATH + cd $CONFIG_PATH/$selected_config for f in .[!.]* *; do echo $TICK_MOVE$GREEN"Copying $BLUE$f$GREEN to $BLUE$HOME" cp -rf $f $HOME # Going to need to figure out a way to move all files except for the backup folder @@ -165,40 +236,20 @@ distribute_files(){ } main(){ - if [ $# -eq 0 ]; then - echo $GREEN"-------------------------------------------------------------------"$ENDCOLOR - echo $GREEN"*** $BLUE AutoDeploy - A pure bash configuration management tool$GREEN ***"$ENDCOLOR - echo $GREEN"-------------------------------------------------------------------"$ENDCOLOR - echo " -$BLUE Usage: - autodeploy -h - -$BLUE Options: - $GREEN -h$BLUE Show this [h]elp screen. - $GREEN-l$BLUE [L]ist available configuration files - $GREEN-p$BLUE [P]ush files to remote repository - $GREEN-g$BLUE [G]et files from the remote repository - $GREEN-c$BLUE [C]ollect configuration files on the local file system and prepare them for a remote push (-p) - $GREEN-f$BLUE Re-run [f]irst time setup - $GREEN-a$BLUE [I]nstalls applications found in global_apps.conf - $GREEN-b$BLUE [B]acks up files defined in global_dotFiles.conf - $GREEN-m$BLUE [M]oves dotfiles defined in global_dotFiles.conf to their correct locations on the local machine - $GREEN-e $BLUE[E]dits autodeploy's configuration files - - --moored Moored (anchored) mine. - --drifting Drifting mine. - - - " - exit 0 - fi # Check if this is the first time autodeploy is being ran +echo $selected_config if !(test -d $CONFIG_PATH);then first_setup + + echo $NOCOLOR$TICK$GREEN"First time setup complete, use$BLUE autodeploy -f $GREEN to rerun first time setup" + fi + + if [ $# -eq 0 ]; then + usage fi # Process command line arugments - while getopts "m c p s f a b l :e:" o; do + while getopts "d n m c p s f a b l :e:" o; do case "${o}" in # Pull config l) @@ -224,7 +275,7 @@ $BLUE Options: c) c=${OPTARG} echo $TICK$GREEN"Collecting config files and storing them in $BLUE$HOST_CONFIG_PATH"$ENDCOLOR - stage_files + collect_files ;; f) f=${OPTARG} @@ -234,7 +285,7 @@ $BLUE Options: a) a=${OPTARG} echo $TICK$BLUE"Installing applications"$ENDCOLOR - install_apt + install_apps ;; b) b=${OPTARG} @@ -247,6 +298,19 @@ $BLUE Options: echo $GREEN$BOLD"---------------------------------------"$ENDCOLOR distribute_files ;; + n) + n=${OPTARG} + echo $BOLD$BLUE"Running full new client install"$ENDCOLOR + echo $GREEN$BOLD"---------------------------------------"$ENDCOLOR + new_client + ;; + d) + d=${OPTARG} + echo $BOLD$BLUE"Running move dev"$ENDCOLOR + echo $OPTARG + echo $GREEN$BOLD"---------------------------------------"$ENDCOLOR + move_dev "$@" + ;; ?) usage ;;