-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.rs
160 lines (131 loc) · 6.16 KB
/
build.rs
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
152
153
154
155
156
157
158
159
160
use std::env;
use std::fs::{create_dir_all, File};
use std::io;
use std::io::Write;
use std::path::{Path, PathBuf};
use const_format::formatcp;
use flate2::read::GzDecoder;
use sha2::{Digest, Sha256};
use tar::Archive;
use zip::ZipArchive;
const DOCKER_VERSION: &str = "27.4.0";
const DOCKER_URL: &str =
formatcp!("https://download.docker.com/win/static/stable/x86_64/docker-{DOCKER_VERSION}.zip");
const DOCKER_SHA: &str = "afe80ee73916f577ae2603b05a707991d966be2b05382503b7ee07ef3d503c67";
const DOCKER_BUILDX_VERSION: &str = "0.19.2";
const DOCKER_BUILDX_URL: &str = formatcp!("https://github.com/docker/buildx/releases/download/v{DOCKER_BUILDX_VERSION}/buildx-v{DOCKER_BUILDX_VERSION}.windows-amd64.exe");
const DOCKER_BUILDX_SHA: &str = "6b13e5bdd8a40548886b69cc94716ff2f9a06db513983a0f158f80a3f2486432";
const DOCKER_COMPOSE_VERSION: &str = "2.31.0";
const DOCKER_COMPOSE_URL: &str = formatcp!("https://github.com/docker/compose/releases/download/v{DOCKER_COMPOSE_VERSION}/docker-compose-windows-x86_64.exe");
const DOCKER_COMPOSE_SHA: &str = "93fa851954c19e0e19b753817eca37c81c9f4394b0db1853ebc66cd62230ea75";
const WINCRED_VERSION: &str = "0.8.2";
const WINCRED_URL: &str = formatcp!("https://github.com/docker/docker-credential-helpers/releases/download/v{WINCRED_VERSION}/docker-credential-wincred-v{WINCRED_VERSION}.windows-amd64.exe");
const WINCRED_SHA: &str = "57d3ea7a97e73abd913f71b0ba4f497f729c640b022108716207b4bd47a9d658";
const CONTAINERD_VERSION: &str = "1.7.24";
const CONTAINERD_URL: &str = formatcp!("https://github.com/containerd/containerd/releases/download/v{CONTAINERD_VERSION}/containerd-{CONTAINERD_VERSION}-windows-amd64.tar.gz");
const CONTAINERD_SHA: &str = "3260984ce37c927a8d1de4c25471ebb0533abb5b9e6c4e30e3685df0ecbfb629";
const NERDCTL_VERSION: &str = "2.0.2";
const NERDCTL_URL: &str = formatcp!("https://github.com/containerd/nerdctl/releases/download/v{NERDCTL_VERSION}/nerdctl-{NERDCTL_VERSION}-windows-amd64.tar.gz");
const NERDCTL_SHA: &str = "e8273570f4ca3590bf97ca1365111ca62bd94ddd26133a8c85c36d0278d48fdf";
const BUILDKIT_VERSION: &str = "0.18.1";
const BUILDKIT_URL: &str = formatcp!("https://github.com/moby/buildkit/releases/download/v{BUILDKIT_VERSION}/buildkit-v{BUILDKIT_VERSION}.windows-amd64.tar.gz");
const BUILDKIT_SHA: &str = "8ae42a1a7bac280895d3a8faa6da315fd703f1bc191a81f6fbc71a4613f36296";
const CNI_VERSION: &str = "0.3.1";
const CNI_URL: &str = formatcp!("https://github.com/microsoft/windows-container-networking/releases/download/v{CNI_VERSION}/windows-container-networking-cni-amd64-{CNI_VERSION}.zip");
const CNI_SHA: &str = "0cc4ea9f8773b0b607bd78b7de2bbc85ddf5feb840677563e9f6c405798816d2";
fn get_dest_dir() -> PathBuf {
//<root or manifest path>/target/<profile>/
let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap();
let build_type = env::var("PROFILE").unwrap();
PathBuf::from(manifest_dir_string)
.join("target")
.join(build_type)
}
fn unzip(file: &Path, dest_dir: &Path) {
let compressed_data = File::open(file).unwrap();
let mut zip_archive = ZipArchive::new(compressed_data).unwrap();
for i in 0..zip_archive.len() {
let mut file = zip_archive.by_index(i).unwrap();
if file.is_dir() {
continue;
}
let path = dest_dir.join(file.enclosed_name().unwrap());
if let Some(p) = path.parent() {
create_dir_all(p).unwrap();
}
let mut outfile = File::create(&path).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
}
fn untar(file: &Path, dest_dir: &Path) {
let tar_gz = File::open(file).unwrap();
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
archive.unpack(dest_dir).unwrap();
}
fn build_docker(dest_dir: &Path) {
let compressed_path = dest_dir.join("docker.zip");
download_file(DOCKER_URL, DOCKER_SHA, &compressed_path);
unzip(&compressed_path, dest_dir);
}
fn build_wincred(dest_dir: &Path) {
let dest_path = dest_dir.join("docker-credential-wincred.exe");
download_file(WINCRED_URL, WINCRED_SHA, &dest_path);
}
fn build_containerd(dest_dir: &Path) {
let compressed_path = dest_dir.join("containerd.tar.gz");
download_file(CONTAINERD_URL, CONTAINERD_SHA, &compressed_path);
untar(&compressed_path, dest_dir);
}
fn build_nerdctl(dest_dir: &Path) {
let compressed_path = dest_dir.join("nerdctl.tar.gz");
download_file(NERDCTL_URL, NERDCTL_SHA, &compressed_path);
untar(&compressed_path, dest_dir);
}
fn build_buildkit(dest_dir: &Path) {
let compressed_path = dest_dir.join("buildkit.tar.gz");
download_file(BUILDKIT_URL, BUILDKIT_SHA, &compressed_path);
untar(&compressed_path, dest_dir);
}
fn build_cni(dest_dir: &Path) {
let compressed_path = dest_dir.join("cni.zip");
download_file(CNI_URL, CNI_SHA, &compressed_path);
unzip(&compressed_path, dest_dir);
}
fn download_file(uri: &str, expected_sha: &str, dest: &Path) {
if let Ok(mut file) = File::open(dest) {
let mut digest = Sha256::new();
io::copy(&mut file, &mut digest).unwrap();
let actual_sha = digest.finalize();
if expected_sha == format!("{actual_sha:x}") {
return;
}
}
let data = reqwest::blocking::get(uri).unwrap().bytes().unwrap();
let actual_sha = Sha256::digest(&data);
if format!("{actual_sha:x}") != expected_sha {
panic!("Checksum mismatch for {uri}: expected {expected_sha} but got {actual_sha:x}");
}
let mut outfile = File::create(dest).unwrap();
outfile.write_all(&data).unwrap();
}
fn build_docker_buildx(dest_dir: &Path) {
let dest_path = dest_dir.join("docker-buildx.exe");
download_file(DOCKER_BUILDX_URL, DOCKER_BUILDX_SHA, &dest_path);
}
fn build_docker_compose(dest_dir: &Path) {
let dest_path = dest_dir.join("docker-compose.exe");
download_file(DOCKER_COMPOSE_URL, DOCKER_COMPOSE_SHA, &dest_path);
}
fn main() {
let dest_dir = get_dest_dir();
build_docker(&dest_dir);
build_docker_buildx(&dest_dir);
build_docker_compose(&dest_dir);
build_wincred(&dest_dir);
build_containerd(&dest_dir);
build_nerdctl(&dest_dir);
build_buildkit(&dest_dir);
build_cni(&dest_dir);
println!("cargo:rerun-if-changed=build.rs");
}