amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
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
// Deployment strategies module
// Provides Blue-Green, Canary, and Rolling deployment support
/// Deployment configuration
#[derive(Debug, Clone)]
pub struct DeploymentConfig {
    /// Deployment strategy
    pub strategy: String,
    /// Service name
    pub service_name: String,
    /// Environment
    pub environment: String,
    /// Current version
    pub current_version: String,
    /// Next version
    pub next_version: String,
    /// Additional parameters
    pub parameters: std::collections::HashMap<String, String>,
}
/// Deployment status
#[derive(Debug, Clone)]
pub struct DeploymentStatus {
    /// Success status
    pub success: bool,
    /// Status message
    pub message: String,
    /// Current version
    pub current_version: String,
    /// Previous version
    pub previous_version: Option<String>,
    /// Traffic split (for canary deployments)
    pub traffic_split: Option<u32>,
}
/// Blue-Green deployment strategy
#[derive(Debug, Clone)]
pub struct BlueGreenDeployment {
    /// Service name
    pub service_name: String,
    /// Environment
    pub environment: String,
    /// Current version
    pub current_version: String,
    /// Next version
    pub next_version: String,
    /// Pre-deployment hook
    pub pre_hook: Option<Vec<String>>,
    /// Post-deployment hook
    pub post_hook: Option<Vec<String>>,
    /// Rollback on failure
    pub rollback_on_failure: bool,
}
impl BlueGreenDeployment {
    /// Create a new Blue-Green deployment
    pub fn new(
        service_name: String,
        environment: String,
        current_version: String,
        next_version: String,
    ) -> Self {
        Self {
            service_name,
            environment,
            current_version,
            next_version,
            pre_hook: None,
            post_hook: None,
            rollback_on_failure: true,
        }
    }
    /// Set pre-deployment hook
    pub fn pre_hook(mut self, hook: Vec<String>) -> Self {
        self.pre_hook = Some(hook);
        self
    }
    /// Set post-deployment hook
    pub fn post_hook(mut self, hook: Vec<String>) -> Self {
        self.post_hook = Some(hook);
        self
    }
    /// Set rollback on failure
    pub fn rollback_on_failure(mut self, rollback: bool) -> Self {
        self.rollback_on_failure = rollback;
        self
    }
    /// Execute Blue-Green deployment
    pub fn execute(&self) -> Result<DeploymentStatus, Error> {
        println!(
            "Executing Blue-Green deployment for {}/{}: {} -> {}",
            self.service_name, self.environment, self.current_version, self.next_version
        );
        // In a real implementation, this would:
        // 1. Deploy the next version to the green environment
        // 2. Run health checks
        // 3. Switch traffic to green
        // 4. Optionally decommission blue
        Ok(DeploymentStatus {
            success: true,
            message: format!(
                "Blue-Green deployment completed successfully for {}/{}",
                self.service_name, self.environment
            ),
            current_version: self.next_version.clone(),
            previous_version: Some(self.current_version.clone()),
            traffic_split: None,
        })
    }
}
/// Canary deployment strategy
#[derive(Debug, Clone)]
pub struct CanaryDeployment {
    /// Service name
    pub service_name: String,
    /// Environment
    pub environment: String,
    /// Current version
    pub current_version: String,
    /// Next version
    pub next_version: String,
    /// Traffic split percentage
    pub traffic_split: u32,
    /// Auto-promote
    pub auto_promote: bool,
    /// Promotion threshold
    pub promotion_threshold: u32,
    /// Health check interval
    pub health_check_interval: u32,
    /// Rollback on failure
    pub rollback_on_failure: bool,
}
impl CanaryDeployment {
    /// Create a new Canary deployment
    pub fn new(
        service_name: String,
        environment: String,
        current_version: String,
        next_version: String,
        traffic_split: u32,
    ) -> Self {
        Self {
            service_name,
            environment,
            current_version,
            next_version,
            traffic_split,
            auto_promote: false,
            promotion_threshold: 95,
            health_check_interval: 30,
            rollback_on_failure: true,
        }
    }
    /// Set auto-promote
    pub fn auto_promote(mut self, auto: bool) -> Self {
        self.auto_promote = auto;
        self
    }
    /// Set promotion threshold
    pub fn promotion_threshold(mut self, threshold: u32) -> Self {
        self.promotion_threshold = threshold;
        self
    }
    /// Set health check interval
    pub fn health_check_interval(mut self, interval: u32) -> Self {
        self.health_check_interval = interval;
        self
    }
    /// Set rollback on failure
    pub fn rollback_on_failure(mut self, rollback: bool) -> Self {
        self.rollback_on_failure = rollback;
        self
    }
    /// Execute Canary deployment
    pub fn execute(&self) -> Result<DeploymentStatus, Error> {
        println!(
            "Executing Canary deployment for {}/{}: {} -> {} ({}% traffic)",
            self.service_name,
            self.environment,
            self.current_version,
            self.next_version,
            self.traffic_split
        );
        // In a real implementation, this would:
        // 1. Deploy the next version with limited traffic
        // 2. Monitor metrics and health
        // 3. Gradually increase traffic if healthy
        // 4. Auto-promote if threshold reached
        Ok(DeploymentStatus {
            success: true,
            message: format!(
                "Canary deployment started for {}/{} with {}% traffic",
                self.service_name, self.environment, self.traffic_split
            ),
            current_version: self.current_version.clone(),
            previous_version: None,
            traffic_split: Some(self.traffic_split),
        })
    }
    /// Promote canary to full deployment
    pub fn promote_canary(&self) -> Result<DeploymentStatus, Error> {
        println!(
            "Promoting Canary deployment for {}/{} to 100% traffic",
            self.service_name, self.environment
        );
        Ok(DeploymentStatus {
            success: true,
            message: format!(
                "Canary promoted to full deployment for {}/{}",
                self.service_name, self.environment
            ),
            current_version: self.next_version.clone(),
            previous_version: Some(self.current_version.clone()),
            traffic_split: Some(100),
        })
    }
    /// Rollback canary deployment
    pub fn rollback_canary(&self) -> Result<DeploymentStatus, Error> {
        println!(
            "Rolling back Canary deployment for {}/{}",
            self.service_name, self.environment
        );
        Ok(DeploymentStatus {
            success: true,
            message: format!(
                "Canary deployment rolled back for {}/{}",
                self.service_name, self.environment
            ),
            current_version: self.current_version.clone(),
            previous_version: Some(self.next_version.clone()),
            traffic_split: Some(0),
        })
    }
}
/// Rolling deployment strategy
#[derive(Debug, Clone)]
pub struct RollingDeployment {
    /// Service name
    pub service_name: String,
    /// Environment
    pub environment: String,
    /// Current version
    pub current_version: String,
    /// Next version
    pub next_version: String,
    /// Maximum unavailable pods
    pub max_unavailable: u32,
    /// Maximum surge pods
    pub max_surge: u32,
    /// Minimum ready seconds
    pub min_ready_seconds: u32,
    /// Progress deadline seconds
    pub progress_deadline_seconds: u32,
    /// Rollback on failure
    pub rollback_on_failure: bool,
}
impl RollingDeployment {
    /// Create a new Rolling deployment
    pub fn new(
        service_name: String,
        environment: String,
        current_version: String,
        next_version: String,
    ) -> Self {
        Self {
            service_name,
            environment,
            current_version,
            next_version,
            max_unavailable: 1,
            max_surge: 1,
            min_ready_seconds: 0,
            progress_deadline_seconds: 600,
            rollback_on_failure: true,
        }
    }
    /// Set maximum unavailable pods
    pub fn max_unavailable(mut self, max: u32) -> Self {
        self.max_unavailable = max;
        self
    }
    /// Set maximum surge pods
    pub fn max_surge(mut self, max: u32) -> Self {
        self.max_surge = max;
        self
    }
    /// Set minimum ready seconds
    pub fn min_ready_seconds(mut self, seconds: u32) -> Self {
        self.min_ready_seconds = seconds;
        self
    }
    /// Set progress deadline seconds
    pub fn progress_deadline_seconds(mut self, seconds: u32) -> Self {
        self.progress_deadline_seconds = seconds;
        self
    }
    /// Set rollback on failure
    pub fn rollback_on_failure(mut self, rollback: bool) -> Self {
        self.rollback_on_failure = rollback;
        self
    }
    /// Execute Rolling deployment
    pub fn execute(&self) -> Result<DeploymentStatus, Error> {
        println!(
            "Executing Rolling deployment for {}/{}: {} -> {} (max_unavailable={}, max_surge={})",
            self.service_name,
            self.environment,
            self.current_version,
            self.next_version,
            self.max_unavailable,
            self.max_surge
        );
        // In a real implementation, this would:
        // 1. Gradually replace old pods with new ones
        // 2. Respect max_unavailable and max_surge limits
        // 3. Wait for pods to be ready (min_ready_seconds)
        // 4. Monitor progress and rollback if needed
        Ok(DeploymentStatus {
            success: true,
            message: format!(
                "Rolling deployment completed for {}/{}",
                self.service_name, self.environment
            ),
            current_version: self.next_version.clone(),
            previous_version: Some(self.current_version.clone()),
            traffic_split: None,
        })
    }
}
/// Deployment strategy enum
#[derive(Debug, Clone)]
pub enum DeploymentStrategy {
    /// Blue-Green deployment
    BlueGreen(BlueGreenDeployment),
    /// Canary deployment
    Canary(CanaryDeployment),
    /// Rolling deployment
    Rolling(RollingDeployment),
}
impl DeploymentStrategy {
    /// Create a new deployment strategy manager
    pub fn new() -> Self {
        // This is a placeholder - actual manager would be different
        // We'll implement a proper manager below
        panic!("Use DeploymentManager::new() instead");
    }
    /// Execute the deployment
    pub fn execute(&self) -> Result<DeploymentStatus, Error> {
        match self {
            DeploymentStrategy::BlueGreen(deployment) => deployment.execute(),
            DeploymentStrategy::Canary(deployment) => deployment.execute(),
            DeploymentStrategy::Rolling(deployment) => deployment.execute(),
        }
    }
}
/// Deployment manager
pub struct DeploymentManager {
    /// Deployment strategies
    strategies: Vec<DeploymentStrategy>,
}
impl DeploymentManager {
    /// Create a new deployment manager
    pub fn new() -> Self {
        Self {
            strategies: Vec::new(),
        }
    }
    /// Add deployment strategy
    pub fn add_strategy(&mut self, strategy: DeploymentStrategy) {
        self.strategies.push(strategy);
    }
    /// Get all strategies
    pub fn get_strategies(&self) -> &[DeploymentStrategy] {
        &self.strategies
    }
    /// Clear all strategies
    pub fn clear(&mut self) {
        self.strategies.clear();
    }
}
impl Default for DeploymentManager {
    fn default() -> Self {
        Self::new()
    }
}
/// Deployment strategy selector
pub struct DeploymentStrategySelector {
    /// Manager instance
    manager: DeploymentManager,
}
impl DeploymentStrategySelector {
    /// Create a new strategy selector
    pub fn new() -> Self {
        Self {
            manager: DeploymentManager::new(),
        }
    }
    /// Select and create deployment strategy based on configuration
    pub fn select_strategy(
        &mut self,
        config: &DeploymentConfig,
    ) -> Result<DeploymentStrategy, Error> {
        match config.strategy.to_lowercase().as_str() {
            "blue-green" | "blue_green" => {
                let strategy: _ = BlueGreenDeployment::new(
                    config.service_name.clone(),
                    config.environment.clone(),
                    config.current_version.clone(),
                    config.next_version.clone(),
                );
                Ok(DeploymentStrategy::BlueGreen(strategy))
            }
            "canary" => {
                let traffic_split: _ = config
                    .parameters
                    .get("traffic_split")
                    .and_then(|s| s.parse().ok())
                    .unwrap_or(10);
                let mut strategy = CanaryDeployment::new(
                    config.service_name.clone(),
                    config.environment.clone(),
                    config.current_version.clone(),
                    config.next_version.clone(),
                    traffic_split,
                );
                if let Some(threshold) = config.parameters.get("promotion_threshold") {
                    if let Ok(t) = threshold.parse() {
                        strategy = strategy.promotion_threshold(t);
                    }
                }
                if let Some(interval) = config.parameters.get("health_check_interval") {
                    if let Ok(i) = interval.parse() {
                        strategy = strategy.health_check_interval(i);
                    }
                }
                Ok(DeploymentStrategy::Canary(strategy))
            }
            "rolling" => {
                let mut strategy = RollingDeployment::new(
                    config.service_name.clone(),
                    config.environment.clone(),
                    config.current_version.clone(),
                    config.next_version.clone(),
                );
                if let Some(max_unavailable) = config.parameters.get("max_unavailable") {
                    if let Ok(m) = max_unavailable.parse() {
                        strategy = strategy.max_unavailable(m);
                    }
                }
                if let Some(max_surge) = config.parameters.get("max_surge") {
                    if let Ok(m) = max_surge.parse() {
                        strategy = strategy.max_surge(m);
                    }
                }
                if let Some(min_ready) = config.parameters.get("min_ready_seconds") {
                    if let Ok(m) = min_ready.parse() {
                        strategy = strategy.min_ready_seconds(m);
                    }
                }
                Ok(DeploymentStrategy::Rolling(strategy))
            }
            _ => Err(Error::InvalidStrategy {
                strategy: config.strategy.clone(),
            }),
        }
    }
    /// Execute deployment with selected strategy
    pub fn execute_deployment(
        &mut self,
        config: &DeploymentConfig,
    ) -> Result<DeploymentStatus, Error> {
        let strategy: _ = self.select_strategy(config)?;
        let result: _ = strategy.execute();
        if result.is_ok() {
            self.manager.add_strategy(strategy);
        }
        result
    }
}
impl Default for DeploymentStrategySelector {
    fn default() -> Self {
        Self::new()
    }
}
/// Error types
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Invalid deployment strategy: {strategy}")]
    InvalidStrategy { strategy: String },
    #[error("Deployment failed: {0}")]
    DeploymentFailed(String),
    #[error("Rollback failed: {0}")]
    RollbackFailed(String),
    #[error("Configuration error: {0}")]
    ConfigurationError(String),
    #[error("Deployment error: {0}")]
    DeploymentError(String),
}
#[cfg(test)]
mod tests {

