plane_drone/
config.rs

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
use crate::{cert::acme::AcmeConfiguration, ip::IpSource, keys::KeyCertPathPair};
use plane_core::{nats_connection::NatsConnectionSpec, types::DroneId};
use serde::{Deserialize, Serialize};
use std::{
    net::{IpAddr, Ipv4Addr},
    path::PathBuf,
};

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum NatsAuthentication {
    Token { token: String },
    User { username: String, password: String },
    // JWT
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum DockerConnection {
    Socket { socket: String },
    Http { http: String },
}

impl Default for DockerConnection {
    fn default() -> Self {
        DockerConnection::Socket {
            socket: "/var/run/docker.sock".into(),
        }
    }
}

#[derive(Serialize, Deserialize, Default)]
pub struct DockerConfig {
    pub runtime: Option<String>,
    #[serde(default)]
    pub connection: DockerConnection,

    pub network: Option<String>,
}

#[derive(Serialize, Deserialize)]
struct CertRefreshOptions {
    acme: AcmeConfiguration,
}

#[derive(Serialize, Deserialize)]
pub struct ProxyOptions {
    #[serde(default = "default_bind_address")]
    pub bind_ip: IpAddr,
    #[serde(default = "default_https_port")]
    pub https_port: u16,
}

fn default_bind_address() -> IpAddr {
    IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))
}

fn default_https_port() -> u16 {
    443
}

#[derive(Serialize, Deserialize)]
pub struct AgentOptions {
    #[serde(default)]
    pub docker: DockerConfig,

    pub ip: IpSource,

    pub drone_id: Option<DroneId>,
}

#[derive(Serialize, Deserialize)]
pub struct DroneConfig {
    /// Unique string used to identify this drone. If not provided, a
    /// UUID is generated.
    pub drone_id: Option<DroneId>,

    /// Path to use for the sqlite3 database through which the agent and
    /// proxy share a persisted state.
    #[serde(default = "default_db_path")]
    pub db_path: PathBuf,

    /// The domain to which this drone belongs.
    pub cluster_domain: String,

    /// How to connect to NATS. Required by agent and certificate refresh.
    pub nats: Option<NatsConnectionSpec>,

    /// Paths to certificate and private key files. Required by proxy and
    /// certificate refresh.
    pub cert: Option<KeyCertPathPair>,

    /// Settings for ACME certificate refresh. If not provided, certificates
    /// are not refreshed by this drone process.
    pub acme: Option<AcmeConfiguration>,

    /// Settings for the agent. If not provided, the agent does not run in
    /// this drone process.
    pub agent: Option<AgentOptions>,

    /// Settings for the proxy. If not provided, the proxy does not run in
    /// this drone process.
    pub proxy: Option<ProxyOptions>,
}

fn default_db_path() -> PathBuf {
    PathBuf::from("drone.sqlite3")
}