auberge 0.10.2

CLI tool for managing self-hosted infrastructure with Ansible
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
use crate::output;
use crate::prompt::select_item;
use crate::services::dns::DnsService;
use clap::{Subcommand, ValueEnum};
use dialoguer::{Input, theme::ColorfulTheme};
use eyre::Result;

#[derive(Clone, ValueEnum)]
pub enum OutputFormat {
    Human,
    Json,
    Tsv,
}

#[derive(Subcommand)]
pub enum DnsCommands {
    #[command(alias = "l", about = "List DNS records")]
    List {
        #[arg(short, long, help = "Filter by subdomain name")]
        subdomain: Option<String>,
        #[arg(short = 'P', long, help = "Use production API (default: sandbox)")]
        production: bool,
    },
    #[command(alias = "st", about = "Show DNS status and health")]
    Status {
        #[arg(short = 'P', long, help = "Use production API (default: sandbox)")]
        production: bool,
    },
    #[command(alias = "s", about = "Set an A record for a subdomain")]
    Set {
        #[arg(short, long, help = "Subdomain name")]
        subdomain: Option<String>,
        #[arg(short, long, help = "IP address")]
        ip: Option<String>,
        #[arg(short = 'P', long, help = "Use production API (default: sandbox)")]
        production: bool,
    },
    #[command(alias = "m", about = "Migrate all A records to a new IP")]
    Migrate {
        #[arg(short, long, help = "New IP address")]
        ip: String,
        #[arg(short = 'n', long, help = "Dry run (don't actually migrate)")]
        dry_run: bool,
        #[arg(short = 'P', long, help = "Use production API (default: sandbox)")]
        production: bool,
    },
    #[command(
        alias = "sa",
        about = "Batch create A records for all app subdomains",
        long_about = "Interactively or automatically create DNS A records for all configured \
                      app subdomains (blocky, calibre, freshrss, etc.) pointing to a selected \
                      host's IP address."
    )]
    SetAll {
        #[arg(
            short = 'H',
            long,
            value_name = "HOST",
            help = "Target host (auberge, auberge-old, vibecoder)"
        )]
        host: Option<String>,
        #[arg(
            short,
            long,
            value_name = "IP",
            conflicts_with = "host",
            help = "Override IP address"
        )]
        ip: Option<String>,
        #[arg(short = 'n', long, help = "Preview changes without executing")]
        dry_run: bool,
        #[arg(short = 'y', long, help = "Skip confirmation prompt")]
        yes: bool,
        #[arg(
            short,
            long,
            help = "Fail if any subdomain env var is missing (non-interactive)"
        )]
        strict: bool,
        #[arg(
            short = 'S',
            long,
            value_name = "NAMES",
            value_delimiter = ',',
            help = "Only process specific subdomains (comma-separated)"
        )]
        subdomains: Vec<String>,
        #[arg(
            long,
            value_name = "NAMES",
            value_delimiter = ',',
            help = "Skip specific subdomains (comma-separated)"
        )]
        skip: Vec<String>,
        #[arg(
            short = 'o',
            long,
            value_enum,
            default_value = "human",
            help = "Output format"
        )]
        output: OutputFormat,
        #[arg(long, help = "Continue on errors instead of failing fast")]
        continue_on_error: bool,
        #[arg(short = 'P', long, help = "Use production API (default: sandbox)")]
        production: bool,
    },
}

pub async fn run_dns_list(subdomain: Option<String>, production: bool) -> Result<()> {
    let service = DnsService::new_with_production(Some(production)).await?;
    print_mode_banner();

    let records = service.list_records().await?;

    let filtered: Vec<_> = match &subdomain {
        Some(name) => records.iter().filter(|r| r.name == *name).collect(),
        None => records.iter().collect(),
    };

    if filtered.is_empty() {
        output::info("No DNS records found");
        return Ok(());
    }

    eprintln!(
        "DNS Records for {}\n{:<40} {:<8} {:<24} {:>6}",
        service.domain(),
        "NAME",
        "TYPE",
        "CONTENT",
        "TTL"
    );
    eprintln!("{}", "-".repeat(80));

    for record in filtered {
        let (record_type, content) = format_dns_content(&record.content);
        eprintln!(
            "{:<40} {:<8} {:<24} {:>6}",
            record.name, record_type, content, record.ttl
        );
    }

    Ok(())
}

