om-snapper 0.6.5-alpha

A very basic commandline tool to download AWS EC2/EBS snapshots.
Documentation
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
use anyhow::Error;
use bytesize::ByteSize;
use indicatif::MultiProgress;
use indicatif::ProgressBar;
use indicatif::ProgressStyle;
use memmapix::MmapMut;
use std::collections::VecDeque;
use std::path::PathBuf;

use std::fs::OpenOptions;

use std::fs::File;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::path::Path;

#[derive(Debug, Default)]
enum ChunkState {
    #[default]
    Todo,
    InProgress,
    Failed,
    Invalid,
    Done,
}

impl From<u8> for ChunkState {
    fn from(u: u8) -> Self {
        match u {
            0b0000_0000 => ChunkState::Todo,
            0b0100_0000 => ChunkState::InProgress,
            0b1000_0000 => ChunkState::Failed,
            0b1111_1111 => ChunkState::Done,
            //            0b100 => ChunkState::Done,     // :HACK:
            0b100 => ChunkState::Todo, // :HACK:
            //            0b1 => ChunkState::InProgress, // :HACK:
            0b1010_1010 => ChunkState::Invalid,
            _ => ChunkState::Invalid,
        }
    }
}

impl From<ChunkState> for u8 {
    fn from(cs: ChunkState) -> Self {
        match cs {
            ChunkState::Todo => 0b0000_0000,
            ChunkState::InProgress => 0b0100_0000,
            ChunkState::Failed => 0b1000_0000,
            ChunkState::Invalid => 0b1010_1010, // Note: Invalid should never be written
            ChunkState::Done => 0b1111_1111,
        }
    }
}

#[derive(Debug)]
struct ChunkMap {
    number_of_chunks: usize,
    mmap: MmapMut,
}

impl ChunkMap {
    pub fn create(name: &Path, number_of_chunks: usize) -> anyhow::Result<Self> {
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .open(&name)?;

        file.set_len(number_of_chunks as u64)?;

        let mmap = unsafe { MmapMut::map_mut(&file)? };
        let s = Self {
            number_of_chunks,
            mmap,
        };

        Ok(s)
    }

    pub fn open(name: &Path) -> anyhow::Result<Self> {
        let file = OpenOptions::new().read(true).write(true).open(&name)?;

        let mmap = unsafe { MmapMut::map_mut(&file)? };
        let number_of_chunks = mmap.len();
        let s = Self {
            number_of_chunks,
            mmap,
        };

        Ok(s)
    }

    pub fn for_each_todo<F>(&self, mut f: F) -> anyhow::Result<()>
    where
        F: FnMut(usize, ChunkState) -> anyhow::Result<()>,
    {
        for (i, c) in self.mmap.iter().enumerate() {
            let cs = ChunkState::from(*c);
            match cs {
                ChunkState::Todo => f(i, cs)?,
                _ => {}
            }
        }
        Ok(())
    }

    pub fn for_each_inprogress<F>(&self, mut f: F) -> anyhow::Result<()>
    where
        F: FnMut(usize, ChunkState) -> anyhow::Result<()>,
    {
        for (i, c) in self.mmap.iter().enumerate() {
            let cs = ChunkState::from(*c);
            // eprintln!("{} -> {:?} ({:#b})", i, cs, c);
            match cs {
                ChunkState::InProgress => f(i, cs)?,
                _ => {}
            }
        }
        Ok(())
    }

    pub fn set_chunk_state(&mut self, i: usize, s: ChunkState) -> anyhow::Result<()> {
        if i >= self.number_of_chunks {
            anyhow::bail!("Out of bounds")
        }

        self.mmap[i] = u8::from(s); // as u8;
        Ok(())
    }

    pub fn chunk_count(&self) -> usize {
        self.mmap.len()
    }

    pub fn get_chunk_state(&self, i: usize) -> Option<ChunkState> {
        if i >= self.mmap.len() {
            return None;
        }

        let c = self.mmap[i];
        let cs = ChunkState::from(c);
        Some(cs)
    }
}

#[derive(Debug)]
pub struct Snapshot {
    id: String,
    progress: Option<MultiProgress>,
    r#continue: bool,

    image_file: PathBuf,
    map_file: PathBuf,
}

const BLOCKS_PER_CHUNK: usize = 100; // >=100 as per AWS API
const BLOCK_SIZE: usize = 524288; // 512KiB
const CHUNK_SIZE: usize = BLOCK_SIZE * BLOCKS_PER_CHUNK;

impl Snapshot {
    pub fn new(id: &str) -> Self {
        Self {
            id: id.to_string(),
            progress: None,
            r#continue: false,

            image_file: Path::new(&format!("./{}.img", &id)).to_path_buf(),
            map_file: Path::new(&format!("./{}.omsmap", &id)).to_path_buf(),
            //                    let filename = format!("./{}.img", &self.id);
        }
    }

    pub fn enable_continue(&mut self) {
        self.r#continue = true;
    }

    pub fn use_progress(&mut self, m: MultiProgress) {
        self.progress = Some(m);
    }

    async fn ec2_client(&self) -> anyhow::Result<aws_sdk_ec2::Client> {
        let config = aws_config::load_from_env().await;
        let ec2_client = aws_sdk_ec2::Client::new(&config);

        Ok::<aws_sdk_ec2::Client, Error>(ec2_client)
    }

    async fn ebs_client(&self) -> anyhow::Result<aws_sdk_ebs::Client> {
        let config = aws_config::load_from_env().await;
        let ebs_client = aws_sdk_ebs::Client::new(&config);

        Ok(ebs_client)
    }

    pub fn image_file(&self) -> &Path {
        &self.image_file
    }

    pub fn map_file(&self) -> &Path {
        &self.map_file
    }

    pub async fn status(&mut self) -> anyhow::Result<bool> {
        let mut all_good = true;

        let image_file = self.image_file();
        println!("Image file {image_file:?}");

        let mut file_size = 0;
        match image_file.try_exists() {
            Ok(true) => {
                println!("\t ... exists.");
                let attr = std::fs::metadata(image_file)?;

                if !attr.is_file() {
                    println!("\t ... is NOT a plain file.");
                    all_good = false;
                } else {
                    let l = attr.len();
                    println!("\t ... contains {l} bytes.");
                    println!("\t ... contains {}.", ByteSize::b(l));

                    file_size = l;
                }
            }
            Ok(false) => {
                println!("\t ... NOT exists.");
                all_good = false;
            }
            Err(o) => {
                anyhow::bail!("Failed checking image file {image_file:?}  -> {o}")
            }
        };

        let map_file = self.map_file();
        println!("Map file {map_file:?}");
        match map_file.try_exists() {
            Ok(true) => {
                println!("\t ... exists.");
                let attr = std::fs::metadata(map_file)?;

                if !attr.is_file() {
                    println!("\t ... is NOT a plain file.");
                    all_good = false;
                } else {
                    let l = attr.len();
                    println!("\t ... contains {l} chunks.");
                    let min_size = l * CHUNK_SIZE as u64;
                    let max_size = min_size + CHUNK_SIZE as u64;
                    println!("\t ... expected file size {} - {}.", min_size, max_size);
                    println!("\t ... expected file size ~{}.", ByteSize::b(min_size));

                    if file_size > max_size {
                        println!(
                            "\t ... Image file size is too big {} > {}",
                            file_size, max_size
                        );
                        all_good = false;
                    } else if file_size < min_size {
                        println!(
                            "\t ... Image file size is too small {} < {}",
                            file_size, min_size
                        );
                        all_good = false;
                    } else {
                        println!(
                            "\t ... Image file size matches: {} < {} < {}",
                            min_size, file_size, max_size
                        );
                    }
                }
            }
            Ok(false) => {
                println!("\t ... NOT exists.");
                all_good = false;
            }
            Err(o) => {
                anyhow::bail!("Failed checking map file {map_file:?}  -> {o}")
            }
        };

        // :TODO: make configurable
        const CODE_TODO: &str = "⬛️";
        const CODE_INPROGRESS: &str = "🚚";
        const CODE_FAILED: &str = "🔺";
        const CODE_INVALID: &str = "❗️";
        const CODE_DONE: &str = "🟩";

        if all_good {
            let map = ChunkMap::open(map_file)?;
            // dbg!(&map);

            for i in 0..map.chunk_count() {
                if let Some(cs) = map.get_chunk_state(i) {
                    /*
                    let code = match cs {
                        ChunkState::Todo => 't',
                        ChunkState::InProgress => 'p',
                        ChunkState::Failed => 'F',
                        ChunkState::Invalid => 'I',
                        ChunkState::Done => 'D',
                    };
                    */
                    let code = match cs {
                        ChunkState::Todo => CODE_TODO,
                        ChunkState::InProgress => CODE_INPROGRESS,
                        ChunkState::Failed => CODE_FAILED,
                        ChunkState::Invalid => CODE_INVALID,
                        ChunkState::Done => CODE_DONE,
                    };
                    print!("{code}");
                } else {
                    print!("!");
                }

                if i % 20 == 19 {
                    println!("");
                }
            }
        }
        println!("");
        println!("{CODE_DONE} = Done");
        println!("{CODE_INPROGRESS} = InProgress");
        println!("{CODE_FAILED} = Failed");
        println!("{CODE_TODO} = todo");
        println!("{CODE_INVALID} = Invalid");
        Ok(all_good)
    }

    pub async fn verify(&mut self) -> anyhow::Result<()> {
        Ok(())
    }

