-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlib.rs
441 lines (378 loc) · 13 KB
/
lib.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Definitions for types exposed by the propolis-server API
use std::{fmt, net::SocketAddr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
// Re-export types that are of a public struct
use crate::instance_spec::VersionedInstanceSpec;
pub use crucible_client_types::VolumeConstructionRequest;
pub mod instance_spec;
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstanceVCRReplace {
pub name: String,
pub vcr_json: String,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstanceNameParams {
pub instance_id: String,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstancePathParams {
pub instance_id: Uuid,
}
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
pub struct InstanceMetadata {
pub silo_id: Uuid,
pub project_id: Uuid,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceEnsureRequest {
pub properties: InstanceProperties,
#[serde(default)]
pub nics: Vec<NetworkInterfaceRequest>,
#[serde(default)]
pub disks: Vec<DiskRequest>,
pub migrate: Option<InstanceMigrateInitiateRequest>,
// base64 encoded cloud-init ISO
pub cloud_init_bytes: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceSpecEnsureRequest {
pub properties: InstanceProperties,
pub instance_spec: VersionedInstanceSpec,
pub migrate: Option<InstanceMigrateInitiateRequest>,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstanceEnsureResponse {
pub migrate: Option<InstanceMigrateInitiateResponse>,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceMigrateInitiateRequest {
pub migration_id: Uuid,
pub src_addr: SocketAddr,
pub src_uuid: Uuid,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceMigrateInitiateResponse {
pub migration_id: Uuid,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceMigrateStartRequest {
pub migration_id: Uuid,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceMigrateStatusRequest {
pub migration_id: Uuid,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq, Eq)]
pub struct InstanceMigrateStatusResponse {
pub migration_id: Uuid,
pub state: MigrationState,
}
#[derive(
Clone,
Copy,
Debug,
Deserialize,
PartialEq,
Eq,
PartialOrd,
Ord,
Serialize,
JsonSchema,
)]
pub enum MigrationState {
Sync,
RamPush,
Pause,
RamPushDirty,
Device,
Resume,
RamPull,
Server,
Finish,
Error,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstanceGetResponse {
pub instance: Instance,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstanceSpecGetResponse {
pub properties: InstanceProperties,
pub state: InstanceState,
pub spec: VersionedInstanceSpec,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct InstanceStateMonitorRequest {
pub gen: u64,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceStateMonitorResponse {
pub gen: u64,
pub state: InstanceState,
pub migration: Option<InstanceMigrateStatusResponse>,
}
/// Requested state of an Instance.
#[derive(Clone, Copy, Deserialize, Serialize, JsonSchema)]
pub struct InstanceStateChange {
pub state: InstanceStateRequested,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, JsonSchema)]
pub enum InstanceStateRequested {
Run,
Stop,
Reboot,
}
/// Current state of an Instance.
#[derive(
Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize, JsonSchema,
)]
pub enum InstanceState {
Creating,
Starting,
Running,
Stopping,
Stopped,
Rebooting,
Migrating,
Repairing,
Failed,
Destroyed,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, JsonSchema)]
pub struct InstanceProperties {
/// Unique identifier for this Instance.
pub id: Uuid,
/// Human-readable name of the Instance.
pub name: String,
/// Free-form text description of an Instance.
pub description: String,
/// Metadata used to track statistics for this Instance.
pub metadata: InstanceMetadata,
/// ID of the image used to initialize this Instance.
pub image_id: Uuid,
/// ID of the bootrom used to initialize this Instance.
pub bootrom_id: Uuid,
/// Size of memory allocated to the Instance, in MiB.
pub memory: u64,
/// Number of vCPUs to be allocated to the Instance.
pub vcpus: u8,
}
impl InstanceProperties {
/// Return the name of the VMM resource backing this VM.
pub fn vm_name(&self) -> String {
self.id.to_string()
}
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct Instance {
pub properties: InstanceProperties,
pub state: InstanceState,
pub disks: Vec<DiskAttachment>,
pub nics: Vec<NetworkInterface>,
}
/// Request a specific range of an Instance's serial console output history.
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)]
pub struct InstanceSerialConsoleHistoryRequest {
/// Character index in the serial buffer from which to read, counting the bytes output since
/// instance start. If this is not provided, `most_recent` must be provided, and if this *is*
/// provided, `most_recent` must *not* be provided.
pub from_start: Option<u64>,
/// Character index in the serial buffer from which to read, counting *backward* from the most
/// recently buffered data retrieved from the instance. (See note on `from_start` about mutual
/// exclusivity)
pub most_recent: Option<u64>,
/// Maximum number of bytes of buffered serial console contents to return. If the requested
/// range runs to the end of the available buffer, the data returned will be shorter than
/// `max_bytes`.
pub max_bytes: Option<u64>,
}
/// Contents of an Instance's serial console buffer.
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct InstanceSerialConsoleHistoryResponse {
/// The bytes starting from the requested offset up to either the end of the buffer or the
/// request's `max_bytes`. Provided as a u8 array rather than a string, as it may not be UTF-8.
pub data: Vec<u8>,
/// The absolute offset since boot (suitable for use as `byte_offset` in a subsequent request)
/// of the last byte returned in `data`.
pub last_byte_offset: u64,
}
/// Connect to an Instance's serial console via websocket, optionally sending
/// bytes from the buffered history first.
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)]
pub struct InstanceSerialConsoleStreamRequest {
/// Character index in the serial buffer from which to read, counting the bytes output since
/// instance start. If this is provided, `most_recent` must *not* be provided.
// TODO: if neither is specified, send enough serial buffer history to reconstruct
// the current contents and cursor state of an interactive terminal
pub from_start: Option<u64>,
/// Character index in the serial buffer from which to read, counting *backward* from the most
/// recently buffered data retrieved from the instance. (See note on `from_start` about mutual
/// exclusivity)
pub most_recent: Option<u64>,
}
/// Control message(s) sent through the websocket to serial console clients.
///
/// Note: Because this is associated with the websocket, and not some REST
/// endpoint, Dropshot lacks the ability to communicate it via the OpenAPI
/// document underpinning the exposed interfaces. As such, clients (including
/// the `propolis-client` crate) are expected to define their own identical copy
/// of this type in order to consume it.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum InstanceSerialConsoleControlMessage {
Migrating { destination: SocketAddr, from_start: u64 },
}
/// Describes how to connect to one or more storage agent services.
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct StorageAgentDescription {
/// Addresses of storage agents.
pub agents: Vec<std::net::SocketAddrV6>,
/// Opaque key material for encryption and decryption.
/// May become more structured as encryption scheme is solidified.
pub key: Vec<u8>,
/// Minimum number of redundant copies of a block which must
/// be written until data is considered "persistent".
pub write_redundancy_threshold: u32,
}
/// Refer to RFD 135 for more information on Virtual Storage Interfaces.
/// This describes the type of disk which should be exposed to the guest VM.
#[derive(Clone, Copy, Deserialize, Serialize, JsonSchema)]
pub enum DiskType {
NVMe,
VirtioBlock,
}
/// Describes a virtual disk.
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct Disk {
/// Unique identifier for this disk.
pub id: Uuid,
/// Storage agents which implement networked block device servers.
pub storage_agents: StorageAgentDescription,
/// Size of the disk (blocks).
pub block_count: u64,
/// Block size (bytes).
pub block_size: u32,
/// Storage interface.
pub interface: DiskType,
}
// TODO: Struggling to make this struct derive "JsonSchema"
/*
bitflags! {
#[derive(Deserialize, Serialize)]
pub struct DiskFlags: u32 {
const READ = 0b0000_0001;
const WRITE = 0b0000_0010;
const READ_WRITE = Self::READ.bits | Self::WRITE.bits;
}
}
*/
// TODO: Remove this; it's a hack to workaround the above bug.
#[allow(dead_code)]
pub const DISK_FLAG_READ: u32 = 0b0000_0001;
#[allow(dead_code)]
pub const DISK_FLAG_WRITE: u32 = 0b0000_0010;
#[allow(dead_code)]
pub const DISK_FLAG_READ_WRITE: u32 = DISK_FLAG_READ | DISK_FLAG_WRITE;
type DiskFlags = u32;
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct DiskRequest {
pub name: String,
pub slot: Slot,
pub read_only: bool,
pub device: String,
// Crucible related opts
pub volume_construction_request:
crucible_client_types::VolumeConstructionRequest,
}
#[derive(Clone, Deserialize, Serialize, JsonSchema)]
pub struct DiskAttachmentInfo {
pub flags: DiskFlags,
pub slot: u16,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub enum DiskAttachmentState {
Attached(Uuid),
Detached,
Destroyed,
Faulted,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct DiskAttachment {
pub generation_id: u64,
pub disk_id: Uuid,
pub state: DiskAttachmentState,
}
/// A stable index which is translated by Propolis
/// into a PCI BDF, visible to the guest.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct Slot(pub u8);
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct NetworkInterfaceRequest {
pub name: String,
pub slot: Slot,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub struct NetworkInterface {
pub name: String,
pub attachment: NetworkInterfaceAttachmentState,
}
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)]
pub enum NetworkInterfaceAttachmentState {
Attached(Slot),
Detached,
Faulted,
}
#[derive(Deserialize, JsonSchema)]
pub struct SnapshotRequestPathParams {
pub id: Uuid,
pub snapshot_id: Uuid,
}
#[derive(Deserialize, JsonSchema)]
pub struct VCRRequestPathParams {
pub id: Uuid,
}
/// Error codes used to populate the `error_code` field of Dropshot API responses.
#[derive(
Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize, JsonSchema,
)]
pub enum ErrorCode {
/// This `propolis-server` process has not received an `InstanceEnsure`
/// request yet.
NoInstance,
/// This `propolis-server` process has already received an `InstanceEnsure`
/// request with a different ID.
AlreadyInitialized,
/// Cannot update a running server.
AlreadyRunning,
/// Instance creation failed
CreateFailed,
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::str::FromStr for ErrorCode {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
s if s.eq_ignore_ascii_case("NoInstance") => Ok(Self::NoInstance),
s if s.eq_ignore_ascii_case("AlreadyInitialized") => {
Ok(ErrorCode::AlreadyInitialized)
}
s if s.eq_ignore_ascii_case("AlreadyRunning") => {
Ok(ErrorCode::AlreadyRunning)
}
s if s.eq_ignore_ascii_case("CreateFailed") => {
Ok(ErrorCode::CreateFailed)
}
_ => Err("unknown error code, expected one of: \
'NoInstance', 'AlreadyInitialized', 'AlreadyRunning', \
'CreateFailed'"),
}
}
}