DEV Community

Cover image for Automation with Bash - Creating a Script to Install and Configure Applications on multiple flavours of OS.
UWABOR KING COLLINS
UWABOR KING COLLINS

Posted on

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

Introduction

Welcome to day 9 of my #120DaysOfDevOps journey! In our previous session on day 8, we delved into the world of Bash scripting. We explored the power of variables and conditional statements to create robust scripts. Additionally, we ventured into the realm of some advanced Linux commands, equipping ourselves with valuable tools for automation.

During our hands-on projects, we had the opportunity to apply our newfound knowledge by creating scripts to add user accounts with passwords. This practical experience further solidified our understanding of Bash scripting and its practical applications in real-world scenarios.

If you missed day 8, I highly recommend checking it out to catch up on the foundations we laid for today's session. Get ready to dive deeper into the exciting world of DevOps as we continue our journey together!

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 my day 9 journey, we will focus on two popular Linux distributions: red-hat versions, and Debian versions. likewise two popular OS distribution 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 Mac employs Homesbrew (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

In the next section, we will delve into the core of our script and explore how to write the Bash script to automate the installation and configuration of applications on different distributions.

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.

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

Top comments (0)