Python for DevOps Essentials
Python for DevOps Essentials
1. File Operations
Read a file:
python
Write to a file:
python
2. Environment Variables
Get an environment variable:
python
import os
db_user = [Link]('DB_USER')
print(db_user)
python
import os
[Link]['NEW_VAR'] = 'value'
3. Subprocess Management
Run shell commands:
python
import subprocess
4. API Requests
Make a GET request:
python
import requests
response = [Link]('[Link]
print([Link]())
5. JSON Handling
Read JSON from a file:
python
import json
python
import json
6. Logging
Basic logging setup:
python
import logging
[Link](level=[Link])
[Link]('This is an informational message')
python
import sqlite3
conn = [Link]('[Link]')
cursor = [Link]()
[Link]('CREATE TABLE IF NOT EXISTS users (id INTEGER
PRIMARY KEY, name TEXT)')
[Link]()
[Link]()
python
import paramiko
ssh = [Link]()
ssh.set_missing_host_key_policy([Link]())
[Link]('hostname', username='user', password='password')
9. Error Handling
Try-except block:
python
try:
# code that may raise an exception
risky_code()
except Exception as e:
print(f'Error occurred: {e}')
python
import docker
client = docker.from_env()
containers = [Link]()
for container in containers:
print([Link])
python
import yaml
python
import yaml
python
import argparse
args = parser.parse_args()
print([Link])
python
import psutil
python
app = Flask(__name__)
@[Link]('/health', methods=['GET'])
def health_check():
return jsonify({'status': 'healthy'})
if __name__ == '__main__':
[Link](host='[Link]', port=5000)
15. Creating Docker Containers
Using the Docker SDK to create a container:
python
import docker
client = docker.from_env()
container = [Link]('ubuntu', 'echo Hello World', detach=True)
print([Link]())
python
import schedule
import time
def job():
print("Running scheduled job...")
[Link](1).[Link](job)
while True:
schedule.run_pending()
[Link](1)
python
import git
repo = [Link]('/path/to/repo')
[Link]('[Link]')
[Link]('Added [Link]')
18. Email Notifications
Sending emails using smtplib:
python
import smtplib
from [Link] import MIMEText
python
import os
import subprocess
python
import requests
url = '[Link]
response = [Link](url, auth=('user', 'token'))
print(response.status_code)
bash
python
import unittest
class TestMathFunctions([Link]):
def test_add(self):
[Link](add(2, 3), 5)
if __name__ == '__main__':
[Link]()
python
import pandas as pd
df = pd.read_csv('[Link]')
df['new_column'] = df['existing_column'] * 2
df.to_csv('[Link]', index=False)
python
import boto3
ec2 = [Link]('ec2')
instances = [Link](Filters=[{'Name': 'instance-state-name',
'Values': ['running']}])
for instance in instances:
print([Link], [Link])
python
import requests
from bs4 import BeautifulSoup
response = [Link]('[Link]
soup = BeautifulSoup([Link], '[Link]')
print([Link])
python
python
import boto3
s3 = [Link]('s3')
# Upload a file
s3.upload_file('local_file.txt', 'bucket_name', 's3_file.txt')
# Download a file
s3.download_file('bucket_name', 's3_file.txt', 'local_file.txt')
python
import time
def tail_f(file):
[Link](0, 2) # Move to the end of the file
while True:
line = [Link]()
if not line:
[Link](0.1) # Sleep briefly
continue
print(line)
python
import docker
client = docker.from_env()
container = [Link]('container_id')
print([Link]['State']['Health']['Status'])
python
import requests
import time
url = '[Link]
while True:
response = [Link](url)
if response.status_code == 200:
print([Link]())
break
elif response.status_code == 429: # Too Many Requests
[Link](60) # Wait a minute before retrying
else:
print('Error:', response.status_code)
break
31. Docker Compose Integration
Using docker-compose in Python:
python
import os
import subprocess
# Stop services
[Link](['docker-compose', 'down'])
python
import subprocess
# Initialize Terraform
[Link](['terraform', 'init'])
# Apply configuration
[Link](['terraform', 'apply', '-auto-approve'])
python
import requests
response = [Link]('[Link]
metrics = [Link]()
for metric in metrics:
print(metric)
python
def test_add():
assert add(2, 3) == 5
python
app = Flask(__name__)
@[Link]('/webhook', methods=['POST'])
def webhook():
data = [Link]
print('Received data:', data)
return 'OK', 200
if __name__ == '__main__':
[Link](port=5000)
python
# Generate a key
key = Fernet.generate_key()
cipher_suite = Fernet(key)
# Encrypt
encrypted_text = cipher_suite.encrypt(b'Secret Data')
# Decrypt
decrypted_text = cipher_suite.decrypt(encrypted_text)
print(decrypted_text.decode())
python
import sentry_sdk
sentry_sdk.init('your_sentry_dsn')
try:
divide(1, 0)
except ZeroDivisionError as e:
sentry_sdk.capture_exception(e)
yaml
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
pip install -r [Link]
- name: Run tests
run: |
pytest
python
app = FastAPI()
@[Link]('/items/{item_id}')
async def read_item(item_id: int):
return {'item_id': item_id}
if __name__ == '__main__':
import uvicorn
[Link](app, host='[Link]', port=8000)
python
es = Elasticsearch(['[Link]
log = {'level': 'info', 'message': 'This is a log message'}
[Link](index='logs', body=log)
python
import pandas as pd
# Extract
data = pd.read_csv('[Link]')
# Transform
data['new_column'] = data['existing_column'].apply(lambda x: x * 2)
# Load
data.to_csv('[Link]', index=False)
python
import json
python
import redis
# Set a key
[Link]('foo', 'bar')
# Get a key
print([Link]('foo'))
python
python
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
[Link](debug=True)
python
import asyncio
python
def packet_callback(packet):
print([Link]())
sniff(prn=packet_callback, count=10)
python
import configparser
config = [Link]()
[Link]('[Link]')
print(config['DEFAULT']['SomeSetting'])
config['DEFAULT']['NewSetting'] = 'Value'
with open('[Link]', 'w') as configfile:
[Link](configfile)
python
import websocket
ws = [Link]("[Link]
on_message=on_message)
ws.run_forever()
python
import docker
client = docker.from_env()
# Dockerfile content
dockerfile_content = """
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install -r [Link]
CMD ["python", "[Link]"]
"""
python
import psutil
python
alembic_cfg = [Link]("[Link]")
[Link](alembic_cfg, "head")
import paramiko
client = [Link]()
client.set_missing_host_key_policy([Link]())
[Link]('hostname', username='user', password='your_password')
python
import boto3
cloudformation = [Link]('cloudformation')
response = cloudformation.create_stack(
StackName='MyStack',
TemplateBody=template_body,
Parameters=[
{
'ParameterKey': 'InstanceType',
'ParameterValue': '[Link]'
},
],
TimeoutInMinutes=5,
Capabilities=['CAPABILITY_NAMED_IAM'],
)
print(response)
python
import boto3
ec2 = [Link]('ec2')
# Start an instance
instance = [Link]('instance_id')
[Link]()
# Stop an instance
[Link]()
57. Automated Backup with shutil
Backup files to a specific directory:
python
import shutil
import os
source_dir = '/path/to/source'
backup_dir = '/path/to/backup'
[Link](source_dir, backup_dir)
python
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print(f'File modified: {event.src_path}')
event_handler = MyHandler()
observer = Observer()
[Link](event_handler, path='path/to/monitor', recursive=False)
[Link]()
try:
while True:
[Link](1)
except KeyboardInterrupt:
[Link]()
[Link]()
59. Load Testing with locust
Basic Locust load testing setup:
python
class MyUser(HttpUser):
wait_time = between(1, 3)
@task
def load_test(self):
[Link]('/')
python
import requests
url = '[Link]
response = [Link](url, headers={'Authorization': 'token
YOUR_GITHUB_TOKEN'})
repo_info = [Link]()
print(repo_info)
python
import subprocess
# Get pods
[Link](['kubectl', 'get', 'pods'])
# Apply a configuration
[Link](['kubectl', 'apply', '-f', '[Link]'])
python
# test_example.py
def test_addition():
assert 1 + 1 == 2
python
import argparse
args = parser.parse_args()
print([Link]([Link]))
python
load_dotenv()
database_url = [Link]('DATABASE_URL')
print(database_url)
python
import requests
from bs4 import BeautifulSoup
response = [Link]('[Link]
soup = BeautifulSoup([Link], '[Link]')
python
import yaml
python
import pika
# Sending messages
connection =
[Link]([Link]('localhost'))
channel = [Link]()
channel.queue_declare(queue='hello')
# Receiving messages
def callback(ch, method, properties, body):
print("Received:", body)
connection =
[Link]([Link]('localhost'))
channel = [Link]()
channel.queue_declare(queue='hello')
channel.basic_consume(queue='hello', on_message_callback=callback,
auto_ack=True)
channel.start_consuming()
python
import sentry_sdk
sentry_sdk.init("YOUR_SENTRY_DSN")
try:
# Your code that may throw an exception
1/0
except Exception as e:
sentry_sdk.capture_exception(e)
python
python
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
engine = create_engine('sqlite:///[Link]')
[Link].create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
# Create
new_user = User(name='Alice')
[Link](new_user)
python
import docker
client = docker.from_env()
containers = [Link]()
python
app = Flask(__name__)
@[Link]('/api/data', methods=['GET'])
def get_data():
return jsonify({"message": "Hello, World!"})
if __name__ == '__main__':
[Link](debug=True)
python
import subprocess
# Renew certificates
[Link](['certbot', 'renew'])
74. Using numpy for Data Analysis
Performing basic numerical operations:
python
import numpy as np
python
import smtplib
from [Link] import MIMEText
sender = 'you@[Link]'
recipient = 'recipient@[Link]'
msg = MIMEText('This is a test email.')
msg['Subject'] = 'Test Email'
msg['From'] = sender
msg['To'] = recipient
python
import schedule
import time
def job():
print("Job is running...")
[Link](10).[Link](job)
while True:
schedule.run_pending()
[Link](1)
python
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
[Link](x, y)
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Simple Plot')
[Link]()
markdown
my_package/
├── __init__.py
├── [Link]
└── [Link]
setup(
name='my_package',
version='0.1',
packages=find_packages(),
install_requires=[
'requests',
'flask'
],
)
python
# test_sample.py
def add(a, b):
return a + b
def test_add():
assert add(1, 2) == 3
python
oauth = OAuth1Session(client_key='YOUR_CLIENT_KEY',
client_secret='YOUR_CLIENT_SECRET')
response = [Link]('[Link]
print([Link]())
81. Using pandas for Data Manipulation
Load and manipulate data in a CSV file:
python
import pandas as pd
df = pd.read_csv('[Link]')
print([Link]())
# Filter data
filtered_df = df[df['column_name'] > 10]
print(filtered_df)
python
import requests
# GET request
response = [Link]('[Link]
print([Link]())
# POST request
data = {'key': 'value'}
response = [Link]('[Link] json=data)
print([Link]())
python
from [Link] import SimpleHTTPRequestHandler, HTTPServer
PORT = 8000
handler = SimpleHTTPRequestHandler
python
app = Flask(__name__)
@[Link]('/webhook', methods=['POST'])
def webhook():
data = [Link]
print(data)
return '', 200
if __name__ == '__main__':
[Link](port=5000)
python
import subprocess
python
import subprocess
python
import boto3
from moto import mock_s3
@mock_s3
def test_s3_upload():
s3 = [Link]('s3', region_name='us-east-1')
s3.create_bucket(Bucket='my-bucket')
s3.upload_file('[Link]', 'my-bucket', '[Link]')
# Test logic here
python
import asyncio
[Link](main())
python
app = Flask(__name__)
CORS(app)
@[Link]('/data', methods=['GET'])
def data():
return {"message": "Hello from CORS!"}
if __name__ == '__main__':
[Link]()
python
import pytest
@[Link]
def sample_data():
data = {"key": "value"}
yield data # This is the test data
# Teardown code here (if necessary)
def test_sample_data(sample_data):
assert sample_data['key'] == 'value'
python
import [Link]
conn = [Link]("[Link]")
[Link]("GET", "/")
response = [Link]()
print([Link], [Link])
data = [Link]()
[Link]()
python
import redis
import json
python
import [Link] as ET
tree = [Link]('[Link]')
root = [Link]()
python
import venv
[Link]('myenv', with_pip=True)
python
import psutil
memory = psutil.virtual_memory()
print(f'Total Memory: {[Link]}, Available Memory:
{[Link]}')
python
import sqlite3
conn = [Link]('[Link]')
c = [Link]()
[Link]()
bash
python
import argparse
args = parser.parse_args()
print([Link]([Link]))
python
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name", "age"]
}