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
|
use axum::Extension;
use axum_macros::debug_handler;
use axum::{response::IntoResponse, Json, extract::Path};
use solarlib::ship::{DbShip, Ship};
use tokio::process::Command;
use tracing::{error, instrument};
use solarlib::star::{Star, NewPlanet};
use solarlib::planet::Planet;
use solarlib::errors::Error as SolarlibError;
use crate::{errors::*, get_star, State};
use std::sync::Arc;
pub async fn list() -> JsonResult<Json<Vec<Planet>>> {
let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
let mut star = Star::new(con_url)?;
let inhabitants = star.inhabitants()?;
Ok(Json(inhabitants))
}
pub async fn get(Path(uuid): Path<String>) -> JsonResult<Json<Planet>> {
let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
let mut star = Star::new(con_url)?;
if let Ok(p) = star.find_planet(uuid) {
return Ok(Json(p));
} else {
return Err(ServiceError::NotFound);
}
}
pub async fn shutdown(Path(uuid): Path<String>) -> NoneResult {
let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
let mut star = Star::new(con_url)?;
if let Ok(p) = star.find_planet(uuid) {
p.shutdown()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
pub async fn start(Path(uuid): Path<String>) -> NoneResult {
let mut s = get_star()?;
if let Ok(p) = s.find_planet(uuid) {
p.start()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
pub async fn pause(Path(uuid): Path<String>) -> NoneResult {
let mut s = get_star()?;
if let Ok(p) = s.find_planet(uuid) {
p.pause()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
pub async fn reboot(Path(uuid): Path<String>) -> NoneResult {
let mut s = get_star()?;
if let Ok(p) = s.find_planet(uuid) {
p.reboot()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
pub async fn force_reboot(Path(uuid): Path<String>) -> NoneResult {
let mut s = get_star()?;
if let Ok(p) = s.find_planet(uuid) {
p.hard_reboot()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
pub async fn force_shutdown(Path(uuid): Path<String>) -> NoneResult {
let mut s = get_star()?;
if let Ok(p) = s.find_planet(uuid) {
p.hard_shutdown()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
#[debug_handler]
pub async fn new_planet(Json(new_planet): Json<NewPlanet>) -> JsonResult<Json<Planet>> {
let hw_url = std::env::var("HOMEWORLD_URL").unwrap();
let ship_shasum = new_planet.clone().ship;
let res: DbShip = reqwest::get(format!("http://{}/ships/get/{}", hw_url, ship_shasum)).await?.json().await?;
let ship: Ship = res.into();
let mut s = get_star()?;
println!("{:?}", s);
// Try to create right away, if the Ship already exists on the system, it'll go through. If
// not, we can download it by using the shasum
let r = s.planet(new_planet.clone().name, new_planet.max_mem, new_planet.max_cpus, new_planet.disk_size_mb, ship.clone());
match r {
Err(e) => {
match e {
SolarlibError::MissingImage(_) => {
ship.download(&s)?;
return Ok(Json(s.planet(new_planet.name, new_planet.max_mem, new_planet.max_cpus, new_planet.disk_size_mb, ship.clone())?));
},
_ => {
return Err(ServiceError::Solarlib(e));
}
}
},
Ok(r) => {
let tmp = s.inhabitants()?[0].clone();
return Ok(Json(tmp));
}
};
}
pub async fn no_planet(Path(uuid): Path<String>) -> NoneResult {
let mut s = get_star()?;
if let Ok(p) = s.find_planet(uuid) {
p.deathstar()?;
} else {
return Err(ServiceError::NotFound);
}
Ok(())
}
|