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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use std::path::PathBuf;

use hyper::{Body, Client, Method, Request};
use hyperlocal::{UnixClientExt, UnixConnector};
use log::{debug, error, trace};
use tokio::time::timeout;

use crate::{
    model::{
        balloon::Balloon,
        balloon_stats::BalloonStatistics,
        balloon_stats_update::BalloonStatsUpdate,
        balloon_update::BalloonUpdate,
        boot_source::BootSource,
        cpu_template::CPUConfig,
        drive::Drive,
        entropy_device::EntropyDevice,
        firecracker_version::FirecrackerVersion,
        full_vm_configuration::FullVmConfiguration,
        instance_action_info::InstanceActionInfo,
        instance_info::InstanceInfo,
        logger::Logger,
        machine_configuration::MachineConfiguration,
        metrics::Metrics,
        mmds_config::{MmdsConfig, MmdsContentsObject},
        network_interface::NetworkInterface,
        partial_drive::PartialDrive,
        partial_network_interface::PartialNetworkInterface,
        snapshot_create_params::SnapshotCreateParams,
        snapshot_load_params::SnapshotLoadParams,
        vm::Vm,
        vsock::Vsock,
    },
    utils::{
        Json, DEFAULT_FIRECRACKER_INIT_TIMEOUT_SECONDS, DEFAULT_FIRECRACKER_REQUEST_TIMEOUT_SECONDS,
    },
};

#[derive(thiserror::Error, Debug)]
pub enum AgentError {
    #[error("Could not initate worksapce for machine, reason: {0}")]
    WorkspaceCreation(String),
    #[error("Could not delete worksapce for machine, reason: {0}")]
    WorkspaceDeletion(String),
    #[error("Could not execute command, reason: {0}")]
    CommandExecution(String),
    #[error("Failed to manage socket, reason: {0}")]
    Socket(String),
    #[error("Could not send request on uri {0}, reason: {1}")]
    Request(hyper::Uri, String),
    #[error("Could not serialize request or deserialize response, reason: {0}")]
    Serde(#[from] serde_json::Error),
    #[error("Socket didn't start on time")]
    Unhealthy,
}

pub struct Agent {
    pub(super) socket_path: PathBuf,
    client: Client<UnixConnector>,
    pub(super) firecracker_request_timeout: f64,
    pub(super) firecracker_init_timeout: f64,
}

impl Agent {
    pub(super) fn blank() -> Self {
        Agent {
            socket_path: "".into(),
            client: Client::unix(),
            firecracker_request_timeout: DEFAULT_FIRECRACKER_REQUEST_TIMEOUT_SECONDS,
            firecracker_init_timeout: DEFAULT_FIRECRACKER_INIT_TIMEOUT_SECONDS,
        }
    }

    pub fn new(socket_path: &PathBuf, request_timeout: f64, init_timeout: f64) -> Self {
        Agent {
            socket_path: socket_path.to_path_buf(),
            client: Client::unix(),
            firecracker_request_timeout: request_timeout,
            firecracker_init_timeout: init_timeout,
        }
    }

    async fn send_request(
        &self,
        url: hyper::Uri,
        method: Method,
        body: String,
    ) -> Result<String, AgentError> {
        debug!("Send request to socket: {}", url);
        trace!("Sent body to socket [{}]: {}", url, body);
        let request = Request::builder()
            .method(method)
            .uri(url.clone())
            .header("Content-Type", "application/json")
            .header("Accept", "application/json")
            .body(Body::from(body))
            .map_err(|e| AgentError::Request(url.clone(), e.to_string()))?;

        let response = timeout(
            tokio::time::Duration::from_secs_f64(self.firecracker_request_timeout),
            self.client.request(request),
        )
        .await
        .map_err(|e| {
            error!(
                target: "Agent::send_request",
                "timeout after {} seconds: {}",
                self.firecracker_request_timeout,
                e
            );
            AgentError::Request(
                url.clone(),
                format!(
                    "requesting: {} timeout after {} seconds",
                    url, self.firecracker_request_timeout
                ),
            )
        })?
        .map_err(|e| AgentError::Request(url.clone(), e.to_string()))?;
        // let response = self
        //     .client
        //     .request(request)
        //     .await
        //     .map_err(|e| AgentError::Request(url.clone(), e.to_string()))?;

        trace!("Response status: {:#?}", response.status());

        let status = response.status();
        if !status.is_success() {
            error!("Request to socket failed [{}]: {:#?}", url, status);
            // body stream to string
            let body = hyper::body::to_bytes(response.into_body())
                .await
                .map_err(|e| AgentError::Request(url.clone(), e.to_string()))?;
            error!(
                "Request [{}] body: {}",
                url,
                String::from_utf8(body.to_vec()).unwrap()
            );
            return Err(AgentError::CommandExecution(format!(
                "Failed to send request to {}, status: {}",
                url, status
            )));
        } else {
            let body = hyper::body::to_bytes(response.into_body())
                .await
                .map_err(|e| AgentError::Request(url.clone(), e.to_string()))?;
            let string = String::from_utf8(body.to_vec()).unwrap();
            Ok(string)
        }
    }

