Automation with Bash - Creating a Script to Install and Configure Applications on multiple flavours of OS.

devopsking

UWABOR KING COLLINS

Posted on July 17, 2023

Automation with Bash - Creating a Script to Install and Configure Applications on multiple flavours of OS.

Table of Contents

Introduction

Building a cross-distribution application installer can be a daunting task, given the diverse landscape of Linux distributions and package management systems. This article explores how to leverage Bash scripting to create a versatile installer that works seamlessly across Red Hat-based systems, Debian-based systems, and even Darwin (macOS).

We'll guide you through the process of crafting a script that can detect the underlying operating system, adapt to different package managers (such as APT, Yum/DNF, and Homebrew), and handle distribution-specific nuances. By the end of this guide, you'll have the knowledge to create a powerful tool that streamlines software deployment across multiple platforms, saving time and reducing the complexity of managing installations in heterogeneous environments.

Whether you're a system administrator managing a diverse Linux ecosystem or a developer looking to simplify your application's installation process, this article will provide you with practical insights and step-by-step instructions to build an efficient cross-distribution installer using Bash.

Building a Cross-Distribution Application Installer with Bash Scripting

Efficiently managing software installations across multiple Linux distributions can be a challenging task. Each distribution has its own package management system and configuration nuances, making the process complex and time-consuming. However, there is a solution: Bash scripting. By harnessing the power of Bash, we can create a robust and adaptable installer script that automates the installation and configuration of applications across different distributions.

In this article, we will focus on two popular Linux distributions: Red Hat versions and Debian versions, as well as two popular OS distributions, Linux and Darwin (for macOS). We will guide you through the process of building a cross-distribution application installer using Bash scripting, providing practical examples and step-by-step instructions. By the end of this post, you will have a powerful tool at your disposal to streamline application deployment and save valuable time.

Our approach revolves around understanding the nuances of each distribution, leveraging their respective package managers (APT, Yum/DNF, Brew), and adapting the installation steps accordingly. We will explore how to detect the distribution dynamically within the script, handle distribution-specific variations, and ensure compatibility across multiple flavours.

By combining our knowledge of Bash scripting and distribution-specific peculiarities, we can create an installer script that will seamlessly handle application installations across multiple platforms. No longer will you need to manually navigate different package managers and configuration files. With this script in your toolkit, you can ensure consistent and efficient software deployment, regardless of the distribution.

Preparing the Environment

Before we dive into creating our cross-distribution application installer with Bash scripting, it is essential to ensure that our environment is properly set up. This preparation phase lays the foundation for successful and efficient application installations across these platforms.

Understanding Package Managers

To begin, it's important to familiarise yourself with the package management systems used by each distribution. Ubuntu relies on the widely-used Advanced Package Tool (APT), CentOS utilizes Yellowdog Updater Modified (Yum) or Dandified Yum (DNF), while macOS employs Homebrew (brew).

Package Repository Configuration

Verify that the package repositories on each distribution are correctly configured. These repositories provide the necessary software packages and updates. Ensure that you have access to the appropriate repositories and that they are properly enabled. This step ensures that the installer script can fetch the required packages from the designated repositories during the installation process.

Dependency Management

Consider the dependencies required by the applications you intend to install. Identify the specific packages needed to satisfy these dependencies. Keep in mind that package names or versions might differ across distributions. It's crucial to handle these variations within our Bash script to ensure a smooth and successful installation process.

Environment Detection

Implement a mechanism within the script to dynamically detect the current distribution. This allows the script to adapt its behavior and package manager commands accordingly. There are multiple ways to accomplish this, such as checking for specific files or commands unique to each distribution. By incorporating environment detection, our installer script becomes versatile and capable of accommodating various Linux distributions.

Script Execution Permissions

Grant execution permissions to the Bash script to ensure it can be run successfully. Use the chmod command to set the appropriate permissions, making the script executable for the user executing it. This step is crucial to avoid any permission-related issues during the execution of the installer script.

By addressing these key aspects during the environment preparation phase, we lay the groundwork for a smooth and streamlined application installation process. Understanding the package managers, configuring the package repositories, managing dependencies, and implementing environment detection will set the stage for our cross-distribution application installer to function seamlessly across these platforms.

Writing the Script


!/bin/bash

Determine OS name

os=$(uname)

Install git

if [ "$os" = "Linux" ]; then

echo "This is a Linux machine"

if [[ -f /etc/redhat-release ]]; then
pkg_manager=yum
elif [[ -f /etc/debian_version ]]; then
pkg_manager=apt
fi

if [ $pkg_manager = "yum" ]; then
yum install git -y
elif [ $pkg_manager = "apt" ]; then

apt install git -y
fi

elif [ "$os" = "Darwin" ]; then

echo "This is a Mac Machine"
brew install git

else
echo "Unsupported OS"
exit 1

fi

echo "Git installed!"

Grant execution permissions to the script

chmod +x script.sh

Enter fullscreen mode Exit fullscreen mode




The screenshot of the code in my terminal

Image description

continuation of the code screenshot

Image description

Now let run our script

Image description

Now you can see we have successfully installed a software application (git) into our machine. This script will work for all the OS be it Linux or Unix(Mac).

Testing our configuration and application using scripts.

Below is the complete script code from installation to configuration and testing.


!/bin/bash

Determine OS name

os=$(uname)

Install git

if [ "$os" = "Linux" ]; then
echo "This is a Linux Machine"
if [[ -f /etc/redhat-release ]]; then
pkg_manager=yum
elif [[ -f /etc/debian_version ]]; then
pkg_manager=apt
fi

if [ $pkg_manager = "yum" ]; then
sudo yum install git -y
elif [ $pkg_manager = "apt" ]; then
sudo apt install git -y
fi

elif [ "$os" = "Darwin" ]; then
brew install git
echo "This is an Apple Mac Machine"
else
echo "Unsupported OS"
exit 1

fi

echo "Congratulations, Git has now successfully been installed!"

Grant execution permissions to the script

chmod +x installing_app_git.sh

Test the configuration

echo "Testing git configuration..."

if git --version >/dev/null 2>&1; then
echo "Git is configured correctly."
else
echo "Git configuration test failed. Please check the installation."
fi

Enter fullscreen mode Exit fullscreen mode




screenshot of the script

Image description

Now we have written the test let's confirm our code on the CLI.

Image description

The script now includes a test of the git configuration using the git --version command. It checks if the command runs successfully and outputs an appropriate message indicating whether the configuration is correct or not.

with This I believe you can now write a perfect script to install, configure and test any application on your various machines.

Conclusion

Thanks for reading! I hope to see you another day as we learn more complex automation scripting.

Connect with Me

Let's connect on LinkedIn! ā¤

Follow me on Twitter! šŸ¦

Check out my projects on GitHub! šŸ‘Øā€šŸ’»
šŸ‘Øā€šŸ’»

šŸ’– šŸ’Ŗ šŸ™… šŸš©
devopsking
UWABOR KING COLLINS

Posted on July 17, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related