Bash Scripting

Himanshu SharmaHimanshu Sharma
3 min read

Bash Scripting?

Bash scripting automates tasks in Unix-based operating systems using the Bash command language. It involves writing commands in a script to perform repetitive tasks, manage system operations, and automate processes. Bash scripts can include loops and conditionals for complex workflows and are commonly used in system administration, data processing, and software development.

In DevOps, bash scripting can be beneficial in many ways, automating numerous tasks, and enabling the creation of custom commands using bash scripts.

problem 1

I am deploying some UAT applications as containers on my UAT server! Sometimes, developers need application logs. On platforms like Google Chat/Zoho, there is a limit on the number of words I can share in chats. How do I share such custom logs with them? Do I always manually copy them and make a file to share?

No! You don’t need to do that. Instead,

  • We will configure a custom script that will take input as a container name and the number of lines you want from the container logs.

  • Then, we will configure it with a Google Chat webhook. We will create a space in Google Chat and use the webhook.

  • Whenever the script runs, it will share the logs of the specific container in the Google Chat space, where the developer can access the logs.

  1. Create space in Google Chatbox

  2. Generate a webhook for the space:

    • Expand Space Chat

    • Click on the arrow near the space name (left side)

    • Click on App & Integrations

    • Add Webhook

  3. Create a script on the UAT server:

     vim containerlogs.sh
    
  4. Add This script code to the script

     #!/bin/bash
    
     # Default number of lines to fetch if not specified
     DEFAULT_LINES=10
    
     # Function to print usage
     print_usage() {
       echo "Usage: $0 [--line <number_of_lines>] <container_name>"
       exit 1
     }
    
     # Parse the input arguments
     if [[ "$#" -eq 1 ]]; then
       # Only container name is provided
       NUMBER_OF_LINES=$DEFAULT_LINES
       CONTAINER_NAME=$1
     elif [[ "$#" -eq 3 && "$1" == "--line" ]]; then
       # --line and number_of_lines are provided
       NUMBER_OF_LINES=$2
       CONTAINER_NAME=$3
     else
       print_usage
     fi
    
     # Set your Google Chat webhook URL
     GOOGLE_CHAT_WEBHOOK_URL="https://Google-chat-webhook"
    
     # Fetch the specified number of lines of logs from the Docker container
     LOGS=$(docker logs --tail "$NUMBER_OF_LINES" "$CONTAINER_NAME" 2>&1)
    
     # Escape special characters in the logs for JSON
     ESCAPED_LOGS=$(echo "$LOGS" | jq -R -s '.')
    
     # Prepare the JSON payload for Google Chat
     PAYLOAD=$(cat <<EOF
     {
       "text": $ESCAPED_LOGS
     }
     EOF
     )
    
     # Send the logs to Google Chat
     curl -X POST -H "Content-Type: application/json" -d "$PAYLOAD" "$GOOGLE_CHAT_WEBHOOK_URL"
    
     # Check if the request was successful
     if [[ $? -eq 0 ]]; then
       echo "Logs sent to Google Chat successfully."
     else
       echo "Error sending logs to Google Chat."
     fi
    
  5. Make this script Executable

     chmod a+x containerlogs.sh
    
  6. Just to use this script as a command, copy it to /usr/bin, run:

     cp -p containerlogs.sh /usr/bin/containerlogs
    
  7. Now test the script; run:

     containerlogs --line 30 <container-name>
    
  8. The Logs should be transferred to Google chat space.

0
Subscribe to my newsletter

Read articles from Himanshu Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu Sharma
Himanshu Sharma