switch to toml

This commit is contained in:
askiiart 2024-12-29 23:10:29 -06:00
parent df0cf3b93d
commit e6b521262d
Signed by untrusted user who does not match committer: askiiart
GPG key ID: EA85979611654C30
12 changed files with 218 additions and 160 deletions

View file

@ -19,7 +19,7 @@ pub enum Commands {
///Runs it
Run {
///Path to the config file
#[arg(short, long, default_value = "gregory.yml")]
#[arg(short, long, default_value = "gregory.toml")]
config: String,
/* Not yet supported
#[arg(short, long)]

View file

@ -14,7 +14,7 @@ pub(crate) struct Config {
/// - 2: Info
/// - 3: Debug
#[serde(default = "log_level", rename = "log-level")]
// the rename lets it use `log-level` instead in the yaml file - this is not an alias, `log_level` in the yaml will *not* work
// the rename lets it use `log-level` instead in the toml file - this is not an alias, `log_level` in the toml will *not* work
pub(crate) log_level: u8,
/// Maximum number of jobs to run simultaneously
#[serde(default = "max_jobs", rename = "max-jobs")]
@ -98,8 +98,8 @@ pub(crate) struct JobExitStatus {
}
pub(crate) fn config_from_file(filename: String) -> Config {
let yaml: Config = serde_yml::from_str(fs::read_to_string(filename).unwrap().as_str()).unwrap();
return yaml;
let toml: Config = toml::from_str(fs::read_to_string(filename).unwrap().as_str()).unwrap();
return toml;
}
// ==========================

View file

@ -5,6 +5,7 @@ use clap::{CommandFactory, Parser};
use clap_complete::aot::{generate, Bash, Elvish, Fish, PowerShell, Zsh};
use std::fs;
use std::fs::create_dir_all;
use std::fs::remove_dir_all;
use std::fs::write;
use std::fs::File;
use std::io::stdout;
@ -84,13 +85,30 @@ fn run_job(conf: Config, job: Job) -> JobExitStatus {
let container_name: String = format!("gregory-{}-{}-{}", job.id, job.revision, Uuid::now_v7());
let log_path = &format!("{}/logs/{container_name}.txt", conf.data_dir); // can't select fields in the format!() {} thing, have to do this
let log_dir: &Path = Path::new(log_path);
create_dir_all(log_dir.parent().unwrap()).unwrap();
write(log_path, job.commands.join("\n")).unwrap();
// create log path
let log_path = &format!("{}/logs/{container_name}", conf.data_dir); // can't select fields in the format!() {} thing, have to do this
let log_dir: &Path = Path::new(log_path).parent().unwrap();
create_dir_all(log_dir).unwrap();
// write the script
let script_path = &format!("{}/tmp/{container_name}.sh", conf.data_dir); // can't select fields in the format!() {} thing, have to do this
// create dir for the script
let script_dir: &Path = Path::new(script_path).parent().unwrap();
create_dir_all(script_dir).unwrap();
write(
script_path,
job.commands
//.iter()
//.map(|item| {
// // TODO: FIGURE OUT HOW TO HANDLE IT ESCAPING IT OR WHATEVER AAAAAAAAAAAAA
//})
//.collect::<Vec<String>>()
.join("\n"),
)
.unwrap();
// set permissions - *unix specific*
let mut perms = File::open(log_path)
let mut perms = File::open(script_path)
.unwrap()
.metadata()
.unwrap()
@ -103,13 +121,22 @@ fn run_job(conf: Config, job: Job) -> JobExitStatus {
format!("--name={container_name}"),
format!("--cpus={threads}"),
format!("--privileged={}", job.privileged),
format!("-v={log_path}:/gregory-entrypoint.sh"),
format!("--entrypoint=['{}', '/gregory-entrypoint.sh']", &job.shell),
job.clone().image
format!("-v={script_path}:/gregory-entrypoint.sh"),
format!(
"--entrypoint=[\"{}\", \"/gregory-entrypoint.sh\"]",
&job.shell
),
job.clone().image,
];
println!("{:?}", cmd_args);
let cmd_output = Command::new("podman").args(cmd_args).output().unwrap();
let elapsed = now.elapsed();
// remove tmp dir
remove_dir_all(script_dir).unwrap();
// write logs - TEMPORARY
write(log_path, &cmd_output.stdout).unwrap();
write(format!("{log_path}.err"), &cmd_output.stderr).unwrap();
println!("{:?}", cmd_output);

View file

@ -1,11 +1,18 @@
use crate::data;
#[cfg(test)]
use alphanumeric_sort::sort_str_slice;
use crate::data::*;
#[test]
fn test_config() {
// It's a pain to make the config manually so I'm just doing this lol
let conf = "Config { log_level: 0, max_jobs: 4, max_threads: 10, packages: {\"librewolf\": Package { compilation: Some(Job { threads: 8, image: \"docker.io/library/debian\", commands: [\"cd ~/librewolf\", \"./mach build\"], volumes: Some([\"librewolf\"]), privileged: false }), packaging: {\"fedora\": Job { threads: 8, image: \"docker.io/library/fedora\", commands: [\"git clone http://example.com/librewolf-fedora-packaging.git && cd librewolf-fedora-packaging/\", \"do-rpm-stuff-idk\"], volumes: Some([\"librewolf\"]), privileged: false }} }}, update_repo: {\"fedora\": Job { threads: 4, image: \"docker.io/library/fedora\", commands: [\"idkkkkk\"], volumes: Some([\"librewolf\"]), privileged: false }}, volumes: {\"librewolf\": \"./data/librewolf:/librewolf\"} }";
assert_eq!(format!("{:?}", config_from_file("gregory.example.yml".to_string())), conf);
let conf = "Config { log_level: 0, max_jobs: 4, max_threads: 10, data_dir: \"./data\", packages: {\"librewolf\": Package { compilation: Some(Job { id: \"1\", revision: \"2\", threads: 8, image: \"docker.io/library/debian\", commands: [\"echo hi\", \"echo helloooooooooo\"], volumes: Some([\"librewolf\"]), privileged: false, shell: \"/bin/sh\" }), packaging: {\"fedora\": Job { id: \"-1\", revision: \"1\", threads: 8, image: \"docker.io/library/fedora\", commands: [\"echo did you ever hear the tragedy of darth plageuis the wise?\", \"echo it\\\\'s not a story the jedi would tell you\"], volumes: Some([\"librewolf\"]), privileged: false, shell: \"/bin/sh\" }} }}, update_repo: {\"fedora\": Job { id: \"-1\", revision: \"1\", threads: 4, image: \"docker.io/library/fedora\", commands: [\"echo hai\"], volumes: Some([\"librewolf\"]), privileged: false, shell: \"/bin/sh\" }}, volumes: {\"librewolf\": \"./data/librewolf:/librewolf\"} }";
assert_eq!(
format!(
"{:?}",
data::config_from_file("gregory.example.toml".to_string())
),
conf
);
}
#[test]