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
//! Error recovery and resilience for remote deployments
//!
//! Provides retry logic, connection recovery, and rollback capabilities
//! for SSH and deployment failures.

use crate::core::error::{Error, Result};
use crate::deployment::ssh::SshDeploymentClient;
use blueprint_core::{debug, error, info, warn};
use blueprint_std::time::Duration;
use tokio::time::{sleep, timeout};

/// Recovery strategy for deployment failures
#[derive(Debug, Clone)]
pub enum RecoveryStrategy {
    /// Retry with exponential backoff
    Retry {
        max_attempts: u32,
        initial_delay: Duration,
        max_delay: Duration,
        exponential_base: f64,
    },
    /// Attempt rollback to previous state
    Rollback { checkpoint: DeploymentCheckpoint },
    /// Fail immediately without recovery
    FailFast,
    /// Try alternative deployment method
    Fallback { alternative: Box<RecoveryStrategy> },
}

impl Default for RecoveryStrategy {
    fn default() -> Self {
        Self::Retry {
            max_attempts: 3,
            initial_delay: Duration::from_secs(2),
            max_delay: Duration::from_secs(30),
            exponential_base: 2.0,
        }
    }
}

/// Checkpoint for rollback operations
#[derive(Debug, Clone)]
pub struct DeploymentCheckpoint {
    pub instance_id: String,
    pub container_id: Option<String>,
    pub timestamp: std::time::SystemTime,
    pub state: CheckpointState,
}

#[derive(Debug, Clone)]
pub enum CheckpointState {
    PreDeployment,
    ContainerCreated,
    ContainerStarted,
    HealthCheckPassed,
    Completed,
}

/// Error recovery coordinator
pub struct ErrorRecovery {
    strategy: RecoveryStrategy,
    checkpoints: Vec<DeploymentCheckpoint>,
}

impl ErrorRecovery {
    pub fn new(strategy: RecoveryStrategy) -> Self {
        Self {
            strategy,
            checkpoints: Vec::new(),
        }
    }

    /// Save a deployment checkpoint
    pub fn checkpoint(&mut self, checkpoint: DeploymentCheckpoint) {
        info!("Saving deployment checkpoint: {:?}", checkpoint.state);
        self.checkpoints.push(checkpoint);
    }

    /// Execute an operation with recovery
    pub async fn execute_with_recovery<F, T>(&self, operation: F) -> Result<T>
    where
        F: Fn() -> futures::future::BoxFuture<'static, Result<T>> + Send + Sync,
        T: Send,
    {
        match &self.strategy {
            RecoveryStrategy::Retry {
                max_attempts,
                initial_delay,
                max_delay,
                exponential_base,
            } => {
                self.retry_with_backoff(
                    operation,
                    *max_attempts,
                    *initial_delay,
                    *max_delay,
                    *exponential_base,
                )
                .await
            }
            RecoveryStrategy::FailFast => operation().await,
            RecoveryStrategy::Rollback { checkpoint } => match operation().await {
                Ok(result) => Ok(result),
                Err(e) => {
                    warn!("Operation failed, attempting rollback: {}", e);
                    self.rollback_to_checkpoint(checkpoint).await?;
                    Err(e)
                }
            },
            RecoveryStrategy::Fallback { alternative } => match operation().await {
                Ok(result) => Ok(result),
                Err(_) => {
                    warn!("Primary strategy failed, trying fallback");
                    let fallback_recovery = Self::new((**alternative).clone());
                    fallback_recovery.execute_with_recovery(operation).await
                }
            },
        }
    }

    async fn retry_with_backoff<F, T>(
        &self,
        operation: F,
        max_attempts: u32,
        initial_delay: Duration,
        max_delay: Duration,
        exponential_base: f64,
    ) -> Result<T>
    where
        F: Fn() -> futures::future::BoxFuture<'static, Result<T>>,
        T: Send,
    {
        let mut attempt = 0;
        let mut delay = initial_delay;

        loop {
            attempt += 1;
            debug!("Attempt {} of {}", attempt, max_attempts);

            match operation().await {
                Ok(result) => {
                    if attempt > 1 {
                        info!("Operation succeeded after {} attempts", attempt);
                    }
                    return Ok(result);
                }
                Err(e) if attempt >= max_attempts => {
                    error!("Operation failed after {} attempts: {}", max_attempts, e);
                    return Err(e);
                }
                Err(e) => {
                    warn!("Attempt {} failed: {}, retrying in {:?}", attempt, e, delay);
                    sleep(delay).await;

                    // Exponential backoff
                    delay = Duration::from_secs_f64(
                        (delay.as_secs_f64() * exponential_base).min(max_delay.as_secs_f64()),
                    );
                }
            }
        }
    }

