blueprint-remote-providers 0.2.0-alpha.2

Remote service providers for Tangle Blueprints
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Blueprint update and rollback management
//!
//! Provides safe blueprint updates with automatic rollback on failure,
//! blue-green deployments, and version history tracking.

use crate::core::error::{Error, Result};
use crate::core::resources::ResourceSpec;
use crate::deployment::ssh::SshDeploymentClient;
use crate::infra::traits::{BlueprintDeploymentResult, CloudProviderAdapter};
use blueprint_core::{debug, error, info, warn};
use blueprint_std::collections::{HashMap, VecDeque};
use blueprint_std::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
use tokio::time::{sleep, timeout};

/// Maximum number of deployment versions to keep
const MAX_VERSION_HISTORY: usize = 10;

/// Parameters for deployment updates
#[derive(Debug, Clone)]
pub struct UpdateParams {
    pub version: String,
    pub new_image: String,
    pub resource_spec: ResourceSpec,
    pub env_vars: HashMap<String, String>,
}

/// Parameters for rolling deployment updates
#[derive(Debug, Clone)]
pub struct RollingUpdateParams {
    pub base: UpdateParams,
    pub max_unavailable: u32,
    pub max_surge: u32,
}

/// Parameters for canary deployment updates
#[derive(Debug, Clone)]
pub struct CanaryUpdateParams {
    pub base: UpdateParams,
    pub initial_percentage: u8,
    pub increment: u8,
    pub interval: Duration,
}

/// Deployment update strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateStrategy {
    /// Replace existing deployment immediately
    RollingUpdate {
        max_unavailable: u32,
        max_surge: u32,
    },
    /// Deploy new version alongside old, switch traffic when ready
    BlueGreen {
        switch_timeout: Duration,
        health_check_duration: Duration,
    },
    /// Gradually shift traffic to new version
    Canary {
        initial_percentage: u8,
        increment: u8,
        interval: Duration,
    },
    /// Replace in-place without safety checks (fast but risky)
    Recreate,
}

impl Default for UpdateStrategy {
    fn default() -> Self {
        Self::BlueGreen {
            switch_timeout: Duration::from_secs(300),
            health_check_duration: Duration::from_secs(60),
        }
    }
}

/// Deployment version information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeploymentVersion {
    pub version: String,
    pub blueprint_image: String,
    pub resource_spec: ResourceSpec,
    pub env_vars: HashMap<String, String>,
    pub deployment_time: SystemTime,
    pub status: VersionStatus,
    pub metadata: HashMap<String, String>,
    pub container_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum VersionStatus {
    Active,
    Inactive,
    Failed,
    RolledBack,
    Staging,
}

/// Manages blueprint updates and rollbacks
pub struct UpdateManager {
    versions: VecDeque<DeploymentVersion>,
    active_version: Option<String>,
    strategy: UpdateStrategy,
}

impl UpdateManager {
    pub fn new(strategy: UpdateStrategy) -> Self {
        Self {
            versions: VecDeque::new(),
            active_version: None,
            strategy,
        }
    }

    /// Add a new deployment version
    pub fn add_version(&mut self, version: DeploymentVersion) {
        info!("Adding deployment version: {}", version.version);

        // Keep only the latest versions
        if self.versions.len() >= MAX_VERSION_HISTORY {
            self.versions.pop_front();
        }

        self.versions.push_back(version);
    }

    /// Get the currently active version
    pub fn active_version(&self) -> Option<&DeploymentVersion> {
        self.active_version
            .as_ref()
            .and_then(|v| self.versions.iter().find(|ver| ver.version == *v))
    }

    /// Get a specific version
    pub fn get_version(&self, version: &str) -> Option<&DeploymentVersion> {
        self.versions.iter().find(|v| v.version == version)
    }

    /// List all versions
    pub fn list_versions(&self) -> Vec<&DeploymentVersion> {
        self.versions.iter().collect()
    }

