+++ /dev/null
-use std::collections::HashMap;
-use std::ops::Deref;
-use std::process;
-use std::sync::{Arc, Mutex};
-use std::time::{Instant, Duration};
-use podman_api::models::{Container, MountPoint, Port, PortMapping};
-use podman_api::opts::ContainerListOpts;
-use podman_api::Podman;
-use crate::utils::Id;
-
-pub(crate) struct Containers {
- cache: Arc<Mutex<Option<(Instant, HashMap<[u8; 32], Container>)>>>,
- connection: Podman,
-}
-
-impl Containers {
- pub(crate) fn connect(uri: &str) -> Containers {
- let connection = match Podman::new(uri) {
- Ok(a) => a,
- Err(e) => {
- eprintln!("An error has occurred: {}", e);
- process::exit(32);
- }
- };
- let c = Containers {
- cache: Arc::new(Mutex::new(None)),
- connection,
- };
- c
- }
-
- fn time_since_update(&self) -> Duration {
- let i = match self.cache.lock().unwrap().deref() {
- Some(i) => { i.0 }
- None => { return Duration::MAX; }
- };
- Instant::now().duration_since(i)
- }
-
- pub(crate) async fn sync(&self) -> Result<(), ContainerError> {
- if self.time_since_update() > Duration::new(60, 0) {
- return match self.sync_helper().await {
- Ok(_) => { Ok(()) }
- Err(e) => { Err(e) }
- };
- }
- Ok(())
- }
-
- async fn sync_helper(&self) -> Result<(), ContainerError> {
- let containers_options = ContainerListOpts::builder().all(true).sync(true).build();
-
- let list_containers_response = match self.connection.containers().list(&containers_options).await {
- Ok(r) => { r }
- Err(e) => { return Err(ContainerError::PodmanError(e)); }
- };
-
- let mut containers: HashMap<[u8; 32], Container> = HashMap::new();
-
- for c in list_containers_response {
- let id = Id::hex_id_to_byte_array(match &c.id {
- Some(id) => { id }
- None => { return Err(ContainerError::UnableToParseId); }
- });
-
- let container: Container = Container {
- adjust_cpu_shares: None,
- command: Some(match c.command {
- None => { String::from("Unknown") }
- Some(command) => { command[0].clone() }
- }),
- config: None,
- created: match c.created {
- Some(created) => {
- Some(created.timestamp())
- }
- None => { None }
- },
- host_config: None,
- id: c.id,
- image: c.image,
- image_id: c.image_id,
- labels: c.labels,
- mounts: None,
- name: match &c.names {
- None => { None }
- Some(n) => { Some(n[0].clone()) }
- },
- names: c.names,
- network_settings: None,
- networking_config: None,
- platform: None,
- ports: match c.ports {
- None => { None }
- Some(p) => {
- let mut ports: Vec<Port> = Vec::new();
- for port_mapping in p {
- let port = Port {
- ip: port_mapping.host_ip,
- private_port: match port_mapping.container_port {
- None => { continue; }
- Some(p) => { p }
- },
- public_port: match port_mapping.host_port {
- None => { None }
- Some(p) => { Some(p) }
- },
- type_: "".to_string(),
- };
- ports.push(port);
- }
- Some(ports)
- }
- },
- size_root_fs: None,
- size_rw: None,
- state: c.state,
- status: c.status,
- };
-
- containers.insert(id, container);
- }
-
- Ok(())
- }
-}
-
-#[derive(Debug)]
-pub(crate) enum ContainerError {
- UnableToParseId,
- PodmanError(podman_api::Error),
-}
\ No newline at end of file