summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 1969398de90080b1859c28dda819fca27496a67f (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
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
use std::fs;
use std::process::Command;

use clap::Parser;
mod errors;
use errors::CliError;
use reqwest::header;
use reqwest::{blocking::Client, StatusCode};
use serde::{Deserialize, Serialize};
use solarlib::planet::{CpuCount, Memory, Planet};
use solarlib::star::NewPlanet;
use tabular::{row, Table};

/// Manage solard and homeworld instances
#[derive(Parser)]
#[clap(author, version, about)]
struct Args {
    /// The solard server to connect to
    server: String,
    /// The action to be taken
    #[clap(subcommand)]
    action: Action,
}

#[derive(Serialize, Deserialize)]
struct ServerConfig {
    pub token: String,
}

#[derive(clap::Subcommand)]
enum Action {
    /// List planets on the server
    List,
    /// Shuts down a virtual machine
    Stop {
        /// The UUID of the machine to stop
        uuid: String,

        #[clap(long, short)]
        force: bool,
    },
    Start {
        /// The UUID of the machine to start
        uuid: String,
    },
    Pause {
        /// The UUID of the machine to pause
        uuid: String,
    },
    Reboot {
        uuid: String,

        #[clap(long, short)]
        force: bool,
    },
    View {
        uuid: String,
    },

    Create {
        max_mem: u64,

        max_cpus: u64,

        disk_size_mb: u64,

        name: String,

        /// The Sha256 hash of the ship
        ship: String,
    },

    /// Goes through the first-time authentication process to create a token that expires after one
    /// year, storing it in the process.
    Login {
        key: String,
    },
}

fn main() {
    color_eyre::install().unwrap();
    let args = Args::parse();
    let xdg_dirs = xdg::BaseDirectories::with_profile("solarctl", args.server.clone()).unwrap();

    let mut headers = header::HeaderMap::new();

    if let Some(p) = xdg_dirs.find_config_file("config.json") {
        let c = fs::read_to_string(p).unwrap();
        if let Ok(cfg) = serde_json::from_str::<ServerConfig>(&c) {
            headers.insert(
                header::AUTHORIZATION,
                header::HeaderValue::from_str(&cfg.token).unwrap(),
            );
        }
    } else {
        println!("No config file found! You will need to authenticate with the `login` subcommand");
    }

    let client = reqwest::blocking::ClientBuilder::new()
        .default_headers(headers)
        .build()
        .unwrap();

    let root = args.server.clone();

    match args.action {
        Action::List => {
            list(args, client).unwrap();
        }
        Action::Stop { uuid, force } => {
            stop(root, client, uuid, force).unwrap();
        }
        Action::Start { uuid } => {
            start(root, client, uuid).unwrap();
        }
        Action::Pause { uuid } => {
            pause(root, client, uuid).unwrap();
        }
        Action::Reboot { uuid, force } => {
            reboot(root, client, uuid, force).unwrap();
        }
        Action::View { uuid } => {
            view(root, uuid).unwrap();
        }
        Action::Create {
            max_mem,
            max_cpus,
            disk_size_mb,
            name,
            ship,
        } => {
            create(root, client, max_mem, max_cpus, disk_size_mb, name, ship).unwrap();
        }
        Action::Login { key } => {
            login(root, client, key);
        }
    };
}

fn list(a: Args, c: Client) -> Result<(), CliError> {
    let res: Vec<Planet> = c
        .get(format!("http://{}/planets/list", a.server))
        .send()?
        .json()?;

    let mut table = Table::new("{:<} | {:<} | {:<} | {:<}");

    table.add_row(row!("name", "uuid", "status", "running"));
    table.add_row(row!("----", "----", "------", "-------"));

    for p in res {
        table.add_row(row!(
            &p.name,
            &p.uuid,
            &p.status,
            if p.orbiting { "yes" } else { "no" },
        ));
    }

    println!("{}", table);

    Ok(())
}

fn stop(server: String, c: Client, u: String, f: bool) -> Result<(), CliError> {
    let mut url = format!("http://{}/planets/{}/shutdown", server, u);
    if f {
        url.push_str("/hard");
    }
    let res = c.post(url).send()?;

    match res.status() {
        StatusCode::OK => {
            println!("Stopped.");
        }
        _ => {
            return Err(CliError::Cli(format!("Could not stop VM: {}", res.text()?)));
        }
    };

    Ok(())
}

fn start(server: String, c: Client, u: String) -> Result<(), CliError> {
    let res = c
        .post(format!("http://{}/planets/{}/start", server, u))
        .send()?;

    match res.status() {
        StatusCode::OK => {
            println!("Started.");
        }
        _ => {
            return Err(CliError::Cli(format!(
                "Could not start VM: {}",
                res.text()?
            )));
        }
    };

    Ok(())
}

fn pause(server: String, c: Client, u: String) -> Result<(), CliError> {
    let res = c
        .post(format!("http://{}/planets/{}/pause", server, u))
        .send()?;

    match res.status() {
        StatusCode::OK => {
            println!("Paused.");
        }
        _ => {
            return Err(CliError::Cli(format!(
                "Could not pause VM: {}",
                res.text()?
            )));
        }
    };

    Ok(())
}

fn reboot(server: String, c: Client, u: String, f: bool) -> Result<(), CliError> {
    let mut url = format!("http://{}/planets/{}/reboot", server, u);
    if f {
        url.push_str("/hard");
    }
    let res = c.post(url).send()?;

    match res.status() {
        StatusCode::OK => {
            println!("Rebooted.");
        }
        _ => {
            return Err(CliError::Cli(format!(
                "Could not reboot VM: {}",
                res.text()?
            )));
        }
    };

    Ok(())
}

fn view(server: String, u: String) -> Result<(), CliError> {
    let host = server.split(':').next().unwrap();
    let qemu_url = format!("qemu+ssh://{}/system", host);

    if let Err(e) = Command::new("virt-viewer")
        .arg("-c")
        .arg(qemu_url)
        .arg(u)
        .output()
    {
        println!("Could not run virt-viewer: {}", e);
    }

    Ok(())
}

fn create(
    s: String,
    c: Client,
    mem: u64,
    cpus: u64,
    disk_size: u64,
    name: String,
    ship: String,
) -> Result<(), CliError> {
    let url = format!("http://{}/planets/new", s);

    let new_p: NewPlanet = NewPlanet {
        name,
        ship,
        disk_size_mb: disk_size,
        max_mem: Memory(mem),
        max_cpus: CpuCount(cpus),
    };

    println!("Creating new planet...");

    let res = c.post(url).json(&new_p).send()?;

    match res.status() {
        StatusCode::OK => {
            let js: Planet = res.json()?;

            println!("Created. UUID: {}", js.uuid);
        }
        _ => {
            return Err(CliError::Cli(format!(
                "Could not create VM: {}",
                res.text()?
            )));
        }
    };

    Ok(())
}

fn login(s: String, c: Client, k: String) -> Result<(), CliError> {
    let url = format!("http://{}/auth/begin?key={}", s, k);

    println!("Obtaining token...");
    let res = c.post(url).send()?;

    match res.status() {
        StatusCode::OK => {
            let token = res.text()?;
            let xdg_dirs = xdg::BaseDirectories::with_profile("solarctl", s).unwrap();

            if let Some(p) = xdg_dirs.find_config_file("config.json") {
                let cfg = ServerConfig { token };

                fs::write(p, serde_json::to_string_pretty(&cfg).unwrap()).unwrap();
            } else {
                let p = xdg_dirs.place_config_file("config.json").unwrap();
                let cfg = ServerConfig { token };

                fs::write(p, serde_json::to_string_pretty(&cfg).unwrap()).unwrap();
            }
        }
        _ => {
            return Err(CliError::Cli(format!(
                "Could not authenticate: {}",
                res.text()?
            )));
        }
    };

    Ok(())
}