-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic image pulling and dvs file experiments
- Loading branch information
1 parent
4a02ef7
commit c64d937
Showing
10 changed files
with
217 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
use crate::driver::Driver; | ||
|
||
pub struct DockerDriver {} | ||
impl DockerDriver { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
impl Driver for DockerDriver { | ||
// TODO: Support --platform | ||
fn pull_image(&self, image: &str) -> String { | ||
format!("docker pull {image}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
use crate::driver::docker::DockerDriver; | ||
|
||
mod docker; | ||
mod podman; | ||
|
||
pub trait Driver { | ||
fn pull_image(&self, image: &str) -> String; | ||
} | ||
|
||
pub fn get_bundled(item: &str) -> Option<Box<dyn Driver>> { | ||
match item { | ||
"bundled-docker" => Some(Box::new(DockerDriver::new())), | ||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use thiserror::Error; | ||
|
||
#[allow(dead_code)] // TODO: Remove | ||
#[derive(Debug, Error)] | ||
pub enum CommandError { | ||
#[error("The child process exited with code `{0}`")] | ||
ExitCode(i32), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[info] | ||
id = "rust" # dvs id | ||
|
||
[container] | ||
workspace = "/project" # -w | ||
mount_pwd = "/project" # --mount type=bind,source="$(pwd)",target=/project | ||
mounts = ["$HOME/.cargo/registry:/usr/local/cargo/registry"] # --mount type=bind,source=$HOME/.cargo/registry,target=/usr/local/cargo/registry | ||
env = ["CARGO_TARGET_DIR=/target"] # -e CARGO_TARGET_DIR=/target | ||
|
||
[image] | ||
default = "latest" | ||
versions = [ | ||
"latest", | ||
"alpine", | ||
"bookworm", | ||
"bullseye", | ||
"buster", | ||
"slim", | ||
"slim-bookworm", | ||
"slim-bullseye", | ||
"slim-buster", | ||
] | ||
pull = "rust" # Applies to all image unless overridden | ||
pull_updates = true | ||
entrypoint = "/bin/sh" | ||
cmd = "-c /bin/bash" | ||
|
||
[image.alpine] | ||
entrypoint = "/bin/sh" | ||
cmd = "-c /bin/sh" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use phf::phf_map; | ||
|
||
static BUNDLED_MAP: phf::Map<&'static str, &'static str> = phf_map! { | ||
"rust" => include_str!("./bundled/rust.toml") | ||
}; | ||
|
||
pub fn get_bundled(item: &str) -> Option<&&'static str> { | ||
BUNDLED_MAP.get(item) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,74 @@ | ||
use crate::cli::ArgCommand; | ||
use clap::Parser; | ||
use std::{ffi::OsString, process::Command}; | ||
use toml::Table; | ||
|
||
mod cli; | ||
mod driver; | ||
mod errors; | ||
mod image; | ||
|
||
#[cfg(target_family = "wasm")] | ||
compile_error!("Wasm is not a supported target!"); | ||
|
||
fn main() { | ||
let _args = cli::Args::parse(); | ||
let args = cli::Args::parse(); | ||
#[cfg(debug_assertions)] | ||
dbg!(_args); | ||
dbg!(&args); | ||
|
||
// TODO: Get config here! | ||
let driver = driver::get_bundled("bundled-docker").unwrap(); | ||
|
||
match &args.subcommand { | ||
ArgCommand::Run { id } => { | ||
let image = image::get_bundled(id).unwrap(); | ||
let image = image.parse::<Table>().unwrap(); | ||
|
||
let pull_img = image["image"]["pull"].as_str().unwrap(); | ||
let pull_img_default = image["image"]["default"].as_str().unwrap(); | ||
let pull_img = if !pull_img.contains(':') { | ||
format!("{pull_img}:{pull_img_default}") | ||
} | ||
else { | ||
pull_img.to_string() | ||
}; | ||
|
||
dbg!(&pull_img); | ||
|
||
let cmd = driver.pull_image(&pull_img); | ||
run_cmd(&cmd, None).expect("FFF"); | ||
} | ||
_ => todo!(), | ||
} | ||
} | ||
|
||
fn run_cmd(cmd: &str, end: Option<OsString>) -> Result<(), String> { | ||
#[cfg(target_family = "unix")] | ||
let mut command = Command::new("sh"); | ||
#[cfg(target_family = "unix")] | ||
command.arg("-c"); | ||
|
||
#[cfg(target_family = "windows")] | ||
let mut command = Command::new("cmd"); | ||
#[cfg(target_family = "windows")] | ||
command.arg("/C"); | ||
|
||
command.arg(cmd); | ||
if let Some(str) = end { | ||
command.arg(str); | ||
} | ||
|
||
dbg!(&command); | ||
|
||
// let out = command.status().expect("exec failed"); | ||
|
||
let child = command.spawn().expect("Could not spawn child process."); | ||
let _out = child.wait_with_output(); | ||
|
||
// match command.spawn().and_then(|mut child| child.wait()) { | ||
// Ok(status) => std::process::exit(status.code().unwrap_or(1)), | ||
// Err(error) => die!("fatal: {}", error), | ||
// }; | ||
|
||
println!("Hello, world!"); | ||
Ok(()) | ||
} |