From e5cbd9f9e5e0797d527ef5ec80b3caa00462864e Mon Sep 17 00:00:00 2001 From: Didier Date: Thu, 13 Jul 2023 22:47:09 +0200 Subject: [PATCH] feat: implemented the whole process --- bootpkg/Cargo.toml | 1 + bootpkg/src/args.rs | 2 -- bootpkg/src/main.rs | 27 ++++++++++----------- bootpkg/src/prelude.rs | 35 +++++++++++++++++++++++++-- package.toml | 14 ++++++----- pkg/scripts/bootstrap/dirty_install | 3 +++ pkg/scripts/bootstrap/download_latest | 2 ++ pkg/scripts/check_installed | 3 +++ pkgfile/src/lib.rs | 2 +- 9 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 pkg/scripts/bootstrap/dirty_install create mode 100644 pkg/scripts/check_installed diff --git a/bootpkg/Cargo.toml b/bootpkg/Cargo.toml index 8292150..d6c3186 100644 --- a/bootpkg/Cargo.toml +++ b/bootpkg/Cargo.toml @@ -13,3 +13,4 @@ serde = { version = "1.0.171", features = ["derive"] } regex = "1.9.1" reqwest = { version = "0.11.18", features = ["blocking"] } uuid = { version = "1.4.0", features = ["serde", "v4"] } +tar = "0.4.39" diff --git a/bootpkg/src/args.rs b/bootpkg/src/args.rs index cf061e3..0df76c9 100644 --- a/bootpkg/src/args.rs +++ b/bootpkg/src/args.rs @@ -1,6 +1,5 @@ pub enum Command { Strap, - Unpack, None, } @@ -8,7 +7,6 @@ impl From for Command { fn from(value: String) -> Self { match value.to_lowercase().as_str() { "strap" => Command::Strap, - "unpack" => Command::Unpack, _ => Command::None, } } diff --git a/bootpkg/src/main.rs b/bootpkg/src/main.rs index 8853c9b..6bc16a8 100644 --- a/bootpkg/src/main.rs +++ b/bootpkg/src/main.rs @@ -1,7 +1,7 @@ use std::env; use std::env::temp_dir; -use std::fs::{create_dir_all, File, remove_dir_all}; -use std::io::Write; +use std::fs::{create_dir_all, remove_dir_all}; +use std::io::Cursor; use std::process::exit; use uuid::Uuid; @@ -9,6 +9,7 @@ use uuid::Uuid; use pkgfile::PKGFile; use crate::args::{Args, Command}; +use crate::prelude::run_bootstrap; mod args; mod prelude; @@ -67,27 +68,25 @@ fn main() { create_dir_all(&tmp_dir) .expect("Could not create tmp dir."); + println!("** extracting pkgtar..."); { - println!("** writing tar to tmp file..."); - let mut file = File::create(format!("{}/pkgtar", tmp_dir.to_str().unwrap_or("/tmp"))) - .expect("Could not create tmp pkgtar file"); - - file.write(&pkg.data) - .expect("Could not write pkgtar to tmp file"); + let mut archive = tar::Archive::new(Cursor::new(pkg.data)); + archive.unpack(format!("{}/contents", &tmp_dir.to_str().unwrap())) + .expect("Could not extract archive"); } + env::set_current_dir(format!("{}/contents", &tmp_dir.to_str().unwrap())) + .expect("could not enter tmp dir"); - // TODO: untar - // TODO: cd into pkg - // TODO: run bootstrap commands - + println!("** running bootstrap commands..."); + let res_bootstrap = run_bootstrap(mani); println!("** removing temporary directory..."); remove_dir_all(&tmp_dir) .expect(&*format!("Could not remove tmp dir: {}", &tmp_dir.to_str().unwrap())); + exit(if res_bootstrap { println!("!! bootstrap success"); 0 } else { 1 }) } - Command::Unpack => {} _ => { - println!("Unsupported command, allowed commands: strap/unpack."); + println!("Unsupported command, allowed commands: strap."); exit(1); } } diff --git a/bootpkg/src/prelude.rs b/bootpkg/src/prelude.rs index 13a9936..aff3164 100644 --- a/bootpkg/src/prelude.rs +++ b/bootpkg/src/prelude.rs @@ -12,8 +12,8 @@ pub struct PKGR { #[derive(Serialize, Deserialize)] pub struct Bootstrap { - check_installed_commands: Vec, - commands: Vec + pub check_installed_commands: Vec, + pub commands: Vec } #[derive(Serialize, Deserialize)] @@ -33,3 +33,34 @@ pub fn mani_from_str(s: &str) -> Manifest> { pkgr: bmani.pkgr } } + +pub fn run_bootstrap(mani: Manifest>) -> bool { + if let Some(pkgr) = mani.pkgr { + if let Some(bootstrap) = pkgr.bootstrap { + fn run_command>(s: S) -> i32 { + std::process::Command::new("sh") + .arg("-c") + .arg(s.into()) + .spawn().expect("Could not spawn process.") + .wait().expect("Could not wait for process.") + .code().expect("Could not fetch exit code.") + } + + for command in &bootstrap.check_installed_commands { + if run_command(command) != 0 { + println!("!! Command failed: {}", command); + println!("!! Already installed."); + return false + } + } + for command in &bootstrap.commands { + if run_command(command) != 0 { + println!("!! Command failed: {}", command); + println!("!! Bootstrap failed!!!"); + return false + } + } + } + } + return true +} diff --git a/package.toml b/package.toml index 54d55b5..c99108d 100644 --- a/package.toml +++ b/package.toml @@ -78,8 +78,8 @@ root = "/root" #* # After the install script the build directory will be deleted and an CTREE (changed tree) will be generated. # Then the CTREE will be used to copy the files over. [build] -build_script = "/scripts/build" # relative to pkg -install_script = "/scripts/install" # relative to pkg +build_script = "scripts/build" # relative to pkg +install_script = "scripts/install" # relative to pkg [build.dependencies] base = "latest,stable" # selected by default @@ -95,11 +95,13 @@ base = "latest,stable" # selected by default # and only 1 release channel for pkgr [pkgr.bootstrap] ## any non-zero = installed -check_installed_commands = [ "/scripts/check_installed" ] +check_installed_commands = [ + "sh scripts/check_installed" +] # any non-zero = fail commands = [ - "/scripts/bootstrap/download_latest @version /tmp/pkgr.pkg", - "/scripts/bootstrap/dirty_install /tmp/pkgr.pkg", - "/scripts/bootstrap/check_install" + "sh scripts/bootstrap/download_latest @version /tmp/pkgr.pkg", + "sh scripts/bootstrap/dirty_install /tmp/pkgr.pkg", + "sh scripts/check_installed" ] diff --git a/pkg/scripts/bootstrap/dirty_install b/pkg/scripts/bootstrap/dirty_install new file mode 100644 index 0000000..41d3f5d --- /dev/null +++ b/pkg/scripts/bootstrap/dirty_install @@ -0,0 +1,3 @@ +#!/bin/sh + +printf "" \ No newline at end of file diff --git a/pkg/scripts/bootstrap/download_latest b/pkg/scripts/bootstrap/download_latest index 229aaa2..178bd4e 100644 --- a/pkg/scripts/bootstrap/download_latest +++ b/pkg/scripts/bootstrap/download_latest @@ -1,7 +1,9 @@ #!/bin/sh fetch_latest_version() { + printf "" } download_file() { + printf "" } \ No newline at end of file diff --git a/pkg/scripts/check_installed b/pkg/scripts/check_installed new file mode 100644 index 0000000..41d3f5d --- /dev/null +++ b/pkg/scripts/check_installed @@ -0,0 +1,3 @@ +#!/bin/sh + +printf "" \ No newline at end of file diff --git a/pkgfile/src/lib.rs b/pkgfile/src/lib.rs index 6025cd5..c3c056b 100644 --- a/pkgfile/src/lib.rs +++ b/pkgfile/src/lib.rs @@ -13,7 +13,7 @@ impl TryFrom> for PKGFile { .iter() .map(|v| u32::from(*v)) .collect(); - let manifest_size: u32 = ((header[1] << 8) | header[2]); + let manifest_size: u32 = (header[1] << 8) | header[2]; if manifest_size > value.len() as u32 { return Err(()); }