    /// Update blueprint with new version
    pub async fn update_blueprint(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        new_image: &str,
        resource_spec: &ResourceSpec,
        env_vars: HashMap<String, String>,
        current_deployment: &BlueprintDeploymentResult,
    ) -> Result<BlueprintDeploymentResult> {
        let new_version = self.generate_version();
        info!("Starting blueprint update to version {}", new_version);

        match &self.strategy {
            UpdateStrategy::BlueGreen {
                switch_timeout,
                health_check_duration,
            } => {
                let params = UpdateParams {
                    version: new_version.clone(),
                    new_image: new_image.to_string(),
                    resource_spec: resource_spec.clone(),
                    env_vars,
                };
                self.blue_green_update(
                    adapter,
                    &params,
                    current_deployment,
                    *switch_timeout,
                    *health_check_duration,
                )
                .await
            }
            UpdateStrategy::RollingUpdate {
                max_unavailable,
                max_surge,
            } => {
                let params = RollingUpdateParams {
                    base: UpdateParams {
                        version: new_version.clone(),
                        new_image: new_image.to_string(),
                        resource_spec: resource_spec.clone(),
                        env_vars,
                    },
                    max_unavailable: *max_unavailable,
                    max_surge: *max_surge,
                };
                self.rolling_update(adapter, &params, current_deployment)
                    .await
            }
            UpdateStrategy::Canary {
                initial_percentage,
                increment,
                interval,
            } => {
                let params = CanaryUpdateParams {
                    base: UpdateParams {
                        version: new_version.clone(),
                        new_image: new_image.to_string(),
                        resource_spec: resource_spec.clone(),
                        env_vars,
                    },
                    initial_percentage: *initial_percentage,
                    increment: *increment,
                    interval: *interval,
                };
                self.canary_update(adapter, &params, current_deployment)
                    .await
            }
            UpdateStrategy::Recreate => {
                self.recreate_update(
                    adapter,
                    &new_version,
                    new_image,
                    resource_spec,
                    env_vars,
                    current_deployment,
                )
                .await
            }
        }
    }

    /// Blue-green deployment update
    async fn blue_green_update(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        params: &UpdateParams,
        current_deployment: &BlueprintDeploymentResult,
        _switch_timeout: Duration,
        health_check_duration: Duration,
    ) -> Result<BlueprintDeploymentResult> {
        info!(
            "Starting blue-green deployment for version {}",
            params.version
        );

        // Deploy new version (green)
        let mut green_env = params.env_vars.clone();
        green_env.insert("DEPLOYMENT_VERSION".to_string(), params.version.clone());
        green_env.insert("DEPLOYMENT_COLOR".to_string(), "green".to_string());

        let green_deployment = adapter
            .deploy_blueprint(
                &current_deployment.instance,
                &params.new_image,
                &params.resource_spec,
                green_env.clone(),
            )
            .await
            .map_err(|e| {
                error!("Failed to deploy green version: {}", e);
                e
            })?;

        // Add to version history
        self.add_version(DeploymentVersion {
            version: params.version.clone(),
            blueprint_image: params.new_image.clone(),
            resource_spec: params.resource_spec.clone(),
            env_vars: green_env,
            deployment_time: SystemTime::now(),
            status: VersionStatus::Staging,
            metadata: green_deployment.metadata.clone(),
            container_id: Some(green_deployment.blueprint_id.clone()),
        });

        // Health check green deployment
        info!("Performing health checks on green deployment");
        let health_check_result = timeout(
            health_check_duration,
            self.wait_for_healthy(&green_deployment, adapter),
        )
        .await;

        match health_check_result {
            Ok(Ok(true)) => {
                info!("Green deployment is healthy, switching traffic");

                // Switch traffic to green
                if let Err(e) = self
                    .switch_traffic(&green_deployment, current_deployment)
                    .await
                {
                    warn!("Failed to switch traffic: {}, rolling back", e);
                    adapter.cleanup_blueprint(&green_deployment).await?;
                    return Err(e);
                }

                // Mark green as active
                if let Some(v) = self
                    .versions
                    .iter_mut()
                    .find(|v| v.version == params.version)
                {
                    v.status = VersionStatus::Active;
                }

                // Mark old version as inactive
                if let Some(old_version) = &self.active_version {
                    if let Some(v) = self.versions.iter_mut().find(|v| v.version == *old_version) {
                        v.status = VersionStatus::Inactive;
                    }
                }

                self.active_version = Some(params.version.clone());

                // Cleanup old deployment after switch
                sleep(Duration::from_secs(30)).await;
                if let Err(e) = adapter.cleanup_blueprint(current_deployment).await {
                    warn!("Failed to cleanup old deployment: {}", e);
                }

                Ok(green_deployment)
            }
            _ => {
                error!("Green deployment health check failed, cleaning up");

                // Mark as failed
                if let Some(v) = self
                    .versions
                    .iter_mut()
                    .find(|v| v.version == params.version)
                {
                    v.status = VersionStatus::Failed;
                }

                // Cleanup failed green deployment
                adapter.cleanup_blueprint(&green_deployment).await?;

                Err(Error::Other("Green deployment health check failed".into()))
            }
        }
    }

