]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/bin/proxmox-backup-manager.rs
move required_X_param to pbs_tools::json
[proxmox-backup.git] / src / bin / proxmox-backup-manager.rs
index 11ceaa277c0e025df7e4f11c0f5cf2f934b7cf3a..93d6de57b4a8f9daf805a060c5b45f93b6f16ad9 100644 (file)
-use std::path::PathBuf;
 use std::collections::HashMap;
+use std::io::{self, Write};
 
-use failure::*;
+use anyhow::{format_err, Error};
 use serde_json::{json, Value};
-use chrono::{Local, TimeZone};
 
-use proxmox::api::{api, cli::*};
+use proxmox::api::{api, cli::*, RpcEnvironment};
 
-use proxmox_backup::configdir;
-use proxmox_backup::tools;
-use proxmox_backup::config::{self, remote::{self, Remote}};
-use proxmox_backup::api2::{self, types::* };
-use proxmox_backup::client::*;
-use proxmox_backup::tools::ticket::*;
-use proxmox_backup::auth_helpers::*;
-
-fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
-    if value.is_null() { return Ok(String::new()); }
-    let text = match value.as_i64() {
-        Some(epoch) => {
-            Local.timestamp(epoch, 0).format("%c").to_string()
-        }
-        None => {
-            value.to_string()
-        }
-    };
-    Ok(text)
-}
-
-fn render_status(value: &Value, record: &Value) -> Result<String, Error> {
-    if record["endtime"].is_null() {
-        Ok(value.as_str().unwrap_or("running").to_string())
-    } else {
-        Ok(value.as_str().unwrap_or("unknown").to_string())
-    }
-}
-
-async fn view_task_result(
-    client: HttpClient,
-    result: Value,
-    output_format: &str,
-) -> Result<(), Error> {
-    let data = &result["data"];
-    if output_format == "text" {
-        if let Some(upid) = data.as_str() {
-            display_task_log(client, upid, true).await?;
-        }
-    } else {
-        format_and_print_result(&data, &output_format);
-    }
-
-    Ok(())
-}
-
-fn connect() -> Result<HttpClient, Error> {
-
-    let uid = nix::unistd::Uid::current();
-
-    let mut options = HttpClientOptions::new()
-        .prefix(Some("proxmox-backup".to_string()))
-        .verify_cert(false); // not required for connection to localhost
-
-    let client = if uid.is_root()  {
-        let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None)?;
-        options = options.password(Some(ticket));
-        HttpClient::new("localhost", "root@pam", options)?
-    } else {
-        options = options.ticket_cache(true).interactive(true);
-        HttpClient::new("localhost", "root@pam", options)?
-    };
-
-    Ok(client)
-}
-
-#[api(
-    input: {
-        properties: {
-            "output-format": {
-                schema: OUTPUT_FORMAT,
-                optional: true,
-            },
-        }
-    }
-)]
-/// List configured remotes.
-async fn list_remotes(param: Value) -> Result<Value, Error> {
-
-    let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
-
-    let client = connect()?;
-
-    let mut result = client.get("api2/json/config/remote", None).await?;
-
-    let mut data = result["data"].take();
-    let schema = api2::config::remote::API_RETURN_SCHEMA_LIST_REMOTES;
-
-    let mut column_config = Vec::new();
-    column_config.push(ColumnConfig::new("name"));
-    column_config.push(ColumnConfig::new("host"));
-    column_config.push(ColumnConfig::new("userid"));
-    column_config.push(ColumnConfig::new("fingerprint"));
-    column_config.push(ColumnConfig::new("comment"));
-
-    let options = TableFormatOptions::new()
-        .noborder(false)
-        .noheader(false)
-        .column_config(column_config);
-
-
-    format_and_print_result_full(&mut data, schema, &output_format, &options);
-
-    Ok(Value::Null)
-}
-
-fn remote_commands() -> CommandLineInterface {
+use pbs_client::{connect_to_localhost, display_task_log, view_task_result};
+use pbs_tools::percent_encoding::percent_encode_component;
+use pbs_tools::json::required_string_param;
 
-    let cmd_def = CliCommandMap::new()
-        //.insert("list", CliCommand::new(&api2::config::remote::API_METHOD_LIST_REMOTES))
-        .insert("list", CliCommand::new(&&API_METHOD_LIST_REMOTES))
-        .insert(
-            "create",
-            // fixme: howto handle password parameter?
-            CliCommand::new(&api2::config::remote::API_METHOD_CREATE_REMOTE)
-                .arg_param(&["name"])
-        )
-        .insert(
-            "update",
-            CliCommand::new(&api2::config::remote::API_METHOD_UPDATE_REMOTE)
-                .arg_param(&["name"])
-                .completion_cb("name", config::remote::complete_remote_name)
-        )
-        .insert(
-            "remove",
-            CliCommand::new(&api2::config::remote::API_METHOD_DELETE_REMOTE)
-                .arg_param(&["name"])
-                .completion_cb("name", config::remote::complete_remote_name)
-        );
-
-    cmd_def.into()
-}
-
-fn datastore_commands() -> CommandLineInterface {
-
-    let cmd_def = CliCommandMap::new()
-        .insert("list", CliCommand::new(&api2::config::datastore::API_METHOD_LIST_DATASTORES))
-        .insert("create",
-                CliCommand::new(&api2::config::datastore::API_METHOD_CREATE_DATASTORE)
-                .arg_param(&["name", "path"])
-        )
-        .insert("update",
-                CliCommand::new(&api2::config::datastore::API_METHOD_UPDATE_DATASTORE)
-                .arg_param(&["name"])
-                .completion_cb("name", config::datastore::complete_datastore_name)
-        )
-        .insert("remove",
-                CliCommand::new(&api2::config::datastore::API_METHOD_DELETE_DATASTORE)
-                .arg_param(&["name"])
-                .completion_cb("name", config::datastore::complete_datastore_name)
-        );
-
-    cmd_def.into()
-}
+use proxmox_backup::config;
+use proxmox_backup::api2::{self, types::* };
+use proxmox_backup::server::wait_for_local_worker;
 
