halldyll_deploy_pods 0.1.0

Declarative, idempotent, and reconcilable deployment system for RunPod GPU pods
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
//! Halldyll CLI entrypoint.
//!
//! This is the main entrypoint for the halldyll command-line tool.

use std::io::Write;
use std::path::PathBuf;
use std::process::ExitCode;

use halldyll_deploy_pods::cli::{Cli, Commands, OutputFormatter, StateCommands};
use halldyll_deploy_pods::config::{
    find_config_file, ConfigHasher, ConfigParser, ConfigValidator, StateBackend,
};
use halldyll_deploy_pods::error::Result;
use halldyll_deploy_pods::planner::{DeploymentPlan, DiffEngine};
use halldyll_deploy_pods::reconciler::Reconciler;
use halldyll_deploy_pods::runpod::{HealthChecker, PodObserver, PodProvisioner, RunPodClient};
use halldyll_deploy_pods::state::{DeploymentState, LocalStateStore, S3StateStore, StateStore};

use clap::Parser;
use tracing::{debug, error, info};
use tracing_subscriber::EnvFilter;

/// Main entrypoint.
fn main() -> ExitCode {
    let cli = Cli::parse();

    // Initialize logging
    init_logging(cli.verbose);

    // Run async runtime
    let runtime = match tokio::runtime::Runtime::new() {
        Ok(rt) => rt,
        Err(e) => {
            eprintln!("Failed to create async runtime: {e}");
            return ExitCode::FAILURE;
        }
    };

    match runtime.block_on(run(cli)) {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("Error: {e}");
            ExitCode::FAILURE
        }
    }
}

/// Initializes the logging system.
fn init_logging(verbose: bool) {
    let filter = if verbose {
        EnvFilter::new("debug")
    } else {
        EnvFilter::new("info")
    };

    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_target(false)
        .init();
}

/// Main async entry point.
async fn run(cli: Cli) -> Result<()> {
    let formatter = OutputFormatter::new(cli.output);

    match cli.command {
        Commands::Init { path, force } => cmd_init(&path, force),
        Commands::Validate { warnings } => cmd_validate(cli.config.as_ref(), warnings, &formatter),
        Commands::Plan { detailed } => cmd_plan(cli.config.as_ref(), detailed, &formatter).await,
        Commands::Apply { yes, continue_on_error } => {
            cmd_apply(cli.config.as_ref(), yes, continue_on_error, &formatter).await
        }
        Commands::Status { detailed, health } => {
            cmd_status(cli.config.as_ref(), detailed, health, &formatter).await
        }
        Commands::Reconcile { yes, max_attempts } => {
            cmd_reconcile(cli.config.as_ref(), yes, max_attempts, &formatter).await
        }
        Commands::Destroy { yes, keep_volumes } => {
            cmd_destroy(cli.config.as_ref(), yes, keep_volumes, &formatter).await
        }
        Commands::Logs { pod, follow, tail } => cmd_logs(cli.config.as_ref(), pod, follow, tail),
        Commands::Drift => cmd_drift(cli.config.as_ref(), &formatter).await,
        Commands::State { command } => cmd_state(cli.config.as_ref(), command, &formatter).await,
    }
}

/// Initialize a new project.
fn cmd_init(path: &PathBuf, force: bool) -> Result<()> {
    info!("Initializing new Halldyll project in: {}", path.display());

    let config_path = path.join("halldyll.deploy.yaml");
    let env_path = path.join(".env.example");
    let gitignore_path = path.join(".gitignore");

    // Check if files exist
    if !force && config_path.exists() {
        eprintln!("Configuration file already exists: {}", config_path.display());
        eprintln!("Use --force to overwrite.");
        return Ok(());
    }

    // Create directory if needed
    if !path.exists() {
        std::fs::create_dir_all(path)?;
    }

    // Write config template
    let config_template = include_str!("../templates/halldyll.deploy.yaml");
    std::fs::write(&config_path, config_template)?;
    eprintln!("Created: {}", config_path.display());

    // Write .env.example
    let env_template = include_str!("../templates/.env.example");
    std::fs::write(&env_path, env_template)?;
    eprintln!("Created: {}", env_path.display());

    // Write/update .gitignore
    let gitignore_content = ".env\n.halldyll/\n";
    if gitignore_path.exists() {
        let existing = std::fs::read_to_string(&gitignore_path)?;
        if !existing.contains(".env") || !existing.contains(".halldyll") {
            let mut file = std::fs::OpenOptions::new()
                .append(true)
                .open(&gitignore_path)?;
            writeln!(file, "\n# Halldyll")?;
            if !existing.contains(".env") {
                writeln!(file, ".env")?;
            }
            if !existing.contains(".halldyll") {
                writeln!(file, ".halldyll/")?;
            }
            eprintln!("Updated: {}", gitignore_path.display());
        }
    } else {
        std::fs::write(&gitignore_path, gitignore_content)?;
        eprintln!("Created: {}", gitignore_path.display());
    }

    eprintln!("\nProject initialized successfully!");
    eprintln!("Next steps:");
    eprintln!("  1. Copy .env.example to .env and fill in your API keys");
    eprintln!("  2. Edit halldyll.deploy.yaml with your pod configuration");
    eprintln!("  3. Run 'halldyll validate' to check your configuration");
    eprintln!("  4. Run 'halldyll plan' to see what will be deployed");
    eprintln!("  5. Run 'halldyll apply' to deploy your pods");

    Ok(())
}

