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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! `Local`: local runtimes (cua-vmm: container/gVisor, QEMU, Lume) and
//! images (cua-image). Local *sandboxes* are created with
//! `Sandboxes.create(provider = Local, image = …)`; the image string picks
//! the backend (`container:<ref>` or a bare OCI ref → container/gVisor,
//! `vm:<ref>` / `disk:<path>` → QEMU, `lume:<ref>` or `os = macos` → Lume).
use super::{Backend, run};
use crate::{CuaError, Result};
/// Status of one local runtime component.
#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
pub enum RuntimeCheckStatus {
/// Present and usable.
Ok,
/// Missing; `setup` can provision it.
Installable,
/// Missing or broken; needs user action.
Error,
/// Not used on this host.
NotApplicable,
/// Not reported.
Unknown,
}
/// One doctor check (one backend).
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct RuntimeCheck {
/// Backend (`container`, `qemu`, `lume`, ...).
pub name: String,
/// Status.
pub status: RuntimeCheckStatus,
/// Version, when detected.
pub version: Option<String>,
/// Explanation, missing pieces and what `setup` would do.
pub detail: String,
}
fn status_of(s: i32) -> RuntimeCheckStatus {
use cua_proto::daemon::v1::CheckStatus as S;
match S::try_from(s).unwrap_or_default() {
S::Ok => RuntimeCheckStatus::Ok,
S::Installable => RuntimeCheckStatus::Installable,
S::Error => RuntimeCheckStatus::Error,
S::NotApplicable => RuntimeCheckStatus::NotApplicable,
S::Unspecified => RuntimeCheckStatus::Unknown,
}
}
impl From<cua_proto::daemon::v1::RuntimeCheck> for RuntimeCheck {
fn from(c: cua_proto::daemon::v1::RuntimeCheck) -> Self {
RuntimeCheck {
name: c.name,
status: status_of(c.status),
version: (!c.version.is_empty()).then_some(c.version),
detail: c.detail,
}
}
}
/// One setup step.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct SetupStep {
/// Component.
pub name: String,
/// Status after the step.
pub status: RuntimeCheckStatus,
/// What was (or would be) done.
pub detail: String,
}
/// Doctor output.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct DoctorReport {
/// One check per backend.
pub checks: Vec<RuntimeCheck>,
/// The full cua-vmm host report as JSON.
pub report_json: String,
}
/// A local image.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct LocalImage {
/// Reference.
pub reference: String,
/// `container` or `vm`.
pub kind: String,
/// Disk path (VM) or engine image reference (container).
pub location: String,
/// Size in bytes, when known.
pub size_bytes: u64,
}
/// Space on the volume a local engine writes to.
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct StorageVolume {
/// Bytes available to this user.
pub available_bytes: u64,
/// Volume size in bytes.
pub total_bytes: u64,
/// What a person calls it: `Macintosh HD`, `Colima`, `Docker Desktop`.
pub name: String,
}
/// What local Spaces have room for (see [`Local::storage`]).
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]
pub struct LocalStorage {
/// This host's architecture (`arm64`, `amd64`).
pub host_arch: String,
/// Bytes cua keeps free on a volume: a create that would leave less
/// fails (`CUA_DISK_MIN_FREE`, default 5 GiB).
pub reserve_bytes: u64,
/// Where Lume writes macOS VMs.
pub lume: Option<StorageVolume>,
/// Where QEMU writes VM disks and the containerDisk cache.
pub qemu: Option<StorageVolume>,
/// The container engine's data disk (Colima, Docker Desktop, OrbStack,
/// `/var/lib/docker`), when its free space can be read.
pub container: Option<StorageVolume>,
/// Catalog image refs already pulled here at their published digest.
pub pulled: Vec<String>,
pub linux_runtime_download_bytes: u64,
}
impl From<cua_daemon::storage::StorageReport> for LocalStorage {
fn from(r: cua_daemon::storage::StorageReport) -> Self {
let vol = |v: Option<cua_daemon::storage::Volume>| {
v.map(|v| StorageVolume {
available_bytes: v.available,
total_bytes: v.total,
name: v.name,
})
};
LocalStorage {
host_arch: r.host_arch,
reserve_bytes: r.reserve,
lume: vol(r.lume),
qemu: vol(r.qemu),
container: vol(r.container),
pulled: r.pulled,
linux_runtime_download_bytes: r.linux_runtime_download_bytes,
}
}
}
/// Local runtimes and images.
#[derive(uniffi::Object)]
pub struct Local {
pub(crate) backend: Backend,
}
#[uniffi::export]
impl Local {
/// Inspects local runtimes (read-only: never installs or starts
/// anything).
pub async fn doctor(&self) -> Result<DoctorReport> {
let backend = self.backend.clone();
run(async move {
let r = match &backend {
Backend::Embedded(_) => cua_daemon::local::doctor_response().await,
Backend::Daemon(d) => d.doctor().await?,
};
Ok(DoctorReport {
checks: r.checks.into_iter().map(Into::into).collect(),
report_json: r.report_json,
})
})
.await
}
/// Free space where local Spaces are written (Lume's VM storage, the
/// cua home for QEMU, the container engine's data disk) and which
/// catalog images are already pulled. Read-only and bounded; probed in
/// this process (the daemon runs on the same host).
pub async fn storage(&self) -> Result<LocalStorage> {
run(async move { Ok(cua_daemon::storage::storage_report().await.into()) }).await
}
/// Provisions `components` (`qemu`, `lume`; `runsc` reports its plan).
/// Empty = everything provisionable. `dry_run` only reports.
pub async fn setup(&self, components: Vec<String>, dry_run: bool) -> Result<Vec<SetupStep>> {
let backend = self.backend.clone();
run(async move {
let steps = match &backend {
Backend::Embedded(_) => cua_daemon::local::setup(&components, dry_run).await?,
Backend::Daemon(d) => d.setup(components, dry_run).await?,
};
Ok(steps
.into_iter()
.map(|s| SetupStep {
name: s.name,
status: status_of(s.status),
detail: s.detail,
})
.collect())
})
.await
}
/// Pulls an image (container engine for container refs, the image cache
/// for `vm:` containerDisks).
pub async fn pull_image(&self, reference: String) -> Result<LocalImage> {
let backend = self.backend.clone();
run(async move {
Ok(match &backend {
Backend::Embedded(rt) => {
let i = cua_daemon::local::pull_image(rt.vmm()?, &reference).await?;
LocalImage {
reference: i.reference,
kind: i.kind,
location: i.location,
size_bytes: i.size_bytes,
}
}
Backend::Daemon(d) => {
let i = d.pull_image(&reference).await?;
LocalImage {
reference: i.reference,
kind: i.kind,
location: i.location,
size_bytes: i.size_bytes,
}
}
})
})
.await
}
/// Builds an image from an `images.cua.ai/v1alpha1` Image resource
/// (JSON) on `base`; pushes to `push` when set. Returns
/// `reference@digest` or the local output directory.
pub async fn build_image(
&self,
spec_json: String,
base: String,
push: Option<String>,
) -> Result<String> {
let backend = self.backend.clone();
run(async move {
match &backend {
Backend::Embedded(rt) => {
let out = cua_daemon::cua_home()
.join("build")
.join(format!("out-{}", std::process::id()));
Ok(
cua_daemon::local::build_image(rt.vmm()?, &spec_json, &base, &out, push)
.await?,
)
}
Backend::Daemon(d) => Ok(d.build_image(&spec_json, &base, push).await?),
}
})
.await
}
/// Copies an image (host architecture) to `destination`; returns the
/// pushed manifest digest.
pub async fn push_image(&self, reference: String, destination: String) -> Result<String> {
let backend = self.backend.clone();
run(async move {
match &backend {
Backend::Embedded(_) => {
Ok(cua_daemon::local::push_image(&reference, &destination).await?)
}
Backend::Daemon(d) => Ok(d.push_image(&reference, &destination).await?),
}
})
.await
}
}
impl From<CuaError> for cua_daemon::Error {
fn from(e: CuaError) -> Self {
cua_daemon::Error::Internal(e.to_string())
}
}