alembic-cli 0.5.0

Command line interface for Alembic.
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
//! cli entrypoint for alembic.

pub mod config;
mod diag;
mod io;
mod state;

use alembic_adapter_registry::{create_backend, Plugin};
use alembic_engine::{apply_plan, build_plan, load_inventory, ApplyReport, DriftReport, Plan};
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use std::fs;
use std::path::PathBuf;

use self::diag::err;
use self::io::{
    format_validation_errors, read_plan, warn_misleading_output_extension, write_inventory,
    write_plan,
};
use self::state::load_state;
use crate::app::config::AppConfig;
use alembic_core::TypeName;

#[cfg(test)]
use self::state::{resolve_state_backend_config, state_path, StateBackendConfig};
#[cfg(test)]
use alembic_adapter_django::cast_django::Runner;
#[cfg(test)]
use alembic_engine::PostgresTlsMode;
#[cfg(test)]
use std::path::Path;

/// top-level cli definition.
#[derive(Parser)]
#[command(name = "alembic")]
#[command(
    about = "Data-model-first converger + loader for DCIM/IPAM (YAML/JSON inventories in, JSON plans out)"
)]
#[command(long_about = "\
Data-model-first converger + loader for DCIM/IPAM.

File formats are chosen by file extension:
  - inventories (IR) are authored as YAML or JSON: a .json extension is parsed as
    JSON, anything else (.yaml, .yml, or no extension) is parsed as YAML. each
    inventory carries a schema block plus optional include/imports.
  - plans (plan --output) and observed or transformed IR (import --output and
    map --output) are always written as JSON, regardless of the path extension.
  - apply --plan consumes a JSON plan file as produced by alembic plan.")]
pub(crate) struct Cli {
    #[command(subcommand)]
    command: Command,
}

/// cli subcommands.
#[derive(Subcommand)]
enum Command {
    Validate {
        #[arg(short = 'f', long)]
        file: PathBuf,
    },
    Plan {
        #[arg(short = 'f', long)]
        file: PathBuf,
        #[arg(short = 'o', long)]
        output: PathBuf,
        #[arg(long)]
        backend: Option<String>,
        #[arg(long)]
        backend_config: Option<PathBuf>,
        #[arg(long, default_value_t = false)]
        provision: bool,
        #[arg(long, default_value_t = false)]
        dry_run: bool,
        /// print a read-only drift report (desired vs observed) and exit without
        /// writing a plan file or saving state. mutually exclusive with --dry-run.
        #[arg(long, default_value_t = false, conflicts_with = "dry_run")]
        report: bool,
        #[arg(long, default_value_t = false)]
        allow_delete: bool,
    },
    Apply {
        #[arg(short = 'p', long)]
        plan: PathBuf,
        #[arg(long)]
        backend: Option<String>,
        #[arg(long)]
        backend_config: Option<PathBuf>,
        #[arg(long, default_value_t = false)]
        allow_delete: bool,
        #[arg(short = 'i', long, default_value_t = false)]
        interactive: bool,
    },
    /// transform an ir inventory into another ir inventory (ir -> ir).
    Map {
        #[command(subcommand)]
        action: Option<MapAction>,
        /// input ir inventory file.
        #[arg(short = 'f', long)]
        file: Option<PathBuf>,
        /// map specification (target schema + rules).
        #[arg(long)]
        spec: Option<PathBuf>,
        #[arg(short = 'o', long)]
        output: Option<PathBuf>,
    },
    /// observe a backend's live state into canonical ir.
    Import {
        #[arg(short = 'o', long)]
        output: PathBuf,
        /// inventory whose schema selects which types to observe.
        #[arg(short = 'f', long)]
        file: PathBuf,
        #[arg(long)]
        backend: Option<String>,
        #[arg(long)]
        backend_config: Option<PathBuf>,
    },
}

/// map subcommands.
#[derive(Subcommand)]
enum MapAction {
    /// evaluate a single transform against a json value, for iterating on a
    /// map spec's user-defined transforms without an inventory or backend.
    Transform {
        /// map specification carrying the transforms block.
        #[arg(long)]
        spec: PathBuf,
        /// transform name (user-defined or built-in).
        name: String,
        /// json-encoded input value, e.g. '"nxos"'.
        value: String,
        /// json-encoded extra literal arguments.
        args: Vec<String>,
    },
}

