torc 0.23.0

Workflow management system
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
//! HPC system profile commands
//!
//! Commands for listing, detecting, and querying HPC system profiles.

use clap::Subcommand;
use std::collections::HashMap;
use tabled::Tabled;

use super::output::print_json;

use crate::client::hpc::{HpcPartition, HpcProfile, HpcProfileRegistry};
use crate::config::{ClientHpcConfig, TorcConfig};

use super::table_format::display_table_with_count;

/// Create an HPC profile registry with built-in profiles and user-defined profiles from config
///
/// This is a public version for use by other modules (e.g., main.rs for submit command)
pub fn create_registry_with_config_public(hpc_config: &ClientHpcConfig) -> HpcProfileRegistry {
    create_registry_with_config(hpc_config)
}

/// Resolve an HPC profile by name or auto-detection, with dynamic Slurm fallback.
///
/// Resolution order:
/// 1. If `name` is provided → look up in registry (supports "slurm" for dynamic detection)
/// 2. Else → try auto-detection via registry (built-in profiles, then dynamic Slurm fallback)
/// 3. If nothing found → return Err with helpful message
///
/// When the profile is resolved via dynamic Slurm detection (not a pre-configured profile),
/// an informational message is printed to stderr.
pub fn resolve_hpc_profile(
    registry: &HpcProfileRegistry,
    name: Option<&str>,
) -> Result<HpcProfile, String> {
    if let Some(name) = name {
        if let Some(profile) = registry.get(name) {
            return Ok(profile);
        }
        return Err(format!("Unknown HPC profile: {}", name));
    }

    // Try built-in/custom profile detection first (env vars, hostname patterns)
    if let Some(profile) = registry.profiles().iter().find(|p| p.detect()) {
        return Ok(profile.clone());
    }

    // Fall back to dynamic Slurm detection via registry.detect()
    if let Some(profile) = registry.detect() {
        eprintln!(
            "No pre-configured HPC profile found. Using dynamically detected Slurm cluster: {}",
            profile.display_name
        );
        return Ok(profile);
    }

    Err("No HPC profile specified and no system detected.\n\
         Use --hpc-profile <name> to specify a profile, or run on a Slurm cluster."
        .to_string())
}

/// Create an HPC profile registry with built-in profiles and user-defined profiles from config
fn create_registry_with_config(hpc_config: &ClientHpcConfig) -> HpcProfileRegistry {
    let mut registry = HpcProfileRegistry::with_builtin_profiles();

    // Apply profile overrides to built-in profiles
    // Note: This modifies the default_account for profiles
    // In a real implementation, we'd need mutable access to profiles

    // Add custom profiles from config
    for (name, profile_config) in &hpc_config.custom_profiles {
        let profile = config_to_profile(name, profile_config);
        registry.register(profile);
    }

    registry
}

/// Convert a config profile to an HpcProfile
fn config_to_profile(name: &str, config: &crate::config::HpcProfileConfig) -> HpcProfile {
    let mut detection = Vec::new();

    // Parse detect_env_var (format: "NAME=value")
    if let Some(env_var) = &config.detect_env_var
        && let Some((var_name, var_value)) = env_var.split_once('=')
    {
        detection.push(crate::client::hpc::HpcDetection::EnvVar {
            name: var_name.to_string(),
            value: var_value.to_string(),
        });
    }

    // Parse hostname pattern
    if let Some(pattern) = &config.detect_hostname {
        detection.push(crate::client::hpc::HpcDetection::HostnamePattern {
            pattern: pattern.clone(),
        });
    }

    // Convert partitions
    let partitions: Vec<HpcPartition> = config.partitions.iter().map(config_to_partition).collect();

    HpcProfile {
        name: name.to_string(),
        display_name: config.display_name.clone(),
        description: config.description.clone(),
        detection,
        default_account: config.default_account.clone(),
        partitions,
        charge_factor_cpu: config.charge_factor_cpu,
        charge_factor_gpu: config.charge_factor_gpu,
        metadata: HashMap::new(),
    }
}

/// Convert a config partition to an HpcPartition
fn config_to_partition(config: &crate::config::HpcPartitionConfig) -> HpcPartition {
    HpcPartition {
        name: config.name.clone(),
        description: config.description.clone(),
        cpus_per_node: config.cpus_per_node,
        memory_mb: config.memory_mb,
        max_walltime_secs: config.max_walltime_secs,
        max_nodes: None,
        max_nodes_per_user: None,
        min_nodes: None,
        gpus_per_node: config.gpus_per_node,
        gpu_type: config.gpu_type.clone(),
        gpu_memory_gb: config.gpu_memory_gb,
        local_disk_gb: None,
        shared: config.shared,
        requires_explicit_request: config.requires_explicit_request,
        default_qos: None,
        features: vec![],
    }
}

/// HPC system profile commands
#[derive(Debug, Subcommand)]
pub enum HpcCommands {
    /// List available HPC profiles
    List,
    /// Detect the current HPC system
    Detect,
    /// Show details of an HPC profile
    Show {
        /// Profile name
        name: String,
    },
    /// List partitions for an HPC profile
    Partitions {
        /// Profile name (e.g., "kestrel"). If not specified, tries to detect current system.
        name: Option<String>,
        /// Filter for GPU partitions
        #[arg(long)]
        gpu: bool,
        /// Filter for shared partitions
        #[arg(long)]
        shared: bool,
    },
    /// Find the best partition for given requirements
    Match {
        /// Profile name (e.g., "kestrel"). If not specified, tries to detect current system.
        name: Option<String>,
        /// Required CPUs
        #[arg(long)]
        cpus: u32,
        /// Required memory (e.g., "16g", "512m")
        #[arg(long)]
        memory: String,
        /// Required wall time (e.g., "4:00:00", "30:00")
        #[arg(long)]
        walltime: String,
        /// Required GPUs
        #[arg(long)]
        gpus: Option<u32>,
    },
    /// Generate an HPC profile from the current Slurm cluster
    Generate {
        /// Profile name (e.g., "kestrel")
        #[arg(short, long)]
        name: Option<String>,
        /// Human-readable display name
        #[arg(short, long)]
        display_name: Option<String>,
        /// Output file path (prints to stdout if not specified)
        #[arg(short, long)]
        output: Option<std::path::PathBuf>,
        /// Skip standby partitions (ones ending in -stdby)
        #[arg(long)]
        skip_stdby: bool,
    },
}

use serde::Serialize;

#[derive(Tabled, Serialize)]
struct ProfileRow {
    name: String,
    display: String,
    partitions: usize,
    detected: bool,
}

#[derive(Tabled, Serialize)]
struct PartitionRow {
    name: String,
    cpus: u32,
    #[serde(skip_serializing)]
    memory: String,
    #[serde(skip_serializing)]
    walltime: String,
    #[serde(skip_serializing)]
    gpus: String,

    #[tabled(skip)]
    memory_mb: u64,
    #[tabled(skip)]
    max_walltime_secs: u64,
    #[tabled(skip)]
    gpus_per_node: Option<u32>,
    #[tabled(skip)]
    gpu_type: Option<String>,

    shared: bool,
    explicit: bool,
}

#[derive(Serialize)]
struct MatchResult {
    requirements: MatchRequirements,
    matching_partitions: Vec<HpcPartition>,
    best_partition: Option<HpcPartition>,
}

#[derive(Serialize)]
struct MatchRequirements {
    cpus: u32,
    memory_mb: u64,
    walltime_secs: u64,
    gpus: Option<u32>,
}

