-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathsled_agent.rs
221 lines (200 loc) · 7.15 KB
/
sled_agent.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
// 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/.
//! Simulated sled agent implementation
use crate::nexus::NexusClient;
use crate::params::{
DiskStateRequested, InstanceHardware, InstanceRuntimeStateRequested,
InstanceSerialConsoleData,
};
use crate::serial::ByteOffset;
use futures::lock::Mutex;
use omicron_common::api::external::Error;
use omicron_common::api::internal::nexus::DiskRuntimeState;
use omicron_common::api::internal::nexus::InstanceRuntimeState;
use slog::Logger;
use std::sync::Arc;
use uuid::Uuid;
use super::collection::SimCollection;
use super::config::Config;
use super::disk::SimDisk;
use super::instance::SimInstance;
use super::storage::{CrucibleData, Storage};
/// Simulates management of the control plane on a sled
///
/// The current implementation simulates a server directly in this program.
/// **It's important to be careful about the interface exposed by this struct.**
/// The intent is for it to eventually be implemented using requests to a remote
/// server. The tighter the coupling that exists now, the harder this will be to
/// move later.
pub struct SledAgent {
/// collection of simulated instances, indexed by instance uuid
instances: Arc<SimCollection<SimInstance>>,
/// collection of simulated disks, indexed by disk uuid
disks: Arc<SimCollection<SimDisk>>,
storage: Mutex<Storage>,
pub nexus_client: Arc<NexusClient>,
}
impl SledAgent {
// TODO-cleanup should this instantiate the NexusClient it needs?
// Should it take a Config object instead of separate id, sim_mode, etc?
/// Constructs a simulated SledAgent with the given uuid.
pub fn new_simulated_with_id(
config: &Config,
log: Logger,
nexus_client: Arc<NexusClient>,
) -> SledAgent {
let id = config.id;
let sim_mode = config.sim_mode;
info!(&log, "created simulated sled agent"; "sim_mode" => ?sim_mode);
let instance_log = log.new(o!("kind" => "instances"));
let disk_log = log.new(o!("kind" => "disks"));
let storage_log = log.new(o!("kind" => "storage"));
SledAgent {
instances: Arc::new(SimCollection::new(
Arc::clone(&nexus_client),
instance_log,
sim_mode,
)),
disks: Arc::new(SimCollection::new(
Arc::clone(&nexus_client),
disk_log,
sim_mode,
)),
storage: Mutex::new(Storage::new(
id,
Arc::clone(&nexus_client),
config.storage.ip,
storage_log,
)),
nexus_client,
}
}
/// Idempotently ensures that the given API Instance (described by
/// `api_instance`) exists on this server in the given runtime state
/// (described by `target`).
pub async fn instance_ensure(
self: &Arc<Self>,
instance_id: Uuid,
initial_hardware: InstanceHardware,
target: InstanceRuntimeStateRequested,
) -> Result<InstanceRuntimeState, Error> {
self.instances
.sim_ensure(&instance_id, initial_hardware.runtime, target)
.await
}
/// Idempotently ensures that the given API Disk (described by `api_disk`)
/// is attached (or not) as specified. This simulates disk attach and
/// detach, similar to instance boot and halt.
pub async fn disk_ensure(
self: &Arc<Self>,
disk_id: Uuid,
initial_state: DiskRuntimeState,
target: DiskStateRequested,
) -> Result<DiskRuntimeState, Error> {
self.disks.sim_ensure(&disk_id, initial_state, target).await
}
pub async fn instance_poke(&self, id: Uuid) {
self.instances.sim_poke(id).await;
}
pub async fn disk_poke(&self, id: Uuid) {
self.disks.sim_poke(id).await;
}
/// Adds a Zpool to the simulated sled agent.
pub async fn create_zpool(&self, id: Uuid, size: u64) {
self.storage.lock().await.insert_zpool(id, size).await;
}
/// Adds a Crucible Dataset within a zpool.
pub async fn create_crucible_dataset(
&self,
zpool_id: Uuid,
dataset_id: Uuid,
) {
self.storage.lock().await.insert_dataset(zpool_id, dataset_id).await;
}
/// Returns a crucible dataset within a particular zpool.
pub async fn get_crucible_dataset(
&self,
zpool_id: Uuid,
dataset_id: Uuid,
) -> Arc<CrucibleData> {
self.storage.lock().await.get_dataset(zpool_id, dataset_id).await
}
/// Get contents of an instance's serial console.
pub async fn instance_serial_console_data(
&self,
instance_id: Uuid,
byte_offset: ByteOffset,
max_bytes: Option<usize>,
) -> Result<InstanceSerialConsoleData, String> {
if !self.instances.sim_contains(&instance_id).await {
return Err(format!("No such instance {}", instance_id));
}
// TODO: if instance state isn't running {
// return Ok(InstanceSerialConsoleData { data: vec![], last_byte_offset: 0 });
// }
let gerunds = [
"Loading",
"Reloading",
"Advancing",
"Reticulating",
"Defeating",
"Spoiling",
"Cooking",
"Destroying",
"Resenting",
"Introducing",
"Reiterating",
"Blasting",
"Tolling",
"Delivering",
"Engendering",
"Establishing",
];
let nouns = [
"canon",
"browsers",
"meta",
"splines",
"villains",
"plot",
"books",
"evidence",
"decisions",
"chaos",
"points",
"processors",
"bells",
"value",
"gender",
"shots",
];
let mut entropy = instance_id.as_u128();
let mut buf = format!(
"This is simulated serial console output for {}.\n",
instance_id
);
while entropy != 0 {
let gerund = gerunds[entropy as usize % gerunds.len()];
entropy /= gerunds.len() as u128;
let noun = nouns[entropy as usize % nouns.len()];
entropy /= nouns.len() as u128;
buf += &format!(
"{} {}... {}[\x1b[92m 0K \x1b[m]\n",
gerund,
noun,
" ".repeat(40 - gerund.len() - noun.len())
);
}
buf += "\x1b[2J\x1b[HOS/478 (localhorse) (ttyl)\n\nlocalhorse login: ";
let start = match byte_offset {
ByteOffset::FromStart(offset) => offset,
ByteOffset::MostRecent(offset) => buf.len() - offset,
};
let start = start.min(buf.len());
let end = (start + max_bytes.unwrap_or(16 * 1024)).min(buf.len());
let data = buf[start..end].as_bytes().to_vec();
let last_byte_offset = (start + data.len()) as u64;
Ok(InstanceSerialConsoleData { data, last_byte_offset })
}
}