    pub async fn download(&mut self) -> anyhow::Result<()> {
        let ec2_client = self.ec2_client().await?;

        let snapshots = ec2_client.describe_snapshots().snapshot_ids(&self.id);

        let snapshots = snapshots.send().await?;

        let size_in_bytes; // = 0;
        if let Some(snapshots) = snapshots.snapshots {
            if let Some((_description, _state, size)) = snapshots.iter().find_map(|s| {
                // this is a bit silly since we should expect exactly one result
                if s.snapshot_id != Some(self.id.clone()) {
                    None
                } else {
                    //dbg!(s);
                    Some((s.description.clone(), s.state.clone(), s.volume_size))
                }
            }) {
                size.expect("Volume size is needed");

                let size = size.unwrap() as usize;
                size_in_bytes = size * 1_073_741_824;

                tracing::info!("Downloading {}GiB", size)
            } else {
                anyhow::bail!("Snapshot {} not found", &self.id);
            }
        } else {
            anyhow::bail!("Snapshot {} not found", &self.id);
        }

        let filename = format!("./{}.img", &self.id);
        let path = Path::new(&filename);
        let mut f = match path.try_exists() {
            Ok(true) => {
                // check continue
                if !self.r#continue {
                    anyhow::bail!("{filename} exists, but 'continue' was not requested");
                }
                OpenOptions::new().write(true).open(&path)?
            }
            Ok(false) => {
                // create
                File::create(&path)?
            }
            Err(o) => {
                tracing::error!("Failed verifying if {filename} exists -> {o}");
                anyhow::bail!("Failed verifying if {filename} exists -> {o}")
            }
        };

        // preallocate the file on disk
        f.set_len(size_in_bytes as u64)?;

        let chunks = size_in_bytes / CHUNK_SIZE;

        let map_name = self.map_file(); //format!("./{}.omsmap", &self.id);

        let mut chunk_map = ChunkMap::create(map_name, chunks)?;

        tracing::info!("Queing {} chunks", chunks);

        let mut chunk_queue = VecDeque::new();

        chunk_map.for_each_inprogress(|i, _s| {
            dbg!(i);
            chunk_queue.push_back(i);
            Ok(())
        })?;

        chunk_map.for_each_todo(|i, _s| {
            chunk_queue.push_back(i);
            Ok(())
        })?;

        let chunk_progress = if let Some(mp) = &self.progress {
            let cl = chunk_queue.len();

            let sty = ProgressStyle::with_template(
                "[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} [{eta_precise}] {msg}",
            )
            .unwrap()
            .progress_chars("##-");

            let progress = mp.add(ProgressBar::new(cl as u64));
            progress.set_style(sty.clone());

            Some(progress)
        } else {
            None
        };

        while let Some(c) = chunk_queue.pop_front() {
            //tracing::info!("Downloading chunk {} / {}", c, chunks);
            if let Some(pb) = &chunk_progress {
                pb.set_message(format!("Downloading chunk {} / {}", c, chunks));
            }

            {
                // :TODO: extract
                chunk_map.set_chunk_state(c, ChunkState::InProgress)?;

                let block_progress = if let Some(mp) = &self.progress {
                    let sty = ProgressStyle::with_template(
                        "[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} [{eta_precise}] {msg}",
                    )
                    .unwrap()
                    .progress_chars("##-");

                    let progress = mp.add(ProgressBar::new(BLOCKS_PER_CHUNK as u64));
                    progress.set_style(sty.clone());

                    Some(progress)
                } else {
                    None
                };

                let client = self.ebs_client().await?; //aws_sdk_ebs::Client::new(&config);

                let first_block_in_chunk = (c * BLOCKS_PER_CHUNK) as i32;
                let last_block_in_chunk =
                    (first_block_in_chunk + BLOCKS_PER_CHUNK as i32 - 1) as i32;

                let list = client
                    .list_snapshot_blocks()
                    .snapshot_id(&self.id)
                    .starting_block_index(first_block_in_chunk)
                    .max_results(BLOCKS_PER_CHUNK as i32);
                let list = list.send().await?;

                // :TODO: verify block size
                for block in &list.blocks.unwrap() {
                    match (block.block_index, &block.block_token) {
                        (Some(i), Some(t)) => {
                            // Note: snapshots are sparse, so empty blocks will be skipped
                            // resulting in bleeding into the next chunk here
                            // Plan: A different approach on slicing/chunking this might be better

                            if i >= first_block_in_chunk && i <= last_block_in_chunk {
                                // tracing::info!("Downloading block {}", i);
                                if let Some(pb) = &block_progress {
                                    pb.set_message(format!(
                                        "Downloading block {} [{}-{}]",
                                        i, first_block_in_chunk, last_block_in_chunk
                                    ));
                                }

                                let block = client
                                    .get_snapshot_block()
                                    .snapshot_id(&self.id)
                                    .block_index(i)
                                    .block_token(t);

                                let block = block.send().await?;

                                //dbg!(block);
                                let p = i as u64 * BLOCK_SIZE as u64;

                                f.seek(SeekFrom::Start(p as u64))?;
                                //        let r = u8::read_from(block.block_data)?;
                                let data = block.block_data.collect().await?;
                                //io::copy(&mut data, &mut f)?;
                                f.write(&data.into_bytes())?;

                                if let Some(block_progress) = &block_progress {
                                    block_progress.inc(1);
                                }
                            }
                        }
                        _ => {
                            // :TODO:
                        }
                    }
                }
                chunk_map.set_chunk_state(c, ChunkState::Done)?;
                if let Some(chunk_progress) = &chunk_progress {
                    chunk_progress.inc(1);
                }
            }
        }

        Ok(())
    }
}