    async fn rollback_to_checkpoint(&self, checkpoint: &DeploymentCheckpoint) -> Result<()> {
        info!("Rolling back to checkpoint: {:?}", checkpoint.state);

        // Implementation would depend on the checkpoint state
        match &checkpoint.state {
            CheckpointState::ContainerCreated | CheckpointState::ContainerStarted => {
                if let Some(container_id) = &checkpoint.container_id {
                    warn!("Would remove container: {}", container_id);
                    // In real implementation: ssh_client.remove_container(container_id).await?;
                }
            }
            _ => {
                debug!(
                    "No rollback action needed for state: {:?}",
                    checkpoint.state
                );
            }
        }

        Ok(())
    }
}

/// SSH connection recovery
pub struct SshConnectionRecovery {
    max_reconnect_attempts: u32,
    connection_timeout: Duration,
    #[allow(dead_code)]
    keepalive_interval: Duration,
}

impl Default for SshConnectionRecovery {
    fn default() -> Self {
        Self {
            max_reconnect_attempts: 5,
            connection_timeout: Duration::from_secs(30),
            keepalive_interval: Duration::from_secs(60),
        }
    }
}

impl SshConnectionRecovery {
    /// Verify SSH connection is alive
    pub async fn verify_connection(&self, host: &str, port: u16) -> Result<bool> {
        use tokio::net::TcpStream;

        match timeout(
            self.connection_timeout,
            TcpStream::connect(format!("{host}:{port}")),
        )
        .await
        {
            Ok(Ok(_)) => Ok(true),
            Ok(Err(e)) => {
                warn!("SSH connection check failed: {}", e);
                Ok(false)
            }
            Err(_) => {
                warn!("SSH connection check timed out");
                Ok(false)
            }
        }
    }

    /// Reconnect with retry logic
    pub async fn reconnect(&self, client: &mut SshDeploymentClient) -> Result<()> {
        let mut attempts = 0;

        while attempts < self.max_reconnect_attempts {
            attempts += 1;
            info!("SSH reconnection attempt {}", attempts);

            if client.reconnect().await.is_ok() {
                info!("SSH reconnection successful");
                return Ok(());
            }

            if attempts < self.max_reconnect_attempts {
                let delay = Duration::from_secs(attempts as u64 * 2);
                sleep(delay).await;
            }
        }

        Err(Error::Other(format!(
            "Failed to reconnect after {} attempts",
            self.max_reconnect_attempts
        )))
    }

    /// Execute command with automatic reconnection
    pub async fn execute_with_reconnect<F, T>(
        &self,
        client: &mut SshDeploymentClient,
        operation: F,
    ) -> Result<T>
    where
        F: Fn(&SshDeploymentClient) -> futures::future::BoxFuture<'_, Result<T>>,
    {
        // First attempt
        match operation(client).await {
            Ok(result) => Ok(result),
            Err(e) => {
                warn!("Operation failed, attempting reconnection: {}", e);

                // Try to reconnect
                self.reconnect(client).await?;

                // Retry operation once after reconnection
                operation(client).await
            }
        }
    }
}

/// Transaction-like deployment operations
pub struct DeploymentTransaction {
    operations: Vec<DeploymentOperation>,
    completed: Vec<usize>,
    recovery: ErrorRecovery,
}

#[derive(Clone)]
pub enum DeploymentOperation {
    CreateContainer { image: String, name: String },
    StartContainer { container_id: String },
    StopContainer { container_id: String },
    RemoveContainer { container_id: String },
    ExecuteCommand { command: String, critical: bool },
}

impl DeploymentTransaction {
    pub fn new(recovery_strategy: RecoveryStrategy) -> Self {
        Self {
            operations: Vec::new(),
            completed: Vec::new(),
            recovery: ErrorRecovery::new(recovery_strategy),
        }
    }

    /// Add an operation to the transaction
    pub fn add_operation(&mut self, operation: DeploymentOperation) {
        self.operations.push(operation);
    }

    /// Execute all operations with automatic rollback on failure
    pub async fn execute(&mut self, client: &SshDeploymentClient) -> Result<()> {
        for (index, operation) in self.operations.iter().enumerate() {
            match self.execute_operation(client, operation).await {
                Ok(()) => {
                    self.completed.push(index);
                    self.recovery.checkpoint(DeploymentCheckpoint {
                        instance_id: format!("ssh-deployment-{}", uuid::Uuid::new_v4()),
                        container_id: None, // Would be set based on operation
                        timestamp: std::time::SystemTime::now(),
                        state: self.operation_to_checkpoint_state(operation),
                    });
                }
                Err(e) => {
                    error!("Operation {} failed: {}, rolling back", index, e);
                    self.rollback(client).await?;
                    return Err(e);
                }
            }
        }

        Ok(())
    }

