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
This commit is contained in:
R Tyler Croy 2021-01-01 14:32:51 -08:00
parent a3d6980f71
commit 50ab1a5b14
11 changed files with 41 additions and 31 deletions

13
Cargo.lock generated
View File

@ -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",
]

View File

@ -1,6 +1,6 @@
[package]
name = "zap-cli"
version = "0.1.1"
version = "0.2.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
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" }

View File

@ -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(

View File

@ -1,6 +1,6 @@
[package]
name = "zap-model"
version = "0.1.1"
version = "0.2.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
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"

View File

@ -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<String, String>,
}
impl ExecutableTask {
pub fn new(task: task::Task, parameters: HashMap<String, String>) -> Self {
pub fn new(task: Task, parameters: HashMap<String, String>) -> Self {
Self { task, parameters }
}
}

1
model/src/tasks/mod.rs Normal file
View File

@ -0,0 +1 @@

View File

@ -1,6 +1,7 @@
use crate::ExecutableTask;
use crate::inventory::{Group, Inventory, Target};
use std::path::Path;
use zap_model::ExecutableTask;
pub mod ssh;

View File

@ -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,