fn confirm(prompt: &str) -> Result<bool> {
    use std::io::{self, Write};
    let mut stdout = io::stdout();
    stdout.write_all(prompt.as_bytes())?;
    stdout.flush()?;
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    Ok(matches!(input.trim().to_lowercase().as_str(), "y" | "yes"))
}

/// whether the planner should emit delete ops (objects present on the backend
/// but not declared in intent).
///
/// `--report` never applies the plan, so it forces delete-detection on purely to
/// populate the drift report's `extra` category. without this, the documented
/// `plan ... --report` invocation would silently never surface unmanaged backend
/// objects, regardless of `--allow-delete`. non-report paths are unchanged and
/// remain governed solely by `--allow-delete`.
fn should_detect_deletes(allow_delete: bool, report: bool) -> bool {
    allow_delete || report
}

pub(crate) async fn run(cli: Cli, config: AppConfig) -> Result<()> {
    match cli.command {
        Command::Validate { file } => {
            let inventory = load_inventory(&file)?;
            let report = alembic_engine::validate(&inventory);
            if report.is_ok() {
                println!("ok");
            } else {
                for error in format_validation_errors(report, &inventory.objects) {
                    err("validate", &error);
                }
                return Err(anyhow!("validation failed"));
            }
        }
        Command::Plan {
            file,
            output,
            backend,
            backend_config,
            provision,
            dry_run,
            report,
            allow_delete,
        } => {
            let inventory = load_inventory(&file)?;
            let mut state = load_state().await?;
            let plugins = search_for_plugins(&config);
            let backend = create_backend(&plugins, backend.as_deref(), backend_config)?;
            if provision {
                let provision_report = backend.adapter()?.ensure_schema(&inventory.schema).await?;
                if !provision_report.is_empty() {
                    println!("provision: {provision_report}");
                }
            }

            let plan = build_plan(
                backend.observer()?,
                &inventory,
                &mut state,
                should_detect_deletes(allow_delete, report),
            )
            .await?;
            if report {
                // read-only: describe desired-vs-observed and exit without
                // writing a plan file or saving state.
                println!("{}", DriftReport::from_plan(&plan));
            } else if dry_run {
                let raw = serde_json::to_string_pretty(&plan)?;
                println!("{raw}");
            } else {
                if let Some(msg) = warn_misleading_output_extension(&output) {
                    eprintln!("{msg}");
                }
                write_plan(&output, &plan)?;
                state.save_async().await?;
                if let Some(s) = &plan.summary {
                    println!(
                        "plan: {} to create, {} to update, {} to delete",
                        s.create, s.update, s.delete
                    );
                }
                println!("plan written to {}", output.display());
            }
        }
        Command::Apply {
            plan,
            backend,
            backend_config,
            allow_delete,
            interactive,
        } => {
            let mut state = load_state().await?;
            let plugins = search_for_plugins(&config);
            let backend = create_backend(&plugins, backend.as_deref(), backend_config)?;
            let plan = read_plan(&plan)?;

            if interactive {
                if !allow_delete
                    && plan
                        .ops
                        .iter()
                        .any(|op| matches!(op, alembic_engine::Op::Delete { .. }))
                {
                    return Err(anyhow!(
                        "plan contains delete operations; re-run with --allow-delete"
                    ));
                }
                let ordered = alembic_engine::sort_ops_for_apply(&plan.ops, &plan.schema);
                let mut approved = Vec::new();
                for op in ordered {
                    let prompt = match &op {
                        alembic_engine::Op::Create {
                            type_name, desired, ..
                        } => format!(
                            "create {} {}? [y/N] ",
                            type_name,
                            alembic_core::key_string(&desired.key)
                        ),
                        alembic_engine::Op::Update {
                            type_name, desired, ..
                        } => format!(
                            "update {} {}? [y/N] ",
                            type_name,
                            alembic_core::key_string(&desired.key)
                        ),
                        alembic_engine::Op::Delete { type_name, key, .. } => format!(
                            "delete {} {}? [y/N] ",
                            type_name,
                            alembic_core::key_string(key)
                        ),
                    };
                    if confirm(&prompt)? {
                        approved.push(op);
                    }
                }
                let interactive_plan = Plan {
                    schema: plan.schema.clone(),
                    ops: approved,
                    summary: None,
                };
                let report =
                    apply_plan(&backend, &interactive_plan, &mut state, allow_delete).await?;
                state.save_async().await?;
                print_apply_report(report);
            } else {
                let report = apply_plan(&backend, &plan, &mut state, allow_delete).await?;
                state.save_async().await?;
                print_apply_report(report);
            }
        }
        Command::Map {
            action,
            file,
            spec,
            output,
        } => match action {
            Some(MapAction::Transform {
                spec,
                name,
                value,
                args,
            }) => {
                let spec = alembic_engine::load_map_spec(&spec)?;
                let parse_json = |label: &str, raw: &str| -> Result<serde_json::Value> {
                    serde_json::from_str(raw).map_err(|err| {
                        anyhow!(
                            "{label} is not valid json: {err}\n\
                             hint: string values need json quoting, e.g. '\"{raw}\"'"
                        )
                    })
                };
                let value = parse_json("value", &value)?;
                let args = args
                    .iter()
                    .map(|arg| parse_json(&format!("argument {arg}"), arg))
                    .collect::<Result<Vec<serde_json::Value>>>()?;
                let result = alembic_engine::eval_map_transform(&spec, &name, &value, &args)?;
                println!("{}", serde_json::to_string(&result)?);
            }
            None => {
                let (Some(file), Some(spec), Some(output)) = (file, spec, output) else {
                    return Err(anyhow!(
                        "alembic map requires -f, --spec, and -o (or the transform subcommand)"
                    ));
                };
                let input = load_inventory(&file)?;
                let spec = alembic_engine::load_map_spec(&spec)?;
                let inventory = alembic_engine::compile_map(&input, &spec)?;
                if let Some(msg) = warn_misleading_output_extension(&output) {
                    eprintln!("{msg}");
                }
                write_inventory(&output, &inventory)?;
                println!("ir written to {}", output.display());
            }
        },
        Command::Import {
            output,
            file,
            backend,
            backend_config,
        } => {
            // observe live backend state into ir; the inventory's schema selects
            // which types to observe.
            let inventory = load_inventory(&file)?;
            let plugins = search_for_plugins(&config);
            let backend = create_backend(&plugins, backend.as_deref(), backend_config)?;
            let state = load_state().await?;
            let types: Vec<TypeName> = inventory.schema.types.keys().map(TypeName::new).collect();
            let report = alembic_engine::import_inventory(
                backend.observer()?,
                &inventory.schema,
                &types,
                &state,
            )
            .await?;
            if let Some(msg) = warn_misleading_output_extension(&output) {
                eprintln!("{msg}");
            }
            write_inventory(&output, &report.inventory)?;
            println!("inventory written to {}", output.display());
        }
    }

    Ok(())
}

