summaryrefslogtreecommitdiffstats
path: root/src/config/mod.rs
blob: 6d6ef8a839c3259fc41d323810cac6bee1c877b4 (plain)
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
use std::path::PathBuf;
use std::fmt::Debug;
use std::ops::Deref;
use std::collections::BTreeMap;

use anyhow::Result;
use anyhow::Context;
use getset::Getters;
use getset::CopyGetters;
use serde::Deserialize;
use handlebars::Handlebars;

use crate::phase::PhaseName;
use crate::util::EnvironmentVariableName;
use crate::util::docker::ImageName;

#[derive(Debug, Getters, Deserialize)]
pub struct NotValidatedConfiguration {
    #[getset(get = "pub")]
    repository: PathBuf,

    #[serde(rename = "releases")]
    releases_directory: String,

    #[serde(rename = "staging")]
    staging_directory: String,

    #[getset(get = "pub")]
    #[serde(rename = "database_host")]
    database_host: String,

    #[getset(get = "pub")]
    #[serde(rename = "database_port")]
    database_port: String,

    #[getset(get = "pub")]
    #[serde(rename = "database_user")]
    database_user: String,

    #[getset(get = "pub")]
    #[serde(rename = "database_password")]
    database_password: String,

    #[getset(get = "pub")]
    #[serde(rename = "database_name")]
    database_name: String,

    #[getset(get = "pub")]
    docker: DockerConfig,

    #[getset(get = "pub")]
    containers: ContainerConfig,

    #[getset(get = "pub")]
    available_phases: Vec<PhaseName>,
}

impl<'reg> NotValidatedConfiguration {
    pub fn validate(self) -> Result<Configuration<'reg>> {
        // TODO: Implement proper validation

        let hb = {
            let mut hb = Handlebars::new();
            hb.register_template_string("releases", &self.releases_directory)?;
            hb.register_template_string("staging", &self.staging_directory)?;
            hb
        };

        Ok(Configuration {
            inner: self,
            hb,
        })
    }
}

#[derive(Debug)]
pub struct Configuration<'reg> {
    inner: NotValidatedConfiguration,
    hb: Handlebars<'reg>,
}

impl<'reg> Deref for Configuration<'reg> {
    type Target = NotValidatedConfiguration;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<'reg> Configuration<'reg> {
    /// Get the path to the releases directory, interpolate every variable used in the config
    pub fn releases_directory(&self, hm: &BTreeMap<String, String>) -> Result<PathBuf> {
        self.hb.render("releases", hm)
            .map(PathBuf::from)
            .context("Interpolating variables into 'release' setting from configuration")
    }

    /// Get the path to the staging directory, interpolate every variable used in the config
    pub fn staging_directory(&self, hm: &BTreeMap<String, String>) -> Result<PathBuf> {
        self.hb.render("staging", hm)
            .map(PathBuf::from)
            .context("Interpolating variables into 'staging' setting from configuration")
    }
}


#[derive(Debug, Getters, CopyGetters, Deserialize)]
pub struct DockerConfig {
    /// The required docker version
    ///
    /// If not set, it will not be checked, which might result in weird things?
    ///
    /// # Note
    ///
    /// Because the docker API returns strings, not a version object, each compatible version must
    /// be listed.
    #[getset(get = "pub")]
    docker_versions: Option<Vec<String>>,

    /// The required docker api version
    ///
    /// If not set, it will not be checked, which might result in weird things?
    ///
    /// # Note
    ///
    /// Because the docker API returns strings, not a version object, each compatible version must
    /// be listed.
    #[getset(get = "pub")]
    docker_api_versions: Option<Vec<String>>,

    /// Whether the program should verify that the required images are present.
    /// You want this to be true normally.
    #[getset(get_copy = "pub")]
    verify_images_present: bool,

    #[getset(get = "pub")]
    images: Vec<ImageName>,

    #[getset(get = "pub")]
    endpoints: Vec<Endpoint>,
}

#[derive(Debug, Getters, Deserialize)]
pub struct ContainerConfig {
    #[getset(get = "pub")]
    allowed_env: Vec<EnvironmentVariableName>,
}


#[derive(Clone, Debug, Getters, CopyGetters, Deserialize)]
pub struct Endpoint {
    #[getset(get = "pub")]
    name: String,

    #[getset(get = "pub")]
    uri: String,

    #[getset(get = "pub")]
    endpoint_type: EndpointType,

    /// Relative speed to other endpoints
    ///
    /// So if you have two servers, one with 12 cores and one with 24, you want to set "1" for the
    /// first and "2" for the second (or "12" for the first and "24" for the second - the ratio is
    /// the thing here)!
    #[getset(get_copy = "pub")]
    speed: usize,

    /// Maximum number of jobs which are allowed on this endpoint
    #[getset(get_copy = "pub")]
    maxjobs: usize,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum EndpointType {
    Socket,
    Http,
}