Skip to content

Commit

Permalink
Merge pull request #15 from AaronKutch/dev11
Browse files Browse the repository at this point in the history
Dev11
  • Loading branch information
AaronKutch authored Apr 6, 2024
2 parents d73659a + 935934a commit 763fdcf
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 79 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Changelog

## [0.10.0] - 2024-1-20
## [0.11.0] - 2025-04-06
### Fixes
- Fixed path canonicalization on Windows to use `dunce::simplify` to avoid UNC paths

### Changes
- Replaced `auto_exec_i` with `auto_exec` which allows customizing arguments
- Removed the `log` dependency in favor of `tracing`
- Removed `std_init`

### Additions
- Added a `workdir` option to `Container`

## [0.10.0] - 2024-01-20
### Fixes
- Fixed compilation on Windows
- Fixed an issue with an example
Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "super_orchestrator"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
authors = ["Aaron Kutch <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand All @@ -11,11 +11,9 @@ keywords = ["container", "docker"]
description = "programmable container orchestration tools"

[dependencies]
clap = "4"
ctrlc = { version = "3", default-features = false }
env_logger = { version = "0.11", default-features = false }
log = "0.4"
nix = { version = "0.27", optional = true, default-features = false, features = ["signal"] }
dunce = "1.0"
nix = { version = "0.28", optional = true, default-features = false, features = ["signal"] }
owo-colors = { version = "4.0", default-features = false }
postcard = { version = "1", features = ["use-std"] }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -25,10 +23,12 @@ stacked_errors = { version = "0.5", default-features = false }
#stacked_errors = { git = "https://github.com/AaronKutch/stacked_errors", rev = "74d52fd24ff7ec1faab4f2065f37ff356f089137", default-features = false }
#stacked_errors = { path = "../stacked_errors", default-features = false }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
clap = { version = "4", features = ["derive", "env"] }
tracing-subscriber = "0.3"

[features]
default = []
Expand Down
28 changes: 28 additions & 0 deletions examples/auto_exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use clap::Parser;
use stacked_errors::Result;
use super_orchestrator::{ctrlc_init, docker_helpers::auto_exec};

/// Runs `super_orchestrator::docker_helpers::auto_exec`
#[derive(Parser, Debug)]
#[command(about)]
struct Args {
/// Prefix of the name of the container
#[arg(short, long)]
prefix: String,
/// Adds the `-t` argument to use a TTY, may be needed on Windows to get
/// around issues with carriage returns being passed.
#[arg(short, long, default_value_t = false)]
tty: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt().init();
ctrlc_init()?;
let args = Args::parse();
auto_exec(if args.tty { ["-it"] } else { ["-i"] }, &args.prefix, [
"sh",
])
.await?;
Ok(())
}
21 changes: 0 additions & 21 deletions examples/auto_exec_i.rs

This file was deleted.

6 changes: 3 additions & 3 deletions examples/basic_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::time::Duration;

use stacked_errors::{ensure, ensure_eq, StackableErr};
use super_orchestrator::{
sh, stacked_errors::Result, std_init, Command, CommandResult, CommandResultNoDebug, FileOptions,
sh, stacked_errors::Result, Command, CommandResult, CommandResultNoDebug, FileOptions,
};
use tokio::time::sleep;

#[tokio::main]
async fn main() -> Result<()> {
// logging to detect bad drops
std_init()?;
// tracing to detect bad drops
tracing_subscriber::fmt().init();

println!("example 0\n");

Expand Down
4 changes: 2 additions & 2 deletions examples/basic_containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use stacked_errors::{ensure, ensure_eq, Result, StackableErr};
use super_orchestrator::{
docker::{Container, ContainerNetwork, Dockerfile},
net_message::wait_for_ok_lookup_host,
std_init, FileOptions,
FileOptions,
};

const TIMEOUT: Duration = Duration::from_secs(300);

#[tokio::main]
async fn main() -> Result<()> {
std_init()?;
tracing_subscriber::fmt().init();
let logs_dir = "./logs";

println!("\n\nexample 0\n");
Expand Down
5 changes: 2 additions & 3 deletions examples/clean.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use stacked_errors::{ensure, StackableErr};
use super_orchestrator::{
acquire_dir_path, acquire_file_path, remove_files_in_dir, stacked_errors::Result, std_init,
FileOptions,
acquire_dir_path, acquire_file_path, remove_files_in_dir, stacked_errors::Result, FileOptions,
};
use tokio::fs;

#[tokio::main]
async fn main() -> Result<()> {
std_init()?;
tracing_subscriber::fmt().init();

// note: in regular use you would use `.await.stack()?` on the ends
// to tell what lines are failing
Expand Down
6 changes: 2 additions & 4 deletions examples/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::iter;

use clap::Parser;
use stacked_errors::{ensure, ensure_eq, StackableErr};
use super_orchestrator::{
remove_files_in_dir, stacked_errors::Result, std_init, Command, FileOptions,
};
use super_orchestrator::{remove_files_in_dir, stacked_errors::Result, Command, FileOptions};

// this program calls itself to get stdout and stderr examples
#[derive(Parser, Debug)]
Expand All @@ -20,7 +18,7 @@ struct Args {

#[tokio::main]
async fn main() -> Result<()> {
std_init()?;
tracing_subscriber::fmt().init();
let args = Args::parse();

if args.print {
Expand Down
17 changes: 9 additions & 8 deletions examples/docker_entrypoint_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
//! Note: change working directory to this crate's root in two separate
//! terminals. In one terminal, run
//! `cargo r --example auto_exec_i -- --container-name container0`
//! and in the other `cargo r --example docker_entrypoint_pattern`. The first
//! terminal should catch a running container, and you can run commands on it or
//! ctrl-c to end the container early. The second will finish after building and
//! 20 seconds.
//! `cargo r --example auto_exec -- --container-name container0`
//! and in the other `cargo r --example docker_entrypoint_pattern`. Note that on
//! windows you will need to use WSL 2 or else the cross compilation will fail
//! at linking stage. The first terminal should catch a running container, and
//! you can run commands on it or ctrl-c to end the container early. The second
//! will finish after building and 20 seconds.
use std::time::Duration;

use clap::Parser;
use log::info;
use stacked_errors::{ensure_eq, Error, Result, StackableErr};
use super_orchestrator::{
docker::{Container, ContainerNetwork, Dockerfile},
net_message::NetMessenger,
sh, std_init, FileOptions,
sh, FileOptions,
};
use tokio::time::sleep;
use tracing::info;

const TIMEOUT: Duration = Duration::from_secs(300);
const STD_TRIES: u64 = 300;
Expand Down Expand Up @@ -45,7 +46,7 @@ struct Args {

#[tokio::main]
async fn main() -> Result<()> {
std_init()?;
tracing_subscriber::fmt().init();
let args = Args::parse();

if let Some(ref s) = args.entry_name {
Expand Down
6 changes: 3 additions & 3 deletions examples/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use std::time::Duration;

use clap::Parser;
use log::info;
use super_orchestrator::{
acquire_dir_path,
docker::{Container, ContainerNetwork, Dockerfile},
sh,
stacked_errors::{Error, Result, StackableErr},
std_init, wait_for_ok, Command,
wait_for_ok, Command,
};
use tokio::{fs, time::sleep};
use tracing::info;

// time until the program ends after everything is deployed
const END_TIMEOUT: Duration = Duration::from_secs(1_000_000_000);
Expand Down Expand Up @@ -52,7 +52,7 @@ struct Args {

#[tokio::main]
async fn main() -> Result<()> {
std_init()?;
tracing_subscriber::fmt().init();
let args = Args::parse();

if let Some(ref s) = args.entry_name {
Expand Down
6 changes: 3 additions & 3 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
time::Duration,
};

use log::warn;
use owo_colors::AnsiColors;
use serde::{Deserialize, Serialize};
use stacked_errors::{DisplayStr, Error, Result, StackableErr};
Expand All @@ -23,6 +22,7 @@ use tokio::{
task::{self, JoinHandle},
time::{sleep, timeout},
};
use tracing::warn;

use crate::{acquire_dir_path, next_terminal_color, FileOptions};

Expand Down Expand Up @@ -131,7 +131,7 @@ impl Debug for Command {
/// to make a quick copy or other operation, because the task to record program
/// outputs needs the lock to progress.
///
/// If the `log` crate is used and an implementor is active, warnings from
/// If the `tracing` crate is used and a subscriber is active, warnings from
/// bad `Drop`s can be issued
///
/// The `Default` impl is for if an empty runner not attached to anything is
Expand Down Expand Up @@ -406,7 +406,7 @@ impl Command {
let mut args: Vec<OsString> = vec![];
for (i, part) in program_with_args.as_ref().split_whitespace().enumerate() {
if i == 0 {
program = part.to_owned();
part.clone_into(&mut program)
} else {
args.push(part.into());
}
Expand Down
16 changes: 15 additions & 1 deletion src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::{
time::Duration,
};

use log::{info, warn};
use serde::{Deserialize, Serialize};
use stacked_errors::{Error, Result, StackableErr};
use tokio::time::{sleep, Instant};
use tracing::{info, warn};
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -83,6 +83,8 @@ pub struct Container {
/// Passed as `--volume` to the create args, but these have the advantage of
/// being canonicalized and prechecked
pub volumes: Vec<(String, String)>,
/// Working directory inside the container
pub workdir: Option<String>,
/// Environment variable pairs passed to docker
pub environment_vars: Vec<(String, String)>,
/// When set, this indicates that the container should run an entrypoint
Expand All @@ -105,6 +107,7 @@ impl Container {
build_args: vec![],
create_args: vec![],
volumes: vec![],
workdir: None,
environment_vars: vec![],
entrypoint_file: None,
entrypoint_args: vec![],
Expand Down Expand Up @@ -224,6 +227,12 @@ impl Container {
self
}

/// Sets the working directory inside the container
pub fn workdir<S: AsRef<str>>(mut self, workdir: S) -> Self {
self.workdir = Some(workdir.as_ref().to_string());
self
}

/// Add arguments to be passed to the entrypoint
pub fn entrypoint_args<I, S>(mut self, entrypoint_args: I) -> Self
where
Expand Down Expand Up @@ -683,6 +692,11 @@ impl ContainerNetwork {
&full_name,
];

if let Some(workdir) = container.workdir.as_ref() {
args.push("-w");
args.push(workdir)
}

let mut tmp = vec![];
for var in &container.environment_vars {
tmp.push(format!("{}={}", var.0, var.1));
Expand Down
Loading

0 comments on commit 763fdcf

Please sign in to comment.