fn format_dns_content(content: &cloudflare::endpoints::dns::dns::DnsContent) -> (String, String) {
    use cloudflare::endpoints::dns::dns::DnsContent;
    match content {
        DnsContent::A { content } => ("A".to_string(), content.to_string()),
        DnsContent::AAAA { content } => ("AAAA".to_string(), content.to_string()),
        DnsContent::CNAME { content } => ("CNAME".to_string(), content.clone()),
        DnsContent::MX { content, priority } => {
            ("MX".to_string(), format!("{} ({})", content, priority))
        }
        DnsContent::TXT { content } => ("TXT".to_string(), content.clone()),
        DnsContent::NS { content } => ("NS".to_string(), content.clone()),
        DnsContent::SRV { content } => ("SRV".to_string(), content.clone()),
    }
}

fn print_mode_banner() {
    output::info("CLOUDFLARE DNS");
}

pub async fn run_dns_status(production: bool) -> Result<()> {
    let service = DnsService::new_with_production(Some(production)).await?;
    print_mode_banner();

    let status = service.status().await?;

    eprintln!("DNS Status for {}", status.domain);
    eprintln!("{}", "-".repeat(40));

    eprintln!(
        "\nConfigured subdomains: {}",
        status.configured_subdomains.join(", ")
    );

    use cloudflare::endpoints::dns::dns::DnsContent;
    let a_records: Vec<_> = status
        .active_records
        .iter()
        .filter(|r| matches!(r.content, DnsContent::A { .. }))
        .collect();

    eprintln!("\nActive A records: {}", a_records.len());
    for record in &a_records {
        if let DnsContent::A { content } = record.content {
            eprintln!("  {} -> {}", record.name, content);
        }
    }

    if !status.missing_subdomains.is_empty() {
        eprintln!(
            "\nMissing subdomains: {}",
            status.missing_subdomains.join(", ")
        );
    } else {
        eprintln!("\nAll configured subdomains have A records");
    }

    Ok(())
}

fn resolve_subdomain(subdomain: Option<String>) -> Result<String> {
    match subdomain {
        Some(s) => Ok(s),
        None => {
            crate::config::Config::load()?;
            let subdomains = crate::services::dns::discover_subdomains();
            let mut items: Vec<String> = subdomains.values().map(|e| e.subdomain.clone()).collect();
            if items.is_empty() {
                eyre::bail!("No subdomains defined in config");
            }
            items.sort();
            items.dedup();
            select_item(&items, |s: &String| s.clone(), "Select subdomain")?
                .ok_or_else(|| eyre::eyre!("No subdomain selected"))
        }
    }
}

fn resolve_ip(ip: Option<String>) -> Result<String> {
    match ip {
        Some(i) => Ok(i),
        None => {
            let value = Input::<String>::with_theme(&ColorfulTheme::default())
                .with_prompt("IP address")
                .interact_text()?;
            let value = value.trim().to_string();
            value
                .parse::<std::net::Ipv4Addr>()
                .map_err(|_| eyre::eyre!("Invalid IPv4 address: {}", value))?;
            Ok(value)
        }
    }
}

pub async fn run_dns_set(
    subdomain: Option<String>,
    ip: Option<String>,
    production: bool,
) -> Result<()> {
    let subdomain = resolve_subdomain(subdomain)?;
    let ip = resolve_ip(ip)?;

    let service = DnsService::new_with_production(Some(production)).await?;
    print_mode_banner();

    output::info(&format!(
        "Setting A record: {}.{} -> {}",
        subdomain,
        service.domain(),
        ip
    ));

    service.set_a_record(&subdomain, &ip).await?;
    output::success("A record set successfully");

    Ok(())
}

