use crate::stdio::Stdio;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Resources {
pub cpus: u32,
pub memory_in_bytes: u64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Mount {
pub readonly: bool,
pub source: PathBuf,
pub target: PathBuf,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
IntoGuest,
OutOfGuest,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SocketRelay {
pub source: PathBuf,
pub target: PathBuf,
pub mode: u32,
pub direction: Direction,
}
impl SocketRelay {
pub fn into_guest(source: impl Into<PathBuf>, target: impl Into<PathBuf>) -> Self {
Self {
source: source.into(),
target: target.into(),
mode: 0o666,
direction: Direction::IntoGuest,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Network {
pub ipv4_address: String,
pub ipv4_gateway: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BootSpec {
pub name: String,
pub image: String,
pub resources: Resources,
pub arguments: Vec<String>,
pub environment: Vec<String>,
pub mounts: Vec<Mount>,
pub sockets: Vec<SocketRelay>,
pub workdir: Option<PathBuf>,
pub network: Network,
pub rootfs_capacity_in_bytes: u64,
}
impl BootSpec {
pub const DEFAULT_ROOTFS_CAPACITY_IN_BYTES: u64 = 8 * 1024 * 1024 * 1024;
pub fn new(name: impl Into<String>, image: impl Into<String>, resources: Resources, network: Network) -> Self {
Self {
name: name.into(),
image: image.into(),
resources,
arguments: Vec::new(),
environment: Vec::new(),
mounts: Vec::new(),
sockets: Vec::new(),
workdir: None,
network,
rootfs_capacity_in_bytes: Self::DEFAULT_ROOTFS_CAPACITY_IN_BYTES,
}
}
}
#[derive(Debug)]
pub struct ExecRequest {
pub name: String,
pub id: String,
pub arguments: Vec<String>,
pub environment: Vec<String>,
pub user: Option<String>,
pub workdir: Option<PathBuf>,
pub term: Option<String>,
pub stdio: Stdio,
}
impl ExecRequest {
pub const DEFAULT_TERM: &'static str = "xterm";
pub fn new(name: impl Into<String>, id: impl Into<String>, arguments: Vec<String>, stdio: Stdio) -> Self {
Self {
name: name.into(),
id: id.into(),
arguments,
environment: Vec::new(),
user: None,
workdir: None,
term: Some(Self::DEFAULT_TERM.to_string()),
stdio,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BuildStep {
pub name: String,
pub script: String,
pub user: Option<String>,
pub cache_key: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BuildMount {
pub destination: String,
pub readonly: bool,
pub source: PathBuf,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Shell(pub Vec<String>);
impl Default for Shell {
fn default() -> Self {
Self(
["/bin/bash", "-euo", "pipefail", "-c"]
.map(String::from)
.to_vec(),
)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CachePolicy {
pub restore: bool,
pub keep: usize,
pub keep_for: Duration,
}
impl Default for CachePolicy {
fn default() -> Self {
Self {
restore: true,
keep: 24,
keep_for: Duration::from_secs(14 * 24 * 60 * 60),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BuildPlan {
pub name: String,
pub base: String,
pub tag: String,
pub resources: Resources,
pub mounts: Vec<BuildMount>,
pub steps: Vec<BuildStep>,
pub environment: Vec<String>,
pub labels: BTreeMap<String, String>,
pub user: Option<String>,
pub workdir: Option<PathBuf>,
pub network: Network,
pub base_key: String,
pub rootfs_capacity_in_bytes: u64,
pub cache: CachePolicy,
pub shell: Shell,
pub keepalive: Vec<String>,
pub reclaim: bool,
}
impl BuildPlan {
pub fn default_keepalive() -> Vec<String> {
["/bin/sh", "-c", "while :; do sleep 86400; done"]
.map(String::from)
.to_vec()
}
pub fn new(
name: impl Into<String>,
base: impl Into<String>,
tag: impl Into<String>,
resources: Resources,
network: Network,
base_key: impl Into<String>,
) -> Self {
Self {
name: name.into(),
base: base.into(),
tag: tag.into(),
resources,
mounts: Vec::new(),
steps: Vec::new(),
environment: Vec::new(),
labels: BTreeMap::new(),
user: None,
workdir: None,
network,
base_key: base_key.into(),
rootfs_capacity_in_bytes: BootSpec::DEFAULT_ROOTFS_CAPACITY_IN_BYTES,
cache: CachePolicy::default(),
shell: Shell::default(),
keepalive: Self::default_keepalive(),
reclaim: true,
}
}
}