Ready-to-use code snippets for OceanLinux API integration
Examples for managing wallet balance and transactions
Complete examples for server lifecycle management
Advanced monitoring and alerting examples
Complete automation workflows and scripts
#!/usr/bin/env python3
"""
Auto-scaling script for OceanLinux servers
Monitors server load and automatically scales up/down based on demand
"""
import requests
import time
from datetime import datetime
import os
class AutoScaler:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = 'https://api.oceanlinux.com/v1'
self.headers = {'Authorization': f'Bearer {api_key}'}
def get_wallet_balance(self):
response = requests.get(f'{self.base_url}/wallet', headers=self.headers)
return response.json()['balance']
def should_scale_up(self, metrics):
return (metrics['cpu_usage'] > 80 and
metrics['memory_usage'] > 75 and
metrics['load_average'] > 2.0)
def should_scale_down(self, metrics):
return (metrics['cpu_usage'] < 30 and
metrics['memory_usage'] < 40 and
metrics['load_average'] < 0.5)
def auto_scale(self):
servers = requests.get(f'{self.base_url}/servers/my-servers',
headers=self.headers).json()['servers']
for server in servers:
metrics = requests.get(
f'{self.base_url}/servers/{server["_id"]}/metrics',
headers=self.headers
).json()
print(f"Server {server['ip']}: CPU {metrics['cpu_usage']}%, RAM {metrics['memory_usage']}%")
if self.should_scale_up(metrics):
print(f"🔼 Scaling up server {server['ip']}")
# Implement scale-up logic
elif self.should_scale_down(metrics):
print(f"🔽 Scaling down server {server['ip']}")
# Implement scale-down logic
# Run every 5 minutes
if __name__ == "__main__":
scaler = AutoScaler(os.getenv('OCEANLINUX_API_KEY'))
while True:
try:
scaler.auto_scale()
time.sleep(300) # 5 minutes
except KeyboardInterrupt:
print("Auto-scaler stopped")
break
except Exception as e:
print(f"Error: {e}")
time.sleep(60) # Wait 1 minute on error#!/bin/bash
# OceanLinux Backup Automation Script
# Creates daily backups of all servers
API_KEY="your_api_key_here"
BASE_URL="https://api.oceanlinux.com/v1"
LOG_FILE="/var/log/oceanlinux-backup.log"
log() {
echo "$(date): $1" | tee -a "$LOG_FILE"
}
create_backup() {
local server_id=$1
local server_ip=$2
log "Creating backup for server $server_ip ($server_id)"
backup_name="auto-backup-$(date +%Y%m%d-%H%M%S)"
response=$(curl -s -X POST \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "{\"name\": \"$backup_name\"}" \
"$BASE_URL/servers/$server_id/backup")
if echo "$response" | grep -q '"success":true'; then
log "✅ Backup created successfully for $server_ip"
return 0
else
log "❌ Backup failed for $server_ip: $response"
return 1
fi
}
main() {
log "Starting backup process"
# Get all servers
servers=$(curl -s -H "Authorization: Bearer $API_KEY" \
"$BASE_URL/servers/my-servers")
if [ $? -ne 0 ]; then
log "❌ Failed to fetch servers"
exit 1
fi
# Extract server IDs and IPs using jq
echo "$servers" | jq -r '.servers[] | "\(.._id) \(.ip)"' | \
while read -r server_id server_ip; do
create_backup "$server_id" "$server_ip"
sleep 5 # Wait 5 seconds between backups
done
log "Backup process completed"
}
# Run main function
mainUse our official SDKs for easier integration
Official JavaScript/TypeScript SDK
npm install @oceanlinux/api-sdkOfficial Python library
pip install oceanlinux-apiOfficial PHP package
composer require oceanlinux/api-client