pub async fn run_dns_migrate(ip: String, dry_run: bool, production: bool) -> Result<()> {
    let service = DnsService::new_with_production(Some(production)).await?;
    print_mode_banner();

    if dry_run {
        eprintln!("[DRY RUN] DNS Migration Preview");
    } else {
        eprintln!("DNS Migration");
    }
    eprintln!("{}", "-".repeat(50));
    eprintln!(
        "{:<14} {:<16} {:^3} {:<16}",
        "SUBDOMAIN", "CURRENT", "", "NEW"
    );
    eprintln!("{}", "-".repeat(50));

    let results = service.migrate_all(&ip, dry_run).await?;

    for result in &results {
        eprintln!(
            "{:<14} {:<16} ->  {:<16}",
            result.subdomain, result.old_ip, result.new_ip
        );
    }

    if dry_run {
        eprintln!("\nWould update {} A record(s).", results.len());
    } else {
        let success_count = results.iter().filter(|r| r.success).count();
        eprintln!("\nUpdated {} A record(s).", success_count);
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub async fn run_dns_set_all(
    host: Option<String>,
    ip: Option<String>,
    dry_run: bool,
    yes: bool,
    strict: bool,
    subdomains: Vec<String>,
    skip: Vec<String>,
    _output: OutputFormat,
    continue_on_error: bool,
    production: bool,
) -> Result<()> {
    use crate::hosts::HostManager;
    use crate::services::dns::{SubdomainEntry, discover_subdomains};
    use crate::services::inventory::discover_hosts_with_ips;
    use std::collections::HashSet;

    let service = DnsService::new_with_production(Some(production)).await?;
    print_mode_banner();

    let target_ip = match (&host, &ip) {
        (Some(host_name), None) => {
            let hosts = discover_hosts_with_ips(None)?;
            hosts
                .get(host_name)
                .ok_or_else(|| {
                    eyre::eyre!(
                        "Host '{}' not found in inventory. Available: {}",
                        host_name,
                        hosts.keys().cloned().collect::<Vec<_>>().join(", ")
                    )
                })?
                .clone()
        }
        (None, Some(ip_addr)) => ip_addr.clone(),
        (None, None) => {
            if !strict {
                eyre::bail!("Either --host or --ip must be specified");
            } else {
                eyre::bail!("Either --host or --ip must be specified in strict mode");
            }
        }
        _ => unreachable!(),
    };

    // When `--host` is used, look up the host's cached Tailscale IP so we can
    // auto-fill `ip_override` for tailnet-only apps. With `--ip` the user is
    // being explicit; we don't second-guess.
    //
    // `load_hosts()?` propagates parse/IO errors so a malformed `hosts.toml`
    // surfaces here instead of being silently treated as "no cached IP" —
    // missing-host vs malformed-file diverge intentionally.
    let host_tailscale_ip: Option<String> = if let Some(name) = host.as_deref() {
        HostManager::load_hosts()?
            .into_iter()
            .find(|h| h.name == name)
            .and_then(|h| h.tailscale_ip)
    } else {
        None
    };

    let mut discovered = discover_subdomains();

    if strict && discovered.is_empty() {
        eyre::bail!("No subdomain environment variables found");
    }

    let skip_set: HashSet<_> = skip.iter().cloned().collect();

    let mut subdomains_to_process: Vec<(String, SubdomainEntry)> = if !subdomains.is_empty() {
        subdomains
            .into_iter()
            .filter(|s| !skip_set.contains(s))
            .filter_map(|s| discovered.remove(&s).map(|entry| (s, entry)))
            .collect()
    } else {
        discovered
            .into_iter()
            .filter(|(k, _)| !skip_set.contains(k))
            .collect()
    };

    if subdomains_to_process.is_empty() {
        output::info("No subdomains to process");
        return Ok(());
    }

    // Apply the tailnet-only fallback only to records that survived `--skip`
    // / `--subdomains` filtering — otherwise an excluded tailnet-only app with
    // no cached IP would still abort the run.
    //
    // Skip entirely when `--ip` was used: the user is being explicit and the
    // host-cache fallback should not second-guess (or bail on) that.
    if host.is_some() {
        apply_tailnet_only_fallback(&mut subdomains_to_process, host_tailscale_ip.as_deref())?;
    }

    if dry_run {
        output::info("DRY RUN - Would create the following A records:");
    } else {
        output::info("Creating the following A records:");
    }

    for (_, entry) in &subdomains_to_process {
        let effective_ip = entry.ip_override.as_deref().unwrap_or(&target_ip);
        if entry.ip_override.is_some() {
            eprintln!(
                "{}.{}{} (tailnet)",
                entry.subdomain,
                service.domain(),
                effective_ip
            );
        } else {
            eprintln!(
                "{}.{}{}",
                entry.subdomain,
                service.domain(),
                effective_ip
            );
        }
    }

    if !yes && !dry_run {
        eprint!("\nProceed? [y/N]: ");
        use std::io::{self, BufRead};
        let mut response = String::new();
        io::stdin().lock().read_line(&mut response)?;
        if !response.trim().eq_ignore_ascii_case("y") {
            println!("Operation cancelled");
            return Ok(());
        }
    }

    if dry_run {
        output::info("DRY RUN - No changes were made");
        return Ok(());
    }

    eprintln!();
    let mut succeeded = 0;
    let mut failed = 0;

    for (idx, (_app_name, entry)) in subdomains_to_process.iter().enumerate() {
        let effective_ip = entry.ip_override.as_deref().unwrap_or(&target_ip);
        match service.set_a_record(&entry.subdomain, effective_ip).await {
            Ok(_) => {
                output::success(&format!("Created {}.{}", entry.subdomain, service.domain()));
                succeeded += 1;
            }
            Err(e) => {
                eprintln!("Failed {}.{}: {}", entry.subdomain, service.domain(), e);
                failed += 1;
                if !continue_on_error {
                    return Err(e);
                }
            }
        }

        if idx < subdomains_to_process.len() - 1 {
            tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
        }
    }

    let has_overrides = subdomains_to_process
        .iter()
        .any(|(_, e)| e.ip_override.is_some());
    if has_overrides {
        output::success(&format!(
            "Successfully created {}/{} A records (some with tailnet IP overrides)",
            succeeded,
            subdomains_to_process.len(),
        ));
    } else {
        output::success(&format!(
            "Successfully created {}/{} A records pointing to {}",
            succeeded,
            subdomains_to_process.len(),
            target_ip
        ));
    }

    if failed > 0 {
        eprintln!("Failed to create {} records", failed);
        std::process::exit(1);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::apply_tailnet_only_fallback;
    use crate::services::dns::SubdomainEntry;

    fn entries(items: &[(&str, &str, Option<&str>)]) -> Vec<(String, SubdomainEntry)> {
        items
            .iter()
            .map(|(app, subdomain, ip_override)| {
                (
                    app.to_string(),
                    SubdomainEntry {
                        subdomain: subdomain.to_string(),
                        ip_override: ip_override.map(String::from),
                    },
                )
            })
            .collect()
    }

    fn find<'a>(v: &'a [(String, SubdomainEntry)], app: &str) -> &'a SubdomainEntry {
        &v.iter().find(|(a, _)| a == app).unwrap().1
    }

    #[test]
    fn fills_tailnet_only_app_when_host_has_tailscale_ip() {
        let mut v = entries(&[("bichon", "bichon", None)]);
        apply_tailnet_only_fallback(&mut v, Some("100.64.0.5")).unwrap();
        assert_eq!(
            find(&v, "bichon").ip_override.as_deref(),
            Some("100.64.0.5")
        );
    }

    #[test]
    fn preserves_explicit_override_for_tailnet_only_app() {
        let mut v = entries(&[("bichon", "bichon", Some("100.42.42.42"))]);
        apply_tailnet_only_fallback(&mut v, Some("100.64.0.5")).unwrap();
        assert_eq!(
            find(&v, "bichon").ip_override.as_deref(),
            Some("100.42.42.42"),
        );
    }

    #[test]
    fn leaves_public_app_unchanged_even_with_host_tailscale_ip() {
        let mut v = entries(&[("freshrss", "rss", None)]);
        apply_tailnet_only_fallback(&mut v, Some("100.64.0.5")).unwrap();
        assert!(find(&v, "freshrss").ip_override.is_none());
    }

    #[test]
    fn fails_fast_when_tailnet_only_app_has_no_tailscale_ip() {
        let mut v = entries(&[("bichon", "bichon", None)]);
        let err = apply_tailnet_only_fallback(&mut v, None).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("bichon"));
        assert!(msg.contains("tailnet-only"));
    }

    #[test]
    fn ignores_apps_with_missing_meta_files() {
        let mut v = entries(&[("nonexistent_app_xyz", "nonexistent_app_xyz", None)]);
        apply_tailnet_only_fallback(&mut v, Some("100.64.0.5")).unwrap();
        assert!(find(&v, "nonexistent_app_xyz").ip_override.is_none());
    }

    #[test]
    fn does_not_touch_entries_outside_the_processed_slice() {
        // Simulates the post-filter behavior: when bichon was excluded via
        // --skip, it's not in the slice, so even without a host_tailscale_ip
        // we don't bail.
        let mut v = entries(&[("freshrss", "rss", None)]);
        apply_tailnet_only_fallback(&mut v, None).unwrap();
        assert!(find(&v, "freshrss").ip_override.is_none());
    }
}