pub fn handle_hpc_commands(command: &HpcCommands, format: &str) {
    let config = TorcConfig::load().unwrap_or_default();
    let registry = create_registry_with_config(&config.client.hpc);

    match command {
        HpcCommands::List => {
            let mut rows = Vec::new();
            for profile in registry.profiles() {
                rows.push(ProfileRow {
                    name: profile.name.clone(),
                    display: profile.display_name.clone(),
                    partitions: profile.partitions.len(),
                    detected: profile.detect(),
                });
            }

            if format == "json" {
                print_json(&rows, "hpc profiles");
            } else {
                display_table_with_count(&rows, "HPC profiles");
            }
        }

        HpcCommands::Detect => {
            if let Some(profile) = registry.detect() {
                if format == "json" {
                    print_json(&profile, "detected hpc profile");
                } else {
                    println!(
                        "Detected HPC system: {} ({})",
                        profile.display_name, profile.name
                    );
                }
            } else if format == "json" {
                print_json(&Option::<HpcProfile>::None, "detected hpc profile");
            } else {
                println!("No known HPC system detected.");
            }
        }

        HpcCommands::Show { name } => {
            if let Some(profile) = registry.get(name) {
                if format == "json" {
                    print_json(&profile, "hpc profile");
                } else {
                    println!("HPC Profile: {}", profile.display_name);
                    println!("Identifier:  {}", profile.name);
                    if !profile.description.is_empty() {
                        println!("Description: {}", profile.description);
                    }
                    println!("Partitions:  {}", profile.partitions.len());

                    if !profile.metadata.is_empty() {
                        println!("\nMetadata:");
                        for (k, v) in &profile.metadata {
                            println!("  {}: {}", k, v);
                        }
                    }
                }
            } else {
                eprintln!("Unknown HPC profile: {}", name);
                std::process::exit(1);
            }
        }

        HpcCommands::Partitions { name, gpu, shared } => {
            let profile = if let Some(n) = name {
                registry.get(n)
            } else {
                registry.detect()
            };

            if let Some(profile) = profile {
                let mut rows = Vec::new();
                for p in &profile.partitions {
                    if *gpu && p.gpus_per_node.is_none() {
                        continue;
                    }
                    if *shared && !p.shared {
                        continue;
                    }

                    rows.push(PartitionRow {
                        name: p.name.clone(),
                        cpus: p.cpus_per_node,
                        memory: format!("{:.1} GB", p.memory_gb()),
                        walltime: p.max_walltime_str(),
                        gpus: p
                            .gpus_per_node
                            .map(|n| {
                                format!("{}x {}", n, p.gpu_type.as_deref().unwrap_or("unknown"))
                            })
                            .unwrap_or_else(|| "-".to_string()),
                        memory_mb: p.memory_mb,
                        max_walltime_secs: p.max_walltime_secs,
                        gpus_per_node: p.gpus_per_node,
                        gpu_type: p.gpu_type.clone(),
                        shared: p.shared,
                        explicit: p.requires_explicit_request,
                    });
                }

                if format == "json" {
                    print_json(&rows, &format!("partitions for {}", profile.name));
                } else {
                    display_table_with_count(&rows, &format!("partitions for {}", profile.name));
                }
            } else {
                eprintln!("HPC profile not found or detected.");
                std::process::exit(1);
            }
        }

        HpcCommands::Match {
            name,
            cpus,
            memory,
            walltime,
            gpus,
        } => {
            let profile = if let Some(n) = name {
                registry.get(n)
            } else {
                registry.detect()
            };

            if let Some(profile) = profile {
                let mem_mb = match crate::client::commands::slurm::parse_memory_mb(memory) {
                    Ok(m) => m,
                    Err(e) => {
                        eprintln!("Error parsing memory: {}", e);
                        std::process::exit(1);
                    }
                };
                let walltime_secs =
                    match crate::client::commands::slurm::parse_walltime_secs(walltime) {
                        Ok(w) => w,
                        Err(e) => {
                            eprintln!("Error parsing walltime: {}", e);
                            std::process::exit(1);
                        }
                    };

                let matching =
                    profile.find_matching_partitions(*cpus, mem_mb, walltime_secs, *gpus);
                let best = profile.find_best_partition(*cpus, mem_mb, walltime_secs, *gpus);

                if format == "json" {
                    let result = MatchResult {
                        requirements: MatchRequirements {
                            cpus: *cpus,
                            memory_mb: mem_mb,
                            walltime_secs,
                            gpus: *gpus,
                        },
                        matching_partitions: matching.iter().map(|&p| p.clone()).collect(),
                        best_partition: best.cloned(),
                    };
                    print_json(&result, "match result");
                } else {
                    let rows: Vec<_> = matching
                        .iter()
                        .map(|p| {
                            let mut name_display = p.name.clone();
                            if Some(*p) == best {
                                name_display = format!("{} (BEST)", name_display);
                            }

                            PartitionRow {
                                name: name_display,
                                cpus: p.cpus_per_node,
                                memory: format!("{:.1} GB", p.memory_gb()),
                                walltime: p.max_walltime_str(),
                                gpus: p
                                    .gpus_per_node
                                    .map(|n| format!("{}x", n))
                                    .unwrap_or_else(|| "-".to_string()),
                                memory_mb: p.memory_mb,
                                max_walltime_secs: p.max_walltime_secs,
                                gpus_per_node: p.gpus_per_node,
                                gpu_type: p.gpu_type.clone(),
                                shared: p.shared,
                                explicit: p.requires_explicit_request,
                            }
                        })
                        .collect();

                    display_table_with_count(&rows, "matching partitions");

                    if let Some(best) = best {
                        println!();
                        println!("Recommended: {} partition", best.name);
                        if best.requires_explicit_request {
                            println!("  Use: --partition={}", best.name);
                        } else {
                            println!("  (Auto-routed based on requirements)");
                        }
                    }
                }
            } else {
                eprintln!("HPC profile not found or detected.");
                std::process::exit(1);
            }
        }

        HpcCommands::Generate {
            name,
            display_name,
            output,
            skip_stdby,
        } => match generate_profile_from_slurm(name.clone(), display_name.clone(), *skip_stdby) {
            Ok(toml_output) => {
                if let Some(path) = output {
                    if let Err(e) = std::fs::write(path, &toml_output) {
                        eprintln!("Failed to write output file: {}", e);
                        std::process::exit(1);
                    }
                    eprintln!("Profile written to: {}", path.display());
                } else {
                    println!("{}", toml_output);
                }
            }
            Err(e) => {
                eprintln!("Failed to generate profile: {}", e);
                std::process::exit(1);
            }
        },
    }
}

