aboutsummaryrefslogtreecommitdiff
path: root/src/ship.rs
blob: f38d1f92482b26ad9a9f7e501eb91cb7b468d345 (plain) (blame)
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
/*! A Ship is a star ship, or an installation ISO */

use tokio::process::Command;
use std::process::{ExitStatus, Output};
use std::os::unix::process::ExitStatusExt;
use std::str::FromStr;

use crate::{errors::Error, star::Star};

use serde::{Serialize, Deserialize};

/// Describes a hash of a file
#[derive(Serialize, Deserialize, Debug)]
pub struct Sha256(pub String);

impl FromStr for Sha256 {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let sum = s.clone();

        Ok(Sha256(sum.to_string()))
    }
}

impl ToString for Sha256 {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

/// Describes a starship, or a way to install a distribution
#[derive(Serialize, Deserialize)]
pub struct Ship {
    /// The common name of the distribution (e.g "Arch Linux")
    pub name: String,
    /// The SHA-256 hash of the downloaded file
    pub shasum: Sha256,
    /// Where the ISO can be downloaded from
    pub download_url: String,
    /// The commonly accepted version (e.g "rolling", "21.11", "unstable")
    pub version: String,
}

/// Describes a starship generated from a database
#[derive(Serialize, Deserialize, Debug)]
pub struct DbShip {
    /// The Database ID of the ship
    pub id: i32,

    pub name: String,
    pub shasum: String,
    pub download_url: String,
    pub version: String
}

impl From<DbShip> for Ship {
    fn from(o: DbShip) -> Self {
        Self {
           name: o.name,
           shasum: Sha256(o.shasum),
           download_url: o.download_url,
           version: o.version
        }
    }
}

impl Ship {
    pub fn new(name: String,
               shasum: String,
               download_url: String,
               version: String
               ) -> Self {
        Self {
            name,
            shasum: Sha256(shasum),
            download_url,
            version
        }
    }

    pub async fn download(&self, target: Star) -> Result<(), Error> {

        let mut output: Output;

        if target.remote {
            output = Command::new("ssh")
                .args([
                      "-oStrictHostKeyChecking=accept-new",
                      &target.address.clone(),
                      "wget",
                      "-O",
                      &format!("/var/lib/libvirt/images/{}", self.make_pretty_name().clone()),
                      &self.download_url.clone(),
                ])
                .output()
                .await?;
        } else {
            output = Command::new("wget")
                .args([
                      "-O",
                      &format!("/var/lib/libvirt/images/{}", self.make_pretty_name().clone()),
                      &self.download_url.clone(),
                ])
                .output()
                .await?;
        }

        if output.status != ExitStatus::from_raw(0) {
            Err(Error::RemoteCommand(String::from_utf8(output.stdout).unwrap()))
        } else {
            Ok(())
        }
    }

    pub fn make_pretty_name(&self) -> String {
        let safe_name = self.name.clone().to_lowercase().replace(" ", "-");
        let file_name = format!("{}-{}-{}.van", safe_name, self.version.clone(), self.shasum.0.clone());

        file_name
    }
}