    /// Rolling update deployment
    async fn rolling_update(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        params: &RollingUpdateParams,
        current_deployment: &BlueprintDeploymentResult,
    ) -> Result<BlueprintDeploymentResult> {
        info!("Starting rolling update to version {}", params.base.version);

        // For single instance, this is similar to recreate with health checks
        let mut new_env = params.base.env_vars.clone();
        new_env.insert(
            "DEPLOYMENT_VERSION".to_string(),
            params.base.version.clone(),
        );

        // Deploy new version
        let new_deployment = adapter
            .deploy_blueprint(
                &current_deployment.instance,
                &params.base.new_image,
                &params.base.resource_spec,
                new_env.clone(),
            )
            .await?;

        // Wait for new deployment to be healthy
        if !self.wait_for_healthy(&new_deployment, adapter).await? {
            // Rollback if health check fails
            adapter.cleanup_blueprint(&new_deployment).await?;
            return Err(Error::Other("New deployment failed health check".into()));
        }

        // Cleanup old deployment
        adapter.cleanup_blueprint(current_deployment).await?;

        // Update version tracking
        self.add_version(DeploymentVersion {
            version: params.base.version.clone(),
            blueprint_image: params.base.new_image.clone(),
            resource_spec: params.base.resource_spec.clone(),
            env_vars: new_env,
            deployment_time: SystemTime::now(),
            status: VersionStatus::Active,
            metadata: new_deployment.metadata.clone(),
            container_id: Some(new_deployment.blueprint_id.clone()),
        });

        self.active_version = Some(params.base.version.clone());

        Ok(new_deployment)
    }

    /// Canary deployment update
    async fn canary_update(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        params: &CanaryUpdateParams,
        current_deployment: &BlueprintDeploymentResult,
    ) -> Result<BlueprintDeploymentResult> {
        info!(
            "Starting canary deployment for version {}",
            params.base.version
        );

        // Deploy canary version
        let mut canary_env = params.base.env_vars.clone();
        canary_env.insert(
            "DEPLOYMENT_VERSION".to_string(),
            params.base.version.clone(),
        );
        canary_env.insert("DEPLOYMENT_TYPE".to_string(), "canary".to_string());

        let canary_deployment = adapter
            .deploy_blueprint(
                &current_deployment.instance,
                &params.base.new_image,
                &params.base.resource_spec,
                canary_env.clone(),
            )
            .await?;

        // Gradually increase traffic percentage
        let mut current_percentage = params.initial_percentage;

        while current_percentage < 100 {
            info!("Canary at {}% traffic", current_percentage);

            // Monitor canary health
            if !adapter.health_check_blueprint(&canary_deployment).await? {
                warn!(
                    "Canary health check failed at {}%, rolling back",
                    current_percentage
                );
                adapter.cleanup_blueprint(&canary_deployment).await?;
                return Err(Error::Other(format!(
                    "Canary failed at {current_percentage}%"
                )));
            }

            // Wait before increasing traffic
            sleep(params.interval).await;

            current_percentage = (current_percentage + params.increment).min(100);
        }

        // Full rollout successful
        info!("Canary deployment successful, completing rollout");

        // Cleanup old deployment
        adapter.cleanup_blueprint(current_deployment).await?;

        // Update version tracking
        self.add_version(DeploymentVersion {
            version: params.base.version.clone(),
            blueprint_image: params.base.new_image.clone(),
            resource_spec: params.base.resource_spec.clone(),
            env_vars: canary_env,
            deployment_time: SystemTime::now(),
            status: VersionStatus::Active,
            metadata: canary_deployment.metadata.clone(),
            container_id: Some(canary_deployment.blueprint_id.clone()),
        });

        self.active_version = Some(params.base.version.clone());

        Ok(canary_deployment)
    }

    /// Recreate deployment (fast but with downtime)
    async fn recreate_update(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        version: &str,
        new_image: &str,
        resource_spec: &ResourceSpec,
        env_vars: HashMap<String, String>,
        current_deployment: &BlueprintDeploymentResult,
    ) -> Result<BlueprintDeploymentResult> {
        info!("Starting recreate deployment for version {}", version);

        // Cleanup old deployment first (causes downtime)
        adapter.cleanup_blueprint(current_deployment).await?;

        // Deploy new version
        let mut new_env = env_vars.clone();
        new_env.insert("DEPLOYMENT_VERSION".to_string(), version.to_string());

        let new_deployment = adapter
            .deploy_blueprint(
                &current_deployment.instance,
                new_image,
                resource_spec,
                new_env.clone(),
            )
            .await?;

        // Update version tracking
        self.add_version(DeploymentVersion {
            version: version.to_string(),
            blueprint_image: new_image.to_string(),
            resource_spec: resource_spec.clone(),
            env_vars: new_env,
            deployment_time: SystemTime::now(),
            status: VersionStatus::Active,
            metadata: new_deployment.metadata.clone(),
            container_id: Some(new_deployment.blueprint_id.clone()),
        });

        self.active_version = Some(version.to_string());

        Ok(new_deployment)
    }