/// For each subdomain entry being processed whose playbook meta declares
/// `tailnet_only: true` and which lacks an explicit `<app>_tailscale_ip`
/// override, fill `ip_override` from the host's cached Tailscale IP.
///
/// Bails when a tailnet-only app has no resolvable Tailscale IP — pointing
/// such an app at the host's public IP would be silently broken (Caddy binds
/// only to the Tailscale interface), so failing fast surfaces the missing
/// configuration to the user.
fn apply_tailnet_only_fallback(
    entries: &mut [(String, crate::services::dns::SubdomainEntry)],
    host_tailscale_ip: Option<&str>,
) -> Result<()> {
    use crate::playbook_meta::PlaybookMeta;

    for (app, entry) in entries.iter_mut() {
        if entry.ip_override.is_some() {
            continue;
        }
        let Some(meta) = PlaybookMeta::load_for_app(app)? else {
            continue;
        };
        if !meta.tailnet_only {
            continue;
        }
        match host_tailscale_ip {
            Some(ip) => entry.ip_override = Some(ip.to_string()),
            None => eyre::bail!(
                "App '{app}' is tailnet-only but no Tailscale IP is available. \
                 Either pass --host (after running `auberge host detect-tailscale-ip <name>`), \
                 or set `{app}_tailscale_ip` in config.toml."
            ),
        }
    }
    Ok(())
}