Compare commits

...

4 Commits

Author SHA1 Message Date
R Tyler Croy 872157ce98 Move the ExecutableTask to the root of zap-models 2021-01-01 10:14:58 -08:00
R Tyler Croy a3accf213a Refactor the parser package into a "model" package which is more accurate
Most of its job is to contain the models for different data structures
2021-01-01 10:10:01 -08:00
R Tyler Croy 5003d5eeff Add some comments to all the existing defined ztasks 2021-01-01 10:08:17 -08:00
R Tyler Croy a39a994062 Use the right rule when parsing the tasks
This mistake was causing ztask files not to allow preceding comments and
whitespace at the top of the file.
2021-01-01 10:03:59 -08:00
18 changed files with 78 additions and 29 deletions

4
Cargo.lock generated
View File

@ -619,11 +619,11 @@ dependencies = [
"serde_json",
"serde_yaml",
"ssh2",
"zap-parser",
"zap-model",
]
[[package]]
name = "zap-parser"
name = "zap-model"
version = "0.1.0"
dependencies = [
"handlebars",

View File

@ -1,5 +1,5 @@
[workspace]
members = [
'cli',
'parser',
'model',
]

View File

@ -16,4 +16,4 @@ serde_derive = "~1.0"
serde_json = "~1.0"
serde_yaml = "~0.8"
ssh2 = "~0.9.0"
zap-parser = { path = "../parser" }
zap-model = { path = "../model" }

View File

@ -10,8 +10,9 @@ mod transport;
use crate::inventory::*;
use crate::transport::ssh::Ssh;
use zap_parser::plan::{ExecutableTask, Plan};
use zap_parser::task::Task;
use zap_model::plan::Plan;
use zap_model::task::Task;
use zap_model::ExecutableTask;
fn main() {
pretty_env_logger::init();

View File

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

View File

@ -10,7 +10,7 @@ use std::io::BufReader;
use std::net::TcpStream;
use std::path::Path;
use zap_parser::plan::ExecutableTask;
use zap_model::ExecutableTask;
#[derive(Clone)]
pub struct Ssh {

View File

@ -1,5 +1,5 @@
[package]
name = "zap-parser"
name = "zap-model"
version = "0.1.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
edition = "2018"

25
model/src/lib.rs Normal file
View File

@ -0,0 +1,25 @@
#[macro_use]
extern crate pest;
#[macro_use]
extern crate pest_derive;
use std::collections::HashMap;
pub mod plan;
pub mod task;
/**
* An ExecutableTask is a light container over a Task execpt with user-provided information and is
* therefore ready for execution
*/
#[derive(Clone, Debug)]
pub struct ExecutableTask {
pub task: task::Task,
pub parameters: HashMap<String, String>,
}
impl ExecutableTask {
pub fn new(task: task::Task, parameters: HashMap<String, String>) -> Self {
Self { task, parameters }
}
}

View File

@ -6,22 +6,12 @@ use pest::Parser;
use std::collections::HashMap;
use std::path::PathBuf;
use crate::ExecutableTask;
#[derive(Parser)]
#[grammar = "plan.pest"]
struct PlanParser;
#[derive(Clone, Debug)]
pub struct ExecutableTask {
pub task: crate::task::Task,
pub parameters: HashMap<String, String>,
}
impl ExecutableTask {
pub fn new(task: crate::task::Task, parameters: HashMap<String, String>) -> Self {
Self { task, parameters }
}
}
#[derive(Clone, Debug)]
pub struct Plan {
pub tasks: Vec<ExecutableTask>,

View File

@ -162,7 +162,7 @@ impl Task {
}
pub fn from_str(buf: &str) -> Result<Self, PestError<Rule>> {
let mut parser = TaskParser::parse(Rule::task, buf)?;
let mut parser = TaskParser::parse(Rule::taskfile, buf)?;
while let Some(parsed) = parser.next() {
match parsed.as_rule() {
Rule::task => {
@ -245,6 +245,22 @@ fn parse_str(parser: &mut Pairs<Rule>) -> Result<String, PestError<Rule>> {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_task_with_comments() {
let buf = r#"
/*
* This is a simple one
*/
task Hey {
script {
inline = 'echo "hi"'
}
}"#;
let _task = TaskParser::parse(Rule::taskfile, buf)
.unwrap()
.next()
.unwrap();
}
#[test]
fn parse_simple_script_task() {

View File

@ -1,7 +0,0 @@
#[macro_use]
extern crate pest;
#[macro_use]
extern crate pest_derive;
pub mod plan;
pub mod task;

View File

@ -1,3 +1,8 @@
/*
* The echo test takes a string and just uses the shell `echo` on the target
* to echo it back
*/
task Echo {
parameters {
msg {

View File

@ -1,3 +1,7 @@
/*
* Invoke pkg-install(8) on the FreeBSD machine
*/
task FreeBSDPkgInstall {
parameters {
packages {

View File

@ -1,3 +1,7 @@
/*
* Invoke zypper install on a openSUSE/SLES target
*/
task ZypperInstall {
parameters {
packages {

View File

@ -1,3 +1,10 @@
/*
* The bash task is a simple passthrough to bash.
*
* Since bash is not guaranteed to be in the same location on every machine
* This relies on /usr/bin/env to help find bash in the $PATH on the target
*/
task Bash {
parameters {
script {

View File

@ -1,3 +1,7 @@
/*
* The sh task is a simple passthrough to /bin/sh on the target machine
*/
task Sh {
parameters {
script {