From 503d2dd5d601aaf059d9f1fe0e5006d6b2606b32 Mon Sep 17 00:00:00 2001 From: rskntroot Date: Mon, 17 Feb 2025 01:06:57 +0000 Subject: [PATCH] init commit --- .gitignore | 1 + Cargo.lock | 1181 +++++++++++++++++ Cargo.toml | 14 + README.md | 28 + demo/spec/common/eu.yaml | 2 + demo/spec/common/us.yaml | 2 + demo/spec/xyz/common/ex.yaml | 2 + demo/spec/xyz/ex-edge-r1/common.yaml | 2 + .../xyz/ex-edge-r1/xyz1-ex-edge-r101.yaml | 2 + .../xyz/ex-edge-r1/xyz1-ex-edge-r102.yaml | 2 + demo/spec/xyz/ex-edge-r1/xyz1.common.yaml | 2 + .../xyz/ex-edge-r1/xyz2-ex-edge-r101.yaml | 2 + .../xyz/ex-edge-r1/xyz2-ex-edge-r102.yaml | 2 + demo/spec/xyz/ex-edge-r1/xyz2.common.yaml | 2 + demo/spec/xyz/ex-edge-r2/common.yaml | 3 + .../xyz/ex-edge-r2/xyz1-ex-edge-r201.yaml | 2 + .../xyz/ex-edge-r2/xyz1-ex-edge-r202.yaml | 2 + demo/spec/xyz/ex-edge-r2/xyz1.common.yaml | 2 + .../xyz/ex-edge-r2/xyz2-ex-edge-r201.yaml | 2 + .../xyz/ex-edge-r2/xyz2-ex-edge-r202.yaml | 2 + demo/spec/xyz/ex-edge-r2/xyz2.common.yaml | 2 + demo/tmpl/common/ex/system.tmpl | 4 + demo/tmpl/ex-edge-r/chassis.tmpl | 2 + demo/tmpl/ex-edge-r/interfaces.tmpl | 2 + demo/tmpl/ex-edge-r/protocols.tmpl | 2 + demo/tmpl/ex-edge-r/structure.yaml | 5 + demo/tmpl/ex-edge-r/system.tmpl | 1 + src/cli.rs | 130 ++ src/log.rs | 78 ++ src/main.rs | 31 + src/specs.rs | 232 ++++ src/tmpls.rs | 56 + 32 files changed, 1802 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 demo/spec/common/eu.yaml create mode 100644 demo/spec/common/us.yaml create mode 100644 demo/spec/xyz/common/ex.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/common.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r101.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r102.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/xyz1.common.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r101.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r102.yaml create mode 100644 demo/spec/xyz/ex-edge-r1/xyz2.common.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/common.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r201.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r202.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/xyz1.common.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r201.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r202.yaml create mode 100644 demo/spec/xyz/ex-edge-r2/xyz2.common.yaml create mode 100644 demo/tmpl/common/ex/system.tmpl create mode 100644 demo/tmpl/ex-edge-r/chassis.tmpl create mode 100644 demo/tmpl/ex-edge-r/interfaces.tmpl create mode 100644 demo/tmpl/ex-edge-r/protocols.tmpl create mode 100644 demo/tmpl/ex-edge-r/structure.yaml create mode 120000 demo/tmpl/ex-edge-r/system.tmpl create mode 100644 src/cli.rs create mode 100644 src/log.rs create mode 100644 src/main.rs create mode 100644 src/specs.rs create mode 100644 src/tmpls.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6162762 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1181 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bstr" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531a9155a481e2ee699d4f98f43c0ca4ff8ee1bfd55c31e9e98fb29d2b176fe0" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "bumpalo" +version = "3.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cc" +version = "1.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "windows-targets", +] + +[[package]] +name = "chrono-tz" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93698b29de5e97ad0ae26447b344c482a7284c737d9ddc5f9e52b74a336671bb" +dependencies = [ + "chrono", + "chrono-tz-build", + "phf", +] + +[[package]] +name = "chrono-tz-build" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c088aee841df9c3041febbb73934cfc39708749bf96dc827e3359cd39ef11b1" +dependencies = [ + "parse-zoneinfo", + "phf", + "phf_codegen", +] + +[[package]] +name = "clap" +version = "4.5.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acebd8ad879283633b343856142139f2da2317c96b05b4dd6181c61e2480184" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ba32cbda51c7e1dfd49acc1457ba1a7dec5b64fe360e828acb13ca8dc9c2f9" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "deunicode" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00" + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.13.3+wasi-0.2.2", + "windows-targets", +] + +[[package]] +name = "globset" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "globwalk" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf760ebf69878d9fd8f110c89703d90ce35095324d1f1edcb595c63945ee757" +dependencies = [ + "bitflags", + "ignore", + "walkdir", +] + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "humansize" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" +dependencies = [ + "libm", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ignore" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata", + "same-file", + "walkdir", + "winapi-util", +] + +[[package]] +name = "indexmap" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "libm" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" + +[[package]] +name = "libyml" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e281a65eeba3d4503a2839252f86374528f9ceafe6fed97c1d3b52e1fb625c1" + +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "log" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + +[[package]] +name = "parse-zoneinfo" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24" +dependencies = [ + "regex", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pest" +version = "2.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b7cafe60d6cf8e62e1b9b2ea516a089c008945bb5a275416789e7db0bc199dc" +dependencies = [ + "memchr", + "thiserror 2.0.11", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "816518421cfc6887a0d62bf441b6ffb4536fcc926395a69e1a85852d4363f57e" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d1396fd3a870fc7838768d171b4616d5c91f6cc25e377b673d714567d99377b" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1e58089ea25d717bfd31fb534e4f3afcc2cc569c70de3e239778991ea3b7dea" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + +[[package]] +name = "phf" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_codegen" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.15", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + +[[package]] +name = "ryu" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.217" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.217" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.138" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_yml" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ce6afeda22f0b55dde2c34897bce76a629587348480384231205c14b59a01f" +dependencies = [ + "indexmap", + "itoa", + "libyml", + "log", + "memchr", + "ryu", + "serde", + "serde_json", + "tempfile", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + +[[package]] +name = "skyforge" +version = "0.1.0" +dependencies = [ + "clap", + "regex", + "serde", + "serde_json", + "serde_yml", + "tera", + "thiserror 1.0.69", + "walkdir", +] + +[[package]] +name = "slug" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "882a80f72ee45de3cc9a5afeb2da0331d58df69e4e7d8eeb5d3c7784ae67e724" +dependencies = [ + "deunicode", + "wasm-bindgen", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom 0.3.1", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "tera" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab9d851b45e865f178319da0abdbfe6acbc4328759ff18dafc3a41c16b4cd2ee" +dependencies = [ + "chrono", + "chrono-tz", + "globwalk", + "humansize", + "lazy_static", + "percent-encoding", + "pest", + "pest_derive", + "rand", + "regex", + "serde", + "serde_json", + "slug", + "unic-segment", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" +dependencies = [ + "thiserror-impl 2.0.11", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "ucd-trie" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" + +[[package]] +name = "unic-char-property" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8c57a407d9b6fa02b4795eb81c5b6652060a15a7903ea981f3d723e6c0be221" +dependencies = [ + "unic-char-range", +] + +[[package]] +name = "unic-char-range" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0398022d5f700414f6b899e10b8348231abf9173fa93144cbc1a43b9793c1fbc" + +[[package]] +name = "unic-common" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d7ff825a6a654ee85a63e80f92f054f904f21e7d12da4e22f9834a4aaa35bc" + +[[package]] +name = "unic-segment" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4ed5d26be57f84f176157270c112ef57b86debac9cd21daaabbe56db0f88f23" +dependencies = [ + "unic-ucd-segment", +] + +[[package]] +name = "unic-ucd-segment" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2079c122a62205b421f499da10f3ee0f7697f012f55b675e002483c73ea34700" +dependencies = [ + "unic-char-property", + "unic-char-range", + "unic-ucd-version", +] + +[[package]] +name = "unic-ucd-version" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96bd2f2237fe450fcd0a1d2f5f4e91711124f7857ba2e964247776ebeeb7b0c4" +dependencies = [ + "unic-common", +] + +[[package]] +name = "unicode-ident" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c7ff805 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "skyforge" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.11" } +regex = "1.10.5" +serde = { version = "1.0", features = ["derive"] } +serde_yml = "0.0.10" +serde_json = "1.0.112" +tera = "1.20.0" +thiserror = "1.0" +walkdir = "2.3" diff --git a/README.md b/README.md new file mode 100644 index 0000000..f195902 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# Skyforge + +## Brief + +Skyforge was designed to assist in render thousands of device configurations across the globe. + +## Assumptions + +- Partitions are groups of regions +- Regions are groups of zones +- Zones are groups of devices +- Layers are groups of common devices and facilitate template mapping + +## Functionality + +Skyforge takes a user provide regex pattern, performs a walk on a `./spec` dir, and matches a list of devices specifications that do not have the word "common" in their path. + All group files are labeled with common and mappable from the file itself. + +For each file_path matched, Skyforge then maps to all consituent files: + +- Layer - from by the common file in parent dir and maps the region +- Zonal - from the first group of chars in filename up to a `-` (region_id + zone_id) +- Regional - from the region_id of containing folder +- Partitional - from either layer (common.yaml) or regional yaml + +Once all files are found, a compiled_specifcation is built. + This spec is then passed to Tera as context. + Tera then loads the template files for that layer and renders the configuration file. diff --git a/demo/spec/common/eu.yaml b/demo/spec/common/eu.yaml new file mode 100644 index 0000000..9e3e9aa --- /dev/null +++ b/demo/spec/common/eu.yaml @@ -0,0 +1,2 @@ +partitional: + location: european union diff --git a/demo/spec/common/us.yaml b/demo/spec/common/us.yaml new file mode 100644 index 0000000..22d3aa2 --- /dev/null +++ b/demo/spec/common/us.yaml @@ -0,0 +1,2 @@ +partitional: + location: united states diff --git a/demo/spec/xyz/common/ex.yaml b/demo/spec/xyz/common/ex.yaml new file mode 100644 index 0000000..212095c --- /dev/null +++ b/demo/spec/xyz/common/ex.yaml @@ -0,0 +1,2 @@ +regional: + partition: us diff --git a/demo/spec/xyz/ex-edge-r1/common.yaml b/demo/spec/xyz/ex-edge-r1/common.yaml new file mode 100644 index 0000000..dd23900 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/common.yaml @@ -0,0 +1,2 @@ +common: + layer: ex-edge-r diff --git a/demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r101.yaml b/demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r101.yaml new file mode 100644 index 0000000..cb67c3a --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r101.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz1-ex-edge-r101 diff --git a/demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r102.yaml b/demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r102.yaml new file mode 100644 index 0000000..b768551 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/xyz1-ex-edge-r102.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz1-ex-edge-r102 diff --git a/demo/spec/xyz/ex-edge-r1/xyz1.common.yaml b/demo/spec/xyz/ex-edge-r1/xyz1.common.yaml new file mode 100644 index 0000000..043a24c --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/xyz1.common.yaml @@ -0,0 +1,2 @@ +zonal: + az: 1 diff --git a/demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r101.yaml b/demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r101.yaml new file mode 100644 index 0000000..290fbff --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r101.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz2-ex-edge-r101 diff --git a/demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r102.yaml b/demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r102.yaml new file mode 100644 index 0000000..265a1ac --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/xyz2-ex-edge-r102.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz2-ex-edge-r102 diff --git a/demo/spec/xyz/ex-edge-r1/xyz2.common.yaml b/demo/spec/xyz/ex-edge-r1/xyz2.common.yaml new file mode 100644 index 0000000..9770d50 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r1/xyz2.common.yaml @@ -0,0 +1,2 @@ +zonal: + az: 2 diff --git a/demo/spec/xyz/ex-edge-r2/common.yaml b/demo/spec/xyz/ex-edge-r2/common.yaml new file mode 100644 index 0000000..71770a4 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/common.yaml @@ -0,0 +1,3 @@ +common: + partition: eu + layer: ex-edge-r diff --git a/demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r201.yaml b/demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r201.yaml new file mode 100644 index 0000000..628c83c --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r201.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz1-ex-edge-r201 diff --git a/demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r202.yaml b/demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r202.yaml new file mode 100644 index 0000000..247be03 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/xyz1-ex-edge-r202.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz1-ex-edge-r202 diff --git a/demo/spec/xyz/ex-edge-r2/xyz1.common.yaml b/demo/spec/xyz/ex-edge-r2/xyz1.common.yaml new file mode 100644 index 0000000..043a24c --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/xyz1.common.yaml @@ -0,0 +1,2 @@ +zonal: + az: 1 diff --git a/demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r201.yaml b/demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r201.yaml new file mode 100644 index 0000000..67217b6 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r201.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz2-ex-edge-r201 diff --git a/demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r202.yaml b/demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r202.yaml new file mode 100644 index 0000000..43123bb --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/xyz2-ex-edge-r202.yaml @@ -0,0 +1,2 @@ +device: + hostname: xyz2-ex-edge-r202 diff --git a/demo/spec/xyz/ex-edge-r2/xyz2.common.yaml b/demo/spec/xyz/ex-edge-r2/xyz2.common.yaml new file mode 100644 index 0000000..9770d50 --- /dev/null +++ b/demo/spec/xyz/ex-edge-r2/xyz2.common.yaml @@ -0,0 +1,2 @@ +zonal: + az: 2 diff --git a/demo/tmpl/common/ex/system.tmpl b/demo/tmpl/common/ex/system.tmpl new file mode 100644 index 0000000..72148e5 --- /dev/null +++ b/demo/tmpl/common/ex/system.tmpl @@ -0,0 +1,4 @@ +system { + hostname {{ hostname }}; + location "{{ location }}"; +} diff --git a/demo/tmpl/ex-edge-r/chassis.tmpl b/demo/tmpl/ex-edge-r/chassis.tmpl new file mode 100644 index 0000000..deeb9e9 --- /dev/null +++ b/demo/tmpl/ex-edge-r/chassis.tmpl @@ -0,0 +1,2 @@ +chassis { +} diff --git a/demo/tmpl/ex-edge-r/interfaces.tmpl b/demo/tmpl/ex-edge-r/interfaces.tmpl new file mode 100644 index 0000000..31f4c74 --- /dev/null +++ b/demo/tmpl/ex-edge-r/interfaces.tmpl @@ -0,0 +1,2 @@ +interfaces { +} diff --git a/demo/tmpl/ex-edge-r/protocols.tmpl b/demo/tmpl/ex-edge-r/protocols.tmpl new file mode 100644 index 0000000..162b1d4 --- /dev/null +++ b/demo/tmpl/ex-edge-r/protocols.tmpl @@ -0,0 +1,2 @@ +protocols { +} diff --git a/demo/tmpl/ex-edge-r/structure.yaml b/demo/tmpl/ex-edge-r/structure.yaml new file mode 100644 index 0000000..c90dae7 --- /dev/null +++ b/demo/tmpl/ex-edge-r/structure.yaml @@ -0,0 +1,5 @@ +files: + - system + - chassis + - interfaces + - protocols diff --git a/demo/tmpl/ex-edge-r/system.tmpl b/demo/tmpl/ex-edge-r/system.tmpl new file mode 120000 index 0000000..c2d3b1e --- /dev/null +++ b/demo/tmpl/ex-edge-r/system.tmpl @@ -0,0 +1 @@ +../common/ex/system.tmpl \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..32c4c5d --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,130 @@ +use crate::log::LogLevel; +use clap::{Arg, ArgAction, ArgGroup, Command}; +use regex::Regex; +use std::fmt; + +#[derive(Debug)] +pub struct Args { + pub devices: Regex, + pub loglevel: LogLevel, + pub env: EnvVars, +} + +impl fmt::Display for Args { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "devices: {}, loglevel: {}, env: {}", + self.devices, self.loglevel, self.env + ) + } +} + +#[derive(Debug)] +pub struct EnvVars { + pub spec_path: String, + pub tmpl_path: String, + pub out_path: String, + pub log_path: String, +} + +impl fmt::Display for EnvVars { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "spec_path: {}, tmpl_path: {}, out_path: {}, log_path: {}", + self.spec_path, self.tmpl_path, self.out_path, self.log_path + ) + } +} + +/// loads `command line arguments` and `environment variables` using custom `clap` +pub fn parse_args() -> Args { + let matches: clap::ArgMatches = build().get_matches(); + + let raw_devices = matches.get_one::("devices").unwrap().to_string(); + let devices: Regex = + Regex::new(&raw_devices).expect("Invalid regex pattern provided for `devices`"); + + let loglevel: LogLevel = match matches.get_flag("debug") { + true => LogLevel::Debug, + false => match matches.get_flag("verbose") { + true => LogLevel::Verbose, + false => LogLevel::Info, + }, + }; + + let env: EnvVars = parse_env(); + + Args { + devices, + loglevel, + env, + } +} + +/// loads only environment variables +pub fn parse_env() -> EnvVars { + EnvVars { + spec_path: match std::env::var("SF_SPEC_PATH") { + Ok(path) => path.trim_end_matches('/').to_string(), + Err(_) => String::from("./spec"), + }, + tmpl_path: match std::env::var("SF_TMPL_PATH") { + Ok(path) => path.trim_end_matches('/').to_string(), + Err(_) => String::from("./tmpl"), + }, + out_path: match std::env::var("SF_OUT_PATH") { + Ok(path) => path.trim_end_matches('/').to_string(), + Err(_) => String::from("./out"), + }, + log_path: match std::env::var("SF_LOG_PATH") { + Ok(path) => path.trim_end_matches('/').to_string(), + Err(_) => String::from("./log"), + }, + } +} + +const ABOUT_MSG: &str = r#"Skyforge Config Generation Engine"#; +const ENV_MSG: &str = r#"Environment: + SF_SPEC_PATH Path to the directory containing templates. Defaults to "./spec". + SF_TMPL_PATH Path to the directory containing specifications. Defaults to "./tmpl". + SF_OUT_PATH Path to the directory for command output. Defaults to "./out". + SF_LOG_PATH Path to the directory for log output. Defaults to "./log". +"#; + +/// builds a custom command line argument parser +fn build() -> Command { + Command::new("app") + .about(ABOUT_MSG) + .version(env!("CARGO_PKG_VERSION")) + .author("rskntroot") + .arg( + Arg::new("devices") + .long("devices") + .short('d') + .help("A regular expression pattern") + .required(true), + ) + .arg( + Arg::new("debug") + .long("debug") + .help("Print debug information") + .action(ArgAction::SetTrue) + .required(false), + ) + .arg( + Arg::new("verbose") + .short('v') + .long("verbose") + .help("Print verbose information") + .action(ArgAction::SetTrue) + .required(false), + ) + .group( + ArgGroup::new("loglevel") + .args(&["debug", "verbose"]) + .required(false), + ) + .after_help(ENV_MSG) +} diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 0000000..439d545 --- /dev/null +++ b/src/log.rs @@ -0,0 +1,78 @@ +#![allow(dead_code)] + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum LogLevel { + Debug, + Verbose, + Info, + Warning, + Critical, + None, +} + +impl LogLevel { + pub fn value(&self) -> u8 { + match self { + LogLevel::Debug => u8::MIN, + LogLevel::Verbose => u8::from(30), + LogLevel::Info => u8::from(60), + LogLevel::Warning => u8::from(90), + LogLevel::Critical => u8::from(120), + LogLevel::None => u8::MAX, + } + } +} + +impl std::fmt::Display for LogLevel { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[macro_export] +macro_rules! info { + ($current_level:expr, $($msg:expr),*) => { + if LogLevel::Info.value() >= $current_level.value() { + println!($($msg),*); + } + }; +} + +#[macro_export] +macro_rules! verb { + ($current_level:expr, $($msg:expr),*) => { + if LogLevel::Verbose.value() >= $current_level.value() { + println!($($msg),*); + } + }; +} + +#[macro_export] +macro_rules! dbug { + ($current_level:expr, $($msg:expr),*) => { + if LogLevel::Debug.value() >= $current_level.value() { + let message = format!($($msg),*); + for line in message.lines() { + println!(" |D| {}", line); + } + } + }; +} + +#[macro_export] +macro_rules! warn { + ($current_level:expr, $($msg:expr),*) => { + if LogLevel::Warning.value() >= $current_level.value() { + eprintln!($($msg),*); + } + }; +} + +#[macro_export] +macro_rules! crit { + ($current_level:expr, $($msg:expr),*) => { + if LogLevel::Critical.value() >= $current_level.value() { + eprintln!($($msg),*); + } + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d7c35ca --- /dev/null +++ b/src/main.rs @@ -0,0 +1,31 @@ +mod cli; +mod log; +mod specs; +mod tmpls; + +use log::LogLevel; + +fn main() { + let args: cli::Args = cli::parse_args(); + let dbg: LogLevel = args.loglevel; + + dbug!(dbg, "{}", &args); + + let specifications: Vec = + specs::compile(&args.devices, &args.env.spec_path, dbg); + + for spec in specifications { + let result = tmpls::process_templates(&spec, dbg).ok().unwrap(); + verb!( + dbg, + "Compiled Spec:\n{}", + serde_json::to_string_pretty(&spec.compiled).unwrap() + ); + info!(dbg, "Rendered Config:"); + for line in result { + if line != "\n" { + print!("{}", line) + } + } + } +} diff --git a/src/specs.rs b/src/specs.rs new file mode 100644 index 0000000..2da75c9 --- /dev/null +++ b/src/specs.rs @@ -0,0 +1,232 @@ +use crate::{info, verb, LogLevel}; +use regex::Regex; +use serde_json::{json, Map, Value}; +use serde_yml; +use std::path::{Path, PathBuf}; +use std::str::FromStr; +use std::{env, fmt, fs}; +use walkdir::WalkDir; + +#[derive(Debug)] +pub struct Specification { + pub partitional: String, + pub regional: String, + pub common: String, + pub zonal: String, + pub device: String, + pub compiled: Value, +} + +impl Specification { + pub fn build( + partitional: &str, + regional: &str, + common: &str, + zonal: &str, + device: &str, + ) -> Result> { + // Read all YAML files + let yaml_contents: Vec = [partitional, regional, common, zonal, device] + .iter() + .filter_map(|path| fs::read_to_string(path).ok()) + .collect(); + + // Convert each YAML to JSON Value and collect them + let json_values: Vec = yaml_contents + .iter() + .map(|content| serde_yml::from_str(content)) + .collect::, _>>()?; + + // Merge all objects under a single key + let mut merged_map = Map::new(); + for value in json_values { + if let Some(obj) = value.as_object() { + for (_key, value) in obj { + if let Some(inner_obj) = value.as_object() { + for (k, v) in inner_obj { + merged_map.insert(k.clone(), v.clone()); + } + } + } + } + } + + // Create the final merged object + let compiled = json!({"data": merged_map}); + + Ok(Self { + partitional: String::from(partitional), + regional: String::from(regional), + common: String::from(common), + zonal: String::from(zonal), + device: String::from(device), + compiled, + }) + } + + pub fn get_layer(&self) -> String { + PathBuf::from(&self.device) + .parent() + .unwrap() + .file_name() + .unwrap() + .to_string_lossy() + .chars() + .filter(|c| !c.is_numeric()) + .collect::() + } + + pub fn get_hostname(&self) -> String { + PathBuf::from(&self.device) + .file_name() + .unwrap() + .to_string_lossy() + .into_owned() + } +} + +impl fmt::Display for Specification { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "Specification:\n\ + Partitional: {}\n\ + Regional: {}\n\ + Common: {}\n\ + Zonal: {}\n\ + Device: {}\n\ + Compiled:\n{}", + self.partitional, self.regional, self.common, self.zonal, self.device, self.compiled + ) + } +} + +pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec { + let spec_list: Vec = filter_specs(pattern, get_specs(spec_path, dbg)); + info!( + dbg, + "Matched {} devices against '{}'", + &spec_list.len(), + &pattern + ); + for spec in &spec_list { + verb!(dbg, " | {}", spec); + } + let mut specifications: Vec = Vec::new(); + for spec in spec_list { + let zonal: String = get_zonal(&spec); + let common: String = get_common(&spec); + let regional: String = get_regional(&spec); + let partitional: String = get_partional(&common, ®ional); + specifications.push( + match Specification::build(&partitional, ®ional, &common, &zonal, &spec) { + Ok(compiled_spec) => { + verb!( + dbg, + "Compiled Spec for '{}'\n | {}\n | {}\n | {}\n | {}\n | {}", + compiled_spec.get_hostname(), + compiled_spec.partitional, + compiled_spec.regional, + compiled_spec.common, + compiled_spec.zonal, + compiled_spec.device + ); + compiled_spec + } + Err(e) => panic!("failed to build Specification: {}", e), + }, + ) + } + specifications +} + +fn get_partional(common: &str, regional: &str) -> String { + let re = Regex::new(r"partition: ([^\n\r$]+)").expect("failed"); + + [common, regional] + .iter() + .find_map(|&path| { + fs::read_to_string(path).ok().and_then(|contents| { + re.captures(&contents).and_then(|captures| { + captures + .get(1) + .map(|matched| format!("./spec/common/{}.yaml", matched.as_str().trim())) + }) + }) + }) + .unwrap_or_else(|| format!("./spec/common/default.yaml")) +} + +fn get_regional(path: &str) -> String { + let dir_name = PathBuf::from(path) + .parent() + .unwrap() + .file_name() + .unwrap() + .to_string_lossy() + .into_owned(); + + let new_path = PathBuf::from(path) + .parent() + .unwrap() + .parent() + .unwrap() + .join("common") + .join(format!("{}.yaml", &dir_name[0..2])); + + String::from(new_path.to_str().unwrap()) +} + +fn get_common(spec: &str) -> String { + let path: PathBuf = PathBuf::from_str(&spec).expect("Path was invalid"); + String::from(path.with_file_name("common.yaml").to_str().unwrap()) +} + +fn get_zonal(spec: &str) -> String { + let path: PathBuf = PathBuf::from_str(&spec).expect("Path was invalid"); + + String::from(format!( + "{}.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) -> Vec { + spec_list + .into_iter() + .filter(|spec| { + Path::new(spec) + .file_name() + .and_then(|name| name.to_str()) + .map_or(false, |name| pattern.is_match(name)) + }) + .collect() +} + +fn get_specs(spec_path: &String, dbg: LogLevel) -> Vec { + let mut result = Vec::new(); + let root = Path::new(&spec_path); + for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) { + if let Ok(path) = entry.path().strip_prefix(root) { + let path_str = path.to_string_lossy().to_string(); + + if !path_str.contains("common") && path_str.ends_with("yaml") { + result.push(format!("{}/{}", spec_path, path_str)); + } + } + } + info!( + dbg, + "Skyforge found {} renderable devices in {}", + result.len(), + env::current_dir().unwrap().display() + ); + result +} diff --git a/src/tmpls.rs b/src/tmpls.rs new file mode 100644 index 0000000..6976d26 --- /dev/null +++ b/src/tmpls.rs @@ -0,0 +1,56 @@ +use crate::specs::Specification; +use crate::{verb, LogLevel}; +use serde_json::Value; +use serde_yml; +use std::fs; +use tera::{Context, Tera}; + +#[derive(serde::Deserialize)] +struct TemplateConfig { + files: Vec, +} + +pub fn process_templates( + spec: &Specification, + dbg: LogLevel, +) -> Result, Box> { + // Create Tera context from spec.compiled + let mut context = Context::new(); + if let Value::Object(map) = &spec.compiled { + if let Some(Value::Object(data_map)) = map.get("data") { + for (key, value) in data_map { + context.insert(key, value); + } + } + } + + // Get the base directory path + let base_dir = format!("./tmpl/{}", spec.get_layer()); + + // Read structure.yaml + let structure_path = format!("{}/structure.yaml", base_dir); + let structure_content = fs::read_to_string(&structure_path)?; + let config: TemplateConfig = serde_yml::from_str(&structure_content)?; + + // Initialize Tera with the specific directory + let mut tera = Tera::default(); + + // Process each template + verb!(dbg, "Rendering Templates"); + let mut rendered_templates = Vec::new(); + for template_name in config.files { + // Read the template file directly + let template_path = format!("{}/{}.tmpl", base_dir, template_name); + verb!(dbg, " | {}", &template_path); + let template_content = fs::read_to_string(&template_path)?; + + // Add this specific template to Tera + tera.add_raw_template(&template_name, &template_content)?; + + // Render the template + let rendered = tera.render(&template_name, &context)?; + rendered_templates.push(rendered); + } + + Ok(rendered_templates) +}