oct-config 0.8.0

opencloudtool (oct)
Documentation
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
use std::collections::HashMap;
use std::fs;

use petgraph::Graph;
use petgraph::graph::NodeIndex;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Config {
    pub project: Project,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum Node {
    /// The synthetic root node.
    #[default]
    Root,
    /// A user service in the dependency graph.
    Resource(Service),
}

impl std::fmt::Display for Node {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Node::Root => write!(f, "Root"),
            Node::Resource(service) => write!(f, "service: {service:?}"),
        }
    }
}

impl Config {
    const DEFAULT_CONFIG_PATH: &'static str = "oct.toml";

    pub fn new(path: Option<&str>) -> Result<Self, Box<dyn std::error::Error>> {
        let config =
            fs::read_to_string(path.unwrap_or(Self::DEFAULT_CONFIG_PATH)).map_err(|e| {
                format!(
                    "Failed to read config file {}: {}",
                    Self::DEFAULT_CONFIG_PATH,
                    e
                )
            })?;

        let config_with_injected_envs = Self::render_system_envs(config);

        let toml_data: Config = toml::from_str(&config_with_injected_envs)?;

        Ok(toml_data)
    }

    /// Converts user services to a graph
    pub fn to_graph(&self) -> Result<Graph<Node, String>, Box<dyn std::error::Error>> {
        let mut graph = Graph::<Node, String>::new();
        let mut edges = Vec::new();
        let root = graph.add_node(Node::Root);

        let mut services_map: HashMap<String, NodeIndex> = HashMap::new();
        for service in &self.project.services {
            if services_map.contains_key(&service.name) {
                return Err(format!("Duplicate service name: '{}'", service.name).into());
            }
            let node = graph.add_node(Node::Resource(service.clone()));

            services_map.insert(service.name.clone(), node);
        }

        for service in &self.project.services {
            let resource = services_map
                .get(&service.name)
                .expect("Missed resource value in resource_map");

            if service.depends_on.is_empty() {
                edges.push((root, *resource, String::new()));
            } else {
                for dependency_name in &service.depends_on {
                    let dependency_resource = services_map.get(dependency_name);

                    match dependency_resource {
                        Some(dependency_resource) => {
                            edges.push((*dependency_resource, *resource, String::new()));
                        }
                        None => {
                            return Err(format!(
                                "Missed resource with name '{dependency_name}' referenced as dependency in '{}' service",
                                service.name
                            )
                            .into());
                        }
                    }
                }
            }
        }

        graph.extend_with_edges(&edges);

        Ok(graph)
    }

