From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dietmar@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 5757D60B78
 for <pbs-devel@lists.proxmox.com>; Fri, 14 Aug 2020 07:39:42 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 44A401F351
 for <pbs-devel@lists.proxmox.com>; Fri, 14 Aug 2020 07:39:12 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 3AEE31F341
 for <pbs-devel@lists.proxmox.com>; Fri, 14 Aug 2020 07:39:10 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 057D4445E7
 for <pbs-devel@lists.proxmox.com>; Fri, 14 Aug 2020 07:39:10 +0200 (CEST)
Date: Fri, 14 Aug 2020 07:38:49 +0200 (CEST)
From: Dietmar Maurer <dietmar@proxmox.com>
To: Proxmox Backup Server development discussion <pbs-devel@lists.proxmox.com>, 
 Hannes Laimer <h.laimer@proxmox.com>
Message-ID: <662701269.149.1597383530273@webmail.proxmox.com>
In-Reply-To: <20200813105853.144386-2-h.laimer@proxmox.com>
References: <20200813105853.144386-1-h.laimer@proxmox.com>
 <20200813105853.144386-2-h.laimer@proxmox.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Priority: 3
Importance: Normal
X-Mailer: Open-Xchange Mailer v7.10.3-Rev20
X-Originating-Client: open-xchange-appsuite
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.141 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [directory.rs, systemd.rs, proxmox.com]
Subject: [pbs-devel] applied: [PATCH proxmox-backup v3 1/2]
 api2/node/../disks/directory: added DELETE endpoint for removal of
 mount-units
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 14 Aug 2020 05:39:42 -0000

applied with some cleanup on top - see inline comments

> On 08/13/2020 12:58 PM Hannes Laimer <h.laimer@proxmox.com> wrote:
> 
>  
> Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
> ---
>  src/api2/node/disks/directory.rs | 72 +++++++++++++++++++++++++++++++-
>  src/tools/systemd.rs             | 11 +++++
>  2 files changed, 81 insertions(+), 2 deletions(-)
> 
> diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
> index f05b781f..f8a8d8cb 100644
> --- a/src/api2/node/disks/directory.rs
> +++ b/src/api2/node/disks/directory.rs
> @@ -2,7 +2,7 @@ use anyhow::{bail, Error};
>  use serde_json::json;
>  use ::serde::{Deserialize, Serialize};
>  
> -use proxmox::api::{api, Permission, RpcEnvironment, RpcEnvironmentType};
> +use proxmox::api::{api, Permission, RpcEnvironment, RpcEnvironmentType, HttpError};
>  use proxmox::api::section_config::SectionConfigData;
>  use proxmox::api::router::Router;
>  
> @@ -16,6 +16,8 @@ use crate::tools::systemd::{self, types::*};
>  use crate::server::WorkerTask;
>  
>  use crate::api2::types::*;
> +use proxmox::api::error::StatusCode;
> +use crate::config::datastore::DataStoreConfig;
>  
>  #[api(
>      properties: {
> @@ -175,9 +177,75 @@ pub fn create_datastore_disk(
>      Ok(upid_str)
>  }
>  
> +#[api(
> +    protected: true,
> +    input: {
> +        properties: {
> +            node: {
> +                schema: NODE_SCHEMA,
> +            },
> +            name: {
> +                schema: DATASTORE_SCHEMA,
> +            },
> +        }
> +    },
> +    access: {
> +        permission: &Permission::Privilege(&["system", "disks"], PRIV_SYS_MODIFY, false),
> +    },
> +)]
> +/// Remove a Filesystem mounted under '/mnt/datastore/<name>'.".
> +pub fn delete_datastore_disk(
> +    name: String,
> +    rpcenv: &mut dyn RpcEnvironment,
> +) -> Result<(), Error> {
> +    let path = format!("/mnt/datastore/{}", name);
> +    // path of datastore cannot be changed
> +    let (config, _) = crate::config::datastore::config()?;
> +    let datastores: Vec<DataStoreConfig> = config.convert_to_typed_array("datastore")?;
> +    let conflicting_datastore: Option<DataStoreConfig> = datastores.into_iter()
> +        .filter(|ds| ds.path == path)
> +        .next();
> +
> +    match conflicting_datastore {
> +        Some(conflicting_datastore) =>
> +            Err(Error::from(HttpError::new(StatusCode::CONFLICT,
> +                                           format!("Can't remove '{}' since it's required by datastore '{}'",
> +                                                   conflicting_datastore.path,
> +                                                   conflicting_datastore.name)))),

What do you use HttpError here? Instead, we can simply use bail! as everywhere else.

Also, there is a proxmox::http_bail! macro to simplify above code.

> +        None => {
> +            // disable systemd mount-unit
> +            let mut mount_unit_name = systemd::escape_unit(&path, true);
> +            mount_unit_name.push_str(".mount");
> +            systemd::disable_unit(mount_unit_name.as_str())?;

You ca replace that .as_str() by simply using &mount_unit_name

> +
> +            // delete .mount-file
> +            let mount_unit_path = format!("/etc/systemd/system/{}", mount_unit_name);
> +            let full_path = std::path::Path::new(mount_unit_path.as_str());

same here

> +            log::info!("removing {:?}", full_path);
> +            std::fs::remove_file(&full_path)?;
> +
> +            // try to unmount, if that fails tell the user to reboot or unmount manually
> +            let mut command = std::process::Command::new("umount");
> +            command.arg(path.as_str());

and here

> +            match crate::tools::run_command(command, None) {
> +                Err(_) => bail!(
> +                    "Could not umount '{}' since it is busy. It will stay mounted \
> +                    until the next reboot or until unmounted manually!",
> +                    path
> +                 ),
> +                Ok(_) => Ok(())
> +            }
> +        }
> +    }
> +}
> +
> +const ITEM_ROUTER: Router = Router::new()
> +    .delete(&API_METHOD_DELETE_DATASTORE_DISK);
> +
>  pub const ROUTER: Router = Router::new()
>      .get(&API_METHOD_LIST_DATASTORE_MOUNTS)
> -    .post(&API_METHOD_CREATE_DATASTORE_DISK);
> +    .post(&API_METHOD_CREATE_DATASTORE_DISK)
> +    .match_all("name", &ITEM_ROUTER);
>  
>  
>  fn create_datastore_mount_unit(
> diff --git a/src/tools/systemd.rs b/src/tools/systemd.rs
> index 6dde06a3..9a6439de 100644
> --- a/src/tools/systemd.rs
> +++ b/src/tools/systemd.rs
> @@ -83,6 +83,17 @@ pub fn reload_daemon() -> Result<(), Error> {
>      Ok(())
>  }
>  
> +pub fn disable_unit(unit: &str) -> Result<(), Error> {
> +
> +    let mut command = std::process::Command::new("systemctl");
> +    command.arg("disable");
> +    command.arg(unit);
> +
> +    crate::tools::run_command(command, None)?;
> +
> +    Ok(())
> +}
> +
>  pub fn enable_unit(unit: &str) -> Result<(), Error> {
>  
>      let mut command = std::process::Command::new("systemctl");
> -- 
> 2.20.1
> 
> 
> 
> _______________________________________________
> pbs-devel mailing list
> pbs-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel