solarboat 0.8.9

A CLI tool for intelligent Terraform operations management with automatic dependency detection
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
use std::sync::{Arc, Mutex, atomic::{AtomicUsize, Ordering}};
use std::thread;
use std::time::Duration;
use std::collections::{HashMap, VecDeque};

use crate::utils::terraform_operations::{TerraformOperation, OperationResult};
use crate::utils::error::{SolarboatError, SafeOperations};
use crate::utils::logger;

pub struct ParallelProcessor {
    module_groups: Arc<Mutex<HashMap<String, VecDeque<TerraformOperation>>>>,
    results: Arc<Mutex<Vec<OperationResult>>>,
    total_modules: usize,
    completed_modules: Arc<AtomicUsize>,
    worker_handle: Option<thread::JoinHandle<()>>,
    parallel_limit: usize,
}

impl ParallelProcessor {
    pub fn new(parallel_limit: usize) -> Self {
        Self {
            module_groups: Arc::new(Mutex::new(HashMap::new())),
            results: Arc::new(Mutex::new(Vec::new())),
            total_modules: 0,
            completed_modules: Arc::new(AtomicUsize::new(0)),
            worker_handle: None,
            parallel_limit: parallel_limit.clamp(1, 4),
        }
    }

    pub fn add_operation(&mut self, operation: TerraformOperation) -> Result<(), SolarboatError> {
        let module_path = operation.module_path.clone();
        let workspace = operation.workspace.as_deref().unwrap_or("default");
        
        logger::debug(&format!("Adding operation: module={}, workspace={}", module_path, workspace));
        
        let mut groups = SafeOperations::lock_with_timeout(
            &self.module_groups,
            Duration::from_secs(5),
            "module_groups_add"
        )?;
        
        groups.entry(module_path.clone())
            .or_insert_with(VecDeque::new)
            .push_back(operation);
        
        logger::debug(&format!("Operation added. Total groups: {}, operations in group: {}", 
            groups.len(), 
            groups.get(&module_path).map(|g| g.len()).unwrap_or(0)
        ));
        
        Ok(())
    }

    pub fn start(&mut self) -> Result<(), SolarboatError> {
        let groups = SafeOperations::lock_with_timeout(
            &self.module_groups,
            Duration::from_secs(5),
            "module_groups_count"
        )?;
        
        self.total_modules = groups.len();
        
        if self.total_modules == 0 {
            logger::info("No operations to process");
            return Ok(());
        }
        
        logger::info(&format!("Starting processing of {} modules with {} parallel workers", 
            self.total_modules, self.parallel_limit));
        
        let module_groups = Arc::clone(&self.module_groups);
        let results = Arc::clone(&self.results);
        let completed_modules = Arc::clone(&self.completed_modules);
        let total_modules = self.total_modules;
        let parallel_limit = self.parallel_limit;
        
        let handle = thread::spawn(move || {
            Self::process_modules(
                module_groups,
                results,
                completed_modules,
                total_modules,
                parallel_limit
            );
        });
        
        self.worker_handle = Some(handle);
        Ok(())
    }

    fn process_modules(
        module_groups: Arc<Mutex<HashMap<String, VecDeque<TerraformOperation>>>>,
        results: Arc<Mutex<Vec<OperationResult>>>,
        completed_modules: Arc<AtomicUsize>,
        total_modules: usize,
        parallel_limit: usize,
    ) {
        let active_modules = Arc::new(Mutex::new(HashMap::<String, bool>::new()));
        let start_time = std::time::Instant::now();
        let max_duration = Duration::from_secs(300);
        
        logger::debug(&format!("Worker thread started: processing {} modules with {} parallel limit", 
            total_modules, parallel_limit));
        
        loop {
            if start_time.elapsed() > max_duration {
                logger::warn("Worker thread timeout reached, stopping processing");
                break;
            }
            
            let completed = completed_modules.load(Ordering::Relaxed);
            if completed >= total_modules {
                logger::info(&format!("All {} modules completed successfully", total_modules));
                break;
            }
            
            let can_start_more = {
                let active = match active_modules.lock() {
                    Ok(active) => active,
                    Err(_) => break,
                };
                active.len() < parallel_limit
            };
            
            if can_start_more {
                let module_to_process = {
                    let groups = match SafeOperations::lock_with_timeout(
                        &module_groups,
                        Duration::from_secs(1),
                        "module_groups_process"
                    ) {
                        Ok(groups) => groups,
                        Err(e) => {
                            logger::warn(&format!("Failed to acquire module groups lock: {}", e));
                            break;
                        }
                    };
                    
                    let active = match active_modules.lock() {
                        Ok(active) => active,
                        Err(_) => break,
                    };
                    
                    groups.iter()
                        .find(|(module_path, operations)| {
                            !operations.is_empty() && !active.contains_key(*module_path)
                        })
                        .map(|(module_path, _)| module_path.clone())
                };
                
                if let Some(module_path) = module_to_process {
                    logger::debug(&format!("Starting module: {}", module_path));
                    
                    if let Ok(mut active) = active_modules.lock() {
                        active.insert(module_path.clone(), true);
                    }
                    
                    let module_groups = Arc::clone(&module_groups);
                    let results = Arc::clone(&results);
                    let completed_modules = Arc::clone(&completed_modules);
                    let active_modules_clone = Arc::clone(&active_modules);
                    
                    thread::spawn(move || {
                        Self::process_module_operations(
                            module_path.clone(),
                            module_groups,
                            results,
                            completed_modules,
                            active_modules_clone
                        );
                    });
                }
            }
            
            thread::sleep(Duration::from_millis(100));
        }
        
        logger::debug("Worker thread completed");
    }

    fn process_module_operations(
        module_path: String,
        module_groups: Arc<Mutex<HashMap<String, VecDeque<TerraformOperation>>>>,
        results: Arc<Mutex<Vec<OperationResult>>>,
        completed_modules: Arc<AtomicUsize>,
        active_modules: Arc<Mutex<HashMap<String, bool>>>,
    ) {
        let display_path = format_module_path(&module_path);
        logger::debug(&format!("Processing module: {}", display_path));
        
        let mut operation_count = 0;
        
        loop {
            let operation = {
                let mut groups = match SafeOperations::lock_with_timeout(
                    &module_groups,
                    Duration::from_secs(5),
                    "module_groups_take_next"
                ) {
                    Ok(groups) => groups,
                    Err(e) => {
                        logger::warn(&format!("Failed to acquire module groups lock: {}", e));
                        break;
                    }
                };
                
                if let Some(operations) = groups.get_mut(&module_path) {
                    let op = operations.pop_front();
                    logger::debug(&format!("Module {}: took operation, remaining in group: {}", 
                        display_path, operations.len()));
                    op
                } else {
                    logger::debug(&format!("Module {}: no group found", display_path));
                    None
                }
            };
            
            if let Some(op) = operation {
                operation_count += 1;
                logger::debug(&format!("Module {}: processing operation {} (workspace: {:?})", 
                    display_path, operation_count, op.workspace));
                
                let result = Self::process_single_operation(&op);
                
                {
                    let mut results = match SafeOperations::lock_with_timeout(
                        &results,
                        Duration::from_secs(5),
                        "results_push"
                    ) {
                        Ok(results) => results,
                        Err(e) => {
                            logger::warn(&format!("Failed to acquire results lock: {}", e));
                            break;
                        }
                    };
                    results.push(result);
                }
                
                if operation_count > 1 {
                    let workspace_name = op.workspace.as_deref().unwrap_or("default");
                    logger::debug(&format!("Module {}: waiting between workspace operations for '{}'", 
                        display_path, workspace_name));
                    
                    thread::sleep(Duration::from_secs(3));
                }
            } else {
                logger::debug(&format!("Module {}: no more operations, processed {} total", 
                    display_path, operation_count));
                break;
            }
        }
        
        completed_modules.fetch_add(1, Ordering::Relaxed);
        
        if let Ok(mut active) = active_modules.lock() {
            active.remove(&module_path);
            logger::debug(&format!("Module {} removed from active modules", module_path));
        }
        
        logger::debug(&format!("Module {} completed", display_path));
    }

