Skip to content

Commit ddad62f

Browse files
committed
Add environment manager for mongodb
1 parent ecdc5ab commit ddad62f

File tree

7 files changed

+415
-0
lines changed

7 files changed

+415
-0
lines changed

scripts/integration/mongodb/Cargo.lock

+252
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "vector-mongodb-env-manager"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Vector Contributors <[email protected]>"]
6+
license = "MPL-2.0"
7+
readme = "README.md"
8+
publish = false
9+
10+
[dependencies]
11+
anyhow = "1.0.66"
12+
clap = { version = "4.0.18", features = ["derive"] }
13+
dunce = "1.0.3"
14+
serde_json = "1.0.87"
15+
16+
[workspace]

scripts/integration/mongodb/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# vector-mongodb-env-manager
2+
3+
-----
4+
5+
## Usage
6+
7+
```text
8+
vdev int show mongodb
9+
vdev int start mongodb <ENV>
10+
vdev int stop mongodb <ENV>
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: '3'
2+
3+
services:
4+
mongodb-primary:
5+
image: docker.io/bitnami/mongodb:${MONGODB_VERSION}
6+
environment:
7+
- MONGODB_ADVERTISED_HOSTNAME=mongodb-primary
8+
- MONGODB_REPLICA_SET_MODE=primary
9+
- MONGODB_ROOT_PASSWORD=toor
10+
- MONGODB_REPLICA_SET_KEY=vector
11+
mongodb-secondary:
12+
image: docker.io/bitnami/mongodb:${MONGODB_VERSION}
13+
depends_on:
14+
- mongodb-primary
15+
environment:
16+
- MONGODB_ADVERTISED_HOSTNAME=mongodb-secondary
17+
- MONGODB_REPLICA_SET_MODE=secondary
18+
- MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
19+
- MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
20+
- MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=toor
21+
- MONGODB_REPLICA_SET_KEY=vector
22+
mongodb-arbiter:
23+
image: docker.io/bitnami/mongodb:${MONGODB_VERSION}
24+
depends_on:
25+
- mongodb-primary
26+
environment:
27+
- MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter
28+
- MONGODB_REPLICA_SET_MODE=arbiter
29+
- MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
30+
- MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
31+
- MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=toor
32+
- MONGODB_REPLICA_SET_KEY=vector
33+
34+
networks:
35+
default:
36+
name: ${VECTOR_NETWORK}
37+
external: true
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use anyhow::{bail, Result};
2+
use serde_json::Value;
3+
use std::path::PathBuf;
4+
use std::process::Command;
5+
use std::thread;
6+
use std::time::Duration;
7+
8+
pub fn start(config: Value) -> Result<()> {
9+
let mut command = compose_command();
10+
command.args(["up", "-d"]);
11+
12+
apply_env_vars(&mut command, &config);
13+
14+
let status = command.status()?;
15+
if status.success() {
16+
thread::sleep(Duration::from_secs(20));
17+
return Ok(());
18+
} else {
19+
bail!("failed to execute: {}", render_command(&mut command));
20+
}
21+
}
22+
23+
pub fn stop(config: Value) -> Result<()> {
24+
let mut command = compose_command();
25+
command.args(["down", "-t", "0"]);
26+
27+
apply_env_vars(&mut command, &config);
28+
29+
let status = command.status()?;
30+
if status.success() {
31+
return Ok(());
32+
} else {
33+
bail!("failed to execute: {}", render_command(&mut command));
34+
}
35+
}
36+
37+
fn compose_command() -> Command {
38+
let path = PathBuf::from_iter(["data", "docker-compose.yml"].iter());
39+
let compose_file = match dunce::canonicalize(&path) {
40+
Ok(p) => p.display().to_string(),
41+
Err(_) => path.display().to_string(),
42+
};
43+
44+
let mut command = Command::new("docker");
45+
command.args(["compose", "-f", &compose_file]);
46+
command
47+
}
48+
49+
fn apply_env_vars(command: &mut Command, config: &Value) {
50+
if let Some(version) = config.get("version") {
51+
command.env("MONGODB_VERSION", version.as_str().unwrap());
52+
}
53+
}
54+
55+
fn render_command(command: &mut Command) -> String {
56+
format!(
57+
"{} {}",
58+
command.get_program().to_str().unwrap(),
59+
Vec::from_iter(command.get_args().map(|arg| arg.to_str().unwrap())).join(" ")
60+
)
61+
}

0 commit comments

Comments
 (0)