disposables_protocol/lib.rs
1/*
2 * Copyright 2024 Akash Rawal
3 *
4 * This file is part of Disposables.
5 *
6 * Disposables is free software: you can redistribute it and/or modify it under 
7 * the terms of the GNU General Public License as published by the 
8 * Free Software Foundation, either version 3 of the License, or 
9 * (at your option) any later version.
10 * 
11 * Disposables is distributed in the hope that it will be useful, 
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
14 * See the GNU General Public License for more details.
15 * 
16 * You should have received a copy of the GNU General Public License 
17 * along with Disposables. If not, see <https://www.gnu.org/licenses/>. 
18 */
19//!Protocol related definitions for Disposables/DLC
20
21/**
22 * Environment variable for setup message.
23 */
24pub const V1_ENV_SETUP: &str = "DISPOSABLES_V1_SETUP";
25
26/**
27 * Enumeration of conditions to wait for before accepting that the container
28 * is ready.
29 */
30#[derive(Debug, serde::Serialize, serde::Deserialize)]
31#[serde(tag = "kind", content = "data")]
32pub enum V1WaitCondition {
33    /// Wait for a port to be connectable.
34    Port(u16),
35    /// Wait for a string to be found in the container's stdout.
36    Stdout(String),
37    /// Wait for a command to return successfully.
38    Command{argv: Vec<String>, interval_msec: u64},
39}
40
41/**
42 * Description of the setup message for a container.
43 *
44 * The setup message is serialized in JSON format and passed as an environment
45 * variable to the container. (see `V1_ENV_SETUP`)
46 */
47#[derive(Debug, serde::Serialize, serde::Deserialize)]
48pub struct V1SetupMsg {
49    /// DLC should listen on this port. Disposables client will connect to 
50    /// this port to receive events. When that connection is closed,
51    /// DLC will exit.
52    pub port: u16,
53
54    /// List of conditions to wait for before accepting that the container
55    /// is ready.
56    pub wait_for: Vec<V1WaitCondition>,
57
58    /// Timeout for the container to become ready. When the timeout is reached,
59    /// the container is considered failed to become ready.
60    pub ready_timeout_s: Option<u64>,
61
62    /// List of files to be written before starting the container's entrypoint.
63    pub files: Vec<(String, String)>,
64}
65
66/**
67 * Enumeration of events that can occur in a container.
68 *
69 * The events are serialized in JSON format and sent to the client.
70 */
71#[derive(Debug, serde::Serialize, serde::Deserialize)]
72#[serde(tag = "kind", content = "data")]
73pub enum V1Event {
74    /// The container is ready to use.
75    Ready,
76    /// The container's entrypoint has exited.
77    Exited(Option<i32>),
78    /// Failed to prepare the container.
79    FailedToPrepare(String),
80    /// Failed to start the container's entrypoint.
81    FailedToStartEntrypoint(String),
82    /// Timeout occured while waiting for the container to become ready.
83    FailedTimeout,
84}
85
86