-
Notifications
You must be signed in to change notification settings - Fork 23
/
deploy.rs
164 lines (151 loc) · 5.57 KB
/
deploy.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
161
162
163
164
//! Perform initial setup for a container image based system root
use std::collections::HashSet;
use super::store::LayeredImageState;
use super::{ImageReference, OstreeImageReference};
use crate::container::store::PrepareResult;
use crate::keyfileext::KeyFileExt;
use crate::sysroot::SysrootLock;
use anyhow::Result;
use fn_error_context::context;
use ostree::glib;
/// The key in the OSTree origin which holds a serialized [`super::OstreeImageReference`].
pub const ORIGIN_CONTAINER: &str = "container-image-reference";
/// The name of the default stateroot.
// xref https://github.com/ostreedev/ostree/issues/2794
pub const STATEROOT_DEFAULT: &str = "default";
/// Options configuring deployment.
#[derive(Debug, Default)]
pub struct DeployOpts<'a> {
/// Kernel arguments to use.
pub kargs: Option<&'a [&'a str]>,
/// Target image reference, as distinct from the source.
///
/// In many cases, one may want a workflow where a system is provisioned from
/// an image with a specific digest (e.g. `quay.io/example/os@sha256:...) for
/// reproducibilty. However, one would want `ostree admin upgrade` to fetch
/// `quay.io/example/os:latest`.
///
/// To implement this, use this option for the latter `:latest` tag.
pub target_imgref: Option<&'a OstreeImageReference>,
/// Configuration for fetching containers.
pub proxy_cfg: Option<super::store::ImageProxyConfig>,
/// If true, then no image reference will be written; but there will be refs
/// for the fetched layers. This ensures that if the machine is later updated
/// to a different container image, the fetch process will reuse shared layers, but
/// it will not be necessary to remove the previous image.
pub no_imgref: bool,
}
/// Write a container image to an OSTree deployment.
///
/// This API is currently intended for only an initial deployment.
#[context("Performing deployment")]
pub async fn deploy(
sysroot: &ostree::Sysroot,
stateroot: &str,
imgref: &OstreeImageReference,
options: Option<DeployOpts<'_>>,
) -> Result<Box<LayeredImageState>> {
let cancellable = ostree::gio::Cancellable::NONE;
let options = options.unwrap_or_default();
let repo = &sysroot.repo();
let merge_deployment = sysroot.merge_deployment(Some(stateroot));
let mut imp =
super::store::ImageImporter::new(repo, imgref, options.proxy_cfg.unwrap_or_default())
.await?;
if let Some(target) = options.target_imgref {
imp.set_target(target);
}
if options.no_imgref {
imp.set_no_imgref();
}
let state = match imp.prepare().await? {
PrepareResult::AlreadyPresent(r) => r,
PrepareResult::Ready(prep) => {
if let Some(warning) = prep.deprecated_warning() {
crate::cli::print_deprecated_warning(warning).await;
}
imp.import(prep).await?
}
};
let commit = state.get_commit();
let origin = glib::KeyFile::new();
let target_imgref = options.target_imgref.unwrap_or(imgref);
origin.set_string("origin", ORIGIN_CONTAINER, &target_imgref.to_string());
if sysroot.booted_deployment().is_some() {
let opts = ostree::SysrootDeployTreeOpts {
override_kernel_argv: options.kargs,
..Default::default()
};
sysroot.stage_tree_with_options(
Some(stateroot),
commit,
Some(&origin),
merge_deployment.as_ref(),
&opts,
cancellable,
)?;
} else {
let deployment = &sysroot.deploy_tree(
Some(stateroot),
commit,
Some(&origin),
merge_deployment.as_ref(),
options.kargs.unwrap_or_default(),
cancellable,
)?;
let flags = ostree::SysrootSimpleWriteDeploymentFlags::NONE;
sysroot.simple_write_deployment(
Some(stateroot),
deployment,
merge_deployment.as_ref(),
flags,
cancellable,
)?;
sysroot.cleanup(cancellable)?;
}
Ok(state)
}
/// Query the container image reference for a deployment
fn deployment_origin_container(
deploy: &ostree::Deployment,
) -> Result<Option<OstreeImageReference>> {
let origin = deploy
.origin()
.map(|o| o.optional_string("origin", ORIGIN_CONTAINER))
.transpose()?
.flatten();
let r = origin
.map(|v| OstreeImageReference::try_from(v.as_str()))
.transpose()?;
Ok(r)
}
/// Remove all container images which are not the target of a deployment.
/// This acts equivalently to [`super::store::remove_images()`] - the underlying layers
/// are not pruned.
///
/// The set of removed images is returned.
pub fn remove_undeployed_images(sysroot: &SysrootLock) -> Result<Vec<ImageReference>> {
let repo = &sysroot.repo();
let deployment_origins: Result<HashSet<_>> = sysroot
.deployments()
.into_iter()
.filter_map(|deploy| {
deployment_origin_container(&deploy)
.map(|v| v.map(|v| v.imgref))
.transpose()
})
.collect();
let deployment_origins = deployment_origins?;
// TODO add an API that returns ImageReference instead
let all_images = super::store::list_images(&sysroot.repo())?
.into_iter()
.filter_map(|img| ImageReference::try_from(img.as_str()).ok());
let mut removed = Vec::new();
for image in all_images {
if !deployment_origins.contains(&image) {
super::store::remove_image(repo, &image)?;
removed.push(image);
}
}
Ok(removed)
}