fn print_apply_report(report: ApplyReport) {
    if !report.provision.is_empty() {
        println!("provision: {}", report.provision);
    }
    if let Some(previously_applied_count) = report.previously_applied_count {
        println!(
            "applied {} operations (after resuming, had previously applied {} operations)",
            report.applied.len(),
            previously_applied_count
        );
    } else {
        println!("applied {} operations", report.applied.len());
    }
}

fn search_for_plugins(config: &AppConfig) -> Vec<Plugin> {
    let Ok(dir_contents) = fs::read_dir(&config.plugins_dir) else {
        tracing::debug!("plugin dir '{}' not found", config.plugins_dir.display());
        return vec![];
    };

    dir_contents
        .flatten()
        .filter(|e| {
            e.path()
                .extension()
                .and_then(|s| s.to_str())
                .map(|s| s.to_lowercase() == "yaml" || s.to_lowercase() == "yml")
                .unwrap_or(false)
        })
        .filter_map(|e| {
            let path = e.path();
            let name = path.file_stem().and_then(|s| s.to_str())?;
            if name.is_empty() {
                return None;
            }
            Some(Plugin {
                name: name.to_string().to_lowercase(),
                path: e.path(),
            })
        })
        .collect()
}

#[cfg(test)]
mod test_support;
#[cfg(test)]
mod tests;