/// Validate configuration.
fn cmd_validate(
    config_path: Option<&PathBuf>,
    show_warnings: bool,
    formatter: &OutputFormatter,
) -> Result<()> {
    let config_file = resolve_config_path(config_path)?;
    info!("Validating configuration: {}", config_file.display());

    // Load .env
    let parser = ConfigParser::new().with_base_path(
        config_file
            .parent()
            .unwrap_or_else(|| std::path::Path::new(".")),
    );
    parser.load_dotenv()?;

    // Parse config
    let config = parser.load_file(&config_file)?;

    // Validate
    let validator = ConfigValidator::new();
    let result = validator.validate(&config)?;

    if result.is_valid() {
        eprintln!("Configuration is valid!");
        if show_warnings && !result.warnings.is_empty() {
            eprintln!("\nWarnings:");
            for warning in &result.warnings {
                eprintln!("  - {warning}");
            }
        }
    }

    // Show summary
    eprintln!("\nConfiguration summary:");
    eprintln!("  Project: {}", config.project.name);
    eprintln!("  Environment: {}", config.project.environment);
    eprintln!("  Pods: {}", config.pods.len());
    eprintln!("  Total GPUs: {}", config.total_gpus());

    let _ = formatter;
    Ok(())
}

/// Show deployment plan.
async fn cmd_plan(
    config_path: Option<&PathBuf>,
    detailed: bool,
    formatter: &OutputFormatter,
) -> Result<()> {
    let (config, state_store) = load_config_and_state(config_path).await?;
    let client = create_runpod_client()?;
    let observer = PodObserver::new(client);

    // Load state
    let state = state_store.load().await?;

    // Get observed pods
    let observed_pods = observer
        .list_project_pods(&config.project.name, &config.project.environment)
        .await?;

    // Compute diff
    let hasher = ConfigHasher::new();
    let config_hash = hasher.hash_config(&config);
    let diff_engine = DiffEngine::new();
    let diff = diff_engine.compute_diff(&config, state.as_ref(), &observed_pods);

    // Generate plan
    let plan = DeploymentPlan::from_diff(&diff, &config, &config_hash);

    // Output
    let output = formatter.format_plan(&plan);
    eprintln!("{output}");

    if detailed {
        eprintln!("\nDetailed changes:");
        for action in &plan.actions {
            eprintln!("  {} {} - {}", action.action_type, action.resource_name, action.reason);
        }
    }

    Ok(())
}

/// Apply deployment plan.
async fn cmd_apply(
    config_path: Option<&PathBuf>,
    auto_approve: bool,
    continue_on_error: bool,
    formatter: &OutputFormatter,
) -> Result<()> {
    let (config, state_store) = load_config_and_state(config_path).await?;
    let client = create_runpod_client()?;
    let observer = PodObserver::new(client.clone());
    let mut provisioner = PodProvisioner::new(client);

    // Initialize GPU types
    provisioner.init_gpu_types().await?;

    // Load state
    let mut state = state_store
        .load()
        .await?
        .unwrap_or_else(|| DeploymentState::new(&config.project.name, &config.project.environment));

    // Get observed pods
    let observed_pods = observer
        .list_project_pods(&config.project.name, &config.project.environment)
        .await?;

    // Compute diff and plan
    let hasher = ConfigHasher::new();
    let config_hash = hasher.hash_config(&config);
    let diff_engine = DiffEngine::new();
    let diff = diff_engine.compute_diff(&config, Some(&state), &observed_pods);
    let plan = DeploymentPlan::from_diff(&diff, &config, &config_hash);

    if plan.is_empty() {
        eprintln!("No changes to apply.");
        return Ok(());
    }

    // Show plan
    let output = formatter.format_plan(&plan);
    eprintln!("{output}");

    // Confirm
    if !auto_approve {
        eprint!("Do you want to apply this plan? [y/N]: ");
        std::io::stderr().flush()?;

        let mut input = String::new();
        std::io::stdin().read_line(&mut input)?;

        if !input.trim().eq_ignore_ascii_case("y") {
            eprintln!("Apply cancelled.");
            return Ok(());
        }
    }

    // Execute plan
    let executor = halldyll_deploy_pods::planner::PlanExecutor::new(&provisioner, &config.project)
        .with_continue_on_error(continue_on_error);

    let result = executor.execute(&plan, &mut state).await?;

    // Save state
    state_store.save(&state).await?;

    // Show result
    eprintln!("\n{result}");

    Ok(())
}

