Skip to content

Commit 24174f4

Browse files
committed
Add agent endpoint
1 parent a47f9b7 commit 24174f4

9 files changed

Lines changed: 1173 additions & 16 deletions

File tree

agent/Cargo.lock

Lines changed: 317 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8+
bollard = "0.18.1"
89
reqwest = "0.12.14"
10+
serde_json = "1.0.140"
911
tokio = { version = "1.44.1", features = ["full"] }

agent/src/main.rs

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1-
use tokio::{signal, time::{sleep, Duration}};
1+
use bollard::{
2+
container::ListContainersOptions,
3+
image::ListImagesOptions,
4+
network::ListNetworksOptions,
5+
secret::{ContainerSummary, ImageSummary, Network},
6+
Docker, API_DEFAULT_VERSION,
7+
};
28
use reqwest::Client;
9+
use serde_json::json;
10+
use tokio::{
11+
signal,
12+
time::{sleep, Duration},
13+
};
314

4-
async fn perform_task(client: &Client) {
5-
let url = "https://example.com/api";
6-
match client.get(url).send().await {
15+
async fn send_status_update(
16+
containers: &Vec<ContainerSummary>,
17+
images: &Vec<ImageSummary>,
18+
networks: &Vec<Network>,
19+
) {
20+
let client = Client::new();
21+
22+
let url = "http://localhost:3000/api/v1/agent/status";
23+
24+
let body = json!({
25+
"containers": containers,
26+
"images": images,
27+
"networks": networks,
28+
});
29+
30+
match client
31+
.post(url)
32+
.header("Content-Type", "application/json")
33+
.header("x-agent-token", "10dbcfc6-9e9b-478f-be81-bbd8b1df176e")
34+
.body(body.to_string())
35+
.send()
36+
.await
37+
{
738
Ok(response) => {
839
if let Ok(body) = response.text().await {
940
println!("Received response: {}", body);
@@ -14,18 +45,47 @@ async fn perform_task(client: &Client) {
1445
}
1546

1647
#[tokio::main]
17-
async fn main() {
18-
let client = Client::new();
48+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
49+
let docker = Docker::connect_with_socket(
50+
"/Users/arjunkomath/.docker/run/docker.sock",
51+
120,
52+
API_DEFAULT_VERSION,
53+
)?;
1954

2055
let task_loop = tokio::spawn(async move {
2156
loop {
22-
perform_task(&client).await;
23-
sleep(Duration::from_secs(5)).await;
57+
let containers = &docker
58+
.list_containers(Some(ListContainersOptions::<String> {
59+
all: true,
60+
..Default::default()
61+
}))
62+
.await
63+
.unwrap();
64+
65+
let images = &docker
66+
.list_images(Some(ListImagesOptions::<String> {
67+
all: true,
68+
..Default::default()
69+
}))
70+
.await
71+
.unwrap();
72+
73+
let networks = &docker
74+
.list_networks(Some(ListNetworksOptions::<String> {
75+
..Default::default()
76+
}))
77+
.await
78+
.unwrap();
79+
80+
send_status_update(containers, images, networks).await;
81+
sleep(Duration::from_secs(15)).await;
2482
}
2583
});
2684

2785
let shutdown_signal = async {
28-
signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
86+
signal::ctrl_c()
87+
.await
88+
.expect("Failed to install Ctrl+C handler");
2989
};
3090

3191
tokio::select! {
@@ -34,4 +94,6 @@ async fn main() {
3494
}
3595
_ = task_loop => {}
3696
}
37-
}
97+
98+
Ok(())
99+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE "server" ADD COLUMN "status" text DEFAULT 'unknown' NOT NULL;--> statement-breakpoint
2+
ALTER TABLE "server" ADD COLUMN "configuration" text;

0 commit comments

Comments
 (0)