use serde::Deserialize;
use std::collections::HashMap;
#[derive(Debug, Clone, Deserialize)]
pub struct BuildArg {
pub name: String,
pub default: Option<String>,
}
impl BuildArg {
#[allow(dead_code)]
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
default: None,
}
}
#[allow(dead_code)]
pub fn with_default(mut self, default: &str) -> Self {
self.default = Some(default.to_string());
self
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct ExposePort {
pub port: u16,
#[serde(default = "default_protocol")]
pub protocol: String,
}
fn default_protocol() -> String {
"tcp".to_string()
}
impl ExposePort {
pub fn tcp(port: u16) -> Self {
Self {
port,
protocol: "tcp".to_string(),
}
}
#[allow(dead_code)]
pub fn udp(port: u16) -> Self {
Self {
port,
protocol: "udp".to_string(),
}
}
pub fn parse(s: &str) -> Option<Self> {
let parts: Vec<&str> = s.split('/').collect();
let port = parts.first()?.parse().ok()?;
let protocol = parts
.get(1)
.map(|s| s.to_string())
.unwrap_or_else(default_protocol);
Some(Self { port, protocol })
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct CopySpec {
pub src: String,
pub dest: String,
pub mode: Option<u32>,
pub owner: Option<String>,
}
impl CopySpec {
pub fn new(src: &str, dest: &str) -> Self {
Self {
src: src.to_string(),
dest: dest.to_string(),
mode: None,
owner: None,
}
}
#[allow(dead_code)]
pub fn with_mode(mut self, mode: u32) -> Self {
self.mode = Some(mode);
self
}
#[allow(dead_code)]
pub fn with_owner(mut self, owner: &str) -> Self {
self.owner = Some(owner.to_string());
self
}
}
#[derive(Debug, Clone)]
pub enum Instruction {
From(String),
Arg(BuildArg),
Env(String, String),
Run(String),
Copy(CopySpec),
Workdir(String),
Expose(ExposePort),
Cmd(String),
Entrypoint(String),
User(String),
Label(String, String),
Volume(String),
#[allow(dead_code)]
Comment(String),
}
impl Instruction {
pub fn name(&self) -> &'static str {
match self {
Instruction::From(_) => "FROM",
Instruction::Arg(_) => "ARG",
Instruction::Env(_, _) => "ENV",
Instruction::Run(_) => "RUN",
Instruction::Copy(_) => "COPY",
Instruction::Workdir(_) => "WORKDIR",
Instruction::Expose(_) => "EXPOSE",
Instruction::Cmd(_) => "CMD",
Instruction::Entrypoint(_) => "ENTRYPOINT",
Instruction::User(_) => "USER",
Instruction::Label(_, _) => "LABEL",
Instruction::Volume(_) => "VOLUME",
Instruction::Comment(_) => "#",
}
}
}
#[derive(Debug, Clone, Default, Deserialize)]
pub struct JailfileMetadata {
pub name: Option<String>,
pub version: Option<String>,
pub description: Option<String>,
#[allow(dead_code)]
pub author: Option<String>,
#[serde(default)]
#[allow(dead_code)]
pub labels: HashMap<String, String>,
}
#[derive(Debug, Clone)]
pub struct Jailfile {
pub metadata: JailfileMetadata,
pub from: Option<String>,
pub args: Vec<BuildArg>,
pub instructions: Vec<Instruction>,
pub cmd: Option<String>,
pub entrypoint: Option<String>,
pub workdir: Option<String>,
pub user: Option<String>,
pub expose: Vec<ExposePort>,
pub volumes: Vec<String>,
pub env: HashMap<String, String>,
}
impl Default for Jailfile {
fn default() -> Self {
Self::new()
}
}
impl Jailfile {
pub fn new() -> Self {
Self {
metadata: JailfileMetadata::default(),
from: None,
args: Vec::new(),
instructions: Vec::new(),
cmd: None,
entrypoint: None,
workdir: None,
user: None,
expose: Vec::new(),
volumes: Vec::new(),
env: HashMap::new(),
}
}
#[allow(dead_code)] pub fn from_release(release: &str) -> Self {
let mut jf = Self::new();
jf.from = Some(release.to_string());
jf.instructions.push(Instruction::From(release.to_string()));
jf
}
#[allow(dead_code)] pub fn arg(mut self, name: &str, default: Option<&str>) -> Self {
let arg = BuildArg {
name: name.to_string(),
default: default.map(String::from),
};
self.args.push(arg.clone());
self.instructions.push(Instruction::Arg(arg));
self
}
#[allow(dead_code)] pub fn env(mut self, name: &str, value: &str) -> Self {
self.env.insert(name.to_string(), value.to_string());
self.instructions
.push(Instruction::Env(name.to_string(), value.to_string()));
self
}
#[allow(dead_code)] pub fn run(mut self, command: &str) -> Self {
self.instructions
.push(Instruction::Run(command.to_string()));
self
}
#[allow(dead_code)] pub fn copy(mut self, src: &str, dest: &str) -> Self {
let spec = CopySpec::new(src, dest);
self.instructions.push(Instruction::Copy(spec));
self
}
#[allow(dead_code)] pub fn workdir(mut self, path: &str) -> Self {
self.workdir = Some(path.to_string());
self.instructions
.push(Instruction::Workdir(path.to_string()));
self
}
#[allow(dead_code)] pub fn expose(mut self, port: u16, protocol: &str) -> Self {
let exp = ExposePort {
port,
protocol: protocol.to_string(),
};
self.expose.push(exp.clone());
self.instructions.push(Instruction::Expose(exp));
self
}
#[allow(dead_code)] pub fn cmd(mut self, command: &str) -> Self {
self.cmd = Some(command.to_string());
self.instructions
.push(Instruction::Cmd(command.to_string()));
self
}
#[allow(dead_code)] pub fn entrypoint(mut self, command: &str) -> Self {
self.entrypoint = Some(command.to_string());
self.instructions
.push(Instruction::Entrypoint(command.to_string()));
self
}
#[allow(dead_code)] pub fn user(mut self, user: &str) -> Self {
self.user = Some(user.to_string());
self.instructions.push(Instruction::User(user.to_string()));
self
}
#[allow(dead_code)] pub fn base_release(&self) -> Option<&str> {
self.from.as_deref()
}
#[allow(dead_code)] pub fn run_commands(&self) -> Vec<&str> {
self.instructions
.iter()
.filter_map(|i| match i {
Instruction::Run(cmd) => Some(cmd.as_str()),
_ => None,
})
.collect()
}
#[allow(dead_code)] pub fn copy_specs(&self) -> Vec<&CopySpec> {
self.instructions
.iter()
.filter_map(|i| match i {
Instruction::Copy(spec) => Some(spec),
_ => None,
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_arg() {
let arg = BuildArg::new("VERSION").with_default("1.0");
assert_eq!(arg.name, "VERSION");
assert_eq!(arg.default, Some("1.0".to_string()));
}
#[test]
fn test_expose_port_parse() {
let tcp = ExposePort::parse("80/tcp").unwrap();
assert_eq!(tcp.port, 80);
assert_eq!(tcp.protocol, "tcp");
let udp = ExposePort::parse("53/udp").unwrap();
assert_eq!(udp.port, 53);
assert_eq!(udp.protocol, "udp");
let default = ExposePort::parse("443").unwrap();
assert_eq!(default.port, 443);
assert_eq!(default.protocol, "tcp");
}
#[test]
fn test_jailfile_builder() {
let jf = Jailfile::from_release("14.2-RELEASE")
.arg("VERSION", Some("1.0"))
.env("PATH", "/usr/local/bin:/usr/bin")
.run("pkg install -y nginx")
.copy("nginx.conf", "/usr/local/etc/nginx/nginx.conf")
.workdir("/usr/local")
.expose(80, "tcp")
.cmd("/usr/sbin/service nginx start");
assert_eq!(jf.from, Some("14.2-RELEASE".to_string()));
assert_eq!(jf.args.len(), 1);
assert_eq!(jf.run_commands().len(), 1);
assert_eq!(jf.copy_specs().len(), 1);
assert_eq!(jf.expose.len(), 1);
}
#[test]
fn test_instruction_names() {
assert_eq!(Instruction::From("test".to_string()).name(), "FROM");
assert_eq!(Instruction::Run("test".to_string()).name(), "RUN");
assert_eq!(Instruction::Copy(CopySpec::new("a", "b")).name(), "COPY");
}
}