    fn process_single_operation(operation: &TerraformOperation) -> OperationResult {
        let module_path = &operation.module_path;
        let workspace = &operation.workspace;
        let var_files = &operation.var_files;
        let operation_type = &operation.operation_type;
        let watch = operation.watch;
        let _skip_init = operation.skip_init;

        let init_success = if watch {
            let mut background_tf = crate::utils::terraform_background::BackgroundTerraform::new();
            match background_tf.init_background(module_path) {
                Ok(_) => {
                    match background_tf.wait_for_completion(300) {
                        Ok(success) => success,
                        Err(_) => false,
                    }
                }
                Err(_) => false,
            }
        } else {
            match crate::utils::terraform_operations::ensure_module_initialized(module_path) {
                Ok(_) => true,
                Err(_) => false,
            }
        };

        if !init_success {
            return OperationResult {
                module_path: module_path.clone(),
                workspace: workspace.clone(),
                operation_type: operation_type.clone(),
                success: false,
                error: Some("Initialization failed".to_string()),
                output: Vec::new(),
            };
        }

        if let Some(ref workspace_name) = workspace {
            if let Err(e) = crate::utils::terraform_operations::select_workspace(module_path, workspace_name) {
                return OperationResult {
                    module_path: module_path.clone(),
                    workspace: workspace.clone(),
                    operation_type: operation_type.clone(),
                    success: false,
                    error: Some(format!("Failed to select workspace {}: {}", workspace_name, e)),
                    output: Vec::new(),
                };
            }
        }

        let (success, error, output) = match operation_type {
            crate::utils::terraform_operations::OperationType::Init => {
                (true, None, Vec::new())
            }
            crate::utils::terraform_operations::OperationType::Plan { plan_dir } => {
                logger::operation_status("terraform plan", workspace.as_deref(), var_files.len());

                if watch {
                    let mut background_tf = crate::utils::terraform_background::BackgroundTerraform::new();
                    match background_tf.plan_background(module_path, Some(var_files)) {
                        Ok(_) => {
                            match background_tf.wait_for_completion(600) {
                                Ok(success) => {
                                    if success {
                                        logger::operation_completion(module_path, workspace.as_deref(), true);
                                        if let Some(plan_dir) = plan_dir {
                                            if let Ok(output) = background_tf.get_output() {
                                                if let Err(e) = crate::utils::terraform_operations::save_plan_output(
                                                    module_path, plan_dir, workspace.as_deref(), &output
                                                ) {
                                                    println!("  ⚠️  Failed to save plan output: {}", e);
                                                }
                                            }
                                        }
                                        let output = background_tf.get_output().unwrap_or_else(|_| Vec::new());
                                        (true, None, output)
                                    } else {
                                        logger::operation_completion(module_path, workspace.as_deref(), false);
                                        let output = background_tf.get_output().unwrap_or_else(|_| Vec::new());
                                        (false, Some("Plan failed".to_string()), output)
                                    }
                                }
                                Err(_) => {
                                    logger::operation_completion(module_path, workspace.as_deref(), false);
                                    (false, Some("Plan timeout".to_string()), Vec::new())
                                }
                            }
                        }
                        Err(_) => {
                            logger::operation_completion(module_path, workspace.as_deref(), false);
                            (false, Some("Failed to start plan".to_string()), Vec::new())
                        }
                    }
                } else {
                    match crate::utils::terraform_operations::run_single_plan(
                        module_path, 
                        plan_dir.as_deref(), 
                        workspace.as_deref(), 
                        Some(var_files)
                    ) {
                        Ok(success) => {
                            if success {
                                logger::operation_completion(module_path, workspace.as_deref(), true);
                                (true, None, Vec::new())
                            } else {
                                logger::operation_completion(module_path, workspace.as_deref(), false);
                                (false, Some("Plan failed".to_string()), Vec::new())
                            }
                        }
                        Err(e) => {
                            logger::operation_completion(module_path, workspace.as_deref(), false);
                            (false, Some(format!("Plan error: {}", e)), Vec::new())
                        }
                    }
                }
            }
            crate::utils::terraform_operations::OperationType::Apply => {
                logger::operation_status("terraform apply", workspace.as_deref(), var_files.len());

                if watch {
                    let mut background_tf = crate::utils::terraform_background::BackgroundTerraform::new();
                    match background_tf.apply_background(module_path, Some(var_files)) {
                        Ok(_) => {
                            match background_tf.wait_for_completion(1800) {
                                Ok(success) => {
                                    if success {
                                        logger::operation_completion(module_path, workspace.as_deref(), true);
                                        let output = background_tf.get_output().unwrap_or_else(|_| Vec::new());
                                        (true, None, output)
                                    } else {
                                        logger::operation_completion(module_path, workspace.as_deref(), false);
                                        let output = background_tf.get_output().unwrap_or_else(|_| Vec::new());
                                        (false, Some("Apply failed".to_string()), output)
                                    }
                                }
                                Err(_) => {
                                    logger::operation_completion(module_path, workspace.as_deref(), false);
                                    (false, Some("Apply timeout".to_string()), Vec::new())
                                }
                            }
                        }
                        Err(_) => {
                            logger::operation_completion(module_path, workspace.as_deref(), false);
                            (false, Some("Failed to start apply".to_string()), Vec::new())
                        }
                    }
                } else {
                    match crate::utils::terraform_operations::run_single_apply(module_path, Some(var_files)) {
                        Ok(success) => {
                            if success {
                                logger::operation_completion(module_path, workspace.as_deref(), true);
                                (true, None, Vec::new())
                            } else {
                                logger::operation_completion(module_path, workspace.as_deref(), false);
                                (false, Some("Apply failed".to_string()), Vec::new())
                            }
                        }
                        Err(e) => {
                            logger::operation_completion(module_path, workspace.as_deref(), false);
                            (false, Some(format!("Apply error: {}", e)), Vec::new())
                        }
                    }
                }
            }
        };

        OperationResult {
            module_path: module_path.clone(),
            workspace: workspace.clone(),
            operation_type: operation_type.clone(),
            success,
            error,
            output,
        }
    }

