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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import time
|
3 |
+
import logging
|
4 |
+
import threading
|
5 |
+
import numpy as np
|
6 |
+
from enum import Enum
|
7 |
+
from pathlib import Path
|
8 |
+
from datetime import datetime, timedelta
|
9 |
+
from dataclasses import dataclass, asdict
|
10 |
+
from typing import Dict, List, Optional, Any, Tuple
|
11 |
+
|
12 |
+
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
+
class DeploymentStatus(Enum):
|
16 |
+
INACTIVE = "inactive"
|
17 |
+
PREPARING = "preparing"
|
18 |
+
STAGING = "staging"
|
19 |
+
DEPLOYING = "deploying"
|
20 |
+
ACTIVE = "active"
|
21 |
+
ROLLING_BACK = "rolling_back"
|
22 |
+
FAILED = "failed"
|
23 |
+
COMPLETED = "completed"
|
24 |
+
|
25 |
+
@dataclass
|
26 |
+
class ModelVersion:
|
27 |
+
"""Model version metadata"""
|
28 |
+
version_id: str
|
29 |
+
model_path: str
|
30 |
+
vectorizer_path: str
|
31 |
+
metadata_path: str
|
32 |
+
created_at: str
|
33 |
+
status: str
|
34 |
+
performance_metrics: Dict[str, float]
|
35 |
+
deployment_config: Dict[str, Any]
|
36 |
+
|
37 |
+
@dataclass
|
38 |
+
class DeploymentPlan:
|
39 |
+
"""Deployment plan configuration"""
|
40 |
+
deployment_id: str
|
41 |
+
source_version: str
|
42 |
+
target_version: str
|
43 |
+
strategy: str # 'blue_green', 'canary', 'rolling'
|
44 |
+
traffic_stages: List[Dict[str, Any]]
|
45 |
+
health_checks: Dict[str, Any]
|
46 |
+
rollback_conditions: Dict[str, Any]
|
47 |
+
created_at: str
|
48 |
+
status: str
|
49 |
+
|
50 |
+
class BlueGreenDeploymentManager:
|
51 |
+
"""Manages blue-green deployments with traffic routing and health monitoring"""
|
52 |
+
|
53 |
+
def __init__(self, base_dir: Path = None):
|
54 |
+
self.base_dir = base_dir or Path("/tmp")
|
55 |
+
self.setup_deployment_paths()
|
56 |
+
self.setup_deployment_config()
|
57 |
+
|
58 |
+
# Current deployment state
|
59 |
+
self.current_deployment = None
|
60 |
+
self.active_version = None
|
61 |
+
self.staging_version = None
|
62 |
+
self.traffic_split = {"blue": 100, "green": 0}
|
63 |
+
|
64 |
+
# Monitoring
|
65 |
+
self.deployment_monitor = None
|
66 |
+
self.monitor_thread = None
|
67 |
+
self.monitoring_active = False
|
68 |
+
|
69 |
+
# Load existing state
|
70 |
+
self.load_deployment_state()
|
71 |
+
|
72 |
+
def setup_deployment_paths(self):
|
73 |
+
"""Setup deployment-specific paths"""
|
74 |
+
self.deployment_dir = self.base_dir / "deployment"
|
75 |
+
self.deployment_dir.mkdir(parents=True, exist_ok=True)
|
76 |
+
|
77 |
+
# Model storage
|
78 |
+
self.models_dir = self.deployment_dir / "models"
|
79 |
+
self.models_dir.mkdir(parents=True, exist_ok=True)
|
80 |
+
|
81 |
+
# Deployment logs
|
82 |
+
self.deployment_log_path = self.deployment_dir / "deployment_log.json"
|
83 |
+
self.deployment_state_path = self.deployment_dir / "deployment_state.json"
|
84 |
+
self.traffic_log_path = self.deployment_dir / "traffic_log.json"
|
85 |
+
|
86 |
+
# Blue-Green specific directories
|
87 |
+
self.blue_dir = self.models_dir / "blue"
|
88 |
+
self.green_dir = self.models_dir / "green"
|
89 |
+
self.blue_dir.mkdir(parents=True, exist_ok=True)
|
90 |
+
self.green_dir.mkdir(parents=True, exist_ok=True)
|
91 |
+
|
92 |
+
def setup_deployment_config(self):
|
93 |
+
"""Setup deployment configuration"""
|
94 |
+
self.deployment_config = {
|
95 |
+
'traffic_stages': [
|
96 |
+
{'percentage': 10, 'duration_minutes': 15, 'success_threshold': 0.95},
|
97 |
+
{'percentage': 25, 'duration_minutes': 15, 'success_threshold': 0.95},
|
98 |
+
{'percentage': 50, 'duration_minutes': 30, 'success_threshold': 0.95},
|
99 |
+
{'percentage': 75, 'duration_minutes': 30, 'success_threshold': 0.95},
|
100 |
+
{'percentage': 100, 'duration_minutes': 0, 'success_threshold': 0.95}
|
101 |
+
],
|
102 |
+
'health_checks': {
|
103 |
+
'response_time_threshold': 5.0, # seconds
|
104 |
+
'error_rate_threshold': 0.05, # 5%
|
105 |
+
'confidence_threshold': 0.6, # minimum confidence
|
106 |
+
'check_interval': 30, # seconds
|
107 |
+
'failure_threshold': 3 # consecutive failures
|
108 |
+
},
|
109 |
+
'rollback_conditions': {
|
110 |
+
'error_rate_spike': 0.15, # 15% error rate
|
111 |
+
'response_time_spike': 10.0, # 10 seconds
|
112 |
+
'confidence_drop': 0.4, # below 40% confidence
|
113 |
+
'health_check_failures': 5 # consecutive failures
|
114 |
+
},
|
115 |
+
'deployment_timeouts': {
|
116 |
+
'stage_timeout_minutes': 60,
|
117 |
+
'total_deployment_hours': 6,
|
118 |
+
'rollback_timeout_minutes': 15
|
119 |
+
}
|
120 |
+
}
|
121 |
+
|
122 |
+
def create_model_version(self, model_path: str, vectorizer_path: str,
|
123 |
+
metadata: Dict) -> str:
|
124 |
+
"""Create a new model version"""
|
125 |
+
try:
|
126 |
+
version_id = f"v{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
127 |
+
|
128 |
+
# Create version directory
|
129 |
+
version_dir = self.models_dir / version_id
|
130 |
+
version_dir.mkdir(parents=True, exist_ok=True)
|
131 |
+
|
132 |
+
# Copy model files to version directory
|
133 |
+
import shutil
|
134 |
+
model_dest = version_dir / "model.pkl"
|
135 |
+
vectorizer_dest = version_dir / "vectorizer.pkl"
|
136 |
+
metadata_dest = version_dir / "metadata.json"
|
137 |
+
|
138 |
+
shutil.copy2(model_path, model_dest)
|
139 |
+
shutil.copy2(vectorizer_path, vectorizer_dest)
|
140 |
+
|
141 |
+
# Save metadata
|
142 |
+
version_metadata = {
|
143 |
+
**metadata,
|
144 |
+
'version_id': version_id,
|
145 |
+
'created_at': datetime.now().isoformat(),
|
146 |
+
'model_path': str(model_dest),
|
147 |
+
'vectorizer_path': str(vectorizer_dest),
|
148 |
+
'status': 'created'
|
149 |
+
}
|
150 |
+
|
151 |
+
with open(metadata_dest, 'w') as f:
|
152 |
+
json.dump(version_metadata, f, indent=2)
|
153 |
+
|
154 |
+
# Create ModelVersion object
|
155 |
+
model_version = ModelVersion(
|
156 |
+
version_id=version_id,
|
157 |
+
model_path=str(model_dest),
|
158 |
+
vectorizer_path=str(vectorizer_dest),
|
159 |
+
metadata_path=str(metadata_dest),
|
160 |
+
created_at=version_metadata['created_at'],
|
161 |
+
status='created',
|
162 |
+
performance_metrics=metadata.get('performance_metrics', {}),
|
163 |
+
deployment_config={}
|
164 |
+
)
|
165 |
+
|
166 |
+
# Log version creation
|
167 |
+
self.log_deployment_event("version_created", f"Created model version {version_id}", {
|
168 |
+
'version_id': version_id,
|
169 |
+
'performance_metrics': model_version.performance_metrics
|
170 |
+
})
|
171 |
+
|
172 |
+
logger.info(f"Created model version: {version_id}")
|
173 |
+
return version_id
|
174 |
+
|
175 |
+
except Exception as e:
|
176 |
+
logger.error(f"Failed to create model version: {e}")
|
177 |
+
raise e
|
178 |
+
|
179 |
+
def prepare_deployment(self, target_version_id: str,
|
180 |
+
deployment_strategy: str = "blue_green") -> str:
|
181 |
+
"""Prepare a new deployment"""
|
182 |
+
try:
|
183 |
+
# Generate deployment ID
|
184 |
+
deployment_id = f"deploy_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
185 |
+
|
186 |
+
# Get current active version
|
187 |
+
current_version = self.get_active_version()
|
188 |
+
|
189 |
+
# Validate target version exists
|
190 |
+
if not self.version_exists(target_version_id):
|
191 |
+
raise ValueError(f"Target version {target_version_id} does not exist")
|
192 |
+
|
193 |
+
# Create deployment plan
|
194 |
+
deployment_plan = DeploymentPlan(
|
195 |
+
deployment_id=deployment_id,
|
196 |
+
source_version=current_version['version_id'] if current_version else None,
|
197 |
+
target_version=target_version_id,
|
198 |
+
strategy=deployment_strategy,
|
199 |
+
traffic_stages=self.deployment_config['traffic_stages'].copy(),
|
200 |
+
health_checks=self.deployment_config['health_checks'].copy(),
|
201 |
+
rollback_conditions=self.deployment_config['rollback_conditions'].copy(),
|
202 |
+
created_at=datetime.now().isoformat(),
|
203 |
+
status=DeploymentStatus.PREPARING.value
|
204 |
+
)
|
205 |
+
|
206 |
+
# Stage the new version
|
207 |
+
self.stage_version(target_version_id, deployment_plan)
|
208 |
+
|
209 |
+
# Update deployment state
|
210 |
+
self.current_deployment = deployment_plan
|
211 |
+
self.save_deployment_state()
|
212 |
+
|
213 |
+
self.log_deployment_event("deployment_prepared", f"Prepared deployment {deployment_id}", {
|
214 |
+
'deployment_plan': asdict(deployment_plan)
|
215 |
+
})
|
216 |
+
|
217 |
+
logger.info(f"Prepared deployment: {deployment_id}")
|
218 |
+
return deployment_id
|
219 |
+
|
220 |
+
except Exception as e:
|
221 |
+
logger.error(f"Failed to prepare deployment: {e}")
|
222 |
+
raise e
|
223 |
+
|
224 |
+
def stage_version(self, version_id: str, deployment_plan: DeploymentPlan):
|
225 |
+
"""Stage a model version for deployment"""
|
226 |
+
try:
|
227 |
+
# Determine staging environment (blue or green)
|
228 |
+
staging_env = self.determine_staging_environment()
|
229 |
+
|
230 |
+
# Copy version to staging directory
|
231 |
+
version_dir = self.models_dir / version_id
|
232 |
+
staging_dir = self.blue_dir if staging_env == "blue" else self.green_dir
|
233 |
+
|
234 |
+
# Clear staging directory
|
235 |
+
import shutil
|
236 |
+
if staging_dir.exists():
|
237 |
+
shutil.rmtree(staging_dir)
|
238 |
+
staging_dir.mkdir(parents=True, exist_ok=True)
|
239 |
+
|
240 |
+
# Copy model files
|
241 |
+
for file_name in ["model.pkl", "vectorizer.pkl", "metadata.json"]:
|
242 |
+
source_file = version_dir / file_name
|
243 |
+
if source_file.exists():
|
244 |
+
shutil.copy2(source_file, staging_dir / file_name)
|
245 |
+
|
246 |
+
# Update staging version
|
247 |
+
self.staging_version = {
|
248 |
+
'version_id': version_id,
|
249 |
+
'environment': staging_env,
|
250 |
+
'staged_at': datetime.now().isoformat(),
|
251 |
+
'status': 'staged'
|
252 |
+
}
|
253 |
+
|
254 |
+
# Update deployment status
|
255 |
+
deployment_plan.status = DeploymentStatus.STAGING.value
|
256 |
+
|
257 |
+
logger.info(f"Staged version {version_id} in {staging_env} environment")
|
258 |
+
|
259 |
+
except Exception as e:
|
260 |
+
logger.error(f"Failed to stage version: {e}")
|
261 |
+
raise e
|
262 |
+
|
263 |
+
def start_deployment(self, deployment_id: str) -> bool:
|
264 |
+
"""Start the deployment process"""
|
265 |
+
try:
|
266 |
+
if not self.current_deployment or self.current_deployment.deployment_id != deployment_id:
|
267 |
+
raise ValueError(f"Deployment {deployment_id} not found or not current")
|
268 |
+
|
269 |
+
# Update status
|
270 |
+
self.current_deployment.status = DeploymentStatus.DEPLOYING.value
|
271 |
+
|
272 |
+
# Start monitoring
|
273 |
+
self.start_deployment_monitoring()
|
274 |
+
|
275 |
+
# Begin traffic shifting
|
276 |
+
success = self.execute_traffic_stages()
|
277 |
+
|
278 |
+
if success:
|
279 |
+
self.current_deployment.status = DeploymentStatus.COMPLETED.value
|
280 |
+
self.finalize_deployment()
|
281 |
+
else:
|
282 |
+
self.current_deployment.status = DeploymentStatus.FAILED.value
|
283 |
+
self.initiate_rollback("Deployment failed during traffic shifting")
|
284 |
+
|
285 |
+
self.save_deployment_state()
|
286 |
+
|
287 |
+
return success
|
288 |
+
|
289 |
+
except Exception as e:
|
290 |
+
logger.error(f"Failed to start deployment: {e}")
|
291 |
+
self.initiate_rollback(f"Deployment error: {str(e)}")
|
292 |
+
return False
|
293 |
+
|
294 |
+
def execute_traffic_stages(self) -> bool:
|
295 |
+
"""Execute gradual traffic shifting"""
|
296 |
+
try:
|
297 |
+
stages = self.current_deployment.traffic_stages
|
298 |
+
|
299 |
+
for i, stage in enumerate(stages):
|
300 |
+
logger.info(f"Starting stage {i+1}/{len(stages)}: {stage['percentage']}% traffic")
|
301 |
+
|
302 |
+
# Update traffic split
|
303 |
+
self.update_traffic_split(stage['percentage'])
|
304 |
+
|
305 |
+
# Wait for stage duration
|
306 |
+
if stage['duration_minutes'] > 0:
|
307 |
+
stage_success = self.monitor_stage_health(
|
308 |
+
stage['duration_minutes'],
|
309 |
+
stage['success_threshold']
|
310 |
+
)
|
311 |
+
|
312 |
+
if not stage_success:
|
313 |
+
logger.error(f"Stage {i+1} failed health checks")
|
314 |
+
return False
|
315 |
+
|
316 |
+
self.log_deployment_event("stage_completed", f"Stage {i+1} completed", {
|
317 |
+
'stage': stage,
|
318 |
+
'traffic_split': self.traffic_split
|
319 |
+
})
|
320 |
+
|
321 |
+
return True
|
322 |
+
|
323 |
+
except Exception as e:
|
324 |
+
logger.error(f"Traffic stage execution failed: {e}")
|
325 |
+
return False
|
326 |
+
|
327 |
+
def update_traffic_split(self, green_percentage: int):
|
328 |
+
"""Update traffic routing split"""
|
329 |
+
self.traffic_split = {
|
330 |
+
"blue": 100 - green_percentage,
|
331 |
+
"green": green_percentage
|
332 |
+
}
|
333 |
+
|
334 |
+
# Log traffic change
|
335 |
+
self.log_traffic_change(self.traffic_split)
|
336 |
+
|
337 |
+
logger.info(f"Updated traffic split: Blue {self.traffic_split['blue']}%, Green {self.traffic_split['green']}%")
|
338 |
+
|
339 |
+
def monitor_stage_health(self, duration_minutes: int, success_threshold: float) -> bool:
|
340 |
+
"""Monitor health during a deployment stage"""
|
341 |
+
try:
|
342 |
+
start_time = datetime.now()
|
343 |
+
end_time = start_time + timedelta(minutes=duration_minutes)
|
344 |
+
check_interval = self.deployment_config['health_checks']['check_interval']
|
345 |
+
|
346 |
+
consecutive_failures = 0
|
347 |
+
max_failures = self.deployment_config['health_checks']['failure_threshold']
|
348 |
+
|
349 |
+
while datetime.now() < end_time:
|
350 |
+
# Perform health check
|
351 |
+
health_result = self.perform_health_check()
|
352 |
+
|
353 |
+
if health_result['healthy']:
|
354 |
+
consecutive_failures = 0
|
355 |
+
else:
|
356 |
+
consecutive_failures += 1
|
357 |
+
logger.warning(f"Health check failed: {health_result['issues']}")
|
358 |
+
|
359 |
+
if consecutive_failures >= max_failures:
|
360 |
+
logger.error(f"Too many consecutive failures: {consecutive_failures}")
|
361 |
+
return False
|
362 |
+
|
363 |
+
# Check for immediate rollback conditions
|
364 |
+
if self.should_trigger_immediate_rollback(health_result):
|
365 |
+
logger.error("Immediate rollback conditions met")
|
366 |
+
return False
|
367 |
+
|
368 |
+
time.sleep(check_interval)
|
369 |
+
|
370 |
+
return True
|
371 |
+
|
372 |
+
except Exception as e:
|
373 |
+
logger.error(f"Stage health monitoring failed: {e}")
|
374 |
+
return False
|
375 |
+
|
376 |
+
def perform_health_check(self) -> Dict[str, Any]:
|
377 |
+
"""Perform comprehensive health check"""
|
378 |
+
try:
|
379 |
+
health_result = {
|
380 |
+
'healthy': True,
|
381 |
+
'issues': [],
|
382 |
+
'metrics': {},
|
383 |
+
'timestamp': datetime.now().isoformat()
|
384 |
+
}
|
385 |
+
|
386 |
+
# Check response times
|
387 |
+
avg_response_time = self.get_average_response_time()
|
388 |
+
threshold = self.deployment_config['health_checks']['response_time_threshold']
|
389 |
+
|
390 |
+
health_result['metrics']['response_time'] = avg_response_time
|
391 |
+
|
392 |
+
if avg_response_time > threshold:
|
393 |
+
health_result['healthy'] = False
|
394 |
+
health_result['issues'].append(f"High response time: {avg_response_time:.2f}s")
|
395 |
+
|
396 |
+
# Check error rates
|
397 |
+
error_rate = self.get_current_error_rate()
|
398 |
+
error_threshold = self.deployment_config['health_checks']['error_rate_threshold']
|
399 |
+
|
400 |
+
health_result['metrics']['error_rate'] = error_rate
|
401 |
+
|
402 |
+
if error_rate > error_threshold:
|
403 |
+
health_result['healthy'] = False
|
404 |
+
health_result['issues'].append(f"High error rate: {error_rate:.2%}")
|
405 |
+
|
406 |
+
# Check prediction confidence
|
407 |
+
avg_confidence = self.get_average_confidence()
|
408 |
+
confidence_threshold = self.deployment_config['health_checks']['confidence_threshold']
|
409 |
+
|
410 |
+
health_result['metrics']['confidence'] = avg_confidence
|
411 |
+
|
412 |
+
if avg_confidence < confidence_threshold:
|
413 |
+
health_result['healthy'] = False
|
414 |
+
health_result['issues'].append(f"Low confidence: {avg_confidence:.2f}")
|
415 |
+
|
416 |
+
return health_result
|
417 |
+
|
418 |
+
except Exception as e:
|
419 |
+
logger.error(f"Health check failed: {e}")
|
420 |
+
return {
|
421 |
+
'healthy': False,
|
422 |
+
'issues': [f"Health check error: {str(e)}"],
|
423 |
+
'metrics': {},
|
424 |
+
'timestamp': datetime.now().isoformat()
|
425 |
+
}
|
426 |
+
|
427 |
+
def should_trigger_immediate_rollback(self, health_result: Dict) -> bool:
|
428 |
+
"""Check if immediate rollback should be triggered"""
|
429 |
+
rollback_conditions = self.deployment_config['rollback_conditions']
|
430 |
+
metrics = health_result['metrics']
|
431 |
+
|
432 |
+
# Check error rate spike
|
433 |
+
if metrics.get('error_rate', 0) > rollback_conditions['error_rate_spike']:
|
434 |
+
return True
|
435 |
+
|
436 |
+
# Check response time spike
|
437 |
+
if metrics.get('response_time', 0) > rollback_conditions['response_time_spike']:
|
438 |
+
return True
|
439 |
+
|
440 |
+
# Check confidence drop
|
441 |
+
if metrics.get('confidence', 1) < rollback_conditions['confidence_drop']:
|
442 |
+
return True
|
443 |
+
|
444 |
+
return False
|
445 |
+
|
446 |
+
def initiate_rollback(self, reason: str) -> bool:
|
447 |
+
"""Initiate deployment rollback"""
|
448 |
+
try:
|
449 |
+
logger.warning(f"Initiating rollback: {reason}")
|
450 |
+
|
451 |
+
if self.current_deployment:
|
452 |
+
self.current_deployment.status = DeploymentStatus.ROLLING_BACK.value
|
453 |
+
|
454 |
+
# Immediately route all traffic to blue (current production)
|
455 |
+
self.update_traffic_split(0) # 0% to green, 100% to blue
|
456 |
+
|
457 |
+
# Clear staging environment
|
458 |
+
self.clear_staging_environment()
|
459 |
+
|
460 |
+
# Update deployment state
|
461 |
+
if self.current_deployment:
|
462 |
+
self.current_deployment.status = DeploymentStatus.FAILED.value
|
463 |
+
|
464 |
+
self.save_deployment_state()
|
465 |
+
|
466 |
+
self.log_deployment_event("rollback_initiated", f"Rollback initiated: {reason}", {
|
467 |
+
'reason': reason,
|
468 |
+
'traffic_split': self.traffic_split
|
469 |
+
})
|
470 |
+
|
471 |
+
logger.info("Rollback completed successfully")
|
472 |
+
return True
|
473 |
+
|
474 |
+
except Exception as e:
|
475 |
+
logger.error(f"Rollback failed: {e}")
|
476 |
+
return False
|
477 |
+
|
478 |
+
def finalize_deployment(self):
|
479 |
+
"""Finalize successful deployment"""
|
480 |
+
try:
|
481 |
+
if not self.staging_version:
|
482 |
+
raise ValueError("No staging version to finalize")
|
483 |
+
|
484 |
+
# Move staging to active
|
485 |
+
staging_env = self.staging_version['environment']
|
486 |
+
|
487 |
+
# Update active version
|
488 |
+
self.active_version = {
|
489 |
+
**self.staging_version,
|
490 |
+
'activated_at': datetime.now().isoformat(),
|
491 |
+
'status': 'active'
|
492 |
+
}
|
493 |
+
|
494 |
+
# Clear staging
|
495 |
+
self.staging_version = None
|
496 |
+
|
497 |
+
# Update traffic split to 100% green if that's where new version is
|
498 |
+
if staging_env == "green":
|
499 |
+
self.update_traffic_split(100)
|
500 |
+
else:
|
501 |
+
self.update_traffic_split(0)
|
502 |
+
|
503 |
+
# Archive old version if exists
|
504 |
+
self.archive_old_version()
|
505 |
+
|
506 |
+
self.log_deployment_event("deployment_finalized", "Deployment successfully finalized", {
|
507 |
+
'active_version': self.active_version,
|
508 |
+
'traffic_split': self.traffic_split
|
509 |
+
})
|
510 |
+
|
511 |
+
logger.info("Deployment finalized successfully")
|
512 |
+
|
513 |
+
except Exception as e:
|
514 |
+
logger.error(f"Failed to finalize deployment: {e}")
|
515 |
+
raise e
|
516 |
+
|
517 |
+
def get_active_version(self) -> Optional[Dict]:
|
518 |
+
"""Get currently active model version"""
|
519 |
+
return self.active_version
|
520 |
+
|
521 |
+
def get_staging_version(self) -> Optional[Dict]:
|
522 |
+
"""Get currently staged model version"""
|
523 |
+
return self.staging_version
|
524 |
+
|
525 |
+
def get_traffic_split(self) -> Dict[str, int]:
|
526 |
+
"""Get current traffic split configuration"""
|
527 |
+
return self.traffic_split.copy()
|
528 |
+
|
529 |
+
def determine_staging_environment(self) -> str:
|
530 |
+
"""Determine which environment to use for staging"""
|
531 |
+
if not self.active_version:
|
532 |
+
return "blue" # Default to blue if no active version
|
533 |
+
|
534 |
+
current_env = self.active_version.get('environment', 'blue')
|
535 |
+
return "green" if current_env == "blue" else "blue"
|
536 |
+
|
537 |
+
def version_exists(self, version_id: str) -> bool:
|
538 |
+
"""Check if a version exists"""
|
539 |
+
version_dir = self.models_dir / version_id
|
540 |
+
return version_dir.exists()
|
541 |
+
|
542 |
+
def clear_staging_environment(self):
|
543 |
+
"""Clear the staging environment"""
|
544 |
+
if self.staging_version:
|
545 |
+
staging_env = self.staging_version['environment']
|
546 |
+
staging_dir = self.blue_dir if staging_env == "blue" else self.green_dir
|
547 |
+
|
548 |
+
import shutil
|
549 |
+
if staging_dir.exists():
|
550 |
+
shutil.rmtree(staging_dir)
|
551 |
+
staging_dir.mkdir(parents=True, exist_ok=True)
|
552 |
+
|
553 |
+
self.staging_version = None
|
554 |
+
|
555 |
+
def archive_old_version(self):
|
556 |
+
"""Archive the previously active version"""
|
557 |
+
# Implementation for archiving old versions
|
558 |
+
# This could move old versions to an archive directory
|
559 |
+
pass
|
560 |
+
|
561 |
+
def start_deployment_monitoring(self):
|
562 |
+
"""Start background deployment monitoring"""
|
563 |
+
if not self.monitoring_active:
|
564 |
+
self.monitoring_active = True
|
565 |
+
self.monitor_thread = threading.Thread(target=self.deployment_monitoring_loop, daemon=True)
|
566 |
+
self.monitor_thread.start()
|
567 |
+
|
568 |
+
def stop_deployment_monitoring(self):
|
569 |
+
"""Stop deployment monitoring"""
|
570 |
+
self.monitoring_active = False
|
571 |
+
if self.monitor_thread:
|
572 |
+
self.monitor_thread.join(timeout=10)
|
573 |
+
|
574 |
+
def deployment_monitoring_loop(self):
|
575 |
+
"""Background monitoring loop for deployments"""
|
576 |
+
while self.monitoring_active:
|
577 |
+
try:
|
578 |
+
if (self.current_deployment and
|
579 |
+
self.current_deployment.status == DeploymentStatus.DEPLOYING.value):
|
580 |
+
|
581 |
+
# Perform periodic health checks
|
582 |
+
health_result = self.perform_health_check()
|
583 |
+
|
584 |
+
if self.should_trigger_immediate_rollback(health_result):
|
585 |
+
self.initiate_rollback("Automated rollback due to health check failures")
|
586 |
+
break
|
587 |
+
|
588 |
+
time.sleep(30) # Check every 30 seconds
|
589 |
+
|
590 |
+
except Exception as e:
|
591 |
+
logger.error(f"Deployment monitoring error: {e}")
|
592 |
+
time.sleep(60)
|
593 |
+
|
594 |
+
def get_average_response_time(self) -> float:
|
595 |
+
"""Get average response time from recent requests"""
|
596 |
+
# This would integrate with your monitoring system
|
597 |
+
# For now, return a simulated value
|
598 |
+
return np.random.normal(2.0, 0.5)
|
599 |
+
|
600 |
+
def get_current_error_rate(self) -> float:
|
601 |
+
"""Get current error rate"""
|
602 |
+
# This would integrate with your monitoring system
|
603 |
+
# For now, return a simulated value
|
604 |
+
return np.random.beta(1, 20) # Typically low error rate
|
605 |
+
|
606 |
+
def get_average_confidence(self) -> float:
|
607 |
+
"""Get average prediction confidence"""
|
608 |
+
# This would integrate with your monitoring system
|
609 |
+
# For now, return a simulated value
|
610 |
+
return np.random.beta(8, 2) # Typically high confidence
|
611 |
+
|
612 |
+
def log_deployment_event(self, event: str, message: str, details: Dict = None):
|
613 |
+
"""Log deployment events"""
|
614 |
+
try:
|
615 |
+
log_entry = {
|
616 |
+
'timestamp': datetime.now().isoformat(),
|
617 |
+
'event': event,
|
618 |
+
'message': message,
|
619 |
+
'details': details or {}
|
620 |
+
}
|
621 |
+
|
622 |
+
# Load existing logs
|
623 |
+
logs = []
|
624 |
+
if self.deployment_log_path.exists():
|
625 |
+
try:
|
626 |
+
with open(self.deployment_log_path, 'r') as f:
|
627 |
+
logs = json.load(f)
|
628 |
+
except:
|
629 |
+
logs = []
|
630 |
+
|
631 |
+
logs.append(log_entry)
|
632 |
+
|
633 |
+
# Keep only last 1000 entries
|
634 |
+
if len(logs) > 1000:
|
635 |
+
logs = logs[-1000:]
|
636 |
+
|
637 |
+
# Save logs
|
638 |
+
with open(self.deployment_log_path, 'w') as f:
|
639 |
+
json.dump(logs, f, indent=2)
|
640 |
+
|
641 |
+
except Exception as e:
|
642 |
+
logger.error(f"Failed to log deployment event: {e}")
|
643 |
+
|
644 |
+
def log_traffic_change(self, traffic_split: Dict[str, int]):
|
645 |
+
"""Log traffic routing changes"""
|
646 |
+
try:
|
647 |
+
traffic_entry = {
|
648 |
+
'timestamp': datetime.now().isoformat(),
|
649 |
+
'traffic_split': traffic_split,
|
650 |
+
'deployment_id': self.current_deployment.deployment_id if self.current_deployment else None
|
651 |
+
}
|
652 |
+
|
653 |
+
# Load existing traffic logs
|
654 |
+
logs = []
|
655 |
+
if self.traffic_log_path.exists():
|
656 |
+
try:
|
657 |
+
with open(self.traffic_log_path, 'r') as f:
|
658 |
+
logs = json.load(f)
|
659 |
+
except:
|
660 |
+
logs = []
|
661 |
+
|
662 |
+
logs.append(traffic_entry)
|
663 |
+
|
664 |
+
# Keep only last 500 entries
|
665 |
+
if len(logs) > 500:
|
666 |
+
logs = logs[-500:]
|
667 |
+
|
668 |
+
# Save logs
|
669 |
+
with open(self.traffic_log_path, 'w') as f:
|
670 |
+
json.dump(logs, f, indent=2)
|
671 |
+
|
672 |
+
except Exception as e:
|
673 |
+
logger.error(f"Failed to log traffic change: {e}")
|
674 |
+
|
675 |
+
def save_deployment_state(self):
|
676 |
+
"""Save current deployment state"""
|
677 |
+
try:
|
678 |
+
state = {
|
679 |
+
'current_deployment': asdict(self.current_deployment) if self.current_deployment else None,
|
680 |
+
'active_version': self.active_version,
|
681 |
+
'staging_version': self.staging_version,
|
682 |
+
'traffic_split': self.traffic_split,
|
683 |
+
'last_updated': datetime.now().isoformat()
|
684 |
+
}
|
685 |
+
|
686 |
+
with open(self.deployment_state_path, 'w') as f:
|
687 |
+
json.dump(state, f, indent=2)
|
688 |
+
|
689 |
+
except Exception as e:
|
690 |
+
logger.error(f"Failed to save deployment state: {e}")
|
691 |
+
|
692 |
+
def load_deployment_state(self):
|
693 |
+
"""Load existing deployment state"""
|
694 |
+
try:
|
695 |
+
if self.deployment_state_path.exists():
|
696 |
+
with open(self.deployment_state_path, 'r') as f:
|
697 |
+
state = json.load(f)
|
698 |
+
|
699 |
+
# Restore state
|
700 |
+
if state.get('current_deployment'):
|
701 |
+
self.current_deployment = DeploymentPlan(**state['current_deployment'])
|
702 |
+
|
703 |
+
self.active_version = state.get('active_version')
|
704 |
+
self.staging_version = state.get('staging_version')
|
705 |
+
self.traffic_split = state.get('traffic_split', {"blue": 100, "green": 0})
|
706 |
+
|
707 |
+
logger.info("Loaded deployment state from file")
|
708 |
+
|
709 |
+
except Exception as e:
|
710 |
+
logger.warning(f"Failed to load deployment state: {e}")
|
711 |
+
|
712 |
+
def get_deployment_status(self) -> Dict[str, Any]:
|
713 |
+
"""Get comprehensive deployment status"""
|
714 |
+
try:
|
715 |
+
return {
|
716 |
+
'timestamp': datetime.now().isoformat(),
|
717 |
+
'current_deployment': asdict(self.current_deployment) if self.current_deployment else None,
|
718 |
+
'active_version': self.active_version,
|
719 |
+
'staging_version': self.staging_version,
|
720 |
+
'traffic_split': self.traffic_split,
|
721 |
+
'monitoring_active': self.monitoring_active,
|
722 |
+
'available_versions': self.list_available_versions(),
|
723 |
+
'recent_deployments': self.get_recent_deployments(limit=5)
|
724 |
+
}
|
725 |
+
except Exception as e:
|
726 |
+
logger.error(f"Failed to get deployment status: {e}")
|
727 |
+
return {'error': str(e)}
|
728 |
+
|
729 |
+
def list_available_versions(self) -> List[str]:
|
730 |
+
"""List all available model versions"""
|
731 |
+
try:
|
732 |
+
versions = []
|
733 |
+
if self.models_dir.exists():
|
734 |
+
for item in self.models_dir.iterdir():
|
735 |
+
if item.is_dir() and item.name.startswith('v'):
|
736 |
+
versions.append(item.name)
|
737 |
+
return sorted(versions, reverse=True)
|
738 |
+
except Exception as e:
|
739 |
+
logger.error(f"Failed to list versions: {e}")
|
740 |
+
return []
|
741 |
+
|
742 |
+
def get_recent_deployments(self, limit: int = 10) -> List[Dict]:
|
743 |
+
"""Get recent deployment history"""
|
744 |
+
try:
|
745 |
+
if not self.deployment_log_path.exists():
|
746 |
+
return []
|
747 |
+
|
748 |
+
with open(self.deployment_log_path, 'r') as f:
|
749 |
+
logs = json.load(f)
|
750 |
+
|
751 |
+
# Filter deployment events
|
752 |
+
deployment_events = [
|
753 |
+
log for log in logs
|
754 |
+
if log.get('event') in ['deployment_prepared', 'deployment_finalized', 'rollback_initiated']
|
755 |
+
]
|
756 |
+
|
757 |
+
return deployment_events[-limit:]
|
758 |
+
|
759 |
+
except Exception as e:
|
760 |
+
logger.error(f"Failed to get recent deployments: {e}")
|
761 |
+
return []
|