From 50ab1a5b1406b61a84618b8a86a7aec25b9622af Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Fri, 1 Jan 2021 14:32:51 -0800 Subject: [PATCH] Restructuring the file tree to make it easier to start adding "native tasks" Some number of tasks I will want to be "builtin" and need to know a little bit more about the transport in order to make that work. The traits, they are coming. See #1 --- Cargo.lock | 13 +++++++----- cli/Cargo.toml | 10 ++-------- cli/src/main.rs | 20 ++++++++----------- model/Cargo.toml | 10 +++++++++- .../src/inventory/mod.rs | 0 model/src/lib.rs | 11 ++++++++-- model/src/{plan.rs => plan/mod.rs} | 0 model/src/{task.rs => task/mod.rs} | 0 model/src/tasks/mod.rs | 1 + {cli => model}/src/transport/mod.rs | 3 ++- {cli => model}/src/transport/ssh.rs | 4 ++-- 11 files changed, 41 insertions(+), 31 deletions(-) rename cli/src/inventory.rs => model/src/inventory/mod.rs (100%) rename model/src/{plan.rs => plan/mod.rs} (100%) rename model/src/{task.rs => task/mod.rs} (100%) create mode 100644 model/src/tasks/mod.rs rename {cli => model}/src/transport/mod.rs (95%) rename {cli => model}/src/transport/ssh.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 62342e9..f74e473 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -608,26 +608,29 @@ dependencies = [ [[package]] name = "zap-cli" -version = "0.1.1" +version = "0.2.0" dependencies = [ "colored", "gumdrop", "log", "pretty_env_logger", "serde", - "serde_derive", - "serde_json", "serde_yaml", - "ssh2", "zap-model", ] [[package]] name = "zap-model" -version = "0.1.1" +version = "0.2.0" dependencies = [ + "colored", "handlebars", "log", "pest", "pest_derive", + "serde", + "serde_derive", + "serde_json", + "serde_yaml", + "ssh2", ] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index b697117..d193f14 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zap-cli" -version = "0.1.1" +version = "0.2.0" authors = ["R. Tyler Croy "] edition = "2018" description = "A simple configuration management and orchestration tool" @@ -13,17 +13,11 @@ keywords = ["sysadmin", "management"] name = "zap" path = "src/main.rs" - [dependencies] colored = "2" gumdrop = "~0.8.0" log = "0.4" pretty_env_logger = "0.4" -# Needed for deserializing JSON messages _and_ managing our configuration -# effectively serde = { version = "~1.0", features = ["derive", "rc"] } -serde_derive = "~1.0" -serde_json = "~1.0" serde_yaml = "~0.8" -ssh2 = "~0.9.0" -zap-model = { version = "~0.1", path = "../model" } +zap-model = { version = "~0.2", path = "../model" } diff --git a/cli/src/main.rs b/cli/src/main.rs index 6abec8d..4850af4 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -5,13 +5,9 @@ use std::collections::HashMap; use std::io::BufReader; use std::path::PathBuf; -mod inventory; -mod transport; - -use crate::inventory::*; -use crate::transport::ssh::Ssh; -use zap_model::plan::Plan; -use zap_model::task::Task; +use zap_model::inventory::Inventory; +use zap_model::transport::ssh::Ssh; +use zap_model::{Plan, Task, Transport}; use zap_model::ExecutableTask; fn main() { @@ -29,7 +25,7 @@ fn main() { let inventory: Inventory = serde_yaml::from_reader(reader).expect("Failed to read intenvory"); let mut runner = match &inventory.config.transport { - crate::inventory::Transport::Ssh => Ssh::default(), + zap_model::inventory::Transport::Ssh => Ssh::default(), }; match opts.command.unwrap() { @@ -63,7 +59,7 @@ fn handle_check(opts: CheckOpts) { /** * This function will parse and execute a plan */ -fn handle_plan(opts: PlanOpts, runner: &mut dyn crate::transport::Transport, inventory: Inventory) { +fn handle_plan(opts: PlanOpts, runner: &mut dyn Transport, inventory: Inventory) { println!("{}", format!("Running plan with: {:?}", opts).green()); let mut exit: i32 = -1; @@ -91,7 +87,7 @@ fn handle_plan(opts: PlanOpts, runner: &mut dyn crate::transport::Transport, inv fn execute_task_on( targets: String, task: &ExecutableTask, - runner: &mut dyn crate::transport::Transport, + runner: &mut dyn Transport, inventory: &Inventory, dry_run: bool, ) -> i32 { @@ -109,7 +105,7 @@ fn execute_task_on( /** * This function will handle a task */ -fn handle_task(opts: TaskOpts, runner: &mut dyn crate::transport::Transport, inventory: Inventory) { +fn handle_task(opts: TaskOpts, runner: &mut dyn Transport, inventory: Inventory) { println!("{}", format!("Running task with: {:?}", opts).green()); match Task::from_path(&opts.task) { @@ -154,7 +150,7 @@ fn handle_task(opts: TaskOpts, runner: &mut dyn crate::transport::Transport, inv * In the case of multiple targets, any non-zero status code will be used to exit * non-zero. */ -fn handle_cmd(opts: CmdOpts, runner: &mut dyn crate::transport::Transport, inventory: Inventory) { +fn handle_cmd(opts: CmdOpts, runner: &mut dyn Transport, inventory: Inventory) { let mut task = ExecutableTask::new(Task::new("Dynamic"), HashMap::new()); task.task.script.inline = Some(opts.command); std::process::exit(execute_task_on( diff --git a/model/Cargo.toml b/model/Cargo.toml index 21bb9e6..813a64f 100644 --- a/model/Cargo.toml +++ b/model/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zap-model" -version = "0.1.1" +version = "0.2.0" authors = ["R. Tyler Croy "] edition = "2018" description = "Internal models for zap, a simple configuration management tool" @@ -10,7 +10,15 @@ license = "AGPL-3.0+" keywords = ["sysadmin", "management"] [dependencies] +colored = "2" handlebars = "~3.5" log = "0.4" pest = "~2.1" pest_derive = "~2.1" +# Needed for deserializing JSON messages _and_ managing our configuration +# effectively +serde = { version = "~1.0", features = ["derive", "rc"] } +serde_derive = "~1.0" +serde_json = "~1.0" +serde_yaml = "~0.8" +ssh2 = "~0.9.0" diff --git a/cli/src/inventory.rs b/model/src/inventory/mod.rs similarity index 100% rename from cli/src/inventory.rs rename to model/src/inventory/mod.rs diff --git a/model/src/lib.rs b/model/src/lib.rs index 2ef7024..ab49f55 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -5,8 +5,15 @@ extern crate pest_derive; use std::collections::HashMap; +pub mod inventory; pub mod plan; pub mod task; +pub mod tasks; +pub mod transport; + +pub use crate::transport::Transport; +pub use crate::plan::Plan; +pub use crate::task::Task; /** * An ExecutableTask is a light container over a Task execpt with user-provided information and is @@ -14,12 +21,12 @@ pub mod task; */ #[derive(Clone, Debug)] pub struct ExecutableTask { - pub task: task::Task, + pub task: Task, pub parameters: HashMap, } impl ExecutableTask { - pub fn new(task: task::Task, parameters: HashMap) -> Self { + pub fn new(task: Task, parameters: HashMap) -> Self { Self { task, parameters } } } diff --git a/model/src/plan.rs b/model/src/plan/mod.rs similarity index 100% rename from model/src/plan.rs rename to model/src/plan/mod.rs diff --git a/model/src/task.rs b/model/src/task/mod.rs similarity index 100% rename from model/src/task.rs rename to model/src/task/mod.rs diff --git a/model/src/tasks/mod.rs b/model/src/tasks/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/model/src/tasks/mod.rs @@ -0,0 +1 @@ + diff --git a/cli/src/transport/mod.rs b/model/src/transport/mod.rs similarity index 95% rename from cli/src/transport/mod.rs rename to model/src/transport/mod.rs index 42c09d5..948ec00 100644 --- a/cli/src/transport/mod.rs +++ b/model/src/transport/mod.rs @@ -1,6 +1,7 @@ +use crate::ExecutableTask; use crate::inventory::{Group, Inventory, Target}; + use std::path::Path; -use zap_model::ExecutableTask; pub mod ssh; diff --git a/cli/src/transport/ssh.rs b/model/src/transport/ssh.rs similarity index 99% rename from cli/src/transport/ssh.rs rename to model/src/transport/ssh.rs index 4cd8ca3..fc3894f 100644 --- a/cli/src/transport/ssh.rs +++ b/model/src/transport/ssh.rs @@ -1,5 +1,7 @@ use crate::inventory::{Group, Inventory, Target}; use crate::transport::Transport; +use crate::ExecutableTask; + use colored::*; use log::*; @@ -10,8 +12,6 @@ use std::io::BufReader; use std::net::TcpStream; use std::path::Path; -use zap_model::ExecutableTask; - #[derive(Clone)] pub struct Ssh { session: Session,