Implement find_enabled_services method in DockerAdapter; update Docker struct and main function to retrieve and display enabled services.
All checks were successful
ci/woodpecker/push/test/1 Pipeline was successful
ci/woodpecker/push/test/2 Pipeline was successful

This commit is contained in:
2025-11-13 22:19:42 +00:00
parent 05a839044c
commit 9b7d07cdae
4 changed files with 36 additions and 3 deletions

View File

@@ -8,6 +8,9 @@ services:
volumes:
- caddy_data:/data
- caddy_config:/config
labels:
- "beekeper.enable"
- "beekeper.pre_command=caddy stop"
volumes:
caddy_data:

View File

@@ -1,4 +1,4 @@
use bollard::Docker;
use bollard::{Docker, query_parameters::ListServicesOptions};
use std::sync::Arc;
use tokio::sync::Mutex;
@@ -40,6 +40,28 @@ impl crate::docker::DockerAdapter for BollardAdapter {
Err(anyhow::anyhow!("Docker client is not connected"))
}
}
async fn find_enabled_services(&self) -> anyhow::Result<Vec<String>> {
let client = self.client.lock().await;
let docker = client.as_ref().ok_or_else(|| anyhow::anyhow!("Docker client is not connected"))?;
let services = docker.list_services(None::<ListServicesOptions>).await?;
let mut enabled_services = Vec::new();
for service in services {
if let Some(spec) = service.spec {
let enabled = spec.labels
.map(|hash| hash.contains_key("beekeper.enable"))
.is_some();
if enabled && let Some(name) = spec.name {
enabled_services.push(name);
}
}
}
Ok(enabled_services)
}
}
impl Default for BollardAdapter {

View File

@@ -2,11 +2,11 @@ mod bollard;
pub use bollard::BollardAdapter;
pub struct Docker<T: DockerAdapter> {
pub struct Docker<T> where T: DockerAdapter {
adapter: T,
}
impl<T: DockerAdapter> Docker<T> {
impl<T> Docker<T> where T: DockerAdapter {
pub fn new(adapter: T) -> Self {
Self { adapter }
}
@@ -18,9 +18,14 @@ impl<T: DockerAdapter> Docker<T> {
pub async fn version(&self) -> anyhow::Result<String> {
self.adapter.version().await
}
pub async fn find_enabled_services(&self) -> anyhow::Result<Vec<String>> {
self.adapter.find_enabled_services().await
}
}
pub trait DockerAdapter {
async fn connect_docker(&self, docker_host: Option<String>) -> anyhow::Result<()>;
async fn version(&self) -> anyhow::Result<String>;
async fn find_enabled_services(&self) -> anyhow::Result<Vec<String>>;
}

View File

@@ -17,6 +17,9 @@ async fn main() -> anyhow::Result<()> {
let docker_conn = connect_docker(envs.docker_host).await?;
println!("Docker version: {}", docker_conn.version().await?);
let enabled_services = docker_conn.find_enabled_services().await?;
println!("Enabled services: {:?}", enabled_services);
Ok(())
}