Skip to content

Commit 1dfad84

Browse files
authored
Merge pull request #6 from TNO-S3/pull-image
fix: Add code to pull image
2 parents 6ec4ceb + 91b1324 commit 1dfad84

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ eula = false
1616
[dependencies]
1717
clap = { version = "4.5.28", features = ["cargo", "derive", "env"] }
1818
dockworker = "0.6.0"
19+
futures-util = "0.3.31"
1920
include_dir = "0.7.4"
2021
tokio = "1.43.0"
2122

src/docker_util.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use crate::embed_files;
12
use dockworker::container::ContainerFilters;
23
use dockworker::{ContainerCreateOptions, ContainerHostConfig, Docker, PortBindings};
4+
use futures_util::StreamExt;
35
use include_dir::Dir;
6+
use std::future;
47
use std::path::Path;
58
use std::time::Duration;
69

7-
use crate::embed_files;
8-
910
pub fn connect_docker() -> Docker {
1011
return Docker::connect_with_defaults().expect("[-] Failed to connect to Docker");
1112
}
@@ -67,6 +68,29 @@ pub async fn start_container<'a>(
6768
];
6869
host_config.binds(binds);
6970

71+
// Pull the image
72+
println!("[*] Pulling grafana image ...");
73+
match docker
74+
.create_image("grafana/grafana-enterprise", "latest")
75+
.await
76+
{
77+
Err(error) => {
78+
println!("[-] Error: {:?}\n\tPulling the grafana image failed", error);
79+
panic!("")
80+
}
81+
Ok(result) => {
82+
result
83+
.for_each(|item| {
84+
if let Err(error) = item {
85+
println!("[-] Error: {:?}\n\tPulling the grafana image failed", error);
86+
panic!("")
87+
}
88+
future::ready(())
89+
})
90+
.await
91+
}
92+
}
93+
7094
// Create the container
7195
let mut create_options = ContainerCreateOptions::new("grafana/grafana-enterprise:latest");
7296
create_options
@@ -89,7 +113,9 @@ pub async fn start_container<'a>(
89113
// Start the container
90114
docker.start_container(&container_id).await.unwrap();
91115

92-
println!("[*] Grafana container started successfully. It can be accessed at http://localhost:3000");
116+
println!(
117+
"[*] Grafana container started successfully. It can be accessed at http://localhost:3000"
118+
);
93119
}
94120

95121
pub async fn remove_container(docker: &Docker, container_id: String) {
@@ -156,4 +182,4 @@ pub async fn get_dead_container_id(docker: &Docker, container_name: &str) -> Opt
156182
.iter()
157183
.find(|&c| c.Names.contains(&format!("/{}", container_name)))
158184
.map(|c| c.Id.clone())
159-
}
185+
}

0 commit comments

Comments
 (0)