/// Show deployment status.
async fn cmd_status(
    config_path: Option<&PathBuf>,
    _detailed: bool,
    include_health: bool,
    formatter: &OutputFormatter,
) -> Result<()> {
    let (config, _state_store) = load_config_and_state(config_path).await?;
    let client = create_runpod_client()?;
    let observer = PodObserver::new(client);

    // Get project status
    let status = observer
        .get_project_status(&config.project.name, &config.project.environment)
        .await?;

    // Optionally check health
    let health = if include_health && !status.pods.is_empty() {
        let checker = HealthChecker::new()?;
        Some(checker.check_pods(&status.pods).await)
    } else {
        None
    };

    // Output
    let output = formatter.format_status(&status, health.as_deref());
    eprintln!("{output}");

    Ok(())
}

/// Reconcile deployment.
async fn cmd_reconcile(
    config_path: Option<&PathBuf>,
    auto_approve: bool,
    max_attempts: u32,
    formatter: &OutputFormatter,
) -> Result<()> {
    let (config, state_store) = load_config_and_state(config_path).await?;
    let client = create_runpod_client()?;
    let observer = PodObserver::new(client.clone());
    let mut provisioner = PodProvisioner::new(client);

    // Initialize GPU types
    provisioner.init_gpu_types().await?;

    // Confirm
    if !auto_approve {
        eprint!("This will reconcile your deployment to match the configuration. Continue? [y/N]: ");
        std::io::stderr().flush()?;

        let mut input = String::new();
        std::io::stdin().read_line(&mut input)?;

        if !input.trim().eq_ignore_ascii_case("y") {
            eprintln!("Reconciliation cancelled.");
            return Ok(());
        }
    }

    // Create reconciler
    let reconciler =
        Reconciler::new(&config, &state_store, &provisioner, &observer).with_max_attempts(max_attempts);

    // Run reconciliation
    let result = reconciler.reconcile().await?;

    // Output
    let output = formatter.format_reconciliation(&result);
    eprintln!("{output}");

    Ok(())
}

/// Destroy deployment.
async fn cmd_destroy(
    config_path: Option<&PathBuf>,
    auto_approve: bool,
    _keep_volumes: bool,
    _formatter: &OutputFormatter,
) -> Result<()> {
    let (config, state_store) = load_config_and_state(config_path).await?;
    let client = create_runpod_client()?;
    let observer = PodObserver::new(client.clone());
    let provisioner = PodProvisioner::new(client);

    // Get current pods
    let pods = observer
        .list_project_pods(&config.project.name, &config.project.environment)
        .await?;

    if pods.is_empty() {
        eprintln!("No pods to destroy.");
        return Ok(());
    }

    eprintln!("The following pods will be destroyed:");
    for pod in &pods {
        let name = pod.pod_name.as_deref().unwrap_or(&pod.name);
        eprintln!("  - {name} ({})", pod.id);
    }

    // Confirm
    if !auto_approve {
        eprint!("\nThis action is IRREVERSIBLE. Type 'destroy' to confirm: ");
        std::io::stderr().flush()?;

        let mut input = String::new();
        std::io::stdin().read_line(&mut input)?;

        if input.trim() != "destroy" {
            eprintln!("Destruction cancelled.");
            return Ok(());
        }
    }

    // Destroy pods
    for pod in &pods {
        let name = pod.pod_name.as_deref().unwrap_or(&pod.name);
        eprintln!("Destroying {name}...");
        if let Err(e) = provisioner.terminate_pod(&pod.id).await {
            error!("Failed to destroy {name}: {e}");
        }
    }

    // Clear state
    state_store.delete().await?;

    eprintln!("\nAll pods destroyed.");
    Ok(())
}

