Last active
June 1, 2021 11:40
-
-
Save aravindkumarsvg/96ba15dc1679d3898ff0d53096f9fa20 to your computer and use it in GitHub Desktop.
Shell Script to install, uninstall ChefDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ============================================= | |
# Installs and Uninstalls the ChefDK | |
# ============================================= | |
# | |
# -i Install ChefDK | |
# -u Uninstall ChefDK | |
# -c Verify ChefDK | |
# -h Show Help | |
# | |
# ============================================== | |
# Description: | |
# | |
# Tested and Used in elementary OS, Ubuntu 16.04. | |
# | |
# Tested in BASH shell version 4.3.46 | |
# | |
# Not backward comaptible with older bash versions, | |
# since the usage of Associate Array in the script, | |
# which is added in 4.0 version | |
# | |
# Feel free to Extend, Use, Test in other OS | |
# and post it | |
# | |
# ============================================== | |
# Variable Declarations | |
kernel_supported="linux" | |
architecture_supported="x86_64" | |
os_supported=( "debian" "ubuntu" "redhat" "suse" ) | |
declare -A elementary_ubuntu_version=( ["0.1"]="10.10" ["0.2"]="12.04" ["0.3"]="14.04" ["0.4"]="16.04" ) | |
declare -A chefdk_os_translations=( ["ubuntu"]="ubuntu" ["debian"]="debian" ["suse"]="sles" ["rhel"]="el" ) | |
chefdk_url_part1="https://packages.chef.io/files/stable/chefdk/1.5.0/:distro/:version/" | |
declare -A os_packages=( ["ubuntu"]="deb" ["debian"]="deb" ["suse"]="rpm" ["rhel"]="rpm" ) | |
declare -A chefdk_package_ext=( ["deb"]="chefdk_1.5.0-1_amd64.deb" ["rpm"]="chefdk-1.5.0-1.:distro:version.x86_64.rpm" ) | |
# Function which checks for the root user execution | |
root_checker() { | |
# Checks if the script is ran by the root user | |
if [ $EUID -ne 0 ] | |
then | |
echo -e "\nPlease run as root user!!!!!!!!!!!!!!!!!!!!!!!\n" | |
exit 1 | |
fi | |
} | |
# Checks for the kernel support | |
kernel_checker() { | |
kernel_name=`uname -s | tr "[:upper:]" "[:lower:]"` | |
if [ ! -z ${kernel_name+x} ] | |
then | |
if [ $kernel_name != $kernel_supported ] | |
then | |
echo -e "\nPlease extend the functionality to support your kernel. Current support: linux\n" | |
exit 1 | |
fi | |
else | |
echo -e "\nPlease extend the functionality to support your kernel. Current support: linux\n" | |
exit 1 | |
fi | |
unset kernel_name | |
} | |
# Checks for the processor's architecture | |
architecture_checker() { | |
architecture=`uname -p` | |
if [ ! -z ${architecture+x} ] | |
then | |
if [ $architecture != $architecture_supported ] | |
then | |
echo -e "\nSupports only x86_64 architecture\n" | |
exit 1 | |
fi | |
else | |
echo -e "\nSupports only x86_64 architecture\n" | |
exit 1 | |
fi | |
unset architecture | |
} | |
# Gets the distribution and version of the os | |
distro_checker() { | |
if [ -f /etc/lsb-release ] | |
then | |
distro=`lsb_release -si | tr "[:upper:]" "[:lower:]"` | |
version=`lsb_release -sr | egrep -o "[0-9]+.[0-9]+"` | |
elif [ -f /etc/redhat-release ] | |
then | |
distro="rhel" | |
version=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*// | cut -d. -f1` | |
elif [ -f /etc/SuSE-release ] | |
then | |
distro="suse" | |
version=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ // | cut -d. -f1` | |
elif [ -f /etc/os-release ] | |
then | |
distro=`cat /etc/os-release | grep "^ID=" | egrep -o "[a-zA-Z]+$" | tr "[:upper:]" "[:lower:]"` | |
version=`cat /etc/os-release | grep "^VERSION_ID=" | egrep -o "[0-9]+(\.[0-9]+)?" | head -1` | |
else | |
distro=`hostnamectl | grep Operating | cut -d: -f2 | egrep -o "[a-zA-Z]+" | head -1 | tr "[:upper:]" "[:lower:]"` | |
version=`hostnamectl | grep Operating | cut -d: -f2 | egrep -o "[0-9]+(\.[0-9]+)?" | head -1` | |
fi | |
# Calls the functions which maps to base distro | |
ubuntu_based_distro_checker | |
} | |
# Checks if the distro is based on ubuntu | |
ubuntu_based_distro_checker() { | |
# Condition for Elementary OS | |
if [ $distro == "elementary" ] | |
then | |
distro="ubuntu" | |
# Sets the version related to ubuntu | |
for key in ${!elementary_ubuntu_version[@]} | |
do | |
if [ $key == $version ] | |
then | |
version=${elementary_ubuntu_version[$key]} | |
break | |
fi | |
done | |
fi | |
unset key | |
} | |
# Contains all the package manager cache related operations | |
package_manager_cache() { | |
case $distro in | |
debian|ubuntu) | |
# Updates the apt cache | |
apt update | |
;; | |
esac | |
} | |
# Prepares the ChefDK URL based on the system informations | |
chefdk_url_preparer() { | |
# Gets the package related to the distro | |
local package=${os_packages[$distro]} | |
# Gets the ChefDK extension related to that package | |
local chefdk_url_ext=${chefdk_package_ext[$package]} | |
# Translates the os name to chef dk supported name | |
local chefdk_os_name=${chefdk_os_translations[$distro]} | |
chefdk_url=`echo "${chefdk_url_part1}${chefdk_url_ext}" | sed s/:distro/$chefdk_os_name/g | sed s/:version/$version/g ` | |
} | |
# Downloads and Installs the ChefDK package | |
chefdk_installer() { | |
local chefdk_package_name="chefdk" | |
local package_name="/tmp/chefdk.${os_packages[$distro]}" | |
echo -e "\nDownloading ChefDK ....................\n ${chefdk_url} \n\n" | |
# Downloads the ChefDK package | |
curl -o $package_name $chefdk_url | |
# Checks for the Successful download | |
if [ $? -ne 0 ] | |
then | |
echo -e "\nChefDK download failed......\n" | |
exit 1 | |
fi | |
# Gets the SHA1 checksum of the package | |
sha1_hash=$(curl -I $chefdk_url | grep -i sha1 | cut -d: -f2 | egrep -o "[0-9a-zA-Z]+") | |
# Checks for the Successful download | |
if [ $? -ne 0 ] | |
then | |
echo -e "\nChefDK download failed......\n" | |
exit 1 | |
fi | |
# Checks the sha1 checksum | |
echo $sha1_hash $package_name | sha1sum -c - | |
# Checks for the SHA1 hash | |
if [ $? -ne 0 ] | |
then | |
echo -e "\nChefDK download failed......\n" | |
exit 1 | |
fi | |
echo -e "\nInstalling ChefDK ....................\n" | |
case ${os_packages[$distro]} in | |
deb) dpkg --list | grep -i ${chefdk_package_name} | |
# Checks for the availability of chefdk package | |
if [ $? -eq 0 ] | |
then | |
echo -e "\nChefDK is already installed......\n" | |
exit 1 | |
fi | |
dpkg -i $package_name | |
echo -e "\nChefDK is successfully installed\n" | |
;; | |
rpm) rpm -qa | grep -i ${chefdk_package_name} | |
# Checks for the availability of chefdk package | |
if [ $? -eq 0 ] | |
then | |
echo -e "\nChefDK is already installed......\n" | |
exit 1 | |
fi | |
rpm -ivh $package_name | |
echo -e "\nChefDK is successfully installed\n" | |
;; | |
*) echo -e "\nYour Operating system is not Supported!!!\n" | |
exit 1 | |
;; | |
esac | |
} | |
# Checks for the availability of ChefDK package | |
chefdk_availability_checker() { | |
local chefdk_package_name="chefdk" | |
case ${os_packages[$distro]} in | |
deb) dpkg --list | grep -i ${chefdk_package_name} > /dev/null | |
# Checks for the availability of chefdk package | |
if [ $? -eq 0 ] | |
then | |
echo -e "\nChefDK is already installed......\n" | |
exit 1 | |
fi | |
;; | |
rpm) rpm -qa | grep -i ${chefdk_package_name} > /dev/null | |
# Checks for the availability of chefdk package | |
if [ $? -eq 0 ] | |
then | |
echo -e "\nChefDK is already installed......\n" | |
exit 1 | |
fi | |
;; | |
*) echo -e "\nYour Operating system is not Supported!!!\n" | |
exit 1 | |
;; | |
esac | |
} | |
# Checks whether ChefDK installed successfully or not | |
chefdk_installation_checker() { | |
# Verifies Chef | |
chef verify 2>/dev/null | |
if [ $? -eq 0 ] | |
then | |
echo -e "\nChefDK is installed successfully!!!!!!!!!!!!!!!\n" | |
else | |
echo -e "\nChefDK is not installed or configured properly......\n" | |
exit 1 | |
fi | |
} | |
# Uninstalls the ChefDK package | |
chefdk_uninstaller() { | |
local chefdk_package_name="chefdk" | |
echo -e "\nUninstalling ChefDK ....................\n" | |
case ${os_packages[$distro]} in | |
deb) dpkg --list | grep -i ${chefdk_package_name} > /dev/null | |
# Checks for the availability of chefdk package | |
if [ $? -ne 0 ] | |
then | |
echo -e "\nChefDK not found......\n" | |
exit 1 | |
fi | |
dpkg -r ${chefdk_package_name} | |
echo -e "\nChefDK is successfully uninstalled\n" | |
;; | |
rpm) rpm -qa | grep -i ${chefdk_package_name} > /dev/null | |
# Checks for the availability of chefdk package | |
if [ $? -ne 0 ] | |
then | |
echo -e "\nChefDK not found......\n" | |
exit 1 | |
fi | |
rpm -evv ${chefdk_package_name} | |
echo -e "\nChefDK is successfully uninstalled\n" | |
;; | |
*) echo -e "\nYour Operating system is not Supported!!!\n" | |
exit 1 | |
;; | |
esac | |
} | |
# Installs CURL utility | |
curl_installer() { | |
echo -e "\nInstalling CURL ....................\n" | |
case $distro in | |
debian|ubuntu) apt install -y curl | |
echo -e "\nCURL is successfully installed\n" | |
;; | |
rhel) yum -y install curl | |
echo -e "\nCURL is successfully installed\n" | |
;; | |
suse) zypper --non-interactive install curl | |
echo -e "\nCURL is successfully installed\n" | |
;; | |
*) echo -e "\nYour Operating system is not Supported!!!\n" | |
exit 1 | |
;; | |
esac | |
} | |
# Parses the Input options | |
input_options_parser() { | |
# Available Input options | |
declare -A input_options=( ["-i"]="install" ["-u"]="uninstall" ["-c"]="check" ) | |
local input_options_pattern="<-i><-u><-c>" | |
local input_options_help_pattern="<-h>" | |
# sets the help test | |
read -r -d '' help_text <<EndOfHelp | |
\n | |
\t -i \t Install ChefDK \n | |
\t -u \t Uninstall ChefDK \n | |
\t -c \t Verify ChefDK \n | |
\t -h \t Show Help \n | |
\n | |
EndOfHelp | |
# Error options output | |
local error_options_text="\n\tUse -h for help\n" | |
# Error options flag | |
local error_options_flag=0 | |
# Help options flag | |
local help_options_flag=0 | |
local options_set_flag=0 | |
# Checks for the number of options | |
if [ $# -eq 0 ] | |
then | |
error_options_flag=1 | |
else | |
# Loops through the Input Options | |
for option in $@ | |
do | |
# Prepares the pattern from the input options | |
option_pattern=$( echo "<${option}>" | sed "s/\-/\\\\-/g" ) | |
# Checks for the help option | |
echo $input_options_help_pattern | grep $option_pattern >/dev/null | |
if [ $? -eq 0 ] | |
then | |
help_options_flag=1 | |
break | |
fi | |
# Checks for the other options | |
echo $input_options_pattern | grep $option_pattern >/dev/null | |
if [ $? -ne 0 ] | |
then | |
error_options_flag=1 | |
else | |
if [ $options_set_flag -eq 0 ] | |
then | |
chefdk_operation=${input_options[$option]} | |
options_set_flag=1 | |
else | |
error_options_flag=1 | |
fi | |
fi | |
done | |
fi | |
# Prints the help text | |
if [ $help_options_flag -eq 1 ] | |
then | |
echo -e $help_text | |
exit 0 | |
fi | |
# Prints the Error text | |
if [ $error_options_flag -eq 1 ] | |
then | |
echo -e $error_options_text | |
exit 1 | |
fi | |
unset help_text | |
unset input_options | |
} | |
# Gets all the details related to system | |
system_details() { | |
# Checks for the processor architecture | |
architecture_checker | |
# Checks for the kernel support | |
kernel_checker | |
# Checks the distro and its version | |
distro_checker | |
} | |
# Install some necessary tools | |
utilities_installer() { | |
# Functionalities related to Package Manager | |
package_manager_cache | |
# Installs CURL | |
curl_installer | |
} | |
# Operations related to ChefDK | |
chefdk_stuffs() { | |
case $chefdk_operation in | |
install) | |
# Checks for the availability of ChefDK package | |
chefdk_availability_checker | |
# Installs necessary utilities | |
utilities_installer | |
# Functionalities related to Package Manager | |
package_manager_cache | |
# Prepares the url for downloading ChefDk | |
chefdk_url_preparer | |
# Installs ChefDK | |
chefdk_installer | |
# Verfies ChefDK installation | |
chefdk_installation_checker | |
;; | |
uninstall) | |
# Uninstalls the ChefDK package | |
chefdk_uninstaller | |
;; | |
check) | |
# Verifies the ChefDK | |
chefdk_installation_checker | |
;; | |
*) | |
echo -e "\nInvalid option!!!!!!!\n" | |
exit 1 | |
;; | |
esac | |
} | |
# Main execution context | |
main() { | |
# Parses the input options | |
input_options_parser $@ | |
# Checks for the execution by root user | |
root_checker | |
# Gets the System related details | |
system_details | |
# ChefDK related stuffs execution | |
chefdk_stuffs | |
} | |
# Calls the main function | |
main $@ | |
# Exits with code 0 - success | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment