feat: implemented the whole process
This commit is contained in:
		
							parent
							
								
									59b77fd07f
								
							
						
					
					
						commit
						61e3c450b7
					
				
					 9 changed files with 64 additions and 25 deletions
				
			
		|  | @ -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" | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| pub enum Command { | ||||
|     Strap, | ||||
|     Unpack, | ||||
|     None, | ||||
| } | ||||
| 
 | ||||
|  | @ -8,7 +7,6 @@ impl From<String> for Command { | |||
|     fn from(value: String) -> Self { | ||||
|         match value.to_lowercase().as_str() { | ||||
|             "strap" => Command::Strap, | ||||
|             "unpack" => Command::Unpack, | ||||
|             _ => Command::None, | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -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); | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -12,8 +12,8 @@ pub struct PKGR { | |||
| 
 | ||||
| #[derive(Serialize, Deserialize)] | ||||
| pub struct Bootstrap { | ||||
|     check_installed_commands: Vec<String>, | ||||
|     commands: Vec<String> | ||||
|     pub check_installed_commands: Vec<String>, | ||||
|     pub commands: Vec<String> | ||||
| } | ||||
| 
 | ||||
| #[derive(Serialize, Deserialize)] | ||||
|  | @ -33,3 +33,34 @@ pub fn mani_from_str(s: &str) -> Manifest<Option<PKGR>> { | |||
|         pkgr: bmani.pkgr | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub fn run_bootstrap(mani: Manifest<Option<PKGR>>) -> bool { | ||||
|     if let Some(pkgr) = mani.pkgr { | ||||
|         if let Some(bootstrap) = pkgr.bootstrap { | ||||
|             fn run_command<S: Into<String>>(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 | ||||
| } | ||||
|  |  | |||
							
								
								
									
										14
									
								
								package.toml
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								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" | ||||
| ] | ||||
|  |  | |||
							
								
								
									
										3
									
								
								pkg/scripts/bootstrap/dirty_install
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								pkg/scripts/bootstrap/dirty_install
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| #!/bin/sh | ||||
| 
 | ||||
| printf "" | ||||
|  | @ -1,7 +1,9 @@ | |||
| #!/bin/sh | ||||
| 
 | ||||
| fetch_latest_version() { | ||||
|   printf "" | ||||
| } | ||||
| 
 | ||||
| download_file() { | ||||
|   printf "" | ||||
| } | ||||
							
								
								
									
										3
									
								
								pkg/scripts/check_installed
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								pkg/scripts/check_installed
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| #!/bin/sh | ||||
| 
 | ||||
| printf "" | ||||
|  | @ -13,7 +13,7 @@ impl TryFrom<Vec<u8>> 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(()); | ||||
|                 } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue