1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
use std::collections::HashSet;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FResult};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::common::{CallPattern, Parameter, Type};
use crate::package::{Capability, PackageKind};
use crate::version::Version;
/***** CUSTOM TYPES *****/
type Map<T> = std::collections::HashMap<String, T>;
/***** ERRORS *****/
/// Defines error(s) for the VolumeBind struct.
#[derive(Debug)]
pub enum VolumeBindError {
/// The given path is not an absolute path.
PathNotAbsolute { path: PathBuf },
}
impl Display for VolumeBindError {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
use VolumeBindError::*;
match self {
PathNotAbsolute { path } => write!(f, "Given path '{}' is not absolute", path.display()),
}
}
}
impl std::error::Error for VolumeBindError {}
/// Collect errors relating to the LocalContainer specification.
#[derive(Debug)]
pub enum LocalContainerInfoError {
/// Could not open the target file
FileOpenError { path: PathBuf, err: std::io::Error },
/// Could not parse the target file
FileParseError { err: serde_yaml::Error },
/// Could not create the target file
FileCreateError { path: PathBuf, err: std::io::Error },
/// Could not write to the given writer
FileWriteError { err: serde_yaml::Error },
}
impl Display for LocalContainerInfoError {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
match self {
LocalContainerInfoError::FileOpenError { path, err } => write!(f, "Could not open local container file '{}': {}", path.display(), err),
LocalContainerInfoError::FileParseError { err } => write!(f, "Could not read & parse local container file: {err}"),
LocalContainerInfoError::FileCreateError { path, err } => {
write!(f, "Could not create local container file '{}': {}", path.display(), err)
},
LocalContainerInfoError::FileWriteError { err } => write!(f, "Could not serialize & write local container file: {err}"),
}
}
}
impl Error for LocalContainerInfoError {}
/// Collects errors relating to the Container specification.
#[derive(Debug)]
pub enum ContainerInfoError {
/// Could not open the target file
FileReadError { path: PathBuf, err: std::io::Error },
/// Could not parse the target file
ParseError { err: serde_yaml::Error },
/// Could not create the target file
FileCreateError { path: PathBuf, err: std::io::Error },
/// Could not write to the given writer
FileWriteError { err: serde_yaml::Error },
}
impl Display for ContainerInfoError {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
match self {
ContainerInfoError::FileReadError { path, err } => write!(f, "Could not open & read container file '{}': {}", path.display(), err),
ContainerInfoError::ParseError { err } => write!(f, "Could not parse container file YAML: {err}"),
ContainerInfoError::FileCreateError { path, err } => write!(f, "Could not create container file '{}': {}", path.display(), err),
ContainerInfoError::FileWriteError { err } => write!(f, "Could not serialize & write container file: {err}"),
}
}
}
impl Error for ContainerInfoError {}
/***** SPECIFICATIONS *****/
/// A special struct that prints a given VolumeBindOption as a Docker-compatible string.
#[derive(Debug)]
pub struct VolumeBindOptionDockerDisplay<'a> {
/// The VolumeBindOption to show.
option: &'a VolumeBindOption,
}
impl<'a> Display for VolumeBindOptionDockerDisplay<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
use VolumeBindOption::*;
match self.option {
ReadOnly => write!(f, "ro"),
}
}
}
/// Defines possible options for a Docker volume binding.
#[derive(Clone, Debug)]
pub enum VolumeBindOption {
/// The volume is read-only.
ReadOnly,
}
impl VolumeBindOption {
/// Returns a formatter that writes a Docker-compatible version of this VolumeBindOption.
#[inline]
pub fn docker(&self) -> VolumeBindOptionDockerDisplay { VolumeBindOptionDockerDisplay { option: self } }
}
/// A special struct that prints a given VolumeBind as a Docker-compatible string.
#[derive(Debug)]
pub struct VolumeBindDockerDisplay<'a> {
/// The VolumeBind to show.
bind: &'a VolumeBind,
}
impl<'a> Display for VolumeBindDockerDisplay<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
write!(
f,
"{}:{}{}",
self.bind.host.display(),
self.bind.container.display(),
if !self.bind.options.is_empty() {
format!(":{}", self.bind.options.iter().map(|o| o.docker().to_string()).collect::<Vec<String>>().join(","))
} else {
String::new()
}
)
}
}
/// Defines a single Docker volume binding.
#[derive(Clone, Debug)]
pub struct VolumeBind {
/// The source location, on the host.
pub host: PathBuf,
/// The target location, on the container.
pub container: PathBuf,
/// Any options for the bind.
pub options: Vec<VolumeBindOption>,
}
impl VolumeBind {
/// Constructor for VolumeBind that does not initialize it with special options.
///
/// # Arguments
/// - `host`: The path to the host folder. Note that this path must be absolute.
/// - `container`: The path to the container folder. Note that this path must be absolute.
/// - `options`: The VolumeBindOptions that further customize the bind(s).
///
/// # Returns
/// A new VolumeBind instance.
///
/// # Errors
/// This function may error if either of the given paths is not actually absolute.
pub fn new(host: impl Into<PathBuf>, container: impl Into<PathBuf>, options: Vec<VolumeBindOption>) -> Result<Self, VolumeBindError> {
let host: PathBuf = host.into();
let container: PathBuf = container.into();
// Sanity check both paths are absolute
if !host.is_absolute() {
return Err(VolumeBindError::PathNotAbsolute { path: host });
}
if !container.is_absolute() {
return Err(VolumeBindError::PathNotAbsolute { path: container });
}
// Add them
Ok(Self { host, container, options })
}
/// Constructor for VolumeBind that initializes it as a read-only bind.
///
/// # Arguments
/// - `host`: The path to the host folder. Note that this path must be absolute.
/// - `container`: The path to the container folder. Note that this path must be absolute.
///
/// # Returns
/// A new VolumeBind instance.
///
/// # Errors
/// This function may error if either of the given paths is not actually absolute.
#[inline]
pub fn new_readonly(host: impl Into<PathBuf>, container: impl Into<PathBuf>) -> Result<Self, VolumeBindError> {
Self::new(host, container, vec![VolumeBindOption::ReadOnly])
}
/// Constructor for VolumeBind that initializes it as a read/write bind.
///
/// # Arguments
/// - `host`: The path to the host folder. Note that this path must be absolute.
/// - `container`: The path to the container folder. Note that this path must be absolute.
///
/// # Returns
/// A new VolumeBind instance.
///
/// # Errors
/// This function may error if either of the given paths is not actually absolute.
#[inline]
pub fn new_readwrite(host: impl Into<PathBuf>, container: impl Into<PathBuf>) -> Result<Self, VolumeBindError> {
Self::new(host, container, vec![])
}
/// Returns a formatter that writes a Docker-compatible version of this VolumeBindOption.
#[inline]
pub fn docker(&self) -> VolumeBindDockerDisplay { VolumeBindDockerDisplay { bind: self } }
}
/// Serializes an Image to a way that Docker likes.
#[derive(Debug)]
pub struct ImageDockerFormatter<'a> {
/// The image to format
image: &'a Image,
}
impl<'a> Display for ImageDockerFormatter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
write!(
f,
"{}",
if let Some(digest) = &self.image.digest {
digest[7..].into()
} else {
format!("{}{}", self.image.name, if let Some(version) = &self.image.version { format!(":{version}") } else { String::new() })
}
)
}
}
/// Specifies the name of an Image, possibly with digest.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Image {
/// The name/label of the image.
pub name: String,
/// The version/label of the image, if any.
pub version: Option<String>,
/// If we know a digest of the image, this field tells us it.
pub digest: Option<String>,
}
impl Image {
/// Constructor for the Image that instantiates it with the given name.
///
/// # Arguments
/// - `name`: The name/label of the image.
/// - `digest`: The digest of the image if this is already known.
///
/// # Returns
/// A new Image instance.
#[inline]
pub fn new(name: impl Into<String>, version: Option<impl Into<String>>, digest: Option<impl Into<String>>) -> Self {
Self { name: name.into(), version: version.map(|v| v.into()), digest: digest.map(|d| d.into()) }
}
/// Returns the name-part of the Image (i.e., the name + version).
#[inline]
pub fn name(&self) -> String { format!("{}{}", self.name, if let Some(version) = &self.version { format!(":{version}") } else { String::new() }) }
/// Returns the digest-part of the Image.
#[inline]
pub fn digest(&self) -> Option<&str> { self.digest.as_deref() }
/// Returns the Docker-compatible serialization of this Image.
///
/// # Returns
/// An ImageDockerFormatter which handles the formatting.
#[inline]
pub fn docker(&self) -> ImageDockerFormatter { ImageDockerFormatter { image: self } }
}
impl Display for Image {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
write!(
f,
"{}{}{}",
self.name,
if let Some(version) = &self.version { format!(":{version}") } else { String::new() },
if let Some(digest) = &self.digest { format!("@{digest}") } else { String::new() }
)
}
}
impl AsRef<Image> for Image {
#[inline]
fn as_ref(&self) -> &Self { self }
}
impl From<&Image> for Image {
#[inline]
fn from(value: &Image) -> Self { value.clone() }
}
impl From<&mut Image> for Image {
#[inline]
fn from(value: &mut Image) -> Self { value.clone() }
}
impl From<String> for Image {
fn from(s: String) -> Self { Self::from(s.as_str()) }
}
impl From<&String> for Image {
fn from(s: &String) -> Self { Self::from(s.as_str()) }
}
impl From<&str> for Image {
fn from(s: &str) -> Self {
// First, split between digest and rest, if any '@' is present
let (rest, digest): (&str, Option<&str>) = if let Some(idx) = s.rfind('@') { (&s[..idx], Some(&s[idx + 1..])) } else { (s, None) };
// Next, search if there is a version or something
let (name, version): (&str, Option<&str>) = if let Some(idx) = s.rfind(':') { (&rest[..idx], Some(&rest[idx + 1..])) } else { (rest, None) };
// We can combine those in an Image
Image { name: name.into(), version: version.map(|s| s.into()), digest: digest.map(|s| s.into()) }
}
}
/// Specifies the contents of a contaienr info YAML file that is inside the container itself.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LocalContainerInfo {
/// The name of the package
pub name: String,
/// The kind of the package
pub kind: PackageKind,
/// The entrypoint to the package
pub entrypoint: Entrypoint,
/// The list of actions that this package can do.
pub actions: Map<Action>,
/// The list of types that are declared in this package.
pub types: Map<Type>,
}
impl LocalContainerInfo {
/// Constructor for the LocalContainerInfo that constructs it from the given path.
///
/// **Generic types**
/// * `P`: The Path-like type of the path given.
///
/// **Arguments**
/// * `path`: the Path to read the new LocalContainerInfo from.
///
/// **Returns**
/// A new LocalContainerInfo on success, or else a LocalContainerInfoError.
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, LocalContainerInfoError> {
// Convert the path-like to a Path
let path = path.as_ref();
// Open the file to read it
let handle = match File::open(path) {
Ok(handle) => handle,
Err(err) => {
return Err(LocalContainerInfoError::FileOpenError { path: path.to_path_buf(), err });
},
};
// Do the parsing with from_reader()
Self::from_reader(handle)
}
/// Constructor for the LocalContainerInfo that constructs it from the given reader.
///
/// **Generic types**
/// * `R`: The type of the reader, which implements Read.
///
/// **Arguments**
/// * `reader`: The reader to read from. Will be consumed.
///
/// **Returns**
/// A new LocalContainerInfo on success, or else a LocalContainerInfoError.
pub fn from_reader<R: Read>(reader: R) -> Result<Self, LocalContainerInfoError> {
// Simply pass to serde
match serde_yaml::from_reader(reader) {
Ok(result) => Ok(result),
Err(err) => Err(LocalContainerInfoError::FileParseError { err }),
}
}
/// Writes the LocalContainerInfo to the given location.
///
/// **Generic types**
/// * `P`: The Path-like type of the given target location.
///
/// **Arguments**
/// * `path`: The target location to write to the LocalContainerInfo to.
///
/// **Returns**
/// Nothing on success, or a LocalContainerInfoError otherwise.
pub fn to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), LocalContainerInfoError> {
// Convert the path-like to a path
let path = path.as_ref();
// Open a file
let handle = match File::create(path) {
Ok(handle) => handle,
Err(err) => {
return Err(LocalContainerInfoError::FileCreateError { path: path.to_path_buf(), err });
},
};
// Use ::to_write() to deal with the actual writing
self.to_writer(handle)
}
/// Writes the LocalContainerInfo to the given writer.
///
/// **Generic types**
/// * `W`: The type of the writer, which implements Write.
///
/// **Arguments**
/// * `writer`: The writer to write to. Will be consumed.
///
/// **Returns**
/// Nothing on success, or a LocalContainerInfoError otherwise.
pub fn to_writer<W: Write>(&self, writer: W) -> Result<(), LocalContainerInfoError> {
// Simply write with serde
match serde_yaml::to_writer(writer, self) {
Ok(()) => Ok(()),
Err(err) => Err(LocalContainerInfoError::FileWriteError { err }),
}
}
}
impl From<ContainerInfo> for LocalContainerInfo {
fn from(container_info: ContainerInfo) -> Self {
Self {
name: container_info.name,
kind: container_info.kind,
entrypoint: container_info.entrypoint,
actions: container_info.actions,
types: container_info.types.unwrap_or_default(),
}
}
}
impl From<&ContainerInfo> for LocalContainerInfo {
fn from(container_info: &ContainerInfo) -> Self {
Self {
name: container_info.name.clone(),
kind: container_info.kind,
entrypoint: container_info.entrypoint.clone(),
actions: container_info.actions.clone(),
types: container_info.types.as_ref().cloned().unwrap_or_default(),
}
}
}
/// Specifies the contents of a container info YAML file. Note that this is only the file the user creates.
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContainerInfo {
/// The name/programming ID of this package.
pub name: String,
/// The version of this package.
pub version: Version,
/// The kind of this package.
pub kind: PackageKind,
/// The list of owners of this package.
pub owners: Option<Vec<String>>,
/// A short description of the package.
pub description: Option<String>,
/// The functions that this package supports.
pub actions: Map<Action>,
/// The entrypoint of the image
pub entrypoint: Entrypoint,
/// The types that this package adds.
pub types: Option<Map<Type>>,
/// The base image to use for the package image.
pub base: Option<String>,
/// The dependencies, as install commands for sudo apt-get install -y <...>
pub dependencies: Option<Vec<String>>,
/// Any environment variables that the user wants to be set
pub environment: Option<Map<String>>,
/// The list of additional files to copy to the image
pub files: Option<Vec<String>>,
/// An extra script to run to initialize the working directory
pub initialize: Option<Vec<String>>,
/// An extra set of commands that will be run _before_ the workspace is copied over. Useful for non-standard general dependencies.
pub install: Option<Vec<String>>,
/// An extra set of commands that will be run _after_ the workspace is copied over. Useful for preprocessing or unpacking things.
#[serde(alias = "postinstall", alias = "post-install", alias = "post_install")]
pub unpack: Option<Vec<String>>,
}
impl ContainerInfo {
/// **Edited: now returning ContainerInfoErrors.**
///
/// Returns a ContainerInfo by constructing it from the file at the given path.
///
/// **Generic types**
/// * `P`: The Path-like type of the given target location.
///
/// **Arguments**
/// * `path`: The path to the container info file.
///
/// **Returns**
/// The newly constructed ContainerInfo instance on success, or a ContainerInfoError upon failure.
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ContainerInfo, ContainerInfoError> {
// Convert the path-like to a path
let path = path.as_ref();
// Read the contents in one go
let contents = match fs::read_to_string(path) {
Ok(contents) => contents,
Err(err) => {
return Err(ContainerInfoError::FileReadError { path: path.to_path_buf(), err });
},
};
// Delegate the actual parsing to from_string
ContainerInfo::from_string(contents)
}
/// **Edited: now returning ContainerInfoErrors.**
///
/// Returns a ContainerInfo by constructing it from the given Reader with YAML text.
///
/// **Arguments**
/// * `r`: The reader with the contents of the raw YAML file.
///
/// **Returns**
/// The newly constructed ContainerInfo instance on success, or a ContainerInfoError upon failure.
pub fn from_reader<R: Read>(r: R) -> Result<ContainerInfo, ContainerInfoError> {
match serde_yaml::from_reader(r) {
Ok(result) => Ok(result),
Err(err) => Err(ContainerInfoError::ParseError { err }),
}
}
/// **Edited: now returning ContainerInfoErrors.**
///
/// Returns a ContainerInfo by constructing it from the given string of YAML text.
///
/// **Arguments**
/// * `contents`: The text contents of a raw YAML file.
///
/// **Returns**
/// The newly constructed ContainerInfo instance on success, or a ContainerInfoError upon failure.
pub fn from_string(contents: String) -> Result<ContainerInfo, ContainerInfoError> {
match serde_yaml::from_str(&contents) {
Ok(result) => Ok(result),
Err(err) => Err(ContainerInfoError::ParseError { err }),
}
}
/// Writes the ContainerInfo to the given location.
///
/// **Generic types**
/// * `P`: The Path-like type of the given target location.
///
/// **Arguments**
/// * `path`: The target location to write to the LocalContainerInfo to.
///
/// **Returns**
/// Nothing on success, or a ContainerInfoError otherwise.
pub fn to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), ContainerInfoError> {
// Convert the path-like to a path
let path = path.as_ref();
// Open a file
let handle = match File::create(path) {
Ok(handle) => handle,
Err(err) => {
return Err(ContainerInfoError::FileCreateError { path: path.to_path_buf(), err });
},
};
// Use ::to_write() to deal with the actual writing
self.to_writer(handle)
}
/// Writes the ContainerInfo to the given writer.
///
/// **Generic types**
/// * `W`: The type of the writer, which implements Write.
///
/// **Arguments**
/// * `writer`: The writer to write to. Will be consumed.
///
/// **Returns**
/// Nothing on success, or a ContainerInfoError otherwise.
pub fn to_writer<W: Write>(&self, writer: W) -> Result<(), ContainerInfoError> {
// Simply write with serde
match serde_yaml::to_writer(writer, self) {
Ok(()) => Ok(()),
Err(err) => Err(ContainerInfoError::FileWriteError { err }),
}
}
}
/// Defines the YAML of an action in a package.
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Action {
pub requirements: Option<HashSet<Capability>>,
pub command: Option<ActionCommand>,
pub description: Option<String>,
pub endpoint: Option<ActionEndpoint>,
pub pattern: Option<CallPattern>,
pub input: Option<Vec<Parameter>>,
pub output: Option<Vec<Parameter>>,
}
/// Defines the YAML of a command within an action in a package.
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ActionCommand {
pub args: Vec<String>,
pub capture: Option<String>,
}
/// Defines the YAML of a remote OAS action.
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ActionEndpoint {
pub method: Option<String>,
pub path: String,
}
/// Defines the YAML of the entry point to a package (in terms of function).
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Entrypoint {
pub kind: String,
pub exec: String,
pub content: Option<String>,
pub delay: Option<u64>,
}