Compare commits

...

3 Commits

Author SHA1 Message Date
R Tyler Croy 6b3781b746 Add an Ottofile which runs this repos tests
Basically this requires:

* `make run` to launch the service mesh
* `./scripts/local-run`

And then you wait, because log output from the agent's step invocation is not
yet streaming 😆
2020-11-27 11:57:05 -08:00
R Tyler Croy d054fdf7ea Correct the parsing of keyword arguments and multiple positional arguments.
This is basically me stumbling over a shortcut that I left in the parser code
when I first started on it. Now that I am integration testing, I tripped right
into this.

Fixes #44
2020-11-27 11:47:58 -08:00
R Tyler Croy 74ee25aa1a Ensure that the cache directory for the agent is always absolute
This ensures that caches are not mistakenly referred to within the pipeline work
directory
2020-11-27 11:47:21 -08:00
6 changed files with 185 additions and 14 deletions

18
Ottofile Normal file
View File

@ -0,0 +1,18 @@
pipeline {
steps {
sh 'pwd'
// branch needed until #45 is addressed
git url: 'https://git.brokenco.de/rtyler/otto', branch: 'main', into: '.'
}
stage {
name = 'Build'
steps {
sh 'cargo test'
}
}
}
// vim: sw=2 et ts=2 ft=groovy

View File

