support_kit/
deploy.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
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
use rustls_acme::axum::AxumAcceptor;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Deployment {
    pub artifacts: Option<Artifacts>,
    pub hosts: Vec<Host>,
    #[serde(default)]
    pub security: Tls,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Artifacts {
    pub containers: Option<Containers>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Containers {
    pub registry: Option<Registry>,
    pub images: Vec<Image>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Registry {
    pub account: String,
    pub host: String,
    pub token: String,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Image {
    pub definition: String,
    pub name: String,
    pub label: String,
    pub namespace: String,
    pub repo: String,
}

impl Deployment {
    pub fn new() -> Self {
        Self::default()
    }

    pub async fn init_tls(&self) -> Option<AxumAcceptor> {
        tls::TlsControl::new(self).init().await
    }
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Host {
    pub address: String,
    pub port: Option<u16>,
    pub user: Option<String>,
    pub auth: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
struct Security {
    certificates: Option<Tls>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum Tls {
    Acme {
        domains: Vec<String>,
        emails: Vec<String>,
        cache: Option<String>,
        production: bool,
    },
    #[serde(untagged)]
    #[default]
    Off,
    #[serde(untagged)]
    Unknown(serde_json::Value),
}

mod tls {
    use rustls_acme::{axum::AxumAcceptor, caches::DirCache, AcmeConfig};
    use tokio_stream::StreamExt;

    #[derive(Debug, Default, bon::Builder)]
    pub struct TlsControl {
        config: super::Tls,
    }

    impl TlsControl {
        pub fn new(deployment: &super::Deployment) -> Self {
            Self::builder().config(deployment.security.clone()).build()
        }

        pub async fn init(&self) -> Option<AxumAcceptor> {
            match &self.config {
                super::Tls::Acme {
                    domains,
                    emails,
                    cache,
                    production,
                    ..
                } => {
                    let mut state = AcmeConfig::new(domains)
                        .contact(emails.iter().map(|email| format!("mailto:{email}")))
                        .cache_option(cache.clone().map(DirCache::new))
                        .directory_lets_encrypt(*production)
                        .state();

                    let acceptor = state.axum_acceptor(state.default_rustls_config());

                    tokio::spawn(async move {
                        loop {
                            match state.next().await.unwrap() {
                                Ok(ok) => tracing::info!("tls certification event: {:?}", ok),
                                Err(err) => {
                                    tracing::error!("tls certification error: {:?}", err)
                                }
                            }
                        }
                    });

                    Some(acceptor)
                }
                _ => None,
            }
        }
    }
}