add config output
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
/demo/out
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
regional:
|
regional:
|
||||||
partition: us
|
partition: us
|
||||||
|
username: netadmin
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
common:
|
common:
|
||||||
layer: ex-edge-r
|
layer: ex-edge-r
|
||||||
|
protocol: bgp
|
||||||
|
uplink: et-0/0/0
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
zonal:
|
zonal:
|
||||||
az: 1
|
zone: 1
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
zonal:
|
zonal:
|
||||||
az: 2
|
zone: 2
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
common:
|
common:
|
||||||
partition: eu
|
partition: eu
|
||||||
layer: ex-edge-r
|
layer: ex-edge-r
|
||||||
|
protocol: ospf
|
||||||
|
uplink: et-0/0/36
|
||||||
|
|||||||
@@ -1,2 +1,5 @@
|
|||||||
chassis {
|
chassis {
|
||||||
|
users {
|
||||||
|
{{ username }};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
interfaces {
|
interfaces {
|
||||||
|
{{ uplink }} {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
protocols {
|
protocols {
|
||||||
|
{{ protocol }} {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,10 +51,7 @@ macro_rules! verb {
|
|||||||
macro_rules! dbug {
|
macro_rules! dbug {
|
||||||
($current_level:expr, $($msg:expr),*) => {
|
($current_level:expr, $($msg:expr),*) => {
|
||||||
if LogLevel::Debug.value() >= $current_level.value() {
|
if LogLevel::Debug.value() >= $current_level.value() {
|
||||||
let message = format!($($msg),*);
|
println!($($msg),*);
|
||||||
for line in message.lines() {
|
|
||||||
println!(" |D| {}", line);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
44
src/main.rs
44
src/main.rs
@@ -4,6 +4,11 @@ mod specs;
|
|||||||
mod tmpls;
|
mod tmpls;
|
||||||
|
|
||||||
use log::LogLevel;
|
use log::LogLevel;
|
||||||
|
use serde_yml;
|
||||||
|
use std::fs::{self, create_dir_all, write, OpenOptions};
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
use tmpls::RenderedConfig;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: cli::Args = cli::parse_args();
|
let args: cli::Args = cli::parse_args();
|
||||||
@@ -19,13 +24,42 @@ fn main() {
|
|||||||
verb!(
|
verb!(
|
||||||
dbg,
|
dbg,
|
||||||
"Compiled Spec:\n{}",
|
"Compiled Spec:\n{}",
|
||||||
serde_json::to_string_pretty(&spec.compiled).unwrap()
|
serde_json::to_string_pretty(&spec.compiled["data"]).unwrap()
|
||||||
);
|
);
|
||||||
info!(dbg, "Rendered Config:");
|
output_rendered_configs(result, dbg)
|
||||||
for line in result {
|
|
||||||
if line != "\n" {
|
|
||||||
print!("{}", line)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn output_rendered_configs(rendered_config: RenderedConfig, dbg: LogLevel) {
|
||||||
|
info!(dbg, "Writing Output");
|
||||||
|
let path: String = format!("out/{}", rendered_config.hostname);
|
||||||
|
if Path::new(&path).exists() {
|
||||||
|
fs::remove_dir_all(&path).ok();
|
||||||
}
|
}
|
||||||
|
create_dir_all(&path).ok();
|
||||||
|
let rendered_config_path = format!("{}/all.conf", path);
|
||||||
|
for rendered_template in rendered_config.configs {
|
||||||
|
let outpath = format!("{}/{}.tmpl", path, rendered_template.0);
|
||||||
|
verb!(dbg, " | {}", &outpath);
|
||||||
|
write(&outpath, &rendered_template.1).ok();
|
||||||
|
append_to_file(&rendered_config_path, rendered_template.1).ok();
|
||||||
|
}
|
||||||
|
let outpath = format!("{}/compiled.spec", path);
|
||||||
|
let spec: String = match serde_yml::to_string(&rendered_config.spec) {
|
||||||
|
Ok(yaml) => yaml,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Failed to convert to YAML: {}", e);
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
verb!(dbg, " | {}", &outpath);
|
||||||
|
write(&outpath, spec).ok();
|
||||||
|
info!(dbg, " | {} ", &rendered_config_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_to_file(path: &str, content: String) -> std::io::Result<()> {
|
||||||
|
let mut file = OpenOptions::new().create(true).append(true).open(path)?;
|
||||||
|
|
||||||
|
write!(file, "{}", content)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{info, verb, LogLevel};
|
use crate::{dbug, info, verb, LogLevel};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde_json::{json, Map, Value};
|
use serde_json::{json, Map, Value};
|
||||||
use serde_yml;
|
use serde_yml;
|
||||||
@@ -121,7 +121,7 @@ pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specif
|
|||||||
specifications.push(
|
specifications.push(
|
||||||
match Specification::build(&partitional, ®ional, &common, &zonal, &spec) {
|
match Specification::build(&partitional, ®ional, &common, &zonal, &spec) {
|
||||||
Ok(compiled_spec) => {
|
Ok(compiled_spec) => {
|
||||||
verb!(
|
dbug!(
|
||||||
dbg,
|
dbg,
|
||||||
"Compiled Spec for '{}'\n | {}\n | {}\n | {}\n | {}\n | {}",
|
"Compiled Spec for '{}'\n | {}\n | {}\n | {}\n | {}\n | {}",
|
||||||
compiled_spec.get_hostname(),
|
compiled_spec.get_hostname(),
|
||||||
|
|||||||
27
src/tmpls.rs
27
src/tmpls.rs
@@ -1,5 +1,5 @@
|
|||||||
use crate::specs::Specification;
|
use crate::specs::Specification;
|
||||||
use crate::{verb, LogLevel};
|
use crate::{info, verb, LogLevel};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use serde_yml;
|
use serde_yml;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -10,10 +10,16 @@ struct TemplateConfig {
|
|||||||
files: Vec<String>,
|
files: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct RenderedConfig {
|
||||||
|
pub hostname: String,
|
||||||
|
pub configs: Vec<(String, String)>,
|
||||||
|
pub spec: Value,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn process_templates(
|
pub fn process_templates(
|
||||||
spec: &Specification,
|
spec: &Specification,
|
||||||
dbg: LogLevel,
|
dbg: LogLevel,
|
||||||
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
) -> Result<RenderedConfig, Box<dyn std::error::Error>> {
|
||||||
// Create Tera context from spec.compiled
|
// Create Tera context from spec.compiled
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
if let Value::Object(map) = &spec.compiled {
|
if let Value::Object(map) = &spec.compiled {
|
||||||
@@ -24,6 +30,11 @@ pub fn process_templates(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let device_name = match context.get("hostname") {
|
||||||
|
Some(value) => value.as_str().unwrap_or("default_hostname"),
|
||||||
|
None => "default_hostname",
|
||||||
|
};
|
||||||
|
|
||||||
// Get the base directory path
|
// Get the base directory path
|
||||||
let base_dir = format!("./tmpl/{}", spec.get_layer());
|
let base_dir = format!("./tmpl/{}", spec.get_layer());
|
||||||
|
|
||||||
@@ -36,8 +47,8 @@ pub fn process_templates(
|
|||||||
let mut tera = Tera::default();
|
let mut tera = Tera::default();
|
||||||
|
|
||||||
// Process each template
|
// Process each template
|
||||||
verb!(dbg, "Rendering Templates");
|
info!(dbg, "Rendering {}", &device_name);
|
||||||
let mut rendered_templates = Vec::new();
|
let mut configs = Vec::new();
|
||||||
for template_name in config.files {
|
for template_name in config.files {
|
||||||
// Read the template file directly
|
// Read the template file directly
|
||||||
let template_path = format!("{}/{}.tmpl", base_dir, template_name);
|
let template_path = format!("{}/{}.tmpl", base_dir, template_name);
|
||||||
@@ -49,8 +60,12 @@ pub fn process_templates(
|
|||||||
|
|
||||||
// Render the template
|
// Render the template
|
||||||
let rendered = tera.render(&template_name, &context)?;
|
let rendered = tera.render(&template_name, &context)?;
|
||||||
rendered_templates.push(rendered);
|
configs.push((String::from(&template_name), rendered));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(rendered_templates)
|
Ok(RenderedConfig {
|
||||||
|
hostname: String::from(device_name),
|
||||||
|
configs,
|
||||||
|
spec: spec.compiled.clone(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user