forked from containerd/runwasi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
68 lines (58 loc) · 1.79 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
#[cfg(feature = "oci-v1-tar")]
use {
anyhow::Context,
oci_spec::image::{self as spec, Arch},
oci_tar_builder::Builder,
sha256::try_digest,
std::env,
std::fs::File,
std::path::PathBuf,
};
#[cfg(not(feature = "oci-v1-tar"))]
fn main() {}
#[cfg(feature = "oci-v1-tar")]
fn main() {
env_logger::init();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let p = out_dir.join("img.tar");
let bin_output_dir = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
let app_path = bin_output_dir.join("wasi-demo-app.wasm");
let layer_path = out_dir.join("layer.tar");
tar::Builder::new(File::create(&layer_path).unwrap())
.append_path_with_name(&app_path, "wasi-demo-app.wasm")
.unwrap();
let mut builder = Builder::default();
builder.add_layer(&layer_path);
let config = spec::ConfigBuilder::default()
.entrypoint(vec!["/wasi-demo-app.wasm".to_owned()])
.build()
.unwrap();
let layer_digest = try_digest(layer_path.as_path()).unwrap();
let img = spec::ImageConfigurationBuilder::default()
.config(config)
.os("wasip1")
.architecture(Arch::Wasm)
.rootfs(
spec::RootFsBuilder::default()
.diff_ids(vec!["sha256:".to_owned() + &layer_digest])
.build()
.unwrap(),
)
.build()
.context("failed to build image configuration")
.unwrap();
builder.add_config(
img,
"ghcr.io/containerd/runwasi/wasi-demo-app:latest".to_string(),
spec::MediaType::ImageConfig,
);
let f = File::create(&p).unwrap();
builder.build(f).unwrap();
std::fs::rename(&p, bin_output_dir.join("img.tar")).unwrap();
}