src/api2/node/disks.rs: implement initgpt API

This commit is contained in:
Dietmar Maurer
2020-06-07 10:30:34 +02:00
parent 9069debcd8
commit 707974fdb3
4 changed files with 171 additions and 7 deletions

View File

@ -32,6 +32,24 @@ async fn view_task_result(
Ok(())
}
// Note: local worker already printf logs to stdout, so there is no need
// to fetch/display logs. We just wait for the worker to finish.
pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
let upid: proxmox_backup::server::UPID = upid_str.parse()?;
let sleep_duration = core::time::Duration::new(0, 100_000_000);
loop {
if proxmox_backup::server::worker_is_active_local(&upid) {
tokio::time::delay_for(sleep_duration).await;
} else {
break;
}
}
Ok(())
}
fn connect() -> Result<HttpClient, Error> {
let uid = nix::unistd::Uid::current();

View File

@ -85,6 +85,40 @@ fn smart_attributes(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result
Ok(Value::Null)
}
#[api(
input: {
properties: {
disk: {
schema: BLOCKDEVICE_NAME_SCHEMA,
},
uuid: {
description: "UUID for the GPT table.",
type: String,
optional: true,
max_length: 36,
},
},
},
)]
/// Initialize empty Disk with GPT
async fn initialize_disk(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
param["node"] = "localhost".into();
let info = &api2::node::disks::API_METHOD_INITIALIZE_DISK;
let result = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
crate::wait_for_local_worker(result.as_str().unwrap()).await?;
Ok(Value::Null)
}
pub fn disk_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
@ -93,6 +127,11 @@ pub fn disk_commands() -> CommandLineInterface {
CliCommand::new(&API_METHOD_SMART_ATTRIBUTES)
.arg_param(&["disk"])
.completion_cb("disk", complete_disk_name)
)
.insert("initialize",
CliCommand::new(&API_METHOD_INITIALIZE_DISK)
.arg_param(&["disk"])
.completion_cb("disk", complete_disk_name)
);
cmd_def.into()