    /// Rollback to a previous version
    pub async fn rollback(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        target_version: &str,
        current_deployment: &BlueprintDeploymentResult,
    ) -> Result<BlueprintDeploymentResult> {
        info!("Rolling back to version {}", target_version);

        let version = self
            .get_version(target_version)
            .ok_or_else(|| Error::Other(format!("Version {target_version} not found")))?
            .clone();

        if version.status == VersionStatus::Failed {
            return Err(Error::Other("Cannot rollback to a failed version".into()));
        }

        // Deploy the target version
        let rollback_deployment = adapter
            .deploy_blueprint(
                &current_deployment.instance,
                &version.blueprint_image,
                &version.resource_spec,
                version.env_vars.clone(),
            )
            .await?;

        // Wait for rollback to be healthy
        if !self.wait_for_healthy(&rollback_deployment, adapter).await? {
            error!("Rollback deployment failed health check");
            adapter.cleanup_blueprint(&rollback_deployment).await?;
            return Err(Error::Other("Rollback failed health check".into()));
        }

        // Cleanup current deployment
        adapter.cleanup_blueprint(current_deployment).await?;

        // Update version status
        if let Some(current) = &self.active_version {
            if let Some(v) = self.versions.iter_mut().find(|v| v.version == *current) {
                v.status = VersionStatus::RolledBack;
            }
        }

        // Mark rollback version as active
        if let Some(v) = self
            .versions
            .iter_mut()
            .find(|v| v.version == target_version)
        {
            v.status = VersionStatus::Active;
        }

        self.active_version = Some(target_version.to_string());

        Ok(rollback_deployment)
    }

    /// Wait for deployment to become healthy
    async fn wait_for_healthy(
        &self,
        deployment: &BlueprintDeploymentResult,
        adapter: &dyn CloudProviderAdapter,
    ) -> Result<bool> {
        let max_attempts = 30;
        let check_interval = Duration::from_secs(10);

        for attempt in 1..=max_attempts {
            debug!("Health check attempt {}/{}", attempt, max_attempts);

            match adapter.health_check_blueprint(deployment).await {
                Ok(true) => {
                    info!("Deployment is healthy");
                    return Ok(true);
                }
                Ok(false) => {
                    if attempt < max_attempts {
                        sleep(check_interval).await;
                    }
                }
                Err(e) => {
                    warn!("Health check error: {}", e);
                    if attempt < max_attempts {
                        sleep(check_interval).await;
                    }
                }
            }
        }

        Ok(false)
    }

    /// Switch traffic from old to new deployment
    async fn switch_traffic(
        &self,
        new_deployment: &BlueprintDeploymentResult,
        old_deployment: &BlueprintDeploymentResult,
    ) -> Result<()> {
        // In a real implementation, this would update load balancer rules,
        // DNS records, or service mesh configuration
        info!(
            "Switching traffic from {} to {}",
            old_deployment.blueprint_id, new_deployment.blueprint_id
        );

        // Simulate traffic switch
        sleep(Duration::from_secs(5)).await;

        Ok(())
    }

    /// Generate a new version identifier
    fn generate_version(&self) -> String {
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        format!("v{timestamp}")
    }

    /// Get deployment history
    pub fn get_history(&self, limit: usize) -> Vec<DeploymentVersion> {
        self.versions.iter().rev().take(limit).cloned().collect()
    }

    /// Clean up old inactive versions
    pub async fn cleanup_old_versions(
        &mut self,
        adapter: &dyn CloudProviderAdapter,
        keep_count: usize,
    ) -> Result<()> {
        let inactive_versions: Vec<_> = self
            .versions
            .iter()
            .filter(|v| v.status == VersionStatus::Inactive)
            .skip(keep_count)
            .cloned()
            .collect();

        for version in inactive_versions {
            info!("Cleaning up old version: {}", version.version);

            // Create a dummy deployment result for cleanup
            if let Some(container_id) = version.container_id {
                let deployment = BlueprintDeploymentResult {
                    instance: crate::infra::types::ProvisionedInstance {
                        id: format!("update-cleanup-{}", uuid::Uuid::new_v4()),
                        public_ip: None,
                        private_ip: None,
                        status: crate::infra::types::InstanceStatus::Unknown,
                        provider: crate::core::remote::CloudProvider::Generic,
                        region: "unknown".to_string(),
                        instance_type: "unknown".to_string(),
                    },
                    blueprint_id: container_id,
                    port_mappings: HashMap::new(),
                    metadata: version.metadata.clone(),
                };

                if let Err(e) = adapter.cleanup_blueprint(&deployment).await {
                    warn!("Failed to cleanup version {}: {}", version.version, e);
                }
            }

            // Remove from history
            self.versions.retain(|v| v.version != version.version);
        }

        Ok(())
    }
}

