-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_now.py
More file actions
161 lines (142 loc) · 4.67 KB
/
Copy pathdeploy_now.py
File metadata and controls
161 lines (142 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
AWS EC2 Deployment Script
Deploys MLOps project to EC2 instance
"""
import subprocess
import os
import time
# Configuration
EC2_IP = "13.53.39.56"
SSH_USER = "ubuntu"
KEY_FILE = "your-key.pem" # Update with your EC2 key file
REMOTE_DIR = "/home/ubuntu/mlops_project"
SLACK_WEBHOOK = os.getenv("SLACK_WEBHOOK_URL", "") # Set via environment variable
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
OUTPUT_FILE = os.path.join(PROJECT_DIR, "deploy_output.txt")
def log(msg):
print(msg)
with open(OUTPUT_FILE, "a", encoding="utf-8") as f:
f.write(msg + "\n")
def run_cmd(cmd, shell=True):
"""Run command and return output."""
try:
result = subprocess.run(cmd, shell=shell, capture_output=True, text=True, timeout=300)
return result.stdout + result.stderr, result.returncode
except subprocess.TimeoutExpired:
return "Command timed out", 1
except Exception as e:
return str(e), 1
def ssh_cmd(command):
"""Run command on EC2 via SSH."""
ssh = f'ssh -i "{KEY_FILE}" -o StrictHostKeyChecking=no -o ConnectTimeout=30 {SSH_USER}@{EC2_IP} "{command}"'
return run_cmd(ssh)
def scp_file(local, remote):
"""Copy file to EC2."""
cmd = f'scp -i "{KEY_FILE}" -o StrictHostKeyChecking=no -r "{local}" {SSH_USER}@{EC2_IP}:{remote}'
return run_cmd(cmd)
# Clear output file
with open(OUTPUT_FILE, "w") as f:
f.write("")
log("=" * 60)
log("MLOps AWS EC2 Deployment")
log("=" * 60)
log(f"EC2 IP: {EC2_IP}")
log(f"Key File: {KEY_FILE}")
log(f"Time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
log("")
# Step 1: Test SSH
log("[1/6] Testing SSH connection...")
output, code = ssh_cmd("echo 'SSH OK' && whoami")
if code == 0:
log(f" SUCCESS: {output.strip()}")
else:
log(f" ERROR: {output}")
log("Deployment failed - cannot connect to EC2")
exit(1)
# Step 2: Install Docker
log("\n[2/6] Installing Docker on EC2...")
docker_script = """
if ! command -v docker &> /dev/null; then
sudo apt-get update -qq
sudo apt-get install -y -qq docker.io docker-compose
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ubuntu
fi
docker --version
docker-compose --version
"""
output, code = ssh_cmd(docker_script.replace("\n", " && "))
log(f" {output.strip()}")
# Step 3: Create directory
log("\n[3/6] Creating project directory...")
output, code = ssh_cmd(f"mkdir -p {REMOTE_DIR}")
log(f" Directory: {REMOTE_DIR}")
# Step 4: Copy files
log("\n[4/6] Copying project files to EC2...")
files_to_copy = [
("src", f"{REMOTE_DIR}/"),
("prometheus", f"{REMOTE_DIR}/"),
("alertmanager", f"{REMOTE_DIR}/"),
("grafana", f"{REMOTE_DIR}/"),
("exporters", f"{REMOTE_DIR}/"),
("docker-compose.yml", f"{REMOTE_DIR}/"),
("Dockerfile", f"{REMOTE_DIR}/"),
("requirements.txt", f"{REMOTE_DIR}/"),
]
for local, remote in files_to_copy:
local_path = os.path.join(PROJECT_DIR, local)
if os.path.exists(local_path):
log(f" Copying {local}...")
output, code = scp_file(local_path, remote)
if code != 0:
log(f" Warning: {output}")
log(" Files copied!")
# Step 5: Create .env and start Docker
log("\n[5/6] Starting Docker services...")
start_script = f"""
cd {REMOTE_DIR}
cat > .env << 'ENVEOF'
SLACK_WEBHOOK_URL={SLACK_WEBHOOK}
DATA_LAKE_URL=http://149.40.228.124:6500
API_HOST=0.0.0.0
API_PORT=5000
MODEL_DIR=/app/models
ENVEOF
sudo docker-compose down 2>/dev/null || true
sudo docker-compose up -d --build
sleep 10
sudo docker-compose ps
"""
output, code = ssh_cmd(start_script.replace("\n", " && "))
log(f" {output}")
# Step 6: Verify
log("\n[6/6] Verifying deployment...")
verify_script = f"""
cd {REMOTE_DIR}
curl -s http://localhost:5000/health || echo 'ML API starting...'
curl -s http://localhost:9090/-/healthy || echo 'Prometheus starting...'
curl -s http://localhost:3000/api/health || echo 'Grafana starting...'
"""
output, code = ssh_cmd(verify_script.replace("\n", " && "))
log(f" {output}")
# Send Slack notification
log("\n[7/7] Sending Slack notification...")
import requests
try:
r = requests.post(SLACK_WEBHOOK, json={
"text": f"🚀 MLOps Deployed to AWS!\nEC2: {EC2_IP}\nML API: http://{EC2_IP}:5000\nGrafana: http://{EC2_IP}:3000"
}, timeout=10)
log(f" Slack: {r.status_code}")
except Exception as e:
log(f" Slack error: {e}")
log("\n" + "=" * 60)
log("DEPLOYMENT COMPLETE!")
log("=" * 60)
log("\nAccess your services:")
log(f" ML API: http://{EC2_IP}:5000")
log(f" Predict: http://{EC2_IP}:5000/predict")
log(f" Metrics: http://{EC2_IP}:5000/metrics")
log(f" Prometheus: http://{EC2_IP}:9090")
log(f" Grafana: http://{EC2_IP}:3000 (admin/admin123)")
log(f" Alertmanager: http://{EC2_IP}:9093")