]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-manager.rs
move required_X_param to pbs_tools::json
[proxmox-backup.git] / src / bin / proxmox-backup-manager.rs
CommitLineData
331b869d 1use std::collections::HashMap;
941342f7 2use std::io::{self, Write};
ea0b8b6e 3
b29d046e 4use anyhow::{format_err, Error};
9894469e 5use serde_json::{json, Value};
9894469e 6
380bd7df 7use proxmox::api::{api, cli::*, RpcEnvironment};
769f8c99 8
2b7f8dd5 9use pbs_client::{connect_to_localhost, display_task_log, view_task_result};
4805edc4 10use pbs_tools::percent_encoding::percent_encode_component;
3c8c2827 11use pbs_tools::json::required_string_param;
4805edc4 12
a220a456 13use proxmox_backup::config;
9894469e 14use proxmox_backup::api2::{self, types::* };
72fbe9ff 15use proxmox_backup::server::wait_for_local_worker;
769f8c99 16
a220a456
DM
17mod proxmox_backup_manager;
18use proxmox_backup_manager::*;
19
769f8c99
DM
20#[api(
21 input: {
22 properties: {
23 store: {
24 schema: DATASTORE_SCHEMA,
25 },
26 "output-format": {
27 schema: OUTPUT_FORMAT,
28 optional: true,
29 },
30 }
31 }
32)]
33/// Start garbage collection for a specific datastore.
34async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
691c89a0 35
ac3faaf5 36 let output_format = get_output_format(&param);
691c89a0 37
3c8c2827 38 let store = required_string_param(&param, "store")?;
769f8c99 39
6b68e5d5 40 let mut client = connect_to_localhost()?;
769f8c99
DM
41
42 let path = format!("api2/json/admin/datastore/{}/gc", store);
43
44 let result = client.post(&path, None).await?;
45
e68269fc 46 view_task_result(&mut client, result, &output_format).await?;
769f8c99
DM
47
48 Ok(Value::Null)
49}
50
51#[api(
52 input: {
53 properties: {
54 store: {
55 schema: DATASTORE_SCHEMA,
56 },
57 "output-format": {
58 schema: OUTPUT_FORMAT,
59 optional: true,
60 },
61 }
62 }
63)]
64/// Show garbage collection status for a specific datastore.
65async fn garbage_collection_status(param: Value) -> Result<Value, Error> {
66
ac3faaf5 67 let output_format = get_output_format(&param);
769f8c99 68
3c8c2827 69 let store = required_string_param(&param, "store")?;
769f8c99 70
6b68e5d5 71 let client = connect_to_localhost()?;
769f8c99
DM
72
73 let path = format!("api2/json/admin/datastore/{}/gc", store);
74
9894469e
DM
75 let mut result = client.get(&path, None).await?;
76 let mut data = result["data"].take();
b2362a12 77 let return_type = &api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS.returns;
9894469e 78
ac3faaf5 79 let options = default_table_format_options();
9894469e 80
b2362a12 81 format_and_print_result_full(&mut data, return_type, &output_format, &options);
769f8c99
DM
82
83 Ok(Value::Null)
84}
85
86fn garbage_collection_commands() -> CommandLineInterface {
691c89a0
DM
87
88 let cmd_def = CliCommandMap::new()
89 .insert("status",
769f8c99 90 CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS)
49fddd98 91 .arg_param(&["store"])
9ac1045c 92 .completion_cb("store", config::datastore::complete_datastore_name)
48ef3c33 93 )
691c89a0 94 .insert("start",
769f8c99 95 CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
49fddd98 96 .arg_param(&["store"])
9ac1045c 97 .completion_cb("store", config::datastore::complete_datastore_name)
48ef3c33 98 );
691c89a0
DM
99
100 cmd_def.into()
101}
102
47d47121
DM
103#[api(
104 input: {
105 properties: {
106 limit: {
107 description: "The maximal number of tasks to list.",
108 type: Integer,
109 optional: true,
110 minimum: 1,
111 maximum: 1000,
112 default: 50,
113 },
114 "output-format": {
115 schema: OUTPUT_FORMAT,
116 optional: true,
117 },
118 all: {
119 type: Boolean,
120 description: "Also list stopped tasks.",
121 optional: true,
122 }
123 }
124 }
125)]
126/// List running server tasks.
127async fn task_list(param: Value) -> Result<Value, Error> {
128
ac3faaf5 129 let output_format = get_output_format(&param);
47d47121 130
6b68e5d5 131 let client = connect_to_localhost()?;
47d47121
DM
132
133 let limit = param["limit"].as_u64().unwrap_or(50) as usize;
134 let running = !param["all"].as_bool().unwrap_or(false);
135 let args = json!({
136 "running": running,
137 "start": 0,
138 "limit": limit,
139 });
9894469e 140 let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
47d47121 141
9894469e 142 let mut data = result["data"].take();
b2362a12 143 let return_type = &api2::node::tasks::API_METHOD_LIST_TASKS.returns;
47d47121 144
770a36e5 145 use pbs_tools::format::{render_epoch, render_task_status};
ac3faaf5 146 let options = default_table_format_options()
770a36e5
WB
147 .column(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch))
148 .column(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch))
93fbb4ef 149 .column(ColumnConfig::new("upid"))
770a36e5 150 .column(ColumnConfig::new("status").renderer(render_task_status));
9894469e 151
b2362a12 152 format_and_print_result_full(&mut data, return_type, &output_format, &options);
47d47121
DM
153
154 Ok(Value::Null)
155}
156
157#[api(
158 input: {
159 properties: {
160 upid: {
161 schema: UPID_SCHEMA,
162 },
163 }
164 }
165)]
166/// Display the task log.
167async fn task_log(param: Value) -> Result<Value, Error> {
168
3c8c2827 169 let upid = required_string_param(&param, "upid")?;
47d47121 170
e68269fc 171 let mut client = connect_to_localhost()?;
47d47121 172
e68269fc 173 display_task_log(&mut client, upid, true).await?;
47d47121
DM
174
175 Ok(Value::Null)
176}
177
178#[api(
179 input: {
180 properties: {
181 upid: {
182 schema: UPID_SCHEMA,
183 },
184 }
185 }
186)]
187/// Try to stop a specific task.
188async fn task_stop(param: Value) -> Result<Value, Error> {
189
3c8c2827 190 let upid_str = required_string_param(&param, "upid")?;
47d47121 191
6b68e5d5 192 let mut client = connect_to_localhost()?;
47d47121 193
4805edc4 194 let path = format!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str));
47d47121
DM
195 let _ = client.delete(&path, None).await?;
196
197 Ok(Value::Null)
198}
199
200fn task_mgmt_cli() -> CommandLineInterface {
201
202 let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG)
203 .arg_param(&["upid"]);
204
205 let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP)
206 .arg_param(&["upid"]);
207
208 let cmd_def = CliCommandMap::new()
209 .insert("list", CliCommand::new(&API_METHOD_TASK_LIST))
210 .insert("log", task_log_cmd_def)
211 .insert("stop", task_stop_cmd_def);
212
213 cmd_def.into()
214}
215
4b4eba0b 216// fixme: avoid API redefinition
0eb0e024
DM
217#[api(
218 input: {
219 properties: {
eb506c83 220 "local-store": {
0eb0e024
DM
221 schema: DATASTORE_SCHEMA,
222 },
223 remote: {
167971ed 224 schema: REMOTE_ID_SCHEMA,
0eb0e024
DM
225 },
226 "remote-store": {
227 schema: DATASTORE_SCHEMA,
228 },
40dc1031
DC
229 "remove-vanished": {
230 schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
4b4eba0b 231 optional: true,
4b4eba0b 232 },
0eb0e024
DM
233 "output-format": {
234 schema: OUTPUT_FORMAT,
235 optional: true,
236 },
237 }
238 }
239)]
eb506c83
DM
240/// Sync datastore from another repository
241async fn pull_datastore(
0eb0e024
DM
242 remote: String,
243 remote_store: String,
eb506c83 244 local_store: String,
40dc1031 245 remove_vanished: Option<bool>,
ac3faaf5 246 param: Value,
0eb0e024
DM
247) -> Result<Value, Error> {
248
ac3faaf5 249 let output_format = get_output_format(&param);
0eb0e024 250
6b68e5d5 251 let mut client = connect_to_localhost()?;
0eb0e024 252
8c877436 253 let mut args = json!({
eb506c83 254 "store": local_store,
94609e23 255 "remote": remote,
0eb0e024 256 "remote-store": remote_store,
0eb0e024
DM
257 });
258
8c877436
DC
259 if let Some(remove_vanished) = remove_vanished {
260 args["remove-vanished"] = Value::from(remove_vanished);
261 }
262
eb506c83 263 let result = client.post("api2/json/pull", Some(args)).await?;
0eb0e024 264
e68269fc 265 view_task_result(&mut client, result, &output_format).await?;
0eb0e024
DM
266
267 Ok(Value::Null)
268}
269
355c055e
DM
270#[api(
271 input: {
272 properties: {
273 "store": {
274 schema: DATASTORE_SCHEMA,
275 },
60abf03f
HL
276 "ignore-verified": {
277 schema: IGNORE_VERIFIED_BACKUPS_SCHEMA,
278 optional: true,
279 },
280 "outdated-after": {
281 schema: VERIFICATION_OUTDATED_AFTER_SCHEMA,
282 optional: true,
283 },
355c055e
DM
284 "output-format": {
285 schema: OUTPUT_FORMAT,
286 optional: true,
287 },
288 }
289 }
290)]
291/// Verify backups
292async fn verify(
293 store: String,
60abf03f 294 mut param: Value,
355c055e
DM
295) -> Result<Value, Error> {
296
60abf03f 297 let output_format = extract_output_format(&mut param);
355c055e 298
6b68e5d5 299 let mut client = connect_to_localhost()?;
355c055e 300
60abf03f 301 let args = json!(param);
355c055e
DM
302
303 let path = format!("api2/json/admin/datastore/{}/verify", store);
304
305 let result = client.post(&path, Some(args)).await?;
306
e68269fc 307 view_task_result(&mut client, result, &output_format).await?;
355c055e
DM
308
309 Ok(Value::Null)
310}
311
9a556c8a
HL
312#[api()]
313/// System report
314async fn report() -> Result<Value, Error> {
941342f7
TL
315 let report = proxmox_backup::server::generate_report();
316 io::stdout().write_all(report.as_bytes())?;
9a556c8a
HL
317 Ok(Value::Null)
318}
319
c100fe91
ML
320#[api(
321 input: {
322 properties: {
323 verbose: {
324 type: Boolean,
325 optional: true,
326 default: false,
327 description: "Output verbose package information. It is ignored if output-format is specified.",
328 },
329 "output-format": {
330 schema: OUTPUT_FORMAT,
331 optional: true,
332 }
333 }
334 }
335)]
336/// List package versions for important Proxmox Backup Server packages.
337async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
294466ee 338 let output_format = get_output_format(&param);
c100fe91 339
5e293f13 340 let packages = crate::api2::node::apt::get_versions()?;
fc5a0120 341 let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] });
c100fe91 342
294466ee
TL
343 let options = default_table_format_options()
344 .disable_sort()
d1d74c43 345 .noborder(true) // just not helpful for version info which gets copy pasted often
294466ee
TL
346 .column(ColumnConfig::new("Package"))
347 .column(ColumnConfig::new("Version"))
348 .column(ColumnConfig::new("ExtraInfo").header("Extra Info"))
349 ;
b2362a12 350 let return_type = &crate::api2::node::apt::API_METHOD_GET_VERSIONS.returns;
294466ee 351
b2362a12 352 format_and_print_result_full(&mut packages, return_type, &output_format, &options);
c100fe91
ML
353
354 Ok(Value::Null)
355}
356
211fabd7
DM
357fn main() {
358
ac7513e3
DM
359 proxmox_backup::tools::setup_safe_path_env();
360
6460764d 361 let cmd_def = CliCommandMap::new()
ed3e60ae 362 .insert("acl", acl_commands())
48ef3c33 363 .insert("datastore", datastore_commands())
8e40aa63 364 .insert("disk", disk_commands())
14627d67 365 .insert("dns", dns_commands())
ca0e5347 366 .insert("network", network_commands())
72e311c6 367 .insert("node", node_commands())
579728c6 368 .insert("user", user_commands())
0decd11e 369 .insert("openid", openid_commands())
f357390c 370 .insert("remote", remote_commands())
47d47121 371 .insert("garbage-collection", garbage_collection_commands())
72bd8293 372 .insert("acme", acme_mgmt_cli())
550e0d88 373 .insert("cert", cert_mgmt_cli())
2762481c 374 .insert("subscription", subscription_commands())
a3016d65 375 .insert("sync-job", sync_job_commands())
4a874665 376 .insert("verify-job", verify_job_commands())
0eb0e024
DM
377 .insert("task", task_mgmt_cli())
378 .insert(
eb506c83
DM
379 "pull",
380 CliCommand::new(&API_METHOD_PULL_DATASTORE)
381 .arg_param(&["remote", "remote-store", "local-store"])
382 .completion_cb("local-store", config::datastore::complete_datastore_name)
f357390c 383 .completion_cb("remote", config::remote::complete_remote_name)
331b869d 384 .completion_cb("remote-store", complete_remote_datastore_name)
355c055e
DM
385 )
386 .insert(
387 "verify",
388 CliCommand::new(&API_METHOD_VERIFY)
389 .arg_param(&["store"])
390 .completion_cb("store", config::datastore::complete_datastore_name)
9a556c8a
HL
391 )
392 .insert("report",
393 CliCommand::new(&API_METHOD_REPORT)
c100fe91
ML
394 )
395 .insert("versions",
396 CliCommand::new(&API_METHOD_GET_VERSIONS)
0eb0e024 397 );
34d3ba52 398
355c055e
DM
399
400
7b22acd0 401 let mut rpcenv = CliEnvironment::new();
e6dc35ac 402 rpcenv.set_auth_id(Some(String::from("root@pam")));
525008f7 403
d420962f 404 pbs_runtime::main(run_async_cli_command(cmd_def, rpcenv));
ea0b8b6e 405}
331b869d
DM
406
407// shell completion helper
408pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
409
410 let mut list = Vec::new();
411
9ea4bce4 412 let _ = proxmox::try_block!({
331b869d 413 let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?;
331b869d 414
d420962f 415 let data = pbs_runtime::block_on(async move {
e0100d61
FG
416 crate::api2::config::remote::scan_remote_datastores(remote.clone()).await
417 })?;
331b869d 418
e0100d61
FG
419 for item in data {
420 list.push(item.store);
331b869d
DM
421 }
422
423 Ok(())
424 }).map_err(|_err: Error| { /* ignore */ });
425
426 list
427}