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