+mod proxmox_backup_manager;
+use proxmox_backup_manager::*;
 
 #[api(
    input: {
@@ -179,17 +33,17 @@ fn datastore_commands() -> CommandLineInterface {
 /// Start garbage collection for a specific datastore.
 async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
 
-    let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
+    let output_format = get_output_format(&param);
 
-    let store = tools::required_string_param(&param, "store")?;
+    let store = required_string_param(&param, "store")?;
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
     let path = format!("api2/json/admin/datastore/{}/gc", store);
 
     let result = client.post(&path, None).await?;
 
-    view_task_result(client, result, &output_format).await?;
+    view_task_result(&mut client, result, &output_format).await?;
 
     Ok(Value::Null)
 }
@@ -210,23 +64,21 @@ async fn start_garbage_collection(param: Value) -> Result<Value, Error> {
 /// Show garbage collection status for a specific datastore.
 async fn garbage_collection_status(param: Value) -> Result<Value, Error> {
 
-    let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
+    let output_format = get_output_format(&param);
 
-    let store = tools::required_string_param(&param, "store")?;
+    let store = required_string_param(&param, "store")?;
 
-    let client = connect()?;
+    let client = connect_to_localhost()?;
 
     let path = format!("api2/json/admin/datastore/{}/gc", store);
 
     let mut result = client.get(&path, None).await?;
     let mut data = result["data"].take();
-    let schema = api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS;
+    let return_type = &api2::admin::datastore::API_METHOD_GARBAGE_COLLECTION_STATUS.returns;
 
-    let options = TableFormatOptions::new()
-        .noborder(false)
-        .noheader(false);
+    let options = default_table_format_options();
 
-    format_and_print_result_full(&mut data, schema, &output_format, &options);
+    format_and_print_result_full(&mut data, return_type, &output_format, &options);
 
     Ok(Value::Null)
 }
@@ -274,9 +126,9 @@ fn garbage_collection_commands() -> CommandLineInterface {
 /// List running server tasks.
 async fn task_list(param: Value) -> Result<Value, Error> {
 
-    let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
+    let output_format = get_output_format(&param);
 
-    let client = connect()?;
+    let client = connect_to_localhost()?;
 
     let limit = param["limit"].as_u64().unwrap_or(50) as usize;
     let running = !param["all"].as_bool().unwrap_or(false);
@@ -288,20 +140,16 @@ async fn task_list(param: Value) -> Result<Value, Error> {
     let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?;
 
     let mut data = result["data"].take();
-    let schema = api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS;
-
-    let mut column_config = Vec::new();
-    column_config.push(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch));
-    column_config.push(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch));
-    column_config.push(ColumnConfig::new("upid"));
-    column_config.push(ColumnConfig::new("status").renderer(render_status));
+    let return_type = &api2::node::tasks::API_METHOD_LIST_TASKS.returns;
 
-    let options = TableFormatOptions::new()
-        .noborder(false)
-        .noheader(false)
-        .column_config(column_config);
+    use pbs_tools::format::{render_epoch, render_task_status};
+    let options = default_table_format_options()
+        .column(ColumnConfig::new("starttime").right_align(false).renderer(render_epoch))
+        .column(ColumnConfig::new("endtime").right_align(false).renderer(render_epoch))
+        .column(ColumnConfig::new("upid"))
+        .column(ColumnConfig::new("status").renderer(render_task_status));
 
-    format_and_print_result_full(&mut data, schema, &output_format, &options);
+    format_and_print_result_full(&mut data, return_type, &output_format, &options);
 
     Ok(Value::Null)
 }
@@ -318,11 +166,11 @@ async fn task_list(param: Value) -> Result<Value, Error> {
 /// Display the task log.
 async fn task_log(param: Value) -> Result<Value, Error> {
 
-    let upid = tools::required_string_param(&param, "upid")?;
+    let upid = required_string_param(&param, "upid")?;
 
-    let client = connect()?;
+    let mut client = connect_to_localhost()?;
 
-    display_task_log(client, upid, true).await?;
+    display_task_log(&mut client, upid, true).await?;
 
     Ok(Value::Null)
 }
@@ -339,11 +187,11 @@ async fn task_log(param: Value) -> Result<Value, Error> {
 /// Try to stop a specific task.
 async fn task_stop(param: Value) -> Result<Value, Error> {
 
-    let upid_str = tools::required_string_param(&param, "upid")?;
+    let upid_str = required_string_param(&param, "upid")?;
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
-    let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str);
+    let path = format!("api2/json/nodes/localhost/tasks/{}", percent_encode_component(upid_str));
     let _ = client.delete(&path, None).await?;
 
     Ok(Value::Null)
@@ -365,97 +213,6 @@ fn task_mgmt_cli() -> CommandLineInterface {
     cmd_def.into()
 }
 
-fn x509name_to_string(name: &openssl::x509::X509NameRef) -> Result<String, Error> {
-    let mut parts = Vec::new();
-    for entry in name.entries() {
-        parts.push(format!("{} = {}", entry.object().nid().short_name()?, entry.data().as_utf8()?));
-    }
-    Ok(parts.join(", "))
-}
-
-#[api]
-/// Diplay node certificate information.
-fn cert_info() -> Result<(), Error> {
-
-    let cert_path = PathBuf::from(configdir!("/proxy.pem"));
-
-    let cert_pem = proxmox::tools::fs::file_get_contents(&cert_path)?;
-
-    let cert = openssl::x509::X509::from_pem(&cert_pem)?;
-
-    println!("Subject: {}", x509name_to_string(cert.subject_name())?);
-
-    if let Some(san) = cert.subject_alt_names() {
-        for name in san.iter() {
-            if let Some(v) = name.dnsname() {
-                println!("    DNS:{}", v);
-            } else if let Some(v) = name.ipaddress() {
-                println!("    IP:{:?}", v);
-            } else if let Some(v) = name.email() {
-                println!("    EMAIL:{}", v);
-            } else if let Some(v) = name.uri() {
-                println!("    URI:{}", v);
-            }
-        }
-    }
-
-    println!("Issuer: {}", x509name_to_string(cert.issuer_name())?);
-    println!("Validity:");
-    println!("    Not Before: {}", cert.not_before());
-    println!("    Not After : {}", cert.not_after());
-
-    let fp = cert.digest(openssl::hash::MessageDigest::sha256())?;
-    let fp_string = proxmox::tools::digest_to_hex(&fp);
-    let fp_string = fp_string.as_bytes().chunks(2).map(|v| std::str::from_utf8(v).unwrap())
-        .collect::<Vec<&str>>().join(":");
-
-    println!("Fingerprint (sha256): {}", fp_string);
-
-    let pubkey = cert.public_key()?;
-    println!("Public key type: {}", openssl::nid::Nid::from_raw(pubkey.id().as_raw()).long_name()?);
-    println!("Public key bits: {}", pubkey.bits());
-
-    Ok(())
-}
-
-#[api(
-    input: {
-        properties: {
-            force: {
-               description: "Force generation of new SSL certifate.",
-               type:  Boolean,
-               optional:true,
-           },
-        }
-    },
-)]
-/// Update node certificates and generate all needed files/directories.
-fn update_certs(force: Option<bool>) -> Result<(), Error> {
-
-    config::create_configdir()?;
-
-    if let Err(err) = generate_auth_key() {
-        bail!("unable to generate auth key - {}", err);
-    }
-
-    if let Err(err) = generate_csrf_key() {
-        bail!("unable to generate csrf key - {}", err);
-    }
-
-    config::update_self_signed_cert(force.unwrap_or(false))?;
-
-    Ok(())
-}
-
-fn cert_mgmt_cli() -> CommandLineInterface {
-
-    let cmd_def = CliCommandMap::new()
-        .insert("info", CliCommand::new(&API_METHOD_CERT_INFO))
-        .insert("update", CliCommand::new(&API_METHOD_UPDATE_CERTS));
-
-    cmd_def.into()
-}
-
 // fixme: avoid API redefinition
 #[api(
    input: {
@@ -469,11 +226,9 @@ fn cert_mgmt_cli() -> CommandLineInterface {
             "remote-store": {
                 schema: DATASTORE_SCHEMA,
             },
-            delete: {
-                description: "Delete vanished backups. This remove the local copy if the remote backup was deleted.",
-                type: Boolean,
+            "remove-vanished": {
+                schema: REMOVE_VANISHED_BACKUPS_SCHEMA,
                 optional: true,
-                default: true,
             },
             "output-format": {
                 schema: OUTPUT_FORMAT,
@@ -487,13 +242,13 @@ async fn pull_datastore(
     remote: String,
     remote_store: String,
     local_store: String,
-    delete: Option<bool>,
-    output_format: Option<String>,
+    remove_vanished: Option<bool>,
+    param: Value,
 ) -> Result<Value, Error> {
 
-    let output_format = output_format.unwrap_or("text".to_string());
+    let output_format = get_output_format(&param);
 
-    let mut client = connect()?;
+    let mut client = connect_to_localhost()?;
 
     let mut args = json!({
         "store": local_store,
@@ -501,24 +256,124 @@ async fn pull_datastore(
         "remote-store": remote_store,
     });
 
-    if let Some(delete) = delete {
-        args["delete"] = delete.into();
+    if let Some(remove_vanished) = remove_vanished {
+        args["remove-vanished"] = Value::from(remove_vanished);
     }
 
     let result = client.post("api2/json/pull", Some(args)).await?;
 
-    view_task_result(client, result, &output_format).await?;
+    view_task_result(&mut client, result, &output_format).await?;
+
+    Ok(Value::Null)
+}
+
+#[api(
+   input: {
+        properties: {
+            "store": {
+                schema: DATASTORE_SCHEMA,
+            },
+            "ignore-verified": {
+                schema: IGNORE_VERIFIED_BACKUPS_SCHEMA,
+                optional: true,
+            },
+            "outdated-after": {
+                schema: VERIFICATION_OUTDATED_AFTER_SCHEMA,
+                optional: true,
+            },
+            "output-format": {
+                schema: OUTPUT_FORMAT,
+                optional: true,
+            },
+        }
+   }
+)]
+/// Verify backups
+async fn verify(
+    store: String,
+    mut param: Value,
+) -> Result<Value, Error> {
+
+    let output_format = extract_output_format(&mut param);
+
+    let mut client = connect_to_localhost()?;
+
+    let args = json!(param);
+
+    let path = format!("api2/json/admin/datastore/{}/verify", store);
+
+    let result = client.post(&path, Some(args)).await?;
+
+    view_task_result(&mut client, result, &output_format).await?;
+
+    Ok(Value::Null)
+}
+
+#[api()]
+/// System report
+async fn report() -> Result<Value, Error> {
+    let report = proxmox_backup::server::generate_report();
+    io::stdout().write_all(report.as_bytes())?;
+    Ok(Value::Null)
+}
+
+#[api(
+    input: {
+        properties: {
+            verbose: {
+                type: Boolean,
+                optional: true,
+                default: false,
+                description: "Output verbose package information. It is ignored if output-format is specified.",
+            },
+            "output-format": {
+                schema: OUTPUT_FORMAT,
+                optional: true,
+            }
+        }
+    }
+)]
+/// List package versions for important Proxmox Backup Server packages.
+async fn get_versions(verbose: bool, param: Value) -> Result<Value, Error> {
+    let output_format = get_output_format(&param);
+
+    let packages = crate::api2::node::apt::get_versions()?;
+    let mut packages = json!(if verbose { &packages[..] } else { &packages[1..2] });
+
+    let options = default_table_format_options()
+        .disable_sort()
+        .noborder(true) // just not helpful for version info which gets copy pasted often
+        .column(ColumnConfig::new("Package"))
+        .column(ColumnConfig::new("Version"))
+        .column(ColumnConfig::new("ExtraInfo").header("Extra Info"))
+        ;
+    let return_type = &crate::api2::node::apt::API_METHOD_GET_VERSIONS.returns;
+
+    format_and_print_result_full(&mut packages, return_type, &output_format, &options);
 
     Ok(Value::Null)
 }
 
 fn main() {
 
+    proxmox_backup::tools::setup_safe_path_env();
+
     let cmd_def = CliCommandMap::new()
+        .insert("acl", acl_commands())
         .insert("datastore", datastore_commands())
+        .insert("disk", disk_commands())
+        .insert("dns", dns_commands())
+        .insert("network", network_commands())
+        .insert("node", node_commands())
+        .insert("user", user_commands())
+        .insert("openid", openid_commands())
         .insert("remote", remote_commands())
         .insert("garbage-collection", garbage_collection_commands())
+        .insert("acme", acme_mgmt_cli())
         .insert("cert", cert_mgmt_cli())
+        .insert("subscription", subscription_commands())
+        .insert("sync-job", sync_job_commands())
+        .insert("verify-job", verify_job_commands())
         .insert("task", task_mgmt_cli())
         .insert(
             "pull",
@@ -527,9 +382,26 @@ fn main() {
                 .completion_cb("local-store", config::datastore::complete_datastore_name)
                 .completion_cb("remote", config::remote::complete_remote_name)
                 .completion_cb("remote-store", complete_remote_datastore_name)
+        )
+        .insert(
+            "verify",
+            CliCommand::new(&API_METHOD_VERIFY)
+                .arg_param(&["store"])
+                .completion_cb("store", config::datastore::complete_datastore_name)
+        )
+        .insert("report",
+            CliCommand::new(&API_METHOD_REPORT)
+        )
+        .insert("versions",
+            CliCommand::new(&API_METHOD_GET_VERSIONS)
         );
 
-    proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def));
+
+
+    let mut rpcenv = CliEnvironment::new();
+    rpcenv.set_auth_id(Some(String::from("root@pam")));
+
+   pbs_runtime::main(run_async_cli_command(cmd_def, rpcenv));
 }
 
 // shell completion helper
@@ -539,29 +411,13 @@ pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String
 
     let _ = proxmox::try_block!({
         let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?;
-        let (remote_config, _digest) = remote::config()?;
-
-        let remote: Remote = remote_config.lookup("remote", &remote)?;
-
-        let options = HttpClientOptions::new()
-            .password(Some(remote.password.clone()))
-            .fingerprint(remote.fingerprint.clone());
 
-        let client = HttpClient::new(
-            &remote.host,
-            &remote.userid,
-            options,
-        )?;
+        let data = pbs_runtime::block_on(async move {
+            crate::api2::config::remote::scan_remote_datastores(remote.clone()).await
+        })?;
 
-        let mut rt = tokio::runtime::Runtime::new().unwrap();
-        let result = rt.block_on(client.get("api2/json/admin/datastore", None))?;
-
-        if let Some(data) = result["data"].as_array() {
-            for item in data {
-                if let Some(store) = item["store"].as_str() {
-                    list.push(store.to_owned());
-                }
-            }
+        for item in data {
+            list.push(item.store);
         }
 
         Ok(())