/// SSH-specific update operations
impl UpdateManager {
    /// Update blueprint via SSH
    pub async fn update_via_ssh(
        &mut self,
        ssh_client: &SshDeploymentClient,
        new_image: &str,
        resource_spec: &ResourceSpec,
        env_vars: HashMap<String, String>,
    ) -> Result<String> {
        let version = self.generate_version();
        info!("Starting SSH update to version {}", version);

        match &self.strategy {
            UpdateStrategy::BlueGreen { .. } => {
                // Deploy new container alongside old one with resource limits
                let new_container_name = format!("blueprint-{version}");
                let new_container_id = ssh_client
                    .deploy_container_with_resources(
                        new_image,
                        &new_container_name,
                        env_vars.clone(),
                        Some(resource_spec),
                    )
                    .await?;

                // Health check new container
                if ssh_client.health_check_container(&new_container_id).await? {
                    // Switch traffic (update nginx/haproxy config)
                    ssh_client.switch_traffic_to(&new_container_name).await?;

                    // Stop old container
                    if let Some(old_version) = &self.active_version {
                        let old_container_name = format!("blueprint-{old_version}");
                        ssh_client.stop_container(&old_container_name).await?;
                    }

                    self.active_version = Some(version.clone());
                    Ok(new_container_id)
                } else {
                    // Rollback
                    ssh_client.remove_container(&new_container_id).await?;
                    Err(Error::Other("New container health check failed".into()))
                }
            }
            _ => {
                // Simple replace for other strategies with resource limits
                let new_container_id = ssh_client
                    .update_container_with_resources(new_image, env_vars, Some(resource_spec))
                    .await?;

                self.active_version = Some(version.clone());
                Ok(new_container_id)
            }
        }
    }

    /// Rollback via SSH
    pub async fn rollback_via_ssh(
        &mut self,
        ssh_client: &SshDeploymentClient,
        target_version: &str,
    ) -> Result<()> {
        let version = self
            .get_version(target_version)
            .ok_or_else(|| Error::Other(format!("Version {target_version} not found")))?
            .clone();

        // Redeploy the target version
        ssh_client
            .deploy_container(&version.blueprint_image, version.env_vars)
            .await?;

        self.active_version = Some(target_version.to_string());

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version_management() {
        let mut manager = UpdateManager::new(UpdateStrategy::default());

        let version1 = DeploymentVersion {
            version: "v1".to_string(),
            blueprint_image: "image:v1".to_string(),
            resource_spec: ResourceSpec::basic(),
            env_vars: HashMap::new(),
            deployment_time: SystemTime::now(),
            status: VersionStatus::Active,
            metadata: HashMap::new(),
            container_id: Some("container1".to_string()),
        };

        manager.add_version(version1.clone());
        manager.active_version = Some("v1".to_string());

        assert_eq!(manager.active_version().unwrap().version, "v1");
        assert_eq!(manager.list_versions().len(), 1);
    }

    #[test]
    fn test_version_history_limit() {
        let mut manager = UpdateManager::new(UpdateStrategy::default());

        // Add more than MAX_VERSION_HISTORY versions
        for i in 0..15 {
            let version = DeploymentVersion {
                version: format!("v{i}"),
                blueprint_image: format!("image:v{i}"),
                resource_spec: ResourceSpec::basic(),
                env_vars: HashMap::new(),
                deployment_time: SystemTime::now(),
                status: VersionStatus::Inactive,
                metadata: HashMap::new(),
                container_id: Some(format!("container{i}")),
            };
            manager.add_version(version);
        }

        // Should keep only MAX_VERSION_HISTORY versions
        assert!(manager.list_versions().len() <= MAX_VERSION_HISTORY);
    }

    #[tokio::test]
    async fn test_generate_version() {
        let manager = UpdateManager::new(UpdateStrategy::default());
        let version1 = manager.generate_version();
        sleep(Duration::from_secs(1)).await;
        let version2 = manager.generate_version();

        assert_ne!(version1, version2);
        assert!(version1.starts_with("v"));
        assert!(version2.starts_with("v"));
    }
}