Skip to main content

core_dockpack/utils/
docker_commands.rs

1//! Defines the actions around downloading and unpacking docker images to access the files.
2use std::process::Command;
3use tar::Archive;
4use std::fs::File;
5use super::cache::process_image_name;
6
7
8/// Pulls a docker image from the docker registry.
9///
10/// # Arguments
11/// * `image_name` - A string slice that holds the name of the docker image to pull.
12///
13/// # Returns
14/// None
15pub fn pull_docker_image(image_name: &str) -> Result<(), String> {
16    let status = Command::new("docker")
17        .args(["pull", image_name])
18        .status().map_err(|e| e.to_string())?;
19
20    if status.success() {
21        Ok(())
22    } else {
23        Err("Failed to pull Docker image".to_string())
24    }
25}
26
27
28/// Pushes a Docker image to the Docker registry.
29/// 
30/// # Arguments
31/// * `image_name` - The name of the Docker image to push.
32/// 
33/// # Returns
34/// A result indicating if the Docker image was pushed successfully or an error message
35pub fn push_docker_image(image_name: &str) -> Result<(), String> {
36    let status = Command::new("docker")
37        .args(["push", image_name])
38        .status().map_err(|e| e.to_string())?;
39
40    if status.success() {
41        Ok(())
42    } else {
43        Err("Failed to push Docker image".to_string())
44    }
45}
46
47
48/// Extracts the Tar file from the Docker image, and saves it to the specified path.
49///
50/// # Notes
51/// The pulling of the Docker image is also handled in this function.
52///
53/// # Arguments
54/// * `image_name` - The name of the Docker image to pull and unpack.
55/// * `tar_path` - The path to save the unpacked Docker image.
56///
57/// # Returns
58/// The path to where the compressed Docker image files are stored
59pub fn save_docker_image(image_name: &str, tar_path: &str) -> Result<String, String> {
60    pull_docker_image(image_name)?; // Ensure the image is pulled before saving it
61
62    let tar_path = std::path::Path::new(tar_path);
63    let tar_file = image_name;
64    let tar_file = process_image_name(&tar_file.to_string());
65
66    let binding = tar_path.join(format!("{}.tar", tar_file));
67    let unpack_tar_path = match binding.to_str() {
68        Some(v) => v,
69        None => {
70            return Err("Failed to convert path to string".to_string())
71        }
72    };
73    let package_path = tar_path.join(tar_file);
74
75    println!("Tar path: {:?}", tar_path);
76
77    let _ = Command::new("docker")
78        .args(["save", "-o", unpack_tar_path, image_name])
79        .status().map_err(|e| e.to_string())?;
80
81    let tar_file = File::open(unpack_tar_path).map_err(|e| e.to_string())?;
82    let mut archive = Archive::new(tar_file);
83
84    archive.unpack(&package_path).map_err(|e| e.to_string())?;
85
86    // return statement
87    Ok(match package_path.to_str() {
88        Some(v) => v.to_string(),
89        None => {
90            return Err("Failed to convert path to string".to_string())
91        }
92    })
93}
94
95
96/// Builds a Docker image from a given build context directory.
97/// 
98/// # Arguments
99/// * `image` - A string slice that holds the name of the Docker image to build
100/// * `file_path` - A string slice that holds the path to the Dockerfile
101/// 
102/// # Returns
103/// * `Result<(), String>` - A result that indicates if the Docker image was built successfully or an error message
104pub fn build_docker_image(image: &str, file_path: &str) -> Result<(), String> {
105    let platforms = "linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/s390x,linux/ppc64le";
106    let status = Command::new("docker")
107    .args(["build", "--platform", platforms, "-t", image, "-f", file_path, "."])
108    // .arg(directory) // Add the directory as a separate argument
109    .status().map_err(|e| e.to_string())?;
110
111    if status.success() {
112        Ok(())
113    } else {
114        Err("Failed to build Docker image".to_string())
115    }
116}