    pub fn wait_for_completion(mut self) -> Result<Vec<OperationResult>, SolarboatError> {
        if let Some(handle) = self.worker_handle.take() {
            let start_time = std::time::Instant::now();
            let max_wait_time = Duration::from_secs(300);
            
            logger::debug("Waiting for worker thread to complete...");
            
            while start_time.elapsed() < max_wait_time {
                if handle.is_finished() {
                    break;
                }
                thread::sleep(Duration::from_millis(100));
            }
            
            if !handle.is_finished() {
                logger::warn("Worker thread did not finish within timeout, proceeding with available results");
            } else {
                match handle.join() {
                    Ok(_) => logger::debug("Worker thread completed successfully"),
                    Err(e) => logger::error(&format!("Worker thread panicked: {:?}", e)),
                }
            }
        }
        
        let results = SafeOperations::lock_with_timeout(
            &self.results,
            Duration::from_secs(5),
            "results_clone"
        )?;
        
        Ok(results.clone())
    }

    pub fn get_parallel_limit(&self) -> usize {
        self.parallel_limit
    }
}

fn format_module_path(module_path: &str) -> String {
    if let Some(file_name) = std::path::Path::new(module_path).file_name() {
        if let Some(name) = file_name.to_str() {
            return format!("terraform/projects/{}", name);
        }
    }
    module_path.to_string()
}