From 52a5aede4d8bbb3bf5f0adbedefd212f88fb9564 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 11 Mar 2022 13:20:29 -0500 Subject: [PATCH] test --- 00-dependencies.sh | 19 ++++++------ 01-motd.sh | 1 + 02-topgrade.sh | 12 ++----- 03-bash_aliases.sh | 11 ++----- 04-freeipa_realm_join.sh | 3 +- common/functions.sh | 67 ++++++++++++++++++++++++++++++++++++++++ install/install.sh | 2 +- template_setup.sh | 1 - 8 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 common/functions.sh diff --git a/00-dependencies.sh b/00-dependencies.sh index 6eef0e8..0e2ee4e 100644 --- a/00-dependencies.sh +++ b/00-dependencies.sh @@ -3,8 +3,7 @@ echo "Checking Dependencies" ## Script Location SCRIPTDIR=$(dirname $0) - -apt update +source "$SCRIPTDIR/common/functions.sh" ## Start File Loop ## For .dependency files In The dependencies Directory @@ -12,24 +11,24 @@ DEPENDENCIESALL="$SCRIPTDIR/dependencies/*.dep" for f in $DEPENDENCIESALL do ## Name Of Package - DEPENDENCYNAME=$(echo "`basename $f | cut -f 1 -d '.'`") + DEPENDENCYCOMMAND=$(echo "`basename $f | cut -f 1 -d '.'`") ## Actual Package DEPENDENCYPACKAGE=`cat $f` - echo "* Checking For $DEPENDENCYNAME with package name $DEPENDENCYPACKAGE" + echo "* Checking For command $DEPENDENCYCOMMAND with package name $DEPENDENCYPACKAGE" - if which $DEPENDENCYNAME >/dev/null; + if is_command $DEPENDENCYCOMMAND; then - echo "** $DEPENDENCYNAME Is Already Installed." + echo "** $DEPENDENCYCOMMAND Is Already Installed." else - echo "** Installing $DEPENDENCYNAME" - apt install -y $DEPENDENCYPACKAGE + echo "** Installing $DEPENDENCYCOMMAND" + install_pkg $DEPENDENCYPACKAGE fi - if which $DEPENDENCYNAME >/dev/null; + if is_command $DEPENDENCYCOMMAND; then : else - echo "** Error Installing $DEPENDENCYNAME" + echo "** Error Installing $DEPENDENCYCOMMAND" fi ## End Of loop done diff --git a/01-motd.sh b/01-motd.sh index 1df6cc3..d84fb77 100644 --- a/01-motd.sh +++ b/01-motd.sh @@ -3,6 +3,7 @@ echo "Checking For Custom MOTD" ## Script Location SCRIPTDIR=$(dirname $0) +source "$SCRIPTDIR/common/functions.sh" ## Start File Loop ## For .dependency files In The dependencies Directory diff --git a/02-topgrade.sh b/02-topgrade.sh index 4bef82b..4768808 100644 --- a/02-topgrade.sh +++ b/02-topgrade.sh @@ -2,7 +2,8 @@ echo "Checking Topgrade Situation" ## Script Location -# SCRIPTDIR=$(dirname $0) +SCRIPTDIR=$(dirname $0) +source "$SCRIPTDIR/common/functions.sh" echo "* Checking for topgrade directory" topgrade_install_directory="/opt/topgrade" @@ -38,14 +39,7 @@ fi echo "checking for topgrade confs" custom_topgrade_confs_path="/etc/topgrade" custom_topgrade_confs_git="https://git.deathbybandaid.net/deathbybandaid/topgrade.git" -if [ ! -d "$custom_topgrade_confs_path" ]; then - echo "** Installing topgrade configs" - git clone --quiet $custom_topgrade_confs_git $custom_topgrade_confs_path -else - echo "** Updating topgrade configs" - git -C $custom_topgrade_confs_path pull -fi - +git_update "topgrade" "$custom_topgrade_confs_path" "$custom_topgrade_confs_git" ## Update if [ -f "$topgrade_binary" ]; then diff --git a/03-bash_aliases.sh b/03-bash_aliases.sh index ee4f912..7ceb673 100644 --- a/03-bash_aliases.sh +++ b/03-bash_aliases.sh @@ -2,19 +2,14 @@ echo "Checking Custom Bash Aliases" ## Script Location -# SCRIPTDIR=$(dirname $0) +SCRIPTDIR=$(dirname $0) +source "$SCRIPTDIR/common/functions.sh" BASHRCFILE="$HOME/.bashrc" custom_bash_aliases_path="/etc/bash_aliases" custom_bash_aliases_git="https://git.deathbybandaid.net/deathbybandaid/bash_aliases.git" -if [ ! -d "$custom_bash_aliases_path" ]; then - echo "* Installing Bash Aliases" - git clone --quiet $custom_bash_aliases_git $custom_bash_aliases_path -else - echo "* Updating Bash Aliases" - git -C $custom_bash_aliases_path pull -fi +git_update "topgrade" "$custom_bash_aliases_path" "$custom_bash_aliases_git" echo "* Checking if bash aliases are setup in ~/.bashrc" if [[ $(grep -L "$custom_bash_aliases_path" $BASHRCFILE) ]]; then diff --git a/04-freeipa_realm_join.sh b/04-freeipa_realm_join.sh index 77d5caf..28ee511 100644 --- a/04-freeipa_realm_join.sh +++ b/04-freeipa_realm_join.sh @@ -2,7 +2,8 @@ echo "Checking IPA Realm Join Status" ## Script Location -# SCRIPTDIR=$(dirname $0) +SCRIPTDIR=$(dirname $0) +source "$SCRIPTDIR/common/functions.sh" # TODO setup /etc/hosts with FQDN and FreeIPA servers diff --git a/common/functions.sh b/common/functions.sh new file mode 100644 index 0000000..b8c1509 --- /dev/null +++ b/common/functions.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +is_command() { + local check_command="$1" + command -v "${check_command}" >/dev/null 2>&1 +} + +git_update() { + local git_name="$1" + local git_path="$2" + local git_repo="$3" + if [ ! -d "$git_path" ]; then + echo "* Installing $git_name" + git clone --quiet $git_repo $git_path + else + echo "* Updating $git_name" + git -C $git_path pull + fi +} + +should_apt_last_update() { + LAST_UPDATED=$( stat --format="%X" /var/cache/apt/pkgcache.bin ) + UNIX_TIME=$( date +%s ) + TIME_DIFF=$(( UNIX_TIME - LAST_UPDATED )) + if [[ "${TIME_DIFF}" -gt 43200 ]] + then + # 0 = true + return 0 + else + # 1 = false + return 1 + fi +} + +get_pkg_mgr() { + if is_command apt-get ; then + echo "apt-get" + elif is_command rpm ; then + # Then check if dnf or yum is the package manager + if is_command dnf ; then + echo "dnf" + else + echo "yum" + fi + fi +} + +install_pkg() { + + # package to install + local pkg_to_install="$1" + + # Get Command language + PKG_MANAGER=$( get_pkg_mgr ) + + # Run apt-get update for distros that use apt-get + if [ "$PKG_MANAGER" == "apt-get" ] ; then + if should_apt_last_update ; then + "${PKG_MANAGER} update" + fi + fi + + # Install the package + local PKG_INSTALL=("${PKG_MANAGER}" install -y) + + "${PKG_INSTALL[@]}" "${pkg_to_install}" +} diff --git a/install/install.sh b/install/install.sh index 7caab5b..cbf18cf 100644 --- a/install/install.sh +++ b/install/install.sh @@ -71,7 +71,7 @@ if is_command git; then echo "* git Is Already Installed." else - echo "* Running Update command and Installing git" + echo "* Installing git" install_pkg git fi diff --git a/template_setup.sh b/template_setup.sh index 7004aed..0bc3325 100644 --- a/template_setup.sh +++ b/template_setup.sh @@ -3,7 +3,6 @@ echo "Setting up template environment" ## Script Location SCRIPTDIR=$(dirname $0) -# SCRIPTREPO="https://git.deathbybandaid.net/deathbybandaid/template_setup.git" git -C $SCRIPTDIR pull echo ""