Skip to content

Commit

Permalink
feat(cmd): allow errors to be ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
2moe committed Feb 18, 2024
1 parent ebb8aeb commit e48b329
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 43 deletions.
1 change: 1 addition & 0 deletions .github/workflows/debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
ver: "3.0"
- os: debian
ver: "3.1"
# potato & woody need ready-made i386 rootfs

runs-on: ubuntu-latest
# defaults:
Expand Down
16 changes: 8 additions & 8 deletions assets/manually-build/scripts/build-old-debian-rootfs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ build() {

for series (
potato
# woody
woody
# sarge
# etch
# lenny
Expand All @@ -47,23 +47,23 @@ for series (
case $series {
("potato")
for arch (
## "i386"
### "powerpc"
"i386"
## "powerpc"
# "arm"
# "alpha"
# "sparc"
"m68k"
# "m68k"
) {
build
} ;;
("woody")
for arch (
## "i386"
"i386"
## "ia64"
## "s390"
"powerpc"
"mips"
"mipsel"
# "powerpc"
# "mips"
# "mipsel"
# "alpha"
# "hppa"
# "sparc"
Expand Down
60 changes: 38 additions & 22 deletions crates/get-ctr/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) fn run_curl(url: &Url, fname: &str) {
file = fname.magenta(),
url = url.yellow()
);
run("curl", &["-L", "-o", fname, url.as_str()]);
run("curl", &["-L", "-o", fname, url.as_str()], true);
}

pub(crate) fn spawn_cmd(cmd: &str, args: &[&str]) -> Child {
Expand All @@ -29,7 +29,7 @@ pub(crate) fn spawn_cmd(cmd: &str, args: &[&str]) -> Child {
}

/// Blocks running process and does not catch stdout & stderr (i.e., defaults to direct output to the console)
pub(crate) fn run<A, S>(cmd: S, args: &[A])
pub(crate) fn run<A, S>(cmd: S, args: &[A], exit_if_failure: bool) -> ExitStatus
where
A: AsRef<OsStr>,
S: AsRef<OsStr>,
Expand All @@ -45,30 +45,44 @@ where
};

// 1st run:
if !status().success() {
error!(
"Failed to run : {:?} ({:?} ...)",
cmd.as_ref(),
args.first().map(|x| x.as_ref())
);
eprintln!("Retrying ...");
// 2nd run:
if !status().success() {
eprintln!("Retrying ...");
// 3rd run:
exit_if_failure(status())
}
let status_1st = status();
if status_1st.success() {
return status_1st;
}

error!(
"Failed to run : {:?} ({:?} ...)",
cmd.as_ref(),
args.first().map(|x| x.as_ref())
);
eprintln!("Retrying ...");
// 2nd run:
let status_2nd = status();
if status_1st.success() {
return status_2nd;
}

eprintln!("Retrying ...");
// 3rd run:
let status_3rd = status();
if exit_if_failure {
check_status_and_exit(status_3rd)
}
status_3rd
}

fn exit_if_failure(status: ExitStatus) {
fn check_status_and_exit(status: ExitStatus) {
if !status.success() {
exit(status.into_raw())
}
}

/// If the current uid is not 0 (non-root user), sudo and doas are automatically detected and a new process are run synchronously and blockingly.
pub(crate) fn run_as_root<S, A>(cmd: S, args: &[A])
pub(crate) fn run_as_root<S, A>(
cmd: S,
args: &[A],
exit_if_failure: bool,
) -> ExitStatus
where
// I: IntoIterator<Item = S>,
A: AsRef<OsStr>,
Expand All @@ -77,7 +91,7 @@ where
let uid = unsafe { libc::getuid() };
log::debug!("uid: {uid}");
if uid == 0 {
return run(cmd, args);
return run(cmd, args, exit_if_failure);
}

let root_cmd = static_root_cmd();
Expand All @@ -91,7 +105,7 @@ where
}

info!("cmd: {root_cmd}, args: {new_args:?}");
run(OsStr::new(root_cmd.as_ref()), &new_args);
run(OsStr::new(root_cmd.as_ref()), &new_args, exit_if_failure)
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -163,25 +177,26 @@ pub(crate) fn force_remove_item_as_root<P: AsRef<Path>>(path: P) {
}

// run_as_root("chmod", &[osstr("-R"), osstr("777"), p.as_ref()]);
run_as_root("rm", &[osstr("-rf"), p.as_ref()]);
run_as_root("rm", &[osstr("-rf"), p.as_ref()], true);
}

/// ~= sudo fs::rename(src, dst)
pub(crate) fn move_item_as_root<S: AsRef<OsStr>, D: AsRef<OsStr>>(src: S, dst: D) {
run_as_root("mv", &[OsStr::new("-f"), src.as_ref(), dst.as_ref()]);
run_as_root("mv", &[OsStr::new("-f"), src.as_ref(), dst.as_ref()], true);
}

// pub(crate) fn copy_dir_as_root<S: AsRef<OsStr>, D: AsRef<OsStr>>(src: S, dst: D) {
// run_as_root("cp", &[OsStr::new("-rf"), src.as_ref(), dst.as_ref()]);
// }

pub(crate) fn create_dir_all_as_root<D: AsRef<OsStr>>(dst: D) {
run_as_root("mkdir", &[OsStr::new("-p"), dst.as_ref()]);
run_as_root("mkdir", &[OsStr::new("-p"), dst.as_ref()], true);
}

pub(crate) fn run_nspawn<S: AsRef<OsStr>, R: AsRef<OsStr>>(
rootfs_dir: R,
sh_cmd: S,
exit_if_failure: bool,
) {
#[allow(unused_variables)]
let osstr = OsStr::new;
Expand All @@ -199,6 +214,7 @@ pub(crate) fn run_nspawn<S: AsRef<OsStr>, R: AsRef<OsStr>>(
osstr("-c"),
sh_cmd.as_ref(),
],
exit_if_failure,
);
}

Expand Down
28 changes: 20 additions & 8 deletions crates/get-ctr/src/task/build_rootfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ pub(crate) fn obtain<'a, I: IntoIterator<Item = &'a Repository<'a>>>(
(Some(arch), s) if ["warty", "hoary"].contains(&s) => {
get_rootfs(arch, s)?
}
(Some(arch), s)
if arch != &"i386" && ["potato", "woody"].contains(&s) =>
{
(Some(arch), s) if ["potato", "woody"].contains(&s) => {
get_rootfs(arch, s)?
}
_ => {
Expand Down Expand Up @@ -146,13 +144,13 @@ fn get_rootfs_from_docker(docker_repo: &str, docker_dir: &Path) {
"/host",
];
log::info!("cmd: docker, args: {args:?}");
run("docker", &args)
run("docker", &args, true);
}

fn patch_deb_rootfs(rootfs_dir: &PathBuf, repo: &Repository<'_>) {
// TODO: fix ubuntu16.04: apt-get purge makedev

run_nspawn(rootfs_dir, "apt-get update; exit 0");
run_nspawn(rootfs_dir, "apt-get update", false);
dbg!(repo.get_codename());

// # debian-etch: +debian-backports-keyring
Expand All @@ -162,6 +160,7 @@ fn patch_deb_rootfs(rootfs_dir: &PathBuf, repo: &Repository<'_>) {
rootfs_dir,
"apt-get install --assume-yes --force-yes debian-backports-keyring \
; exit 0",
false,
);
}
_ => {}
Expand All @@ -187,8 +186,8 @@ fn patch_deb_rootfs(rootfs_dir: &PathBuf, repo: &Repository<'_>) {
; for i in apt-utils eatmydata; do \
apt-get install --assume-yes --force-yes $i \
; done \
; apt-get clean \
; exit 0",
; apt-get clean",
false,
);
}

Expand Down Expand Up @@ -262,7 +261,20 @@ fn run_debootstrap(
args.push(rootfs_dir.as_ref());
args.push(osstr(deb_src.get_url().as_str()));

run_as_root("/usr/sbin/debootstrap", &args);
run_as_root("/usr/sbin/debootstrap", &args, true);

let log_file = rootfs_dir.join("debootstrap/debootstrap.log");

if log_file.exists() {
log::debug!(
"log_file: {}, log_path: {log_file:?}",
fs::read_to_string(&log_file).unwrap_or_default()
);
panic!(
"Failed to build: {} (dir: {rootfs_dir:?}) with debootstrap",
repo.get_series()
)
}
}

/// - if deb822: mirrors/mirror.sources -> rootfs/etc/apt/sources.list.d/
Expand Down
5 changes: 3 additions & 2 deletions crates/get-ctr/src/task/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(crate) fn extract_tar_as_root<D: AsRef<Path>>(
osstr("-xf"),
tar_path.as_ref(),
],
true,
);

Ok(())
Expand Down Expand Up @@ -109,12 +110,12 @@ pub(crate) fn pack_tar_as_root<S: AsRef<OsStr>>(

args.extend([osstr("-cf"), tar_path.as_ref(), osstr(".")]);

run_as_root("tar", &args);
run_as_root("tar", &args, true);

let sys_dir = Path::new(src_osdir).join("sys");

if sys_dir.join("kernel").exists() {
run_as_root("umount", &[osstr("-lf"), sys_dir.as_os_str()])
run_as_root("umount", &[osstr("-lf"), sys_dir.as_os_str()], true);
}

force_remove_item_as_root(src_osdir);
Expand Down
2 changes: 1 addition & 1 deletion crates/get-ctr/src/task/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) fn run_docker_push(repo: &str) {
"--all-tags".cyan(),
repo.blue()
);
command::run("docker", &["push", "--all-tags", repo])
command::run("docker", &["push", "--all-tags", repo], true);
}

pub(crate) fn run_docker_build(
Expand Down
4 changes: 2 additions & 2 deletions crates/get-ctr/src/task/old_old_debian/docker_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
}

log::debug!("cmd: {}, args: {:#?}", "docker".green(), args.cyan());
command::run("docker", &args);
command::run("docker", &args, true);
// -----------
let digest = push_docker_manifest(repo)?;
update_repo_digest_map(&mut repo_digest_map, digest_map_key, digest)
Expand Down Expand Up @@ -275,7 +275,7 @@ where
};

log::info!("{} {} {}", "docker".green(), "pull".yellow(), repo.blue());
command::run("docker", &["pull", repo]);
command::run("docker", &["pull", repo], true);

let args = ["inspect", "--format", r##"{{json .RepoDigests}}"##, repo];
log::info!("cmd: {}, args: {:#?}", "docker".green(), args.blue());
Expand Down

0 comments on commit e48b329

Please sign in to comment.