    // PUT /snapshot/create
    pub async fn create_snapshot(
        &self,
        snapshot_create_params: &SnapshotCreateParams,
    ) -> Result<(), AgentError> {
        debug!("create_snapshot: {:#?}", snapshot_create_params);
        let json = snapshot_create_params
            .to_json()
            .map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/snapshot/create").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /actions
    pub async fn create_sync_action(&self, action: &InstanceActionInfo) -> Result<(), AgentError> {
        debug!("create_sync_action: {:#?}", action);
        let json = action.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/actions").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // GET /balloon
    pub async fn describe_balloon_config(&self) -> Result<Balloon, AgentError> {
        debug!("describe_balloon_config");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/balloon").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        let balloon = Balloon::from_json(&string).map_err(AgentError::Serde)?;
        Ok(balloon)
    }

    // GET /balloon/statistics
    pub async fn describe_balloon_stats(&self) -> Result<BalloonStatistics, AgentError> {
        debug!("describe_balloon_stats");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/balloon/statistics").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        let balloon_stats = BalloonStatistics::from_json(&string).map_err(AgentError::Serde)?;
        Ok(balloon_stats)
    }

    // GET /
    pub async fn describe_instance(&self) -> Result<InstanceInfo, AgentError> {
        debug!("describe_instance");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        let instance_info = InstanceInfo::from_json(&string).map_err(AgentError::Serde)?;
        Ok(instance_info)
    }

    // GET /vm/config
    pub async fn get_export_vm_config(&self) -> Result<FullVmConfiguration, AgentError> {
        debug!("get_export_vm_config");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/vm/config").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        let vm_config = FullVmConfiguration::from_json(&string).map_err(AgentError::Serde)?;
        Ok(vm_config)
    }

    // GET /version
    pub async fn get_firecracker_version(&self) -> Result<FirecrackerVersion, AgentError> {
        debug!("get_firecracker_version");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/version").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        let version = FirecrackerVersion::from_json(&string).map_err(AgentError::Serde)?;
        Ok(version)
    }

    // GET /machine-config
    pub(super) async fn get_machine_configuration(&self) -> Result<MachineConfiguration, AgentError> {
        debug!("get_machine_configuration");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/machine-config").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        let machine_config = MachineConfiguration::from_json(&string).map_err(AgentError::Serde)?;
        Ok(machine_config)
    }

    // GET /mmds
    pub async fn get_mmds(&self) -> Result<String, AgentError> {
        debug!("get_mmds");
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/mmds").into();
        let string = self.send_request(url, Method::GET, String::new()).await?;
        Ok(string)
    }

    // PATCH /balloon/statistics
    pub async fn patch_balloon_stats_interval(
        &self,
        balloon_stats_update: &BalloonStatsUpdate,
    ) -> Result<(), AgentError> {
        debug!("patch_balloon_stats_interval: {:#?}", balloon_stats_update);
        let json = balloon_stats_update.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/balloon/statistics").into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PATCH /balloon
    pub async fn patch_balloon(&self, balloon_update: &BalloonUpdate) -> Result<(), AgentError> {
        debug!("patch_balloon: {:#?}", balloon_update);
        let json = balloon_update.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/balloon").into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PATCH /drives/{drive_id}
    pub async fn patch_guest_drive_by_id(
        &self,
        partial_drive: &PartialDrive,
    ) -> Result<(), AgentError> {
        debug!("patch_guest_drive_by_id: {:#?}", partial_drive);
        let drive_id = partial_drive.get_drive_id();
        let json = partial_drive.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri =
            hyperlocal::Uri::new(&self.socket_path, format!("/drives/{drive_id}").as_str()).into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PATCH /network-interfaces/{iface_id}
    pub async fn patch_guest_network_interface_by_id(
        &self,
        partial_network_interface: &PartialNetworkInterface,
    ) -> Result<(), AgentError> {
        debug!(
            "patch_guest_network_interface_by_id: {:#?}",
            partial_network_interface
        );
        let iface_id = partial_network_interface.get_iface_id();
        let json = partial_network_interface
            .to_json()
            .map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(
            &self.socket_path,
            format!("/network-interfaces/{iface_id}").as_str(),
        )
        .into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PATCH /machine-config
    pub async fn patch_machine_configuration(
        &self,
        machine_config: &MachineConfiguration,
    ) -> Result<(), AgentError> {
        debug!("patch_machine_configuration: {:#?}", machine_config);
        let json = machine_config.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/machine-config").into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PATCH /mmds
    pub async fn patch_mmds(
        &self,
        mmds_contents_object: &MmdsContentsObject,
    ) -> Result<(), AgentError> {
        debug!("patch_mmds: {:#?}", mmds_contents_object);
        let json = mmds_contents_object.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/mmds").into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PATCH /vm
    pub async fn patch_vm(&self, vm: &Vm) -> Result<(), AgentError> {
        debug!("patch_vm: {:#?}", vm);
        let json = vm.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/vm").into();
        self.send_request(url, Method::PATCH, json).await?;
        Ok(())
    }

    // PUT /snapshot/load
    pub async fn load_snapshot(
        &self,
        snapshot_load_params: &SnapshotLoadParams,
    ) -> Result<(), AgentError> {
        debug!("load_snapshot: {:#?}", snapshot_load_params);
        let json = snapshot_load_params.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/snapshot/load").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /balloon
    pub async fn put_balloon(&self, balloon: &Balloon) -> Result<(), AgentError> {
        debug!("put_balloon: {:#?}", balloon);
        let json = balloon.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/balloon").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /cpu-config
    pub async fn put_cpu_configuration(&self, cpu_config: &CPUConfig) -> Result<(), AgentError> {
        debug!("put_cpu_configuration: {:#?}", cpu_config);
        let json = cpu_config.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/cpu-config").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /entropy
    pub async fn put_entropy_device(
        &self,
        entropy_device: &EntropyDevice,
    ) -> Result<(), AgentError> {
        debug!("put_entropy_device: {:#?}", entropy_device);
        let json = entropy_device.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/entropy").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /boot-source
    pub async fn put_guest_boot_source(&self, boot_source: &BootSource) -> Result<(), AgentError> {
        debug!("put_guest_boot_source: {:#?}", boot_source);
        let json = boot_source.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/boot-source").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /drives/{drive_id}
    pub async fn put_guest_drive_by_id(&self, drive: &Drive) -> Result<(), AgentError> {
        debug!("put_guest_drive_by_id: {:#?}", drive);
        let drive_id = drive.get_drive_id();
        let json = drive.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri =
            hyperlocal::Uri::new(&self.socket_path, format!("/drives/{drive_id}").as_str()).into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /network-interfaces/{iface_id}
    pub async fn put_guest_network_interface_by_id(
        &self,
        network_interface: &NetworkInterface,
    ) -> Result<(), AgentError> {
        debug!(
            "put_guest_network_interface_by_id: {:#?}",
            network_interface
        );
        let iface_id = network_interface.get_iface_id();
        let json = network_interface.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(
            &self.socket_path,
            format!("/network-interfaces/{iface_id}").as_str(),
        )
        .into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /vsock
    pub async fn put_guest_vsock(&self, vsock: &Vsock) -> Result<(), AgentError> {
        debug!("put_guest_vsock: {:#?}", vsock);
        let json = vsock.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/vsock").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /logger
    pub async fn put_logger(&self, logger: &Logger) -> Result<(), AgentError> {
        debug!("put_logger: {:#?}", logger);
        let json = logger.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/logger").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /machine-config
    pub async fn put_machine_configuration(
        &self,
        machine_config: &MachineConfiguration,
    ) -> Result<(), AgentError> {
        debug!("put_machine_configuration: {:#?}", machine_config);
        let json = machine_config.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/machine-config").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /metrics
    pub async fn put_metrics(&self, metrics: &Metrics) -> Result<(), AgentError> {
        debug!("put_metrics: {:#?}", metrics);
        let json = metrics.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/metrics").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /mmds/config
    pub async fn put_mmds_config(&self, mmds_config: &MmdsConfig) -> Result<(), AgentError> {
        debug!("put_mmds_config: {:#?}", mmds_config);
        let json = mmds_config.to_json().map_err(AgentError::Serde)?;

        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/mmds/config").into();
        self.send_request(url, Method::PUT, json).await?;
        Ok(())
    }

    // PUT /mmds
    pub async fn put_mmds(
        &self,
        mmds_contents_object: &MmdsContentsObject,
    ) -> Result<(), AgentError> {
        debug!("put_mmds: {:#?}", mmds_contents_object);
        let url: hyper::Uri = hyperlocal::Uri::new(&self.socket_path, "/mmds").into();

        self.send_request(url, Method::PUT, mmds_contents_object.to_string())
            .await?;
        Ok(())
    }
}