    async fn execute_operation(
        &self,
        _client: &SshDeploymentClient,
        operation: &DeploymentOperation,
    ) -> Result<()> {
        match operation {
            DeploymentOperation::CreateContainer { image, name } => {
                info!("Creating container {} from image {}", name, image);
                // client.create_container(image, name).await
                Ok(())
            }
            DeploymentOperation::StartContainer { container_id } => {
                info!("Starting container {}", container_id);
                // client.start_container(container_id).await
                Ok(())
            }
            DeploymentOperation::ExecuteCommand {
                command,
                critical: _,
            } => {
                info!("Executing command: {}", command);
                // let result = client.execute_command(command).await;
                // if *critical { result } else { Ok(()) }
                Ok(())
            }
            _ => Ok(()),
        }
    }

    async fn rollback(&mut self, client: &SshDeploymentClient) -> Result<()> {
        warn!("Rolling back {} completed operations", self.completed.len());

        // Rollback in reverse order
        for &index in self.completed.iter().rev() {
            let operation = &self.operations[index];
            self.rollback_operation(client, operation).await?;
        }

        Ok(())
    }

    async fn rollback_operation(
        &self,
        _client: &SshDeploymentClient,
        operation: &DeploymentOperation,
    ) -> Result<()> {
        match operation {
            DeploymentOperation::CreateContainer { name, .. } => {
                info!("Rolling back: removing container {}", name);
                // client.remove_container(name).await
            }
            DeploymentOperation::StartContainer { container_id } => {
                info!("Rolling back: stopping container {}", container_id);
                // client.stop_container(container_id).await
            }
            _ => {
                // Some operations don't need rollback
            }
        }
        Ok(())
    }

    fn operation_to_checkpoint_state(&self, operation: &DeploymentOperation) -> CheckpointState {
        match operation {
            DeploymentOperation::CreateContainer { .. } => CheckpointState::ContainerCreated,
            DeploymentOperation::StartContainer { .. } => CheckpointState::ContainerStarted,
            _ => CheckpointState::PreDeployment,
        }
    }
}

/// Circuit breaker for preventing cascading failures
pub struct CircuitBreaker {
    failure_threshold: u32,
    success_threshold: u32,
    timeout: Duration,
    state: CircuitState,
    failure_count: u32,
    success_count: u32,
    last_failure_time: Option<std::time::Instant>,
}

#[derive(Debug, PartialEq)]
enum CircuitState {
    Closed,
    Open,
    HalfOpen,
}

impl CircuitBreaker {
    pub fn new(failure_threshold: u32, success_threshold: u32, timeout: Duration) -> Self {
        Self {
            failure_threshold,
            success_threshold,
            timeout,
            state: CircuitState::Closed,
            failure_count: 0,
            success_count: 0,
            last_failure_time: None,
        }
    }

    pub async fn call<F, T>(&mut self, operation: F) -> Result<T>
    where
        F: futures::future::Future<Output = Result<T>>,
    {
        // Check if circuit should transition from Open to HalfOpen
        if self.state == CircuitState::Open {
            if let Some(last_failure) = self.last_failure_time {
                if last_failure.elapsed() >= self.timeout {
                    info!("Circuit breaker transitioning to half-open");
                    self.state = CircuitState::HalfOpen;
                }
            }
        }

        match self.state {
            CircuitState::Open => Err(Error::Other("Circuit breaker is open".into())),
            CircuitState::Closed | CircuitState::HalfOpen => match operation.await {
                Ok(result) => {
                    self.on_success();
                    Ok(result)
                }
                Err(e) => {
                    self.on_failure();
                    Err(e)
                }
            },
        }
    }

    fn on_success(&mut self) {
        self.failure_count = 0;

        if self.state == CircuitState::HalfOpen {
            self.success_count += 1;
            if self.success_count >= self.success_threshold {
                info!("Circuit breaker closing after successful operations");
                self.state = CircuitState::Closed;
                self.success_count = 0;
            }
        }
    }

    fn on_failure(&mut self) {
        self.failure_count += 1;
        self.last_failure_time = Some(std::time::Instant::now());

        if self.state == CircuitState::HalfOpen {
            warn!("Circuit breaker reopening after failure in half-open state");
            self.state = CircuitState::Open;
            self.success_count = 0;
        } else if self.failure_count >= self.failure_threshold {
            error!(
                "Circuit breaker opening after {} failures",
                self.failure_count
            );
            self.state = CircuitState::Open;
        }
    }
}