DEV Community

Shivam Agnihotri
Shivam Agnihotri

Posted on

Advanced Scripting Scenarios in DevOps : Day 29 of 50 days DevOps Tools Series

Welcome to Day 28 of our "50 DevOps Tools in 50 Days" series! today, In our journey through the "50 DevOps Tools in 50 Days" series, we've explored essential scripting languages like Bash and Python, covering fundamental and production-level examples. Today, we'll dive into advanced scripting scenarios that weren't previously covered. These scenarios are critical for automating complex tasks and enhancing your efficiency as a DevOps engineer.

1. Multi-Step Deployment Automation

In many production environments, deploying an application involves multiple steps such as pulling the latest code, building it, running tests, and finally deploying it to the server. Automating this process with scripting can save significant time and reduce errors.

Example:
A bash script to automate the deployment process:

#!/bin/bash

# Step 1: Pull the latest code
echo "Pulling latest code..."
git pull origin main

# Step 2: Build the project
echo "Building the project..."
npm install && npm run build

# Step 3: Run tests
echo "Running tests..."
npm test

# Step 4: Deploy to the server
echo "Deploying to server..."
scp -r ./dist user@server:/var/www/html

echo "Deployment complete."
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • The script covers pulling code from a repository, building the project, running tests, and deploying to the server.
  • Each step is logged for better tracking and troubleshooting. Ensures consistency across deployments, reducing the risk of human error.

2. Error Handling and Logging in Scripts

Error handling is a critical aspect of scripting, especially when running scripts in production environments. Proper error handling ensures that the script fails gracefully and logs helpful information for debugging.

Example:
A Python script with error handling and logging:

import os
import logging

# Setup logging
logging.basicConfig(filename='deployment.log', level=logging.INFO)

def run_command(command):
    try:
        result = os.system(command)
        if result != 0:
            raise Exception(f"Command failed: {command}")
        logging.info(f"Successfully ran: {command}")
    except Exception as e:
        logging.error(e)
        exit(1)

# Example usage
run_command("git pull origin main")
run_command("npm install")
run_command("npm run build")
run_command("npm test")
run_command("scp -r ./dist user@server:/var/www/html")
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • The script logs all commands, making it easier to debug if something goes wrong.
  • If any command fails, the script logs the error and exits, preventing further steps from executing in an unstable state.

3. Environment Configuration Management

Managing different environments (development, staging, production) often requires tweaking configurations, which can be prone to errors. Scripting these changes ensures that configurations are applied consistently across environments.

Example:
A bash script to manage environment-specific configurations:

#!/bin/bash

# Load environment-specific variables
source .env.$1

# Apply configurations
echo "Setting up $ENV environment..."
export APP_ENV=$ENV
export DATABASE_URL=$DATABASE_URL

echo "Configuration applied."
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • The script loads environment-specific variables from a .env file based on the argument passed to it (dev, prod, etc.).
  • This approach centralizes configuration management and reduces the risk of configuration errors.

4. Advanced String Manipulation

String manipulation is a common task in scripting, especially when processing logs or handling dynamic configurations.

Example:
Using awk for advanced string manipulation in a bash script:

#!/bin/bash

# Extract specific fields from a log file
awk '{print $1, $3, $7}' /var/log/apache2/access.log > output.txt

# Replace a specific string in a file
sed -i 's/oldstring/newstring/g' config.yaml

echo "String manipulation complete."
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • awk is used to extract specific fields from a log file.
  • sed is used for search and replace operations, which is crucial in dynamic configuration management.

5. Dynamic Resource Allocation

In a cloud-native environment, dynamically allocating resources based on load or other factors is a common use case. Scripting can automate the process of scaling up or down resources as needed.

Example:
A Python script to dynamically allocate resources on AWS:

import boto3

client = boto3.client('ec2')

# Function to scale up instances
def scale_up_instances(count):
    response = client.run_instances(
        ImageId='ami-0abcdef1234567890',
        InstanceType='t2.micro',
        MinCount=count,
        MaxCount=count
    )
    print(f"Scaled up {count} instances.")

# Example usage
scale_up_instances(3)
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • The script uses AWS SDK (boto3) to dynamically allocate resources.
  • This approach can be extended to include monitoring and scaling based on load or other metrics.

6. Dynamic Inventory Management with Ansible and Python

In large-scale environments, managing dynamic inventories is a challenge. Combining Python with Ansible allows you to automate inventory generation based on real-time data from cloud providers or other sources.

Example:

import boto3

def generate_inventory():
    ec2 = boto3.client('ec2')
    instances = ec2.describe_instances()
    inventory = {}
    for reservation in instances['Reservations']:
        for instance in reservation['Instances']:
            instance_id = instance['InstanceId']
            public_ip = instance['PublicIpAddress']
            inventory[instance_id] = public_ip
    return inventory

inventory = generate_inventory()
print(inventory)
Enter fullscreen mode Exit fullscreen mode

Conclusion

Today's advanced scripting scenarios have demonstrated how combining different scripting languages can automate complex tasks, ensuring efficiency and consistency in a DevOps environment. From infrastructure provisioning to dynamic inventory management, these scripts empower you to handle various challenges with ease.

Tomorrow, we'll dive into Ansible, a powerful automation tool that simplifies configuration management, application deployment, and orchestration.

👉 Make sure to follow me on LinkedIn for the latest updates: Shiivam Agnihotri

Top comments (0)