forked from openebs/mayastor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): controller CLI command added
Introduced a new command for listing nvme controllers. Implements CAS-918
- Loading branch information
Showing
9 changed files
with
280 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! | ||
//! methods to interact with NVMe controllers | ||
use super::context::Context; | ||
use crate::{context::OutputFormat, GrpcStatus}; | ||
use ::rpc::mayastor as rpc; | ||
use clap::{App, AppSettings, ArgMatches, SubCommand}; | ||
use colored_json::ToColoredJson; | ||
use snafu::ResultExt; | ||
use tonic::Status; | ||
|
||
pub fn subcommands<'a, 'b>() -> App<'a, 'b> { | ||
let list = | ||
SubCommand::with_name("list").about("List existing NVMe controllers"); | ||
|
||
SubCommand::with_name("controller") | ||
.settings(&[ | ||
AppSettings::SubcommandRequiredElseHelp, | ||
AppSettings::ColoredHelp, | ||
AppSettings::ColorAlways, | ||
]) | ||
.about("NVMe controllers") | ||
.subcommand(list) | ||
} | ||
|
||
pub async fn handler( | ||
ctx: Context, | ||
matches: &ArgMatches<'_>, | ||
) -> crate::Result<()> { | ||
match matches.subcommand() { | ||
("list", Some(args)) => list_controllers(ctx, args).await, | ||
(cmd, _) => { | ||
Err(Status::not_found(format!("command {} does not exist", cmd))) | ||
.context(GrpcStatus) | ||
} | ||
} | ||
} | ||
|
||
fn controller_state_to_str(idx: i32) -> String { | ||
match rpc::NvmeControllerState::from_i32(idx).unwrap() { | ||
rpc::NvmeControllerState::New => "new", | ||
rpc::NvmeControllerState::Initializing => "init", | ||
rpc::NvmeControllerState::Running => "running", | ||
rpc::NvmeControllerState::Faulted => "faulted", | ||
rpc::NvmeControllerState::Unconfiguring => "unconfiguring", | ||
rpc::NvmeControllerState::Unconfigured => "unconfigured", | ||
} | ||
.to_string() | ||
} | ||
|
||
async fn list_controllers( | ||
mut ctx: Context, | ||
_matches: &ArgMatches<'_>, | ||
) -> crate::Result<()> { | ||
let response = ctx | ||
.client | ||
.list_nvme_controllers(rpc::Null {}) | ||
.await | ||
.context(GrpcStatus)?; | ||
|
||
match ctx.output { | ||
OutputFormat::Json => { | ||
println!( | ||
"{}", | ||
serde_json::to_string_pretty(&response.get_ref()) | ||
.unwrap() | ||
.to_colored_json_auto() | ||
.unwrap() | ||
); | ||
} | ||
OutputFormat::Default => { | ||
let controllers = &response.get_ref().controllers; | ||
if controllers.is_empty() { | ||
ctx.v1("No NVMe controllers found"); | ||
return Ok(()); | ||
} | ||
|
||
let table = controllers | ||
.iter() | ||
.map(|c| { | ||
let size = c.size.to_string(); | ||
let blk_size = c.blk_size.to_string(); | ||
let state = controller_state_to_str(c.state); | ||
|
||
vec![c.name.clone(), size, state, blk_size] | ||
}) | ||
.collect(); | ||
|
||
let hdr = vec!["NAMEs", "SIZE", "STATE", "BLKSIZE"]; | ||
ctx.print_list(hdr, table); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::{ | ||
bdev::{ | ||
nexus::nexus_bdev, | ||
NvmeController, | ||
NvmeControllerState, | ||
NVME_CONTROLLERS, | ||
}, | ||
grpc::{rpc_submit, GrpcResult}, | ||
}; | ||
|
||
use ::rpc::mayastor as rpc; | ||
use std::convert::From; | ||
use tonic::{Response, Status}; | ||
|
||
impl<'a> NvmeController<'a> { | ||
fn to_grpc(&self) -> rpc::NvmeController { | ||
let (size, blk_size) = self | ||
.namespace() | ||
.map_or((0, 0), |ns| (ns.size_in_bytes(), ns.block_len() as u32)); | ||
|
||
rpc::NvmeController { | ||
name: self.name.to_string(), | ||
state: rpc::NvmeControllerState::from(self.get_state()) as i32, | ||
size, | ||
blk_size, | ||
} | ||
} | ||
} | ||
|
||
impl From<NvmeControllerState> for rpc::NvmeControllerState { | ||
fn from(state: NvmeControllerState) -> Self { | ||
match state { | ||
NvmeControllerState::New => rpc::NvmeControllerState::New, | ||
NvmeControllerState::Initializing => { | ||
rpc::NvmeControllerState::Initializing | ||
} | ||
NvmeControllerState::Running => rpc::NvmeControllerState::Running, | ||
NvmeControllerState::Faulted(_) => { | ||
rpc::NvmeControllerState::Faulted | ||
} | ||
NvmeControllerState::Unconfiguring => { | ||
rpc::NvmeControllerState::Unconfiguring | ||
} | ||
NvmeControllerState::Unconfigured => { | ||
rpc::NvmeControllerState::Unconfigured | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn list_controllers() -> GrpcResult<rpc::ListNvmeControllersReply> { | ||
let rx = rpc_submit::<_, _, nexus_bdev::Error>(async move { | ||
let controllers = NVME_CONTROLLERS | ||
.controllers() | ||
.iter() | ||
.filter_map(|n| { | ||
NVME_CONTROLLERS | ||
.lookup_by_name(n) | ||
.map(|c| c.lock().to_grpc()) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(rpc::ListNvmeControllersReply { | ||
controllers, | ||
}) | ||
})?; | ||
|
||
rx.await | ||
.map_err(|_| Status::cancelled("cancelled"))? | ||
.map_err(Status::from) | ||
.map(Response::new) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.