    /// Renders environment variables using [tera](https://docs.rs/tera/latest/tera/)
    /// All system environment variables are available under the `env` context variable
    fn render_system_envs(config: String) -> String {
        let mut context = tera::Context::new();
        context.insert("env", &std::env::vars().collect::<HashMap<_, _>>());

        let render_result = tera::Tera::one_off(&config, &context, true);

        match render_result {
            Ok(render_result) => {
                log::info!("Config with injected env vars:\n{render_result}");

                render_result
            }
            Err(e) => {
                log::warn!("Failed to render string: '{config}', error: {e}, context: {context:?}");

                config
            }
        }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum StateBackend {
    #[serde(rename = "local")]
    Local {
        /// Local path to the state file
        path: String,
    },

    #[serde(rename = "s3")]
    S3 {
        /// Bucket region
        region: String,
        /// Bucket name
        bucket: String,
        /// Path to the file inside the S3 bucket
        key: String,
    },
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Project {
    pub name: String,

    pub state_backend: StateBackend,
    pub user_state_backend: StateBackend,

    pub services: Vec<Service>,

    pub domain: Option<String>,
}

/// Configuration for a service
/// This configuration is managed by the user and used to deploy the service
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Service {
    /// Service name
    pub name: String,
    /// Image to use for the container
    pub image: String,
    /// Path to the Dockerfile
    pub dockerfile_path: Option<String>,
    /// Command to run in the container
    pub command: Option<String>,
    /// Internal port exposed from the container
    pub internal_port: Option<u32>,
    /// External port exposed to the public internet
    pub external_port: Option<u32>,
    /// CPU millicores
    pub cpus: u32,
    /// Memory in MB
    pub memory: u64,
    /// List of services that this service depends on
    #[serde(default)]
    pub depends_on: Vec<String>,
    /// Raw environment variables to set in the container
    /// All values are rendered using in `render_envs` method
    #[serde(default)]
    pub envs: HashMap<String, String>,
}

#[cfg(test)]
mod tests {
    use std::io::Write;

    use super::*;

    #[test]
    fn test_config_new_success_path_privided() {
        // Arrange
        let config_file_content = r#" 
[project]
name = "example"
domain = "opencloudtool.com"

[project.state_backend.local]
path = "./state.json"

[project.user_state_backend.local]
path = "./user_state.json"

[[project.services]]
name = "app_1"
image = ""
dockerfile_path = "Dockerfile"
command = "echo Hello World!"
internal_port = 80
external_port = 80
cpus = 250
memory = 64

[project.services.envs]
KEY1 = "VALUE1"
KEY2 = """
Multiline
string"""
KEY_WITH_INJECTED_ENV = "{{ env.CARGO_PKG_NAME }}"
KEY_WITH_OTHER_TEMPLATE_VARIABLE = "{{ other_vars.some_var }}"

[[project.services]]
name = "app_2"
image = "nginx:latest"
cpus = 250
memory = 64
depends_on = ["app_1"]
"#;

        let mut config_file = tempfile::NamedTempFile::new().expect("Failed to create a temp file");
        config_file
            .write_all(config_file_content.as_bytes())
            .expect("Failed to write to file");

        // Act
        let config =
            Config::new(config_file.path().to_str()).expect("Failed to create a new config");

        // Assert
        assert_eq!(
            config,
            Config {
                project: Project {
                    name: String::from("example"),
                    state_backend: StateBackend::Local {
                        path: String::from("./state.json")
                    },
                    user_state_backend: StateBackend::Local {
                        path: String::from("./user_state.json")
                    },
                    services: vec![
                        Service {
                            name: String::from("app_1"),
                            image: String::new(),
                            dockerfile_path: Some(String::from("Dockerfile")),
                            command: Some(String::from("echo Hello World!")),
                            internal_port: Some(80),
                            external_port: Some(80),
                            cpus: 250,
                            memory: 64,
                            depends_on: vec![],
                            envs: HashMap::from([
                                (String::from("KEY1"), String::from("VALUE1")),
                                (String::from("KEY2"), String::from("Multiline\nstring")),
                                (
                                    String::from("KEY_WITH_INJECTED_ENV"),
                                    // "oct-orchestrator" was the previous value because it was in that crate.
                                    // Now it's in oct-config, so CARGO_PKG_NAME will be oct-config.
                                    // Wait, the test uses {{ env.CARGO_PKG_NAME }}.
                                    // When running tests for oct-config, CARGO_PKG_NAME is oct-config.
                                    String::from("oct-config")
                                ),
                                (
                                    String::from("KEY_WITH_OTHER_TEMPLATE_VARIABLE"),
                                    String::from("{{ other_vars.some_var }}")
                                ),
                            ]),
                        },
                        Service {
                            name: String::from("app_2"),
                            image: String::from("nginx:latest"),
                            dockerfile_path: None,
                            command: None,
                            internal_port: None,
                            external_port: None,
                            cpus: 250,
                            memory: 64,
                            depends_on: vec![String::from("app_1")],
                            envs: HashMap::new(),
                        }
                    ],
                    domain: Some(String::from("opencloudtool.com")),
                }
            }
        );
    }

    #[test]
    fn test_config_to_graph_empty() {
        // Arrange
        let config = Config {
            project: Project {
                name: String::from("test"),
                state_backend: StateBackend::Local {
                    path: String::from("state.json"),
                },
                user_state_backend: StateBackend::Local {
                    path: String::from("user_state.json"),
                },
                services: Vec::new(),
                domain: None,
            },
        };

        // Act
        let graph = config.to_graph().expect("Failed to get graph");

        // Assert
        assert_eq!(graph.node_count(), 1); // Root node
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_config_to_graph_single_node() {
        // Arrange
        let service = Service {
            name: String::from("app_1"),
            image: String::from("nginx:latest"),
            dockerfile_path: None,
            command: None,
            internal_port: None,
            external_port: None,
            cpus: 250,
            memory: 64,
            depends_on: vec![],
            envs: HashMap::new(),
        };
        let config = Config {
            project: Project {
                name: String::from("test"),
                state_backend: StateBackend::Local {
                    path: String::from("state.json"),
                },
                user_state_backend: StateBackend::Local {
                    path: String::from("user_state.json"),
                },
                services: vec![service],
                domain: None,
            },
        };

        // Act
        let graph = config.to_graph().expect("Failed to get graph");

        // Assert
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);

        let root_node_index = graph
            .node_indices()
            .find(|i| matches!(graph[*i], Node::Root))
            .expect("Root node not found");
        let service_node_index = graph
            .node_indices()
            .find(|i| matches!(graph[*i], Node::Resource(_)))
            .expect("Service node not found");

        assert!(graph.contains_edge(root_node_index, service_node_index));
    }

    #[test]
    fn test_config_to_graph_with_dependencies() {
        // Arrange
        let service1 = Service {
            name: String::from("app_1"),
            image: String::from("nginx:latest"),
            dockerfile_path: None,
            command: None,
            internal_port: None,
            external_port: None,
            cpus: 250,
            memory: 64,
            depends_on: vec![],
            envs: HashMap::new(),
        };
        let service2 = Service {
            name: String::from("app_2"),
            image: String::from("nginx:latest"),
            dockerfile_path: None,
            command: None,
            internal_port: None,
            external_port: None,
            cpus: 250,
            memory: 64,
            depends_on: vec![String::from("app_1")],
            envs: HashMap::new(),
        };
        let config = Config {
            project: Project {
                name: String::from("test"),
                state_backend: StateBackend::Local {
                    path: String::from("state.json"),
                },
                user_state_backend: StateBackend::Local {
                    path: String::from("user_state.json"),
                },
                services: vec![service1.clone(), service2.clone()],
                domain: None,
            },
        };

        // Act
        let graph = config.to_graph().expect("Failed to get graph");

        // Assert
        assert_eq!(graph.node_count(), 3);
        assert_eq!(graph.edge_count(), 2);

        let root_node_index = graph
            .node_indices()
            .find(|i| matches!(graph[*i], Node::Root))
            .expect("Root node not found");
        let service1_node_index = graph
            .node_indices()
            .find(|i| graph[*i] == Node::Resource(service1.clone()))
            .expect("Service 1 node not found");
        let service2_node_index = graph
            .node_indices()
            .find(|i| graph[*i] == Node::Resource(service2.clone()))
            .expect("Service 2 node not found");

        assert!(graph.contains_edge(root_node_index, service1_node_index));
        assert!(graph.contains_edge(service1_node_index, service2_node_index));
    }

    #[test]
    fn test_config_to_graph_failed_to_get_dependency() {
        // Arrange
        let service = Service {
            name: String::from("app_1"),
            image: String::from("nginx:latest"),
            dockerfile_path: None,
            command: None,
            internal_port: None,
            external_port: None,
            cpus: 250,
            memory: 64,
            depends_on: vec![String::from("INCORRECT_SERVICE_NAME")],
            envs: HashMap::new(),
        };
        let config = Config {
            project: Project {
                name: String::from("test"),
                state_backend: StateBackend::Local {
                    path: String::from("state.json"),
                },
                user_state_backend: StateBackend::Local {
                    path: String::from("user_state.json"),
                },
                services: vec![service],
                domain: None,
            },
        };

        // Act
        let graph = config.to_graph();

        // Assert
        assert!(graph.is_err());
        assert_eq!(
            graph.expect_err("Expected error").to_string(),
            "Missed resource with name 'INCORRECT_SERVICE_NAME' referenced as dependency in 'app_1' service"
        );
    }

    #[test]
    fn test_config_to_graph_duplicate_service_names() {
        // Arrange
        let service1 = Service {
            name: String::from("app_1"),
            image: String::from("nginx:latest"),
            dockerfile_path: None,
            command: None,
            internal_port: None,
            external_port: None,
            cpus: 250,
            memory: 64,
            depends_on: vec![],
            envs: HashMap::new(),
        };
        let service2 = Service {
            name: String::from("app_1"),
            image: String::from("nginx:latest"),
            dockerfile_path: None,
            command: None,
            internal_port: None,
            external_port: None,
            cpus: 250,
            memory: 64,
            depends_on: vec![],
            envs: HashMap::new(),
        };
        let config = Config {
            project: Project {
                name: String::from("test"),
                state_backend: StateBackend::Local {
                    path: String::from("state.json"),
                },
                user_state_backend: StateBackend::Local {
                    path: String::from("user_state.json"),
                },
                services: vec![service1, service2],
                domain: None,
            },
        };

        // Act
        let graph = config.to_graph();

        // Assert
        assert!(graph.is_err());
        assert_eq!(
            graph.expect_err("Expected error").to_string(),
            "Duplicate service name: 'app_1'"
        );
    }
}