use pathbuf in spec; move output to render
This commit is contained in:
43
src/main.rs
43
src/main.rs
@@ -6,10 +6,6 @@ mod tmpl;
|
|||||||
|
|
||||||
use log::LogLevel;
|
use log::LogLevel;
|
||||||
use render::RenderedConfig;
|
use render::RenderedConfig;
|
||||||
use std::fs::{self, File, OpenOptions, create_dir_all, write};
|
|
||||||
use std::io::Write;
|
|
||||||
use std::path::Path;
|
|
||||||
use yaml_serde;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: cli::Args = cli::parse_args();
|
let args: cli::Args = cli::parse_args();
|
||||||
@@ -21,38 +17,11 @@ fn main() {
|
|||||||
spec::compile(&args.devices, &args.env.spec_path, dbg);
|
spec::compile(&args.devices, &args.env.spec_path, dbg);
|
||||||
|
|
||||||
for spec in specifications {
|
for spec in specifications {
|
||||||
let result = RenderedConfig::from_spec(&spec, dbg).unwrap_or_else(|e| {
|
RenderedConfig::from_spec(&spec, dbg)
|
||||||
eprintln!("{}", e);
|
.unwrap_or_else(|e| {
|
||||||
std::process::exit(1);
|
eprintln!("{}", e);
|
||||||
});
|
std::process::exit(1);
|
||||||
output_rendered_configs(result, &args.env.out_path, dbg)
|
})
|
||||||
|
.output(&args.env.out_path, dbg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn output_rendered_configs(rendered_config: RenderedConfig, outdir: &str, dbg: LogLevel) {
|
|
||||||
info!(dbg, "Writing Output");
|
|
||||||
let out_path = Path::new(outdir).join(rendered_config.hostname);
|
|
||||||
if out_path.exists() {
|
|
||||||
fs::remove_dir_all(&out_path).ok();
|
|
||||||
}
|
|
||||||
create_dir_all(&out_path).ok();
|
|
||||||
|
|
||||||
let merged_config_path = out_path.join("all.conf");
|
|
||||||
let mut all_file: File = OpenOptions::new()
|
|
||||||
.create(true)
|
|
||||||
.append(true)
|
|
||||||
.open(&merged_config_path)
|
|
||||||
.expect("unable to open");
|
|
||||||
for config in rendered_config.configs {
|
|
||||||
let template_path = out_path.join(config.name + ".tera");
|
|
||||||
verb!(dbg, " | {}", &template_path.display());
|
|
||||||
write(&template_path, &config.data).ok();
|
|
||||||
all_file.write_all(&config.data.as_bytes()).ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
let spec: String = yaml_serde::to_string(&rendered_config.spec).unwrap();
|
|
||||||
let compiled_spec_path = out_path.join("compiled-spec.yaml");
|
|
||||||
verb!(dbg, " | {}", &compiled_spec_path.display());
|
|
||||||
write(&compiled_spec_path, spec).ok();
|
|
||||||
info!(dbg, " | {} ", &merged_config_path.display());
|
|
||||||
}
|
|
||||||
|
|||||||
119
src/render.rs
119
src/render.rs
@@ -2,8 +2,7 @@ use crate::spec::Specification;
|
|||||||
use crate::tmpl;
|
use crate::tmpl;
|
||||||
use crate::{LogLevel, info, verb};
|
use crate::{LogLevel, info, verb};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::fs;
|
use std::io::Write;
|
||||||
use tera::{Context, Tera};
|
|
||||||
|
|
||||||
pub struct RenderedConfig {
|
pub struct RenderedConfig {
|
||||||
pub hostname: String,
|
pub hostname: String,
|
||||||
@@ -21,56 +20,30 @@ impl RenderedConfig {
|
|||||||
spec: &Specification,
|
spec: &Specification,
|
||||||
dbg: LogLevel,
|
dbg: LogLevel,
|
||||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let mut context = Context::new();
|
let context = get_context_from_specification(&spec);
|
||||||
|
|
||||||
// Flatten "data" map into Tera context
|
let hostname = get_hostname(&context, &spec);
|
||||||
if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) {
|
info!(dbg, "Rendering {}", hostname);
|
||||||
for (key, val) in data_map {
|
|
||||||
context.insert(key, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let hostname = context
|
|
||||||
.get("hostname")
|
|
||||||
.and_then(|v| v.as_str())
|
|
||||||
.unwrap_or("default_hostname")
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
|
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
|
||||||
|
|
||||||
let structure_path = base_dir.join("structure.yaml");
|
let structure_path = base_dir.join("structure.yaml");
|
||||||
let structure = tmpl::Structure::from_file(&structure_path)
|
let structure = tmpl::Structure::from_file(&structure_path)
|
||||||
.map_err(|e| format!("Failed to parse {}: {}", structure_path.display(), e))?;
|
.map_err(|e| format!("Failed to parse {}: {}", structure_path.display(), e))?;
|
||||||
|
|
||||||
let mut tera = Tera::default();
|
let mut renderer = tera::Tera::default();
|
||||||
let mut configs = Vec::new();
|
let mut configs = Vec::new();
|
||||||
|
|
||||||
info!(dbg, "Rendering {}", hostname);
|
let template_data = get_template_data_from_structure(&structure, &base_dir);
|
||||||
|
|
||||||
let template_data: Vec<(String, String)> = structure
|
renderer.add_raw_templates(template_data)?;
|
||||||
.files
|
|
||||||
.iter()
|
|
||||||
.filter_map(|name| {
|
|
||||||
let path = base_dir.join(format!("{}.tera", name));
|
|
||||||
match fs::read_to_string(&path) {
|
|
||||||
Ok(content) => Some((name.clone(), content)),
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Skipping {}: {}", path.display(), e);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
tera.add_raw_templates(template_data)?;
|
|
||||||
|
|
||||||
for template_name in &structure.files {
|
for template_name in &structure.files {
|
||||||
verb!(dbg, " | {}", &template_name);
|
verb!(dbg, " | {}", &template_name);
|
||||||
if !tera.get_template_names().any(|n| n == template_name) {
|
if !renderer.get_template_names().any(|n| n == template_name) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
match tera.render(template_name, &context) {
|
match renderer.render(template_name, &context) {
|
||||||
Ok(rendered) => configs.push(Configuration {
|
Ok(rendered) => configs.push(Configuration {
|
||||||
name: template_name.clone(),
|
name: template_name.clone(),
|
||||||
data: rendered,
|
data: rendered,
|
||||||
@@ -96,4 +69,78 @@ impl RenderedConfig {
|
|||||||
}
|
}
|
||||||
eprintln!("[tera] {}: {}", name, chain);
|
eprintln!("[tera] {}: {}", name, chain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn output(self: Self, outdir: &str, dbg: LogLevel) {
|
||||||
|
info!(dbg, "Writing Output:");
|
||||||
|
|
||||||
|
let out_path = std::path::Path::new(outdir).join(self.hostname);
|
||||||
|
if out_path.exists() {
|
||||||
|
std::fs::remove_dir_all(&out_path).ok();
|
||||||
|
}
|
||||||
|
std::fs::create_dir_all(&out_path).ok();
|
||||||
|
|
||||||
|
let merged_config_path = out_path.join("all.conf");
|
||||||
|
let mut all_file: std::fs::File = std::fs::OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(&merged_config_path)
|
||||||
|
.expect("unable to open");
|
||||||
|
|
||||||
|
for config in &self.configs {
|
||||||
|
let template_path = out_path.join(format!("{}.tera", &config.name));
|
||||||
|
verb!(dbg, " | {}", &template_path.display());
|
||||||
|
std::fs::write(&template_path, &config.data).ok();
|
||||||
|
all_file.write_all(&config.data.as_bytes()).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
let spec: String = yaml_serde::to_string(&self.spec).unwrap();
|
||||||
|
let compiled_spec_path = out_path.join("compiled-spec.yaml");
|
||||||
|
verb!(dbg, " | {}", &compiled_spec_path.display());
|
||||||
|
std::fs::write(&compiled_spec_path, spec).ok();
|
||||||
|
info!(dbg, " | {} ", &merged_config_path.display());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_template_data_from_structure(
|
||||||
|
structure: &tmpl::Structure,
|
||||||
|
base_dir: &std::path::Path,
|
||||||
|
) -> Vec<(String, String)> {
|
||||||
|
structure
|
||||||
|
.files
|
||||||
|
.iter()
|
||||||
|
.filter_map(|name| {
|
||||||
|
let path = base_dir.join(format!("{}.tera", name));
|
||||||
|
match std::fs::read_to_string(&path) {
|
||||||
|
Ok(content) => Some((name.clone(), content)),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Skipping {}: {}", path.display(), e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_context_from_specification(spec: &Specification) -> tera::Context {
|
||||||
|
let mut context = tera::Context::new();
|
||||||
|
if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) {
|
||||||
|
for (key, val) in data_map {
|
||||||
|
context.insert(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_hostname(context: &tera::Context, spec: &Specification) -> String {
|
||||||
|
context
|
||||||
|
.get("hostname")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
spec.device
|
||||||
|
.file_stem()
|
||||||
|
.and_then(|n| n.to_str())
|
||||||
|
.unwrap_or("unknown")
|
||||||
|
.to_string()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
113
src/spec.rs
113
src/spec.rs
@@ -2,28 +2,27 @@ use crate::{LogLevel, info, verb};
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde_json::{Map, Value, json};
|
use serde_json::{Map, Value, json};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
|
||||||
use std::{env, fmt, fs};
|
use std::{env, fmt, fs};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
use yaml_serde;
|
use yaml_serde;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Specification {
|
pub struct Specification {
|
||||||
pub partitional: String,
|
pub partitional: PathBuf,
|
||||||
pub regional: String,
|
pub regional: PathBuf,
|
||||||
pub common: String,
|
pub common: PathBuf,
|
||||||
pub zonal: String,
|
pub zonal: PathBuf,
|
||||||
pub device: String,
|
pub device: PathBuf,
|
||||||
pub compiled: Value,
|
pub compiled: Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Specification {
|
impl Specification {
|
||||||
pub fn build(
|
pub fn build(
|
||||||
partitional: String,
|
partitional: PathBuf,
|
||||||
regional: String,
|
regional: PathBuf,
|
||||||
common: String,
|
common: PathBuf,
|
||||||
zonal: String,
|
zonal: PathBuf,
|
||||||
device: String,
|
device: PathBuf,
|
||||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let json_values: Vec<Value> = [&partitional, ®ional, &common, &zonal, &device]
|
let json_values: Vec<Value> = [&partitional, ®ional, &common, &zonal, &device]
|
||||||
.iter()
|
.iter()
|
||||||
@@ -31,7 +30,6 @@ impl Specification {
|
|||||||
.map(|content| yaml_serde::from_str::<Value>(&content))
|
.map(|content| yaml_serde::from_str::<Value>(&content))
|
||||||
.collect::<Result<Vec<Value>, _>>()?;
|
.collect::<Result<Vec<Value>, _>>()?;
|
||||||
|
|
||||||
// Merge all objects under a single key
|
|
||||||
let merged_map: Map<String, Value> = json_values
|
let merged_map: Map<String, Value> = json_values
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|v| v.as_object().cloned())
|
.filter_map(|v| v.as_object().cloned())
|
||||||
@@ -39,21 +37,20 @@ impl Specification {
|
|||||||
.flatten()
|
.flatten()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Create the final merged object
|
|
||||||
let compiled = json!({"data": merged_map});
|
let compiled = json!({"data": merged_map});
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
partitional: String::from(partitional),
|
partitional,
|
||||||
regional: String::from(regional),
|
regional,
|
||||||
common: String::from(common),
|
common,
|
||||||
zonal: String::from(zonal),
|
zonal,
|
||||||
device: String::from(device),
|
device,
|
||||||
compiled,
|
compiled,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_layer(&self) -> String {
|
pub fn get_layer(&self) -> String {
|
||||||
PathBuf::from(&self.device)
|
self.device
|
||||||
.parent()
|
.parent()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.file_name()
|
.file_name()
|
||||||
@@ -70,13 +67,18 @@ impl fmt::Display for Specification {
|
|||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"Specification:\nPartitional: {}\nRegional: {}\nCommon: {}\nZonal: {}\nDevice: {}\nCompiled:\n{}",
|
"Specification:\nPartitional: {}\nRegional: {}\nCommon: {}\nZonal: {}\nDevice: {}\nCompiled:\n{}",
|
||||||
self.partitional, self.regional, self.common, self.zonal, self.device, self.compiled
|
self.partitional.display(),
|
||||||
|
self.regional.display(),
|
||||||
|
self.common.display(),
|
||||||
|
self.zonal.display(),
|
||||||
|
self.device.display(),
|
||||||
|
self.compiled
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specification> {
|
pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specification> {
|
||||||
let spec_list: Vec<String> = filter_specs(pattern, get_specs(spec_path, dbg));
|
let spec_list: Vec<PathBuf> = filter_specs(pattern, get_specs(spec_path, dbg));
|
||||||
info!(
|
info!(
|
||||||
dbg,
|
dbg,
|
||||||
"Matched {} devices against '{}'",
|
"Matched {} devices against '{}'",
|
||||||
@@ -84,14 +86,14 @@ pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specif
|
|||||||
&pattern
|
&pattern
|
||||||
);
|
);
|
||||||
for spec in &spec_list {
|
for spec in &spec_list {
|
||||||
verb!(dbg, " | {}", spec);
|
verb!(dbg, " | {}", spec.display());
|
||||||
}
|
}
|
||||||
let mut specifications: Vec<Specification> = Vec::new();
|
let mut specifications: Vec<Specification> = Vec::new();
|
||||||
for spec in spec_list {
|
for spec in spec_list {
|
||||||
let zonal: String = get_zonal(&spec);
|
let zonal = get_zonal(&spec);
|
||||||
let common: String = get_common(&spec);
|
let common = get_common(&spec);
|
||||||
let regional: String = get_regional(&spec);
|
let regional = get_regional(&spec);
|
||||||
let partitional: String = get_partional(&common, ®ional);
|
let partitional = get_partitional(&common, ®ional);
|
||||||
specifications.push(
|
specifications.push(
|
||||||
match Specification::build(partitional, regional, common, zonal, spec) {
|
match Specification::build(partitional, regional, common, zonal, spec) {
|
||||||
Ok(compiled_spec) => compiled_spec,
|
Ok(compiled_spec) => compiled_spec,
|
||||||
@@ -102,25 +104,25 @@ pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specif
|
|||||||
specifications
|
specifications
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_partional(common: &str, regional: &str) -> String {
|
fn get_partitional(common: &PathBuf, regional: &PathBuf) -> PathBuf {
|
||||||
let re = Regex::new(r"partition: ([^\n\r$]+)").expect("failed");
|
let re = Regex::new(r"partition: ([^\n\r$]+)").expect("failed");
|
||||||
|
|
||||||
[common, regional]
|
[common, regional]
|
||||||
.iter()
|
.iter()
|
||||||
.find_map(|&path| {
|
.find_map(|path| {
|
||||||
fs::read_to_string(path).ok().and_then(|contents| {
|
fs::read_to_string(path).ok().and_then(|contents| {
|
||||||
re.captures(&contents).and_then(|captures| {
|
re.captures(&contents).and_then(|captures| {
|
||||||
captures
|
captures.get(1).map(|matched| {
|
||||||
.get(1)
|
PathBuf::from(format!("./spec/common/{}.yaml", matched.as_str().trim()))
|
||||||
.map(|matched| format!("./spec/common/{}.yaml", matched.as_str().trim()))
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|| format!("./spec/common/default.yaml"))
|
.unwrap_or_else(|| PathBuf::from("./spec/common/default.yaml"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_regional(path: &str) -> String {
|
fn get_regional(path: &PathBuf) -> PathBuf {
|
||||||
let dir_name = PathBuf::from(path)
|
let dir_name = path
|
||||||
.parent()
|
.parent()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.file_name()
|
.file_name()
|
||||||
@@ -128,59 +130,48 @@ fn get_regional(path: &str) -> String {
|
|||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.into_owned();
|
.into_owned();
|
||||||
|
|
||||||
let new_path = PathBuf::from(path)
|
path.parent()
|
||||||
.parent()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.parent()
|
.parent()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.join("common")
|
.join("common")
|
||||||
.join(format!("{}.yaml", &dir_name[0..2]));
|
.join(format!("{}.yaml", &dir_name[0..2]))
|
||||||
|
|
||||||
String::from(new_path.to_str().unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_common(spec: &str) -> String {
|
fn get_common(spec: &PathBuf) -> PathBuf {
|
||||||
let path: PathBuf = PathBuf::from_str(&spec).expect("Path was invalid");
|
spec.with_file_name("common.yaml")
|
||||||
String::from(path.with_file_name("common.yaml").to_str().unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_zonal(spec: &str) -> String {
|
fn get_zonal(spec: &PathBuf) -> PathBuf {
|
||||||
let path: PathBuf = PathBuf::from_str(&spec).expect("Path was invalid");
|
let zone = spec
|
||||||
|
.file_name()
|
||||||
|
.and_then(|f| f.to_str())
|
||||||
|
.and_then(|f| f.split_once('-'))
|
||||||
|
.map(|(zone, _)| spec.with_file_name(zone))
|
||||||
|
.unwrap_or_else(|| spec.with_file_name("common.yaml"));
|
||||||
|
|
||||||
String::from(format!(
|
PathBuf::from(format!("{}.common.yaml", zone.to_str().unwrap()))
|
||||||
"{}.common.yaml",
|
|
||||||
path.file_name()
|
|
||||||
.and_then(|filename| filename.to_str())
|
|
||||||
.and_then(|filename| filename.split_once('-'))
|
|
||||||
.map(|(zone, _)| path.with_file_name(zone))
|
|
||||||
.unwrap_or_else(|| path.with_file_name("common.yaml"))
|
|
||||||
.to_str()
|
|
||||||
.unwrap()
|
|
||||||
.to_string(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter_specs(pattern: &Regex, spec_list: Vec<String>) -> Vec<String> {
|
fn filter_specs(pattern: &Regex, spec_list: Vec<PathBuf>) -> Vec<PathBuf> {
|
||||||
spec_list
|
spec_list
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|spec| {
|
.filter(|spec| {
|
||||||
Path::new(spec)
|
spec.file_name()
|
||||||
.file_name()
|
|
||||||
.and_then(|name| name.to_str())
|
.and_then(|name| name.to_str())
|
||||||
.map_or(false, |name| pattern.is_match(name))
|
.map_or(false, |name| pattern.is_match(name))
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_specs(spec_path: &String, dbg: LogLevel) -> Vec<String> {
|
fn get_specs(spec_path: &String, dbg: LogLevel) -> Vec<PathBuf> {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
let root = Path::new(&spec_path);
|
let root = Path::new(&spec_path);
|
||||||
for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
|
for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
|
||||||
if let Ok(path) = entry.path().strip_prefix(root) {
|
if let Ok(path) = entry.path().strip_prefix(root) {
|
||||||
let path_str = path.to_string_lossy().to_string();
|
let path_str = path.to_string_lossy().to_string();
|
||||||
|
|
||||||
if !path_str.contains("common") && path_str.ends_with("yaml") {
|
if !path_str.contains("common") && path_str.ends_with("yaml") {
|
||||||
result.push(format!("{}/{}", spec_path, path_str));
|
result.push(root.join(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user