-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
196 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "nautirust" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use std::collections::HashSet; | ||
use std::env; | ||
use std::path::Path; | ||
use std::process::Child; | ||
|
||
use async_std::fs::{self, read_to_string, write}; | ||
use tempdir::TempDir; | ||
|
||
use super::run::Steps; | ||
use crate::channel::Channel; | ||
use crate::runner::Runner; | ||
|
||
/// Create a docker-compose file from a nautirust pipeline | ||
#[derive(clap::Args, Debug)] | ||
pub struct Command { | ||
/// Config file | ||
file: String, | ||
#[clap(short, long)] | ||
output: bool, | ||
/// temporary directory to put step configuration files | ||
#[clap(short, long)] | ||
tmp_dir: Option<String>, | ||
} | ||
|
||
impl Command { | ||
pub(crate) async fn execute( | ||
self, | ||
channels: Vec<Channel>, | ||
runners: Vec<Runner>, | ||
) { | ||
let mut tmp_dir = None; | ||
|
||
if let Some(l) = &self.tmp_dir { | ||
fs::create_dir_all(l).await.unwrap(); | ||
} | ||
|
||
let path = self | ||
.tmp_dir | ||
.as_ref() | ||
.map(|l| Path::new(l).to_owned()) | ||
.unwrap_or_else(|| { | ||
let tmp = TempDir::new("orchestrator").unwrap(); | ||
let out = tmp.path().to_owned(); | ||
tmp_dir = Some(tmp); | ||
out | ||
}); | ||
|
||
let content = read_to_string(self.file).await.unwrap(); | ||
let values: Steps = serde_json::from_str(&content).unwrap(); | ||
|
||
// Check if each runner can docker | ||
let find_runner = |id| runners.iter().find(|r| &r.id == id).unwrap(); | ||
let runners_without_docker = values | ||
.steps | ||
.iter() | ||
.map(|s| find_runner(&s.processor_config.runner_id)) | ||
.filter(|runner| runner.docker.is_none()) | ||
.map(|r| &r.id) | ||
.collect::<HashSet<_>>(); | ||
|
||
if !runners_without_docker.is_empty() { | ||
eprintln!( | ||
"Not all runners support dockerization ({:?})", | ||
runners_without_docker | ||
); | ||
return; | ||
} | ||
|
||
let mut procs: Vec<Child> = Vec::new(); | ||
|
||
let used_channels = super::get_used_channels(&content, &channels); | ||
used_channels.for_each( | ||
|Channel { | ||
docker, location, .. | ||
}| { | ||
super::add_add_subproc(docker, location.as_ref(), &mut procs) | ||
}, | ||
); | ||
|
||
for value in &values.steps { | ||
let file = path.join(format!("{}.json", value.processor_config.id)); | ||
let config = serde_json::to_vec_pretty(&value).unwrap(); | ||
|
||
write(file.clone(), config).await.unwrap(); | ||
|
||
let runner = find_runner(&value.processor_config.runner_id); | ||
|
||
let config_path = format!( | ||
"'{}'", | ||
file.canonicalize().expect("canonicalize path").display() | ||
); | ||
let current_dir = format!( | ||
"'{}'", | ||
env::current_dir() | ||
.unwrap() | ||
.canonicalize() | ||
.expect("canonicalize path") | ||
.display() | ||
); | ||
|
||
let script = runner | ||
.docker | ||
.clone() | ||
.unwrap() | ||
.replace("{config}", &config_path) | ||
.replace("{cwd}", ¤t_dir); | ||
|
||
super::add_add_subproc( | ||
&script.into(), | ||
runner.location.as_ref(), | ||
&mut procs, | ||
); | ||
} | ||
|
||
let docker_header = "services:\n"; | ||
let docker_content: String = [docker_header.to_string()] | ||
.into_iter() | ||
.chain( | ||
procs | ||
.into_iter() | ||
.map(|p| p.wait_with_output().expect("finish process")) | ||
.map(|output| { | ||
String::from_utf8(output.stdout).expect("invalid utf-8") | ||
}), | ||
) | ||
.collect(); | ||
|
||
if self.output { | ||
write("docker-compose.yml", docker_content).await.unwrap(); | ||
} else { | ||
println!("{}", docker_content); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.