/// Show logs (placeholder).
///
/// # Errors
///
/// Returns an error if the pod is not found (once implemented).
fn cmd_logs(
    _config_path: Option<&PathBuf>,
    _pod: Option<String>,
    _follow: bool,
    _tail: u32,
) -> Result<()> {
    Err(halldyll_deploy_pods::error::HalldyllError::internal(
        "Log viewing is not yet implemented. View logs directly in the RunPod dashboard.",
    ))
}

/// Check for drift.
async fn cmd_drift(config_path: Option<&PathBuf>, formatter: &OutputFormatter) -> Result<()> {
    let (config, state_store) = load_config_and_state(config_path).await?;
    let client = create_runpod_client()?;
    let observer = PodObserver::new(client.clone());
    let provisioner = PodProvisioner::new(client);

    let reconciler = Reconciler::new(&config, &state_store, &provisioner, &observer);
    let report = reconciler.check_drift().await?;

    let output = formatter.format_drift(&report);
    eprintln!("{output}");

    Ok(())
}

/// State management commands.
async fn cmd_state(
    config_path: Option<&PathBuf>,
    command: StateCommands,
    formatter: &OutputFormatter,
) -> Result<()> {
    let (_config, state_store) = load_config_and_state(config_path).await?;

    match command {
        StateCommands::Show => {
            if let Some(state) = state_store.load().await? {
                let output = formatter.format_state(&state);
                eprintln!("{output}");
            } else {
                eprintln!("No state found.");
            }
        }
        StateCommands::Lock { holder } => {
            let holder_str = holder.as_deref().unwrap_or("");
            let lock = state_store.acquire_lock(holder_str).await?;
            eprintln!("State locked: {}", lock.lock_id);
        }
        StateCommands::Unlock { lock_id, force } => {
            if force {
                // Force unlock by deleting lock file
                if let Some(lock_info) = state_store.get_lock_info().await? {
                    state_store.release_lock(&lock_info.lock_id).await?;
                    eprintln!("State forcefully unlocked.");
                }
            } else if let Some(id) = lock_id {
                state_store.release_lock(&id).await?;
                eprintln!("State unlocked.");
            } else {
                eprintln!("Please provide --lock-id or use --force");
            }
        }
        StateCommands::Pull => {
            eprintln!("State pull is only applicable for remote backends.");
        }
        StateCommands::Push { force: _ } => {
            eprintln!("State push is only applicable for remote backends.");
        }
    }

    Ok(())
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Resolves the configuration file path.
fn resolve_config_path(config_path: Option<&PathBuf>) -> Result<PathBuf> {
    config_path.map_or_else(|| find_config_file("."), |path| Ok(path.clone()))
}

/// Loads configuration and creates appropriate state store.
async fn load_config_and_state(
    config_path: Option<&PathBuf>,
) -> Result<(halldyll_deploy_pods::config::DeployConfig, Box<dyn StateStore>)> {
    let config_file = resolve_config_path(config_path)?;
    debug!("Loading configuration from: {}", config_file.display());

    let parser = ConfigParser::new().with_base_path(
        config_file
            .parent()
            .unwrap_or_else(|| std::path::Path::new(".")),
    );
    parser.load_dotenv()?;

    let config = parser.load_with_env(&config_file)?;

    // Validate
    let validator = ConfigValidator::new();
    validator.validate(&config)?;

    // Create state store based on config
    let state_store: Box<dyn StateStore> = match config.state.backend {
        StateBackend::Local => {
            let path = config.state.path.as_ref().map_or_else(
                || {
                    config_file
                        .parent()
                        .unwrap_or_else(|| std::path::Path::new("."))
                        .join(".halldyll")
                },
                PathBuf::from,
            );
            Box::new(LocalStateStore::with_base_dir(path))
        }
        StateBackend::S3 => {
            let bucket = config
                .state
                .bucket
                .as_deref()
                .ok_or_else(|| halldyll_deploy_pods::error::HalldyllError::internal("S3 bucket not configured"))?;
            let prefix = config.state.prefix.as_deref();
            let region = config.state.region.as_deref();
            Box::new(S3StateStore::new(bucket, prefix, region).await?)
        }
    };

    Ok((config, state_store))
}

/// Creates a `RunPod` API client.
fn create_runpod_client() -> Result<RunPodClient> {
    let api_key = ConfigParser::get_runpod_api_key()?;
    RunPodClient::new(&api_key)
}