-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathsupport_bundle_collector.rs
1450 lines (1310 loc) · 50.7 KB
/
support_bundle_collector.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/.
//! Background task for managing Support Bundles
use crate::app::background::BackgroundTask;
use camino::Utf8DirEntry;
use camino::Utf8Path;
use camino_tempfile::tempdir;
use camino_tempfile::tempfile;
use camino_tempfile::Utf8TempDir;
use futures::future::BoxFuture;
use futures::FutureExt;
use futures::TryStreamExt;
use nexus_db_model::SupportBundle;
use nexus_db_model::SupportBundleState;
use nexus_db_queries::context::OpContext;
use nexus_db_queries::db::DataStore;
use nexus_types::deployment::SledFilter;
use nexus_types::identity::Asset;
use omicron_common::api::external::DataPageParams;
use omicron_common::api::external::Error;
use omicron_common::api::external::ResourceType;
use omicron_common::update::ArtifactHash;
use omicron_uuid_kinds::DatasetUuid;
use omicron_uuid_kinds::GenericUuid;
use omicron_uuid_kinds::OmicronZoneUuid;
use omicron_uuid_kinds::SledUuid;
use omicron_uuid_kinds::SupportBundleUuid;
use omicron_uuid_kinds::ZpoolUuid;
use serde::Serialize;
use serde_json::json;
use sha2::{Digest, Sha256};
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::sync::Arc;
use tokio::io::AsyncReadExt;
use tokio::io::AsyncSeekExt;
use tokio::io::SeekFrom;
use zip::write::FullFileOptions;
use zip::ZipWriter;
// Specifies the data to be collected within the Support Bundle.
#[derive(Default)]
struct BundleRequest {
// If "false": Skip collecting host-specific info from each sled.
skip_sled_info: bool,
}
// Describes what happened while attempting to clean up Support Bundles.
#[derive(Debug, Default, Serialize, Eq, PartialEq)]
struct CleanupReport {
// Responses from Sled Agents
sled_bundles_deleted_ok: usize,
sled_bundles_deleted_not_found: usize,
sled_bundles_delete_failed: usize,
// Results from updating our database records
db_destroying_bundles_removed: usize,
db_failing_bundles_updated: usize,
}
// Result of asking a sled agent to clean up a bundle
enum SledAgentBundleCleanupResult {
Deleted,
NotFound,
Failed,
}
// Result of asking the database to delete a bundle
enum DatabaseBundleCleanupResult {
DestroyingBundleRemoved,
FailingBundleUpdated,
BadState,
}
/// The background task responsible for cleaning and collecting support bundles
pub struct SupportBundleCollector {
datastore: Arc<DataStore>,
disable: bool,
nexus_id: OmicronZoneUuid,
}
impl SupportBundleCollector {
pub fn new(
datastore: Arc<DataStore>,
disable: bool,
nexus_id: OmicronZoneUuid,
) -> Self {
SupportBundleCollector { datastore, disable, nexus_id }
}
// Tells a sled agent to delete a support bundle
async fn cleanup_bundle_from_sled_agent(
&self,
opctx: &OpContext,
sled_id: SledUuid,
bundle: &SupportBundle,
) -> anyhow::Result<SledAgentBundleCleanupResult> {
let sled_client = nexus_networking::sled_client(
&self.datastore,
&opctx,
sled_id.into_untyped_uuid(),
&opctx.log,
)
.await?;
let result = sled_client
.support_bundle_delete(
&ZpoolUuid::from(bundle.zpool_id),
&DatasetUuid::from(bundle.dataset_id),
&SupportBundleUuid::from(bundle.id),
)
.await;
match result {
Ok(_) => {
info!(
&opctx.log,
"SupportBundleCollector deleted bundle";
"id" => %bundle.id
);
return Ok(SledAgentBundleCleanupResult::Deleted);
}
Err(progenitor_client::Error::ErrorResponse(err))
if err.status() == http::StatusCode::NOT_FOUND =>
{
warn!(
&opctx.log,
"SupportBundleCollector could not delete bundle (not found)";
"id" => %bundle.id
);
return Ok(SledAgentBundleCleanupResult::NotFound);
}
err => {
warn!(
&opctx.log,
"SupportBundleCollector could not delete bundle";
"id" => %bundle.id,
"err" => ?err,
);
return Ok(SledAgentBundleCleanupResult::Failed);
}
}
}
async fn cleanup_bundle_from_database(
&self,
opctx: &OpContext,
bundle: &SupportBundle,
) -> anyhow::Result<DatabaseBundleCleanupResult> {
match bundle.state {
SupportBundleState::Destroying => {
// Destroying is a terminal state; no one should be able to
// change this state from underneath us.
self.datastore.support_bundle_delete(
opctx,
bundle.id.into(),
).await.map_err(|err| {
warn!(
&opctx.log,
"SupportBundleCollector: Could not delete 'destroying' bundle";
"err" => %err
);
anyhow::anyhow!("Could not delete 'destroying' bundle: {:#}", err)
})?;
return Ok(
DatabaseBundleCleanupResult::DestroyingBundleRemoved,
);
}
SupportBundleState::Failing => {
if let Err(err) = self
.datastore
.support_bundle_update(
&opctx,
bundle.id.into(),
SupportBundleState::Failed,
)
.await
{
if matches!(err, Error::InvalidRequest { .. }) {
// It's possible that the bundle is marked "destroying" by a
// user request, concurrently with our operation.
//
// In this case, we log that this happened, but do nothing.
// The next iteration of this background task should treat
// this as the "Destroying" case, and delete the bundle.
info!(
&opctx.log,
"SupportBundleCollector: Concurrent state change failing bundle";
"bundle" => %bundle.id,
"err" => ?err,
);
return Ok(DatabaseBundleCleanupResult::BadState);
} else {
warn!(
&opctx.log,
"Could not delete 'failing' bundle";
"err" => ?err,
);
anyhow::bail!(
"Could not delete 'failing' bundle: {:#}",
err
);
}
}
return Ok(DatabaseBundleCleanupResult::FailingBundleUpdated);
}
other => {
// We should be filtering to only see "Destroying" and
// "Failing" bundles in our database request above.
error!(
&opctx.log,
"SupportBundleCollector: Cleaning bundle in unexpected state";
"id" => %bundle.id,
"state" => ?other
);
return Ok(DatabaseBundleCleanupResult::BadState);
}
}
}
// Monitors all bundles that are "destroying" or "failing" and assigned to
// this Nexus, and attempts to clear their storage from Sled Agents.
async fn cleanup_destroyed_bundles(
&self,
opctx: &OpContext,
) -> anyhow::Result<CleanupReport> {
let pagparams = DataPageParams::max_page();
let result = self
.datastore
.support_bundle_list_assigned_to_nexus(
opctx,
&pagparams,
self.nexus_id,
vec![
SupportBundleState::Destroying,
SupportBundleState::Failing,
],
)
.await;
let bundles_to_destroy = match result {
Ok(r) => r,
Err(err) => {
warn!(
&opctx.log,
"SupportBundleCollector: Failed to list terminating bundles";
"err" => %err
);
anyhow::bail!("failed to query database: {:#}", err);
}
};
let mut report = CleanupReport::default();
// NOTE: This could be concurrent, but the priority for that also seems low
for bundle in bundles_to_destroy {
info!(
&opctx.log,
"SupportBundleCollector starting bundle deletion";
"id" => %bundle.id
);
// Find the sled where we're storing this bundle.
let result = self
.datastore
.zpool_get_sled_if_in_service(&opctx, bundle.zpool_id.into())
.await;
let delete_from_db = match result {
Ok(sled_id) => {
match self
.cleanup_bundle_from_sled_agent(
&opctx, sled_id, &bundle,
)
.await?
{
SledAgentBundleCleanupResult::Deleted => {
report.sled_bundles_deleted_ok += 1;
true
}
SledAgentBundleCleanupResult::NotFound => {
report.sled_bundles_deleted_not_found += 1;
true
}
SledAgentBundleCleanupResult::Failed => {
report.sled_bundles_delete_failed += 1;
// If the sled agent reports any other error, don't
// delete the bundle from the database. It might be
// transiently unavailable.
false
}
}
}
Err(Error::ObjectNotFound {
type_name: ResourceType::Zpool,
..
}) => {
// If the pool wasn't found in the database, it was
// expunged. Delete the support bundle, since there is no
// sled agent state to manage anymore.
true
}
Err(_) => false,
};
if delete_from_db {
match self.cleanup_bundle_from_database(opctx, &bundle).await? {
DatabaseBundleCleanupResult::DestroyingBundleRemoved => {
report.db_destroying_bundles_removed += 1;
}
DatabaseBundleCleanupResult::FailingBundleUpdated => {
report.db_failing_bundles_updated += 1;
}
DatabaseBundleCleanupResult::BadState => {}
}
}
}
Ok(report)
}
async fn collect_bundle(
&self,
opctx: &OpContext,
request: &BundleRequest,
) -> anyhow::Result<Option<CollectionReport>> {
let pagparams = DataPageParams::max_page();
let result = self
.datastore
.support_bundle_list_assigned_to_nexus(
opctx,
&pagparams,
self.nexus_id,
vec![SupportBundleState::Collecting],
)
.await;
let bundle = match result {
Ok(bundles) => {
if let Some(bundle) = bundles.get(0) {
info!(
&opctx.log,
"SupportBundleCollector: Found bundle to collect";
"bundle" => %bundle.id,
"bundles_in_queue" => bundles.len()
);
bundle.clone()
} else {
info!(&opctx.log, "No bundles to collect");
return Ok(None);
}
}
Err(err) => {
warn!(
&opctx.log,
"SupportBundleCollector: Failed to list collecting bundles";
"err" => %err
);
anyhow::bail!("failed to query database: {:#}", err);
}
};
let collection = BundleCollection {
collector: &self,
log: opctx.log.new(slog::o!("bundle" => bundle.id.to_string())),
opctx,
request,
bundle: &bundle,
};
let mut report = collection.collect_bundle_and_store_on_sled().await?;
if let Err(err) = self
.datastore
.support_bundle_update(
&opctx,
bundle.id.into(),
SupportBundleState::Active,
)
.await
{
if matches!(err, Error::InvalidRequest { .. }) {
info!(
&opctx.log,
"SupportBundleCollector: Concurrent state change activating bundle";
"bundle" => %bundle.id,
"err" => ?err,
);
return Ok(Some(report));
} else {
warn!(
&opctx.log,
"SupportBundleCollector: Unexpected error activating bundle";
"bundle" => %bundle.id,
"err" => ?err,
);
anyhow::bail!("failed to activate bundle: {:#}", err);
}
}
report.activated_in_db_ok = true;
Ok(Some(report))
}
}
// Wraps up all arguments to perform a single support bundle collection
struct BundleCollection<'a> {
// The task responsible for this collection
collector: &'a SupportBundleCollector,
log: slog::Logger,
opctx: &'a OpContext,
request: &'a BundleRequest,
bundle: &'a SupportBundle,
}
impl<'a> BundleCollection<'a> {
// Collect the bundle within Nexus, and store it on a target sled.
async fn collect_bundle_and_store_on_sled(
&self,
) -> anyhow::Result<CollectionReport> {
// Create a temporary directory where we'll store the support bundle
// as it's being collected.
let dir = tempdir()?;
let mut collection = Box::pin(self.collect_bundle_as_file(&dir));
// We periodically check the state of the support bundle - if a user
// explicitly cancels it, we should stop the collection process and
// return.
let work_duration = tokio::time::Duration::from_secs(5);
let mut yield_interval = tokio::time::interval_at(
tokio::time::Instant::now() + work_duration,
work_duration,
);
let report = loop {
tokio::select! {
// Timer fired mid-collection - let's check if we should stop.
_ = yield_interval.tick() => {
trace!(
&self.log,
"Checking if Bundle Collection cancelled";
"bundle" => %self.bundle.id
);
let bundle = self.collector.datastore.support_bundle_get(
&self.opctx,
self.bundle.id.into()
).await?;
if !matches!(bundle.state, SupportBundleState::Collecting) {
warn!(
&self.log,
"Support Bundle cancelled - stopping collection";
"bundle" => %self.bundle.id,
"state" => ?self.bundle.state
);
anyhow::bail!("Support Bundle Cancelled");
}
},
// Otherwise, keep making progress on the collection itself.
report = &mut collection => {
info!(
&self.log,
"Bundle Collection completed";
"bundle" => %self.bundle.id
);
break report?;
},
}
};
// Create the zipfile as a temporary file
let mut zipfile = tokio::fs::File::from_std(bundle_to_zipfile(&dir)?);
// Verify the hash locally before we send it over the network
zipfile.seek(SeekFrom::Start(0)).await?;
let hash = sha2_hash(&mut zipfile).await?;
// Find the sled where we're storing this bundle.
let sled_id = self
.collector
.datastore
.zpool_get_sled_if_in_service(
&self.opctx,
self.bundle.zpool_id.into(),
)
.await?;
let sled_client = nexus_networking::sled_client(
&self.collector.datastore,
&self.opctx,
sled_id.into_untyped_uuid(),
&self.log,
)
.await?;
// Stream the zipfile to the sled where it should be kept
zipfile.seek(SeekFrom::Start(0)).await?;
let file_access = hyper_staticfile::vfs::TokioFileAccess::new(zipfile);
let file_stream =
hyper_staticfile::util::FileBytesStream::new(file_access);
let body =
reqwest::Body::wrap(hyper_staticfile::Body::Full(file_stream));
sled_client
.support_bundle_create(
&ZpoolUuid::from(self.bundle.zpool_id),
&DatasetUuid::from(self.bundle.dataset_id),
&SupportBundleUuid::from(self.bundle.id),
&hash.to_string(),
body,
)
.await?;
// Returning from this method should drop all temporary storage
// allocated locally for this support bundle.
Ok(report)
}
// Perform the work of collecting the support bundle into a temporary directory
//
// - "dir" is a directory where data can be stored.
// - "bundle" is metadata about the bundle being collected.
//
// If a partial bundle can be collected, it should be returned as
// an Ok(CollectionReport). Any failures from this function will prevent
// the support bundle from being collected altogether.
//
// NOTE: The background task infrastructure will periodically check to see
// if the bundle has been cancelled by a user while it is being collected.
// If that happens, this function will be CANCELLED at an await point.
//
// As a result, it is important that this function be implemented as
// cancel-safe.
async fn collect_bundle_as_file(
&self,
dir: &Utf8TempDir,
) -> anyhow::Result<CollectionReport> {
let log = &self.log;
info!(&log, "Collecting bundle as local file");
let mut report = CollectionReport::new(self.bundle.id.into());
tokio::fs::write(
dir.path().join("bundle_id.txt"),
self.bundle.id.to_string(),
)
.await?;
if let Ok(all_sleds) = self
.collector
.datastore
.sled_list_all_batched(&self.opctx, SledFilter::InService)
.await
{
report.listed_in_service_sleds = true;
// NOTE: This could be, and probably should be, done concurrently.
for sled in &all_sleds {
info!(&log, "Collecting bundle info from sled"; "sled" => %sled.id());
let sled_path = dir
.path()
.join("rack")
.join(sled.rack_id.to_string())
.join("sled")
.join(sled.id().to_string());
tokio::fs::create_dir_all(&sled_path).await?;
tokio::fs::write(
sled_path.join("sled.txt"),
format!("{sled:?}"),
)
.await?;
if self.request.skip_sled_info {
continue;
}
let Ok(sled_client) = nexus_networking::sled_client(
&self.collector.datastore,
&self.opctx,
sled.id(),
log,
)
.await
else {
tokio::fs::write(
sled_path.join("error.txt"),
"Could not contact sled",
)
.await?;
continue;
};
write_command_result_or_error(
&sled_path,
"dladm",
sled_client.support_dladm_info().await,
)
.await?;
write_command_result_or_error(
&sled_path,
"ipadm",
sled_client.support_ipadm_info().await,
)
.await?;
write_command_result_or_error(
&sled_path,
"zoneadm",
sled_client.support_zoneadm_info().await,
)
.await?;
}
}
Ok(report)
}
}
impl BackgroundTask for SupportBundleCollector {
fn activate<'a>(
&'a mut self,
opctx: &'a OpContext,
) -> BoxFuture<'a, serde_json::Value> {
async move {
if self.disable {
return json!({ "error": "task disabled" });
}
let mut cleanup_report = None;
let mut cleanup_err = None;
let mut collection_report = None;
let mut collection_err = None;
match self.cleanup_destroyed_bundles(&opctx).await {
Ok(report) => cleanup_report = Some(report),
Err(err) => {
cleanup_err =
Some(json!({ "cleanup_error": err.to_string() }))
}
};
let request = BundleRequest::default();
match self.collect_bundle(&opctx, &request).await {
Ok(report) => collection_report = Some(report),
Err(err) => {
collection_err =
Some(json!({ "collect_error": err.to_string() }))
}
};
json!({
"cleanup_report": cleanup_report,
"cleanup_err": cleanup_err,
"collection_report": collection_report,
"collection_err": collection_err,
})
}
.boxed()
}
}
// Takes a directory "dir", and zips the contents into a single zipfile.
fn bundle_to_zipfile(dir: &Utf8TempDir) -> anyhow::Result<std::fs::File> {
let tempfile = tempfile()?;
let mut zip = ZipWriter::new(tempfile);
recursively_add_directory_to_zipfile(&mut zip, dir.path(), dir.path())?;
Ok(zip.finish()?)
}
fn recursively_add_directory_to_zipfile(
zip: &mut ZipWriter<std::fs::File>,
root_path: &Utf8Path,
dir_path: &Utf8Path,
) -> anyhow::Result<()> {
// Readdir might return entries in a non-deterministic order.
// Let's sort it for the zipfile, to be nice.
let mut entries = dir_path
.read_dir_utf8()?
.filter_map(Result::ok)
.collect::<Vec<Utf8DirEntry>>();
entries.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
for entry in &entries {
// Remove the "/tmp/..." prefix from the path when we're storing it in the
// zipfile.
let dst = entry.path().strip_prefix(root_path)?;
let file_type = entry.file_type()?;
if file_type.is_file() {
let opts = FullFileOptions::default();
let src = entry.path();
zip.start_file_from_path(dst, opts)?;
let mut reader = BufReader::new(std::fs::File::open(&src)?);
loop {
let buf = reader.fill_buf()?;
let len = buf.len();
if len == 0 {
break;
}
zip.write_all(&buf)?;
reader.consume(len);
}
}
if file_type.is_dir() {
let opts = FullFileOptions::default();
zip.add_directory_from_path(dst, opts)?;
recursively_add_directory_to_zipfile(zip, root_path, entry.path())?;
}
}
Ok(())
}
async fn sha2_hash(file: &mut tokio::fs::File) -> anyhow::Result<ArtifactHash> {
let mut buf = vec![0u8; 65536];
let mut ctx = Sha256::new();
loop {
let n = file.read(&mut buf).await?;
if n == 0 {
break;
}
ctx.write_all(&buf[0..n])?;
}
let digest = ctx.finalize();
Ok(ArtifactHash(digest.as_slice().try_into()?))
}
// Identifies what we could or could not store within this support bundle.
//
// This struct will get emitted as part of the background task infrastructure.
#[derive(Debug, Serialize, PartialEq, Eq)]
struct CollectionReport {
bundle: SupportBundleUuid,
// True iff we could list in-service sleds
listed_in_service_sleds: bool,
// True iff the bundle was successfully made 'active' in the database.
activated_in_db_ok: bool,
}
impl CollectionReport {
fn new(bundle: SupportBundleUuid) -> Self {
Self {
bundle,
listed_in_service_sleds: false,
activated_in_db_ok: false,
}
}
}
async fn write_command_result_or_error(
path: &Utf8Path,
command: &str,
result: Result<
sled_agent_client::ResponseValue<sled_agent_client::ByteStream>,
sled_agent_client::Error<sled_agent_client::types::Error>,
>,
) -> anyhow::Result<()> {
match result {
Ok(result) => {
let output = result
.into_inner_stream()
.try_collect::<bytes::BytesMut>()
.await?;
tokio::fs::write(path.join(format!("{command}.txt")), output)
.await?;
}
Err(err) => {
tokio::fs::write(
path.join(format!("{command}_err.txt")),
err.to_string(),
)
.await?;
}
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use nexus_db_model::Dataset;
use nexus_db_model::PhysicalDisk;
use nexus_db_model::PhysicalDiskKind;
use nexus_db_model::Zpool;
use nexus_test_utils::SLED_AGENT_UUID;
use nexus_test_utils_macros::nexus_test;
use omicron_common::api::external::Generation;
use omicron_common::api::internal::shared::DatasetKind;
use omicron_common::disk::DatasetConfig;
use omicron_common::disk::DatasetName;
use omicron_common::disk::DatasetsConfig;
use omicron_common::disk::SharedDatasetConfig;
use omicron_common::zpool_name::ZpoolName;
use omicron_uuid_kinds::{DatasetUuid, PhysicalDiskUuid, SledUuid};
use uuid::Uuid;
type ControlPlaneTestContext =
nexus_test_utils::ControlPlaneTestContext<crate::Server>;
// Ensure that we can convert a temporary directory into a zipfile
#[test]
fn test_zipfile_creation() {
let dir = tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("dir-a")).unwrap();
std::fs::create_dir_all(dir.path().join("dir-b")).unwrap();
std::fs::write(dir.path().join("dir-a").join("file-a"), "some data")
.unwrap();
std::fs::write(dir.path().join("file-b"), "more data").unwrap();
let zipfile = bundle_to_zipfile(&dir)
.expect("Should have been able to bundle zipfile");
let archive = zip::read::ZipArchive::new(zipfile).unwrap();
// We expect the order to be deterministically alphabetical
let mut names = archive.file_names();
assert_eq!(names.next(), Some("dir-a/"));
assert_eq!(names.next(), Some("dir-a/file-a"));
assert_eq!(names.next(), Some("dir-b/"));
assert_eq!(names.next(), Some("file-b"));
assert_eq!(names.next(), None);
}
// If we have not populated any bundles needing cleanup, the cleanup
// process should succeed with an empty cleanup report.
#[nexus_test(server = crate::Server)]
async fn test_cleanup_noop(cptestctx: &ControlPlaneTestContext) {
let nexus = &cptestctx.server.server_context().nexus;
let datastore = nexus.datastore();
let opctx = OpContext::for_tests(
cptestctx.logctx.log.clone(),
datastore.clone(),
);
let collector =
SupportBundleCollector::new(datastore.clone(), false, nexus.id());
let report = collector
.cleanup_destroyed_bundles(&opctx)
.await
.expect("Cleanup should succeed with no work to do");
assert_eq!(report, CleanupReport::default());
}
// If there are no bundles in need of collection, the collection task should
// run without error, but return nothing.
#[nexus_test(server = crate::Server)]
async fn test_collect_noop(cptestctx: &ControlPlaneTestContext) {
let nexus = &cptestctx.server.server_context().nexus;
let datastore = nexus.datastore();
let opctx = OpContext::for_tests(
cptestctx.logctx.log.clone(),
datastore.clone(),
);
let collector =
SupportBundleCollector::new(datastore.clone(), false, nexus.id());
let request = BundleRequest::default();
let report = collector
.collect_bundle(&opctx, &request)
.await
.expect("Collection should succeed with no bundles");
assert!(report.is_none());
}
async fn add_zpool_and_debug_dataset(
datastore: &DataStore,
opctx: &OpContext,
id: PhysicalDiskUuid,
sled_id: SledUuid,
) -> (ZpoolUuid, DatasetUuid) {
let zpool = datastore
.zpool_insert(
opctx,
Zpool::new(Uuid::new_v4(), sled_id.into_untyped_uuid(), id),
)
.await
.unwrap();
let dataset = datastore
.dataset_upsert(Dataset::new(
DatasetUuid::new_v4(),
zpool.id(),
None,
DatasetKind::Debug,
))
.await
.unwrap();
(ZpoolUuid::from_untyped_uuid(zpool.id()), dataset.id())
}
async fn make_disk_in_db(
datastore: &DataStore,
opctx: &OpContext,
i: usize,
sled_id: SledUuid,
) -> PhysicalDiskUuid {
let id = PhysicalDiskUuid::new_v4();
let physical_disk = PhysicalDisk::new(
id,
"v".into(),
format!("s-{i})"),
"m".into(),
PhysicalDiskKind::U2,
sled_id.into_untyped_uuid(),
);
datastore
.physical_disk_insert(&opctx, physical_disk.clone())
.await
.unwrap();
id
}
struct TestDataset {
zpool_id: ZpoolUuid,
dataset_id: DatasetUuid,
}
impl TestDataset {
// For the purposes of this test, we're going to provision support
// bundles to a single sled.
async fn setup(
cptestctx: &ControlPlaneTestContext,
datastore: &Arc<DataStore>,
opctx: &OpContext,
count: usize,
) -> Vec<Self> {
let sled_id = SledUuid::from_untyped_uuid(
SLED_AGENT_UUID.parse::<Uuid>().unwrap(),
);
let mut disks = vec![];
for i in 0..count {
// Create the (disk, zpool, dataset) tuple in Nexus
let disk_id =
make_disk_in_db(datastore, opctx, i, sled_id).await;
let (zpool_id, dataset_id) = add_zpool_and_debug_dataset(
&datastore, &opctx, disk_id, sled_id,
)
.await;
// Tell the simulated sled agent to create this storage.
//
// (We could do this via HTTP request, but it's in the test process,
// so we can just directly call the method on the sled agent)
cptestctx.sled_agent.sled_agent.create_zpool(
zpool_id,
disk_id,
1 << 40,
);
disks.push(Self { zpool_id, dataset_id })
}
// Create a configuration for the sled agent consisting of all these
// debug datasets.
let datasets = disks
.iter()
.map(|TestDataset { zpool_id, dataset_id }| {
(
*dataset_id,
DatasetConfig {
id: *dataset_id,
name: DatasetName::new(
ZpoolName::new_external(*zpool_id),
DatasetKind::Debug,
),
inner: SharedDatasetConfig::default(),
},
)
})
.collect();
let dataset_config =
DatasetsConfig { generation: Generation::new(), datasets };
let res = cptestctx
.sled_agent
.sled_agent
.datasets_ensure(dataset_config)
.unwrap();
assert!(!res.has_error());
disks
}
}
#[nexus_test(server = crate::Server)]