/// Generate an HPC profile from the current Slurm cluster
fn generate_profile_from_slurm(
    name: Option<String>,
    display_name: Option<String>,
    skip_stdby: bool,
) -> Result<String, String> {
    let profile =
        crate::client::hpc::slurm::generate_dynamic_slurm_profile(name, display_name, skip_stdby)?;

    // Generate TOML output
    generate_toml_profile(&profile.name, &profile.display_name, &profile.partitions)
}

/// Generate TOML configuration for the profile
fn generate_toml_profile(
    name: &str,
    display_name: &str,
    partitions: &[HpcPartition],
) -> Result<String, String> {
    let mut output = String::new();

    // Header comment
    output.push_str(&format!(
        "# HPC profile for {} generated from Slurm\n",
        display_name
    ));
    output.push_str(&format!(
        "# Generated: {}\n",
        chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
    ));
    output.push_str("#\n");
    output.push_str("# To use this profile, add it to your torc config file:\n");
    output.push_str("#   ~/.config/torc/config.toml (Linux/macOS)\n");
    output.push_str("#   %APPDATA%\\torc\\config.toml (Windows)\n");
    output.push_str("#\n");
    output.push_str("# You may want to review and adjust:\n");
    output.push_str(
        "#   - requires_explicit_request: set to true for partitions that shouldn't auto-route\n",
    );
    output.push_str("#   - gpu_memory_gb: add GPU memory if known\n");
    output.push_str("#   - description: add human-readable descriptions\n");
    output.push('\n');

    // Profile header
    output.push_str(&format!("[client.hpc.custom_profiles.\"{}\"]\n", name));
    output.push_str(&format!("display_name = \"{}\"\n", display_name));

    // Try to generate hostname detection pattern
    if let Ok(hostname) = hostname::get() {
        let hostname = hostname.to_string_lossy();
        // Extract domain pattern (e.g., "node01.cluster.edu" -> ".*\\.cluster\\.edu")
        if let Some(dot_pos) = hostname.find('.') {
            let domain = &hostname[dot_pos + 1..];
            let pattern = format!(".*\\\\.{}", domain.replace('.', "\\\\."));
            output.push_str(&format!("detect_hostname = \"{}\"\n", pattern));
        }
    }

    output.push('\n');

    // Partitions
    for partition in partitions {
        output.push_str(&format!(
            "[[client.hpc.custom_profiles.\"{}\".partitions]]\n",
            name
        ));
        output.push_str(&format!("name = \"{}\"\n", partition.name));
        if !partition.description.is_empty() {
            output.push_str(&format!("description = \"{}\"\n", partition.description));
        } else {
            output.push_str("# description = \"\"\n");
        }
        output.push_str(&format!("cpus_per_node = {}\n", partition.cpus_per_node));
        output.push_str(&format!("memory_mb = {}\n", partition.memory_mb));
        output.push_str(&format!(
            "max_walltime_secs = {}\n",
            partition.max_walltime_secs
        ));

        if let Some(gpus) = partition.gpus_per_node {
            output.push_str(&format!("gpus_per_node = {}\n", gpus));
        }
        if let Some(ref gpu_type) = partition.gpu_type {
            output.push_str(&format!("gpu_type = \"{}\"\n", gpu_type));
        }
        if let Some(gpu_mem) = partition.gpu_memory_gb {
            output.push_str(&format!("gpu_memory_gb = {}\n", gpu_mem));
        } else {
            output.push_str("# gpu_memory_gb = 0\n");
        }
        if partition.shared {
            output.push_str("shared = true\n");
        } else {
            output.push_str("shared = false\n");
        }
        if partition.requires_explicit_request {
            output.push_str("requires_explicit_request = true\n");
        } else {
            output.push_str("requires_explicit_request = false\n");
        }

        output.push('\n');
    }

    Ok(output)
}