@ -63,7 +63,7 @@ async fn main() -> std::io::Result<()> {
mkdir_if_not_exists(&work_dir)?;
mkdir_if_not_exists(&cache_dir)?;
std::env::set_var("CACHES_DIR", cache_dir);
std::env::set_var("CACHES_DIR", cache_dir.canonicalize().expect("Failed to canonicalize cache directory"));
std::env::set_current_dir(work_dir)?;
let (sender, receiver) = channel(MAX_CONTROL_MSGS);

View File

@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use uuid::Uuid;
pub use serde_json::Value;
pub mod osp;
/**

41
scripts/local-run Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env ruby
require 'json'
require 'net/http'
pipeline = nil
# Parse it
Net::HTTP.start('localhost', 7672) do |http|
otto = File.read('Ottofile')
response = http.post('/v1/parse', otto)
if response.code.to_i != 200
puts 'Failed to parse file'
exit 1
end
pipeline = JSON.parse(response.read_body)
end
# Hit the local-orchestrator
Net::HTTP.start('localhost', 7673) do |http|
contexts = []
pipeline['batches'].each do |batch|
contexts += (batch['contexts'])
end
payload = JSON.dump({
:pipeline => pipeline['uuid'],
:contexts => contexts,
})
puts payload
res = http.post('/v1/run', payload)
if res.code.to_i != 200
puts "Failed to orchestrate! #{res.code} #{res.read_body}"
exit 1
end
puts 'Enqueued'
end

View File

@ -69,28 +69,86 @@ fn parse_str(parser: &mut pest::iterators::Pair<Rule>) -> String {
"".to_string()
}
/**
* Parse a pair of keyword arguments (kwarg rule) into a tuple
* of the string and value
*/
fn parse_kwarg(parser: &mut Pairs<Rule>) -> Option<(String, Value)> {
let mut key: Option<String> = None;
let mut value: Option<Value> = None;
while let Some(mut parsed) = parser.next() {
match parsed.as_rule() {
Rule::IDENT => {
key = Some(parsed.as_str().to_string());
},
Rule::STR => {
value = Some(Value::String(parse_str(&mut parsed)));
},
_ => {},
}
}
if key.is_some() && value.is_some() {
return Some((key.unwrap(), value.unwrap()));
}
None
}
/**
* Parse the steps
*
* In the case of orphan steps, the uuid should be the pipeline's uuid
*/
fn parse_steps(parser: &mut Pairs<Rule>, uuid: Uuid) -> Vec<Step> {
use std::collections::HashMap;
let mut steps = vec![];
while let Some(parsed) = parser.next() {
if Rule::step == parsed.as_rule() {
let mut symbol: Option<String> = None;
let mut kwargs: HashMap<String, Value> = HashMap::new();
let mut args: Vec<Value> = vec![];
// Grab the step components
let mut parts: Vec<Pair<Rule>> = parsed.into_inner().collect();
// We need at least two parts here!
assert!(parts.len() > 1);
for part in parsed.into_inner() {
match part.as_rule() {
Rule::IDENT => {
symbol = Some(part.as_str().to_string());
},
Rule::kwarg => {
if let Some((key, value)) = parse_kwarg(&mut part.into_inner()) {
kwargs.insert(key, value);
}
},
Rule::args => {
let mut pairs: Vec<Pair<Rule>> = part.into_inner().collect();
for mut pair in pairs.iter_mut() {
if Rule::STR == pair.as_rule() {
args.push(Value::String(parse_str(&mut pair)));
}
}
},
_ => {},
}
}
let symbol = parts[0].as_str().to_string();
let command = parse_str(&mut parts.pop().unwrap());
let parameters = serde_json::Value::String(command);
let parameters = StepParameters::Positional(vec![parameters]);
let step = Step::new(uuid, symbol, parameters);
steps.push(step);
if let Some(symbol) = symbol {
if kwargs.len() > 0 {
if args.len() > 0 {
error!("Parsed keyword and positional arguments out, discarding positionals");
}
let parameters = StepParameters::Keyword(kwargs);
let step = Step::new(uuid, symbol, parameters);
steps.push(step);
}
else {
let parameters = StepParameters::Positional(args);
let step = Step::new(uuid, symbol, parameters);
steps.push(step);
}
}
}
}
steps
@ -276,4 +334,58 @@ mod tests {
assert!(!pipeline.uuid.is_nil());
assert_eq!(pipeline.batches.len(), 1);
}
#[test]
fn parse_kwargs() {
let buf = r#"
pipeline {
steps {
git url: 'https://example.com', branch: 'main'
}
}"#;
let pipeline = parse_pipeline_string(&buf).expect("Failed to parse");
assert!(!pipeline.uuid.is_nil());
assert_eq!(pipeline.batches.len(), 1);
let context = &pipeline.batches[0].contexts[0];
assert_eq!(context.steps.len(), 1);
let step = &context.steps[0];
assert_eq!(step.symbol, "git");
match &step.parameters {
StepParameters::Positional(_args) => assert!(false, "Shouldn't have positional arguments"),
StepParameters::Keyword(kwargs) => {
assert_eq!(kwargs.get("url").unwrap(), "https://example.com");
assert_eq!(kwargs.get("branch").unwrap(), "main");
}
}
}
#[test]
fn parse_multiple_pos_args() {
let buf = r#"
pipeline {
steps {
git 'https://example.com', 'main'
}
}"#;
let pipeline = parse_pipeline_string(&buf).expect("Failed to parse");
assert!(!pipeline.uuid.is_nil());
assert_eq!(pipeline.batches.len(), 1);
let context = &pipeline.batches[0].contexts[0];
assert_eq!(context.steps.len(), 1);
let step = &context.steps[0];
assert_eq!(step.symbol, "git");
match &step.parameters {
StepParameters::Positional(args) => {
assert_eq!(args.len(), 2);
assert_eq!(args[0], "https://example.com");
assert_eq!(args[1], "main");
},
StepParameters::Keyword(_kwargs) => {
assert!(false, "Not expecting keyword arguments for this step");
},
}
}
}

View File

@ -21,9 +21,9 @@ step = { IDENT ~ (
)
}
args = _{ (STR ~ COMMA?)+ }
args = { (STR ~ COMMA?)+ }
kwargs = _{ (kwarg ~ COMMA?)+ }
kwarg = _{ IDENT~ ":" ~ STR }
kwarg = { IDENT~ ":" ~ STR }
property = { IDENT ~ "=" ~ STR }