Skip to content

Commit 2db1478

Browse files
committed
Add environment manager for prometheus
1 parent ecdc5ab commit 2db1478

File tree

7 files changed

+402
-0
lines changed

7 files changed

+402
-0
lines changed

scripts/integration/prometheus/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-prometheus-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]
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# vector-prometheus-env-manager
2+
3+
-----
4+
5+
## Usage
6+
7+
```text
8+
vdev int show prometheus
9+
vdev int start prometheus <ENV>
10+
vdev int stop prometheus <ENV>
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
3+
services:
4+
influxdb:
5+
image: docker.io/influxdb:${PROMETHEUS_VERSION}
6+
environment:
7+
- INFLUXDB_REPORTING_DISABLED=true
8+
influxdb-tls:
9+
image: docker.io/influxdb:${PROMETHEUS_VERSION}
10+
environment:
11+
- INFLUXDB_REPORTING_DISABLED=true
12+
- INFLUXDB_HTTP_HTTPS_ENABLED=true
13+
- INFLUXDB_HTTP_BIND_ADDRESS=:8087
14+
- INFLUXDB_BIND_ADDRESS=:8089
15+
- INFLUXDB_HTTP_HTTPS_CERTIFICATE=/etc/ssl/intermediate_server/certs/localhost-chain.cert.pem
16+
- INFLUXDB_HTTP_HTTPS_PRIVATE_KEY=/etc/ssl/intermediate_server/private/localhost.key.pem
17+
volumes:
18+
- ../../../../tests/data/ca:/etc/ssl:ro
19+
prometheus:
20+
image: docker.io/prom/prometheus:${PROMETHEUS_VERSION:-v2.33.4}
21+
command: --config.file=/etc/vector/prometheus.yaml
22+
volumes:
23+
- ../../../../tests/data:/etc/vector:ro
24+
25+
networks:
26+
default:
27+
name: ${VECTOR_NETWORK}
28+
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("PROMETHEUS_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+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mod core;
2+
3+
use anyhow::Result;
4+
use clap::{Parser, Subcommand};
5+
6+
#[derive(Parser, Debug)]
7+
#[command(disable_help_subcommand = true)]
8+
struct Cli {
9+
#[command(subcommand)]
10+
command: Commands,
11+
}
12+
13+
#[derive(Subcommand, Debug)]
14+
enum Commands {
15+
Start { json: String },
16+
Stop { json: String },
17+
}
18+
19+
fn main() -> Result<()> {
20+
let cli = Cli::parse();
21+
22+
match &cli.command {
23+
Commands::Start { json } => core::start(serde_json::from_str(&json)?),
24+
Commands::Stop { json } => core::stop(serde_json::from_str(&json)?),
25+
}
26+
}

0 commit comments

Comments
 (0)