    use super::*;
    #[test]
    fn test_blue_green_deployment() {
        let deployment: _ = BlueGreenDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
        );
        let result: _ = deployment.execute();
        assert!(result.is_ok());
        if let Ok(status) = result {
            assert!(status.success);
            assert_eq!(status.current_version, "v1.1.0");
        }
    }
    #[test]
    fn test_canary_deployment() {
        let deployment: _ = CanaryDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
            10,
        );
        let result: _ = deployment.execute();
        assert!(result.is_ok());
        if let Ok(status) = result {
            assert!(status.success);
            assert_eq!(status.traffic_split, Some(10));
        }
    }
    #[test]
    fn test_canary_promotion() {
        let deployment: _ = CanaryDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
            10,
        );
        let result: _ = deployment.promote_canary();
        assert!(result.is_ok());
        if let Ok(status) = result {
            assert!(status.success);
            assert_eq!(status.current_version, "v1.1.0");
            assert_eq!(status.traffic_split, Some(100));
        }
    }
    #[test]
    fn test_canary_rollback() {
        let deployment: _ = CanaryDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
            10,
        );
        let result: _ = deployment.rollback_canary();
        assert!(result.is_ok());
        if let Ok(status) = result {
            assert!(status.success);
            assert_eq!(status.current_version, "v1.0.0");
            assert_eq!(status.traffic_split, Some(0));
        }
    }
    #[test]
    fn test_rolling_deployment() {
        let deployment: _ = RollingDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
        );
        let result: _ = deployment.execute();
        assert!(result.is_ok());
        if let Ok(status) = result {
            assert!(status.success);
            assert_eq!(status.current_version, "v1.1.0");
        }
    }
    #[test]
    fn test_rolling_deployment_params() {
        let deployment: _ = RollingDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
        )
        .max_unavailable(2)
        .max_surge(2)
        .min_ready_seconds(30);
        assert_eq!(deployment.max_unavailable, 2);
        assert_eq!(deployment.max_surge, 2);
        assert_eq!(deployment.min_ready_seconds, 30);
    }
    #[test]
    fn test_strategy_selection() {
        let mut selector = DeploymentStrategySelector::new();
        let config: _ = DeploymentConfig {
            strategy: "blue-green".to_string(),
            service_name: "amberjs-service".to_string(),
            environment: "production".to_string(),
            current_version: "v1.0.0".to_string(),
            next_version: "v1.1.0".to_string(),
            parameters: std::collections::HashMap::new(),
        };
        let result: _ = selector.select_strategy(&config);
        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), DeploymentStrategy::BlueGreen(_)));
    }
    #[test]
    fn test_canary_strategy_with_params() {
        let mut selector = DeploymentStrategySelector::new();
        let mut params = std::collections::HashMap::new();
        params.insert("traffic_split".to_string(), "20".to_string());
        params.insert("promotion_threshold".to_string(), "99".to_string());
        let config: _ = DeploymentConfig {
            strategy: "canary".to_string(),
            service_name: "amberjs-service".to_string(),
            environment: "production".to_string(),
            current_version: "v1.0.0".to_string(),
            next_version: "v1.1.0".to_string(),
            parameters: params,
        };
        let result: _ = selector.select_strategy(&config);
        assert!(result.is_ok());
        if let Ok(DeploymentStrategy::Canary(canary)) = result {
            assert_eq!(canary.traffic_split, 20);
            assert_eq!(canary.promotion_threshold, 99);
        }
    }
    #[test]
    fn test_invalid_strategy() {
        let selector: _ = DeploymentStrategySelector::new();
        let config: _ = DeploymentConfig {
            strategy: "invalid".to_string(),
            service_name: "amberjs-service".to_string(),
            environment: "production".to_string(),
            current_version: "v1.0.0".to_string(),
            next_version: "v1.1.0".to_string(),
            parameters: std::collections::HashMap::new(),
        };
        let result: _ = selector.select_strategy(&config);
        assert!(result.is_err());
    }
    #[test]
    fn test_deployment_manager() {
        let mut manager = DeploymentManager::new();
        let strategy: _ = BlueGreenDeployment::new(
            "amberjs-service".to_string(),
            "production".to_string(),
            "v1.0.0".to_string(),
            "v1.1.0".to_string(),
        );
        manager.add_strategy(DeploymentStrategy::BlueGreen(strategy));
        assert_eq!(manager.get_strategies().len(), 1);
    }
}
use std::collections::{BTreeMap, HashMap};