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