]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/proxmox-backup-manager.rs
move remote config into pbs-config workspace
[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
13 use proxmox_backup::config;
14 use proxmox_backup::api2::{self, types::* };
15 use proxmox_backup::server::wait_for_local_worker;
16
17 mod proxmox_backup_manager;
18 use proxmox_backup_manager::*;
19
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.
34 async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
35
36 let output_format = get_output_format(&param);
37
38 let store = required_string_param(&param, "store")?;
39
40 let mut client = connect_to_localhost()?;
41
42 let path = format!("api2/json/admin/datastore/{}/gc", store);
43
44 let result = client.post(&path, None).await?;
45
46 view_task_result(&mut client, result, &output_format).await?;
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.
65 async fn garbage_collection_status(param: Value) -> Result<Value, Error> {
66
67 let output_format = get_output_format(&param);
68
69 let store = required_string_param(&param, "store")?;
70
71 let client = connect_to_localhost()?;
72
73 let path = format!("api2/json/admin/datastore/{}/gc", store);
74
75 let mut result = client.get(&path, None).await?;
76 let mut data = result["data"].take();
77 let return_type = &api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS.returns;
78
79 let options = default_table_format_options();
80
81 format_and_print_result_full(&mut data, return_type, &output_format, &options);
82
83 Ok(Value::Null)
84 }
85
86 fn garbage_collection_commands() -> CommandLineInterface {
87
88 let cmd_def = CliCommandMap::new()
89 .insert("status",
90 CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS)
91 .arg_param(&["store"])
92 .completion_cb("store", config::datastore::complete_datastore_name)
93 )
94 .insert("start",
95 CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION)
96 .arg_param(&["store"])
97 .completion_cb("store", config::datastore::complete_datastore_name)
98 );
99
100 cmd_def.into()
101 }
102
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.
127 async fn task_list(param: Value) -> Result<Value, Error> {
128
129 let output_format = get_output_format(&param);
130
131 let client = connect_to_localhost()?;
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 });
140 let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
141
142 let mut data = result["data"].take();
143 let return_type = &api2::node::tasks::API_METHOD_LIST_TASKS.returns;
144
145 use pbs_tools::format::{render_epoch, render_task_status};
146 let options = default_table_format_options()
147 .column(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch))
148 .column(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch))
149 .column(ColumnConfig::new("upid"))
150 .column(ColumnConfig::new("status").renderer(render_task_status));
151
152 format_and_print_result_full(&mut data, return_type, &output_format, &options);
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.
167 async fn task_log(param: Value) -> Result<Value, Error> {
168
169 let upid = required_string_param(&param, "upid")?;
170
171 let mut client = connect_to_localhost()?;
172
173 display_task_log(&mut client, upid, true).await?;
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.
188 async fn task_stop(param: Value) -> Result<Value, Error> {
189
190 let upid_str = required_string_param(&param, "upid")?;
191
192 let mut client = connect_to_localhost()?;
193
194 let path = format!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str));
195 let _ = client.delete(&path, None).await?;
196
197 Ok(Value::Null)
198 }
199
200 fn 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
216 // fixme: avoid API redefinition
217 #[api(
218 input: {
219 properties: {
220 "local-store": {
221 schema: DATASTORE_SCHEMA,
222 },
223 remote: {
224 schema: REMOTE_ID_SCHEMA,
225 },
226 "remote-store": {
227 schema: DATASTORE_SCHEMA,
228 },
229 "remove-vanished": {
230 schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
231 optional: true,
232 },
233 "output-format": {
234 schema: OUTPUT_FORMAT,
235 optional: true,
236 },
237 }
238 }
239 )]
240 /// Sync datastore from another repository
241 async fn pull_datastore(
242 remote: String,
243 remote_store: String,
244 local_store: String,
245 remove_vanished: Option<bool>,
246 param: Value,
247 ) -> Result<Value, Error> {
248
249 let output_format = get_output_format(&param);
250
251 let mut client = connect_to_localhost()?;
252
253 let mut args = json!({
254 "store": local_store,
255 "remote": remote,
256 "remote-store": remote_store,
257 });
258
259 if let Some(remove_vanished) = remove_vanished {
260 args["remove-vanished"] = Value::from(remove_vanished);
261 }
262
263 let result = client.post("api2/json/pull", Some(args)).await?;
264
265 view_task_result(&mut client, result, &output_format).await?;
266
267 Ok(Value::Null)
268 }
269
270 #[api(
271 input: {
272 properties: {
273 "store": {
274 schema: DATASTORE_SCHEMA,
275 },
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 },
284 "output-format": {
285 schema: OUTPUT_FORMAT,
286 optional: true,
287 },
288 }
289 }
290 )]
291 /// Verify backups
292 async fn verify(
293 store: String,
294 mut param: Value,
295 ) -> Result<Value, Error> {
296
297 let output_format = extract_output_format(&mut param);
298
299 let mut client = connect_to_localhost()?;
300
301 let args = json!(param);
302
303 let path = format!("api2/json/admin/datastore/{}/verify", store);
304
305 let result = client.post(&path, Some(args)).await?;
306
307 view_task_result(&mut client, result, &output_format).await?;
308
309 Ok(Value::Null)
310 }
311
312 #[api()]
313 /// System report
314 async fn report() -> Result<Value, Error> {
315 let report = proxmox_backup::server::generate_report();
316 io::stdout().write_all(report.as_bytes())?;
317 Ok(Value::Null)
318 }
319
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.
337 async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
338 let output_format = get_output_format(&param);
339
340 let packages = crate::api2::node::apt::get_versions()?;
341 let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] });
342
343 let options = default_table_format_options()
344 .disable_sort()
345 .noborder(true) // just not helpful for version info which gets copy pasted often
346 .column(ColumnConfig::new("Package"))
347 .column(ColumnConfig::new("Version"))
348 .column(ColumnConfig::new("ExtraInfo").header("Extra Info"))
349 ;
350 let return_type = &crate::api2::node::apt::API_METHOD_GET_VERSIONS.returns;
351
352 format_and_print_result_full(&mut packages, return_type, &output_format, &options);
353
354 Ok(Value::Null)
355 }
356
357 fn main() {
358
359 proxmox_backup::tools::setup_safe_path_env();
360
361 let cmd_def = CliCommandMap::new()
362 .insert("acl", acl_commands())
363 .insert("datastore", datastore_commands())
364 .insert("disk", disk_commands())
365 .insert("dns", dns_commands())
366 .insert("network", network_commands())
367 .insert("node", node_commands())
368 .insert("user", user_commands())
369 .insert("openid", openid_commands())
370 .insert("remote", remote_commands())
371 .insert("garbage-collection", garbage_collection_commands())
372 .insert("acme", acme_mgmt_cli())
373 .insert("cert", cert_mgmt_cli())
374 .insert("subscription", subscription_commands())
375 .insert("sync-job", sync_job_commands())
376 .insert("verify-job", verify_job_commands())
377 .insert("task", task_mgmt_cli())
378 .insert(
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)
383 .completion_cb("remote", pbs_config::remote::complete_remote_name)
384 .completion_cb("remote-store", complete_remote_datastore_name)
385 )
386 .insert(
387 "verify",
388 CliCommand::new(&API_METHOD_VERIFY)
389 .arg_param(&["store"])
390 .completion_cb("store", config::datastore::complete_datastore_name)
391 )
392 .insert("report",
393 CliCommand::new(&API_METHOD_REPORT)
394 )
395 .insert("versions",
396 CliCommand::new(&API_METHOD_GET_VERSIONS)
397 );
398
399
400
401 let mut rpcenv = CliEnvironment::new();
402 rpcenv.set_auth_id(Some(String::from("root@pam")));
403
404 pbs_runtime::main(run_async_cli_command(cmd_def, rpcenv));
405 }
406
407 // shell completion helper
408 pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
409
410 let mut list = Vec::new();
411
412 let _ = proxmox::try_block!({
413 let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?;
414
415 let data = pbs_runtime::block_on(async move {
416 crate::api2::config::remote::scan_remote_datastores(remote.clone()).await
417 })?;
418
419 for item in data {
420 list.push(item.store);
421 }
422
423 Ok(())
424 }).map_err(|_err: Error| { /* ignore */ });
425
426 list
427 }