Skip to content

Commit 237cda0

Browse files
committed
Add environment manager for loki
1 parent ecdc5ab commit 237cda0

File tree

7 files changed

+388
-0
lines changed

7 files changed

+388
-0
lines changed

scripts/integration/loki/Cargo.lock

+252
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/integration/loki/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "vector-loki-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/loki/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# vector-loki-env-manager
2+
3+
-----
4+
5+
## Usage
6+
7+
```text
8+
vdev int show loki
9+
vdev int start loki <ENV>
10+
vdev int stop loki <ENV>
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3'
2+
3+
services:
4+
loki:
5+
image: docker.io/grafana/loki:${LOKI_VERSION}
6+
command: -config.file=/etc/loki/local-config.yaml -auth.enabled=true
7+
8+
networks:
9+
default:
10+
name: ${VECTOR_NETWORK}
11+
external: true

scripts/integration/loki/src/core.rs

+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("LOKI_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+
}

scripts/integration/loki/src/main.rs

+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+
}

scripts/integration/loki/test.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
args:
2+
- --features
3+
- loki-integration-tests
4+
- --lib
5+
- '::loki::'
6+
7+
env:
8+
LOKI_ADDRESS: http://loki:3100
9+
10+
matrix:
11+
- version: 2.4.1

0 commit comments

Comments
 (0)