Commit 13a18184 authored by Alexis Janon's avatar Alexis Janon
Browse files

Support for generating a random slice name for "spawn"

If the "name" field is not present, it will generate one for the spawn
command. Other commands do not generate random names if the field is
missing: this may change in the future, and cuttr may then return a
generated name for the "create" command for instance.
parent 33a4df4a
use configuration::sysfscontroller::SysFsControllerList;
use error::*;
use random::random_string;
use serde_json;
use std::collections::HashMap;
use std::rc::Rc;
......@@ -7,14 +8,16 @@ use transaction::*;
const MISSING_PROPERTIES: &str = "missing \"properties\" key for modify command";
const MISSING_CMD: &str = "missing \"cmd\" key for exec command";
const MISSING_NAME: &str = "missing \"name\" field for command";
const INVALID_LIST: &str =
"invalid command list: \"remove\" and \"exec\" are only allowed as ending commands";
const RANDOM_NAME_SIZE: usize = 12;
// TODO: find a more accurate name
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct InCommand {
command: String,
name: String,
name: Option<String>,
properties: Option<HashMap<String, String>>,
cmd: Option<String>,
cmd_args: Option<Vec<String>>,
......@@ -33,79 +36,98 @@ pub struct Arguments {
// TODO: remove all these clone()!
impl InCommand {
fn to_create_slice(&self, controllers: Rc<SysFsControllerList>) -> Result<Box<Command>> {
match self.properties {
Some(ref properties) => Ok(Box::new(CommandList::new(vec![
Box::new(CreateSlice::new(
self.name.clone(),
match self.name {
Some(ref name) => match self.properties {
Some(ref properties) => Ok(Box::new(CommandList::new(vec![
Box::new(CreateSlice::new(
name.clone(),
self.args.clone(),
Rc::clone(&controllers),
)),
Box::new(ModifySlice::new(
name.clone(),
Rc::clone(&controllers),
&properties,
)),
]))),
None => Ok(Box::new(CreateSlice::new(
name.clone(),
self.args.clone(),
Rc::clone(&controllers),
)),
Box::new(ModifySlice::new(
self.name.clone(),
Rc::clone(&controllers),
&properties,
)),
]))),
None => Ok(Box::new(CreateSlice::new(
self.name.clone(),
self.args.clone(),
Rc::clone(&controllers),
))),
))),
},
None => Err(Error::from(MISSING_NAME)),
}
}
fn to_modify_slice(&self, controllers: Rc<SysFsControllerList>) -> Result<Box<Command>> {
match self.properties {
Some(ref properties) => Ok(Box::new(ModifySlice::new(
self.name.clone(),
Rc::clone(&controllers),
properties,
))),
None => Err(Error::from(MISSING_PROPERTIES)),
match self.name {
Some(ref name) => match self.properties {
Some(ref properties) => Ok(Box::new(ModifySlice::new(
name.clone(),
Rc::clone(&controllers),
properties,
))),
None => Err(Error::from(MISSING_PROPERTIES)),
},
None => Err(Error::from(MISSING_NAME)),
}
}
fn to_remove_slice(&self, controllers: Rc<SysFsControllerList>) -> Result<Box<Command>> {
Ok(Box::new(RemoveSlice::new(
self.name.clone(),
Rc::clone(&controllers),
)))
match self.name {
Some(ref name) => Ok(Box::new(RemoveSlice::new(
name.clone(),
Rc::clone(&controllers),
))),
None => Err(Error::from(MISSING_NAME)),
}
}
fn to_exec(&self, controllers: Rc<SysFsControllerList>) -> Result<Box<Command>> {
match self.cmd {
Some(ref cmd) => Ok(Box::new(Exec::new(
self.name.clone(),
cmd,
&self.cmd_args,
&self.env,
&self.uid,
&self.gid,
Rc::clone(&controllers),
))),
None => Err(Error::from(MISSING_CMD)),
match self.name {
Some(ref name) => match self.cmd {
Some(ref cmd) => Ok(Box::new(Exec::new(
name.clone(),
cmd,
&self.cmd_args,
&self.env,
&self.uid,
&self.gid,
Rc::clone(&controllers),
))),
None => Err(Error::from(MISSING_CMD)),
},
None => Err(Error::from(MISSING_NAME)),
}
}
fn to_fork_exec(&self, controllers: Rc<SysFsControllerList>) -> Result<Box<Command>> {
match self.cmd {
Some(ref cmd) => Ok(Box::new(ForkExec::new(
self.name.clone(),
cmd,
&self.cmd_args,
&self.env,
&self.uid,
&self.gid,
Rc::clone(&controllers),
))),
None => Err(Error::from(MISSING_CMD)),
match self.name {
Some(ref name) => match self.cmd {
Some(ref cmd) => Ok(Box::new(ForkExec::new(
name.clone(),
cmd,
&self.cmd_args,
&self.env,
&self.uid,
&self.gid,
Rc::clone(&controllers),
))),
None => Err(Error::from(MISSING_CMD)),
},
None => Err(Error::from(MISSING_NAME)),
}
}
fn to_spawn(&self, controllers: Rc<SysFsControllerList>) -> Result<Box<Command>> {
let name = match self.name {
Some(ref name) => name.clone(),
None => random_string(RANDOM_NAME_SIZE),
};
match self.cmd {
Some(ref cmd) => Ok(Box::new(Spawn::new(
self.name.clone(),
name,
cmd,
&self.cmd_args,
&self.env,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment