Skip to content

Commit

Permalink
feat(server): Add basic "docker ps" endpoint. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
KGB33 authored Dec 13, 2023
1 parent 81adf01 commit 3bd89b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions server/src/schema/docker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anyhow::anyhow;
use async_graphql::{Object, SimpleObject};
use serde::Deserialize;
use tokio::process::Command;

pub struct Docker;

const DOCKER_FORMAT_ARGS: [&str; 2] = ["--format", "{{json .}}"];

/// Information
#[Object]
impl Docker {
async fn ps(&self) -> anyhow::Result<Vec<DockerPsResponse>> {
let output = Command::new("docker")
.arg("ps")
.args(DOCKER_FORMAT_ARGS)
.output()
.await?;
if !&output.status.success() {
return match std::str::from_utf8(&output.stderr) {
Ok(s) => Err(anyhow!("{}", s)),
Err(e) => Err(e.into()),
};
}
let output = std::str::from_utf8(&output.stdout)?;
let mut processes: Vec<DockerPsResponse> = Vec::new();

for line in output.lines() {
let parsed = serde_json::from_str::<DockerPsResponse>(line)?;
processes.push(parsed);
}
Ok(processes)
}
}

#[derive(Deserialize, SimpleObject, Debug)]
#[serde(rename_all = "PascalCase")]
struct DockerPsResponse {
command: String,
created_at: String,
image: String,
names: String,
running_for: String,
state: String,
status: String,
}
7 changes: 7 additions & 0 deletions server/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use hostname::Hostname;
mod ip;
use ip::Ip;

mod docker;
use docker::Docker;

pub fn generate_schema() -> Schema<Query, EmptyMutation, EmptySubscription> {
Schema::build(Query, EmptyMutation, EmptySubscription).finish()
}
Expand All @@ -22,4 +25,8 @@ impl Query {
async fn ip(&self) -> Ip {
Ip
}

async fn docker(&self) -> Docker {
Docker
}
}

0 comments on commit 3bd89b5

Please sign in to comment.