|
| 1 | +## 🧩 **15. Popular DevOps Libraries** |
| 2 | + |
| 3 | +Python provides a wide range of **libraries** that simplify automation and integration tasks across DevOps tools like AWS, Docker, Kubernetes, Jenkins, and more. |
| 4 | +These libraries allow you to manage infrastructure, perform CI/CD operations, monitor systems, and handle configuration files — all through Python scripts. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +### 🟡 **1. AWS Cloud Automation – boto3** |
| 9 | + |
| 10 | +**Definition:** |
| 11 | +`boto3` is the official AWS SDK for Python that enables developers to create, configure, and manage AWS services such as EC2, S3, IAM, Lambda, and more. |
| 12 | + |
| 13 | +**Use Case:** |
| 14 | +Automating AWS resource provisioning, backups, or monitoring. |
| 15 | + |
| 16 | +**Example: Create an S3 bucket** |
| 17 | + |
| 18 | +```python |
| 19 | +import boto3 |
| 20 | + |
| 21 | +s3 = boto3.client('s3') |
| 22 | +s3.create_bucket(Bucket='devops-demo-bucket') |
| 23 | +print("✅ S3 bucket created successfully!") |
| 24 | +``` |
| 25 | + |
| 26 | +**Example: List EC2 Instances** |
| 27 | + |
| 28 | +```python |
| 29 | +import boto3 |
| 30 | + |
| 31 | +ec2 = boto3.client('ec2') |
| 32 | +instances = ec2.describe_instances() |
| 33 | +for reservation in instances['Reservations']: |
| 34 | + for instance in reservation['Instances']: |
| 35 | + print(instance['InstanceId'], instance['State']['Name']) |
| 36 | +``` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### 🟡 **2. SSH / SFTP – paramiko, fabric** |
| 41 | + |
| 42 | +**Definition:** |
| 43 | + |
| 44 | +* `paramiko` provides SSH and SFTP capabilities for remote server management. |
| 45 | +* `fabric` builds on top of `paramiko` for higher-level deployment and automation. |
| 46 | + |
| 47 | +**Use Case:** |
| 48 | +Running remote commands, transferring files, automating deployments. |
| 49 | + |
| 50 | +**Example (Paramiko – Run remote command):** |
| 51 | + |
| 52 | +```python |
| 53 | +import paramiko |
| 54 | + |
| 55 | +ssh = paramiko.SSHClient() |
| 56 | +ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 57 | +ssh.connect('192.168.1.10', username='ubuntu', password='password') |
| 58 | + |
| 59 | +stdin, stdout, stderr = ssh.exec_command('uptime') |
| 60 | +print(stdout.read().decode()) |
| 61 | + |
| 62 | +ssh.close() |
| 63 | +``` |
| 64 | + |
| 65 | +**Example (Fabric – Run command on remote server):** |
| 66 | + |
| 67 | +```python |
| 68 | +from fabric import Connection |
| 69 | + |
| 70 | +conn = Connection( '[email protected]') |
| 71 | +conn.run('ls -l /var/www/') |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### 🟡 **3. REST APIs – requests** |
| 77 | + |
| 78 | +**Definition:** |
| 79 | +`requests` is a simple and powerful HTTP library for sending REST API calls. |
| 80 | + |
| 81 | +**Use Case:** |
| 82 | +Integrate with cloud services, Jenkins, or Kubernetes APIs. |
| 83 | + |
| 84 | +**Example (GET Request):** |
| 85 | + |
| 86 | +```python |
| 87 | +import requests |
| 88 | + |
| 89 | +response = requests.get('https://api.github.com/users/octocat') |
| 90 | +print(response.json()) |
| 91 | +``` |
| 92 | + |
| 93 | +**Example (POST Request):** |
| 94 | + |
| 95 | +```python |
| 96 | +import requests |
| 97 | + |
| 98 | +data = {'name': 'demo', 'description': 'Test Repo'} |
| 99 | +headers = {'Authorization': 'token YOUR_GITHUB_TOKEN'} |
| 100 | +response = requests.post('https://api.github.com/user/repos', json=data, headers=headers) |
| 101 | +print(response.status_code) |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +### 🟡 **4. Docker Automation – docker** |
| 107 | + |
| 108 | +**Definition:** |
| 109 | +`docker` SDK for Python allows programmatic management of Docker containers, images, and networks. |
| 110 | + |
| 111 | +**Use Case:** |
| 112 | +Automate container deployment, build images, or monitor containers. |
| 113 | + |
| 114 | +**Example:** |
| 115 | + |
| 116 | +```python |
| 117 | +import docker |
| 118 | + |
| 119 | +client = docker.from_env() |
| 120 | +container = client.containers.run("nginx", detach=True, ports={'80/tcp': 8080}) |
| 121 | +print(f"Started container: {container.id}") |
| 122 | +``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +### 🟡 **5. Kubernetes Automation – kubernetes** |
| 127 | + |
| 128 | +**Definition:** |
| 129 | +The `kubernetes` Python client allows you to interact with Kubernetes clusters programmatically (e.g., creating pods, deployments, services). |
| 130 | + |
| 131 | +**Use Case:** |
| 132 | +Automate deployment, scaling, or health checks in a Kubernetes cluster. |
| 133 | + |
| 134 | +**Example:** |
| 135 | + |
| 136 | +```python |
| 137 | +from kubernetes import client, config |
| 138 | + |
| 139 | +config.load_kube_config() |
| 140 | +v1 = client.CoreV1Api() |
| 141 | +pods = v1.list_pod_for_all_namespaces(watch=False) |
| 142 | + |
| 143 | +for pod in pods.items: |
| 144 | + print(f"{pod.metadata.namespace}: {pod.metadata.name}") |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### 🟡 **6. YAML Parsing – pyyaml** |
| 150 | + |
| 151 | +**Definition:** |
| 152 | +`pyyaml` is used to read and write YAML configuration files, which are common in DevOps tools like Kubernetes, Ansible, and Terraform. |
| 153 | + |
| 154 | +**Use Case:** |
| 155 | +Modify or create configuration files programmatically. |
| 156 | + |
| 157 | +**Example:** |
| 158 | + |
| 159 | +```python |
| 160 | +import yaml |
| 161 | + |
| 162 | +data = { |
| 163 | + 'apiVersion': 'v1', |
| 164 | + 'kind': 'Pod', |
| 165 | + 'metadata': {'name': 'mypod'}, |
| 166 | + 'spec': {'containers': [{'name': 'nginx', 'image': 'nginx:latest'}]} |
| 167 | +} |
| 168 | + |
| 169 | +with open('pod.yaml', 'w') as file: |
| 170 | + yaml.dump(data, file) |
| 171 | +print("✅ pod.yaml created successfully!") |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +### 🟡 **7. Monitoring – psutil, prometheus_client** |
| 177 | + |
| 178 | +**Definition:** |
| 179 | + |
| 180 | +* `psutil`: monitors system resources like CPU, memory, and disk usage. |
| 181 | +* `prometheus_client`: exposes metrics to Prometheus for monitoring. |
| 182 | + |
| 183 | +**Use Case:** |
| 184 | +Monitor system health, generate metrics, or trigger alerts. |
| 185 | + |
| 186 | +**Example (psutil – System Metrics):** |
| 187 | + |
| 188 | +```python |
| 189 | +import psutil |
| 190 | + |
| 191 | +print("CPU Usage:", psutil.cpu_percent(), "%") |
| 192 | +print("Memory Usage:", psutil.virtual_memory().percent, "%") |
| 193 | +``` |
| 194 | + |
| 195 | +**Example (prometheus_client – Export Metrics):** |
| 196 | + |
| 197 | +```python |
| 198 | +from prometheus_client import start_http_server, Gauge |
| 199 | +import psutil, time |
| 200 | + |
| 201 | +cpu_gauge = Gauge('cpu_usage_percent', 'CPU usage percentage') |
| 202 | + |
| 203 | +start_http_server(8000) |
| 204 | +while True: |
| 205 | + cpu_gauge.set(psutil.cpu_percent()) |
| 206 | + time.sleep(5) |
| 207 | +``` |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +### ✅ **Summary Table** |
| 212 | + |
| 213 | +| **Purpose** | **Library** | **Use Case** | |
| 214 | +| --------------------- | ----------------------------- | ----------------------------------------- | |
| 215 | +| AWS Cloud Automation | `boto3` | Manage AWS resources like EC2, S3, Lambda | |
| 216 | +| SSH / SFTP | `paramiko`, `fabric` | Remote command execution, file transfers | |
| 217 | +| REST APIs | `requests` | Interact with APIs (AWS, Jenkins, etc.) | |
| 218 | +| Docker Automation | `docker` | Create/manage containers | |
| 219 | +| Kubernetes Automation | `kubernetes` | Automate K8s resources | |
| 220 | +| YAML Parsing | `pyyaml` | Read/write YAML config files | |
| 221 | +| Monitoring | `psutil`, `prometheus_client` | System monitoring and alerting | |
| 222 | + |
| 223 | +--- |
0 commit comments