1
0
mirror of https://github.com/containers/youki synced 2024-09-27 22:49:57 +02:00

fighting all the references

This commit is contained in:
yihuaf 2021-08-05 10:17:17 +02:00
parent bbb56392ff
commit 367c7c1d99
3 changed files with 26 additions and 17 deletions

View File

@ -15,16 +15,21 @@ impl fmt::Display for HookTimeoutError {
}
}
pub fn run_hooks(hooks: Option<Vec<Hook>>, container: Option<Container>) -> Result<()> {
pub fn run_hooks(hooks: Option<&Vec<Hook>>, container: Option<&Container>) -> Result<()> {
if let Some(hooks) = hooks {
for hook in hooks {
let envs: HashMap<String, String> = if let Some(env) = hook.env {
let envs: HashMap<String, String> = if let Some(env) = hook.env.as_ref() {
utils::parse_env(env)
} else {
HashMap::new()
};
let mut hook_command = process::Command::new(hook.path)
.args(hook.args.unwrap_or_default())
let args = if let Some(args) = hook.args.clone() {
args
} else {
vec![]
};
let mut hook_command = process::Command::new(&hook.path)
.args(&args)
.env_clear()
.envs(envs)
.stdin(if container.is_some() {
@ -113,7 +118,7 @@ mod test {
timeout: None,
};
let hooks = Some(vec![hook]);
run_hooks(hooks, Some(default_container))?;
run_hooks(hooks.as_ref(), Some(&default_container))?;
}
{
@ -126,7 +131,7 @@ mod test {
timeout: None,
};
let hooks = Some(vec![hook]);
run_hooks(hooks, Some(default_container))?;
run_hooks(hooks.as_ref(), Some(&default_container))?;
}
Ok(())
@ -146,7 +151,7 @@ mod test {
timeout: Some(1),
};
let hooks = Some(vec![hook]);
match run_hooks(hooks, None) {
match run_hooks(hooks.as_ref(), None) {
Ok(_) => {
bail!("The test expects the hook to error out with timeout. Should not execute cleanly");
}

View File

@ -131,7 +131,8 @@ pub fn container_init(args: ContainerInitArgs) -> Result<()> {
let mut envs: Vec<String> = proc.env.as_ref().unwrap_or(&vec![]).clone();
let rootfs = &args.rootfs;
let mut envs: Vec<String> = proc.env.clone();
let hooks = spec.hooks.clone();
let hooks = spec.hooks.as_ref();
let container = args.container.as_ref();
let mut child = args.child;
// if Out-of-memory score adjustment is set in specification. set the score
@ -197,7 +198,7 @@ pub fn container_init(args: ContainerInitArgs) -> Result<()> {
// create_container hook needs to be called after the namespace setup, but
// before pivot_root is called. This runs in the container namespaces.
if let Some(hooks) = hooks {
hook::run_hooks(hooks.create_container, args.container)?
hook::run_hooks(hooks.create_container.as_ref(), container)?
}
rootfs::prepare_rootfs(spec, rootfs, bind_service)
.with_context(|| "Failed to prepare rootfs")?;
@ -270,9 +271,9 @@ pub fn container_init(args: ContainerInitArgs) -> Result<()> {
// Reset the process env based on oci spec.
env::vars().for_each(|(key, _value)| std::env::remove_var(key));
utils::parse_env(envs).iter().for_each(|(key, value)| {
env::set_var(key, value)
});
utils::parse_env(&envs)
.iter()
.for_each(|(key, value)| env::set_var(key, value));
// notify parents that the init process is ready to execute the payload.
child.notify_parent()?;
@ -282,9 +283,12 @@ pub fn container_init(args: ContainerInitArgs) -> Result<()> {
// create_container hook needs to be called after the namespace setup, but
// before pivot_root is called. This runs in the container namespaces.
// if let Some(hooks) = hooks {
// hook::run_hooks(hooks.start_container, args.container)?
// }
if args.init {
if let Some(hooks) = hooks {
hook::run_hooks(hooks.start_container.as_ref(), container)?
}
}
if let Some(args) = proc.args.as_ref() {
utils::do_exec(&args[0], args, &envs)?;
} else {

View File

@ -40,7 +40,7 @@ impl PathBufExt for PathBuf {
}
}
pub fn parse_env(envs: Vec<String>) -> HashMap<String, String> {
pub fn parse_env(envs: &Vec<String>) -> HashMap<String, String> {
envs.iter()
.filter_map(|e| {
let mut split = e.split('=');
@ -241,7 +241,7 @@ mod tests {
let key = "key".to_string();
let value = "value".to_string();
let env_input = vec![format!("{}={}", key, value)];
let env_output = parse_env(env_input);
let env_output = parse_env(&env_input);
assert_eq!(
env_output.len(),
1,