-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdocker.rs
More file actions
151 lines (128 loc) · 4.87 KB
/
Copy pathdocker.rs
File metadata and controls
151 lines (128 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Docker operations for building projects.
//!
//! This module handles Docker image building and container execution for project builds.
use crate::docker::{
build::build_docker_image,
core::{get_current_gid, get_current_uid, image_exists},
};
use crate::embedded_assets;
use crate::paths::foc_localnet_docker_volumes_cache;
use std::collections::HashMap;
use std::fs;
use tracing::info;
use super::Project;
/// Build the builder Docker image.
pub fn build_builder_image(dockerfile_dir: &str) -> Result<String, Box<dyn std::error::Error>> {
let image_tag = "foc-builder";
// Check if image already exists in Docker
if image_exists(image_tag)? {
info!("Docker image {} already exists, skipping build", image_tag);
} else {
info!("Building Docker image for builder...");
build_image_from_dockerfile(dockerfile_dir, image_tag)?;
}
Ok(image_tag.to_string())
}
/// Build Docker image from Dockerfile.
pub fn build_image_from_dockerfile(
dockerfile_dir: &str,
image_tag: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let dockerfile_path = "docker/builder/Dockerfile";
let output = build_docker_image(dockerfile_path, image_tag, dockerfile_dir)?;
if !output.status.success() {
return Err("Failed to build Docker image".into());
}
Ok(())
}
/// Load volume mappings from embedded volumes_map.toml file for a specific image.
pub fn load_volume_map(
image_name: &str,
) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> {
let content_bytes = embedded_assets::get_volumes_map(image_name)
.ok_or_else(|| format!("Embedded volumes map not found for: {}", image_name))?;
let content = std::str::from_utf8(content_bytes)
.map_err(|e| format!("Invalid UTF-8 in volumes map for {}: {}", image_name, e))?;
#[derive(serde::Deserialize)]
struct VolumesMap {
volumes: HashMap<String, String>,
}
let volume_config: VolumesMap = toml::from_str(content).map_err(|e| {
format!(
"Failed to parse embedded volumes map for {}: {}",
image_name, e
)
})?;
Ok(volume_config.volumes)
}
/// Set up the Docker run arguments for the build container.
pub fn setup_docker_run_args(
source_dir: &str,
output_dir: &str,
image_tag: &str,
project: &Project,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let container_source_dir = "/workspace/source";
let container_output_dir = "/workspace/output";
// Give each project a unique container name so they can build simultaneously
let container_name = format!("foc-builder-{}", project);
let mut docker_run_args = vec![
"run".to_string(),
"--rm".to_string(),
"-u".to_string(),
"foc-user".to_string(),
"--name".to_string(),
container_name,
"-e".to_string(),
"HOME=/home/foc-user".to_string(),
"-v".to_string(),
format!("{}:{}", source_dir, container_source_dir),
"-v".to_string(),
format!("{}:{}", output_dir, container_output_dir),
];
// Load and apply volume mappings for this image
let volume_map = load_volume_map("builder")?;
if !volume_map.is_empty() {
let cache_dir = foc_localnet_docker_volumes_cache();
let image_volumes_dir = cache_dir.join("foc-builder");
for (host_subdir, container_path) in volume_map {
let host_path = image_volumes_dir.join(&host_subdir);
// Ensure the directory exists
fs::create_dir_all(&host_path)?;
docker_run_args.push("-v".to_string());
docker_run_args.push(format!("{}:{}", host_path.display(), container_path));
}
}
// Get current user's UID and GID to run container as the same user
let uid = get_current_uid()?;
let gid = get_current_gid()?;
docker_run_args.push("-u".to_string());
docker_run_args.push(format!("{}:{}", uid, gid));
docker_run_args.push(image_tag.to_string());
docker_run_args.push("/bin/bash".to_string());
docker_run_args.push("-c".to_string());
Ok(docker_run_args)
}
/// Set up the build script for the specific project.
pub fn setup_build_script(
project: &Project,
container_source_dir: &str,
container_output_dir: &str,
) -> String {
match project {
Project::Lotus => format!(
r#"git config --global --add safe.directory {} && \
cd {} && \
make clean 2k && \
cp lotus lotus-miner lotus-shed lotus-seed {}"#,
container_source_dir, container_source_dir, container_output_dir
),
Project::Curio => format!(
r#"git config --global --add safe.directory {} && \
cd {} && \
make clean 2k pdptool && \
cp curio sptool pdptool {}"#,
container_source_dir, container_source_dir, container_output_dir
),
}
}