Add prompt for sudo

This commit is contained in:
Gerald Pinder 2025-03-21 11:48:28 -04:00
parent 5ba3061faf
commit db9bf78c94
4 changed files with 135 additions and 44 deletions

View file

@ -36,6 +36,8 @@ use super::{
#[cfg(feature = "rechunk")]
use super::{types::MountId, ContainerMountDriver, RechunkDriver};
const SUDO_PROMPT: &str = "Password for %u required to run 'podman' as privileged";
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
struct PodmanImageMetadata {
@ -144,7 +146,11 @@ impl BuildDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"build",
if !matches!(opts.platform, Platform::Native) => [
@ -186,7 +192,11 @@ impl BuildDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"tag",
opts.src_image.to_string(),
@ -216,7 +226,11 @@ impl BuildDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"push",
format!(
@ -383,7 +397,11 @@ impl ContainerMountDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"mount",
opts.container_id,
@ -412,7 +430,11 @@ impl ContainerMountDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"unmount",
opts.container_id
@ -439,7 +461,11 @@ impl ContainerMountDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"volume",
"rm",
@ -510,7 +536,11 @@ impl RunDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"create",
opts.image.to_string(),
@ -542,7 +572,11 @@ impl RunDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"rm",
opts.container_id,
@ -571,7 +605,11 @@ impl RunDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"rmi",
opts.image.to_string()
@ -606,7 +644,11 @@ impl RunDriver for PodmanDriver {
} else {
"podman"
},
if use_sudo && has_env_var(SUDO_ASKPASS) => "-A",
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"images",
"--format",
@ -644,11 +686,12 @@ fn podman_run(opts: &RunOpts, cid_file: &Path) -> Command {
} else {
"podman"
},
if use_sudo => [
if use_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"--",
"podman",
"-p",
SUDO_PROMPT,
],
if use_sudo => "podman",
"run",
format!("--cidfile={}", cid_file.display()),
if opts.privileged => [

View file

@ -6,6 +6,7 @@ use std::{
thread,
};
use blue_build_utils::{constants::SUDO_ASKPASS, has_env_var};
use comlexr::cmd;
use log::{debug, error, trace, warn};
use nix::{
@ -120,11 +121,22 @@ where
let id = id.trim();
debug!("Killing container {id}");
let status = if cid.requires_sudo {
cmd!("sudo", cid.container_runtime.to_string(), "stop", id).status()
} else {
cmd!(cid.container_runtime.to_string(), "stop", id).status()
};
let status = cmd!(
if cid.requires_sudo {
"sudo".to_string()
} else {
cid.container_runtime.to_string()
},
if cid.requires_sudo && has_env_var(SUDO_ASKPASS) => [
"-A",
"-p",
format!("Password needed to kill container {id}"),
],
if cid.requires_sudo => cid.container_runtime.to_string(),
"stop",
id
)
.status();
if let Err(e) = status {
error!("Failed to kill container {id}: Error {e}");