Skip to content

Commit

Permalink
Merge pull request #61 from mzhong1/master
Browse files Browse the repository at this point in the history
More Commands and Parse Unknowns
  • Loading branch information
cholcombe973 authored Dec 30, 2019
2 parents 682d975 + 6cb22ef commit 2610e3f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 56 deletions.
99 changes: 46 additions & 53 deletions src/ceph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@
use crate::JsonData;

use crate::admin_sockets::*;
use byteorder::{LittleEndian, WriteBytesExt};
use crate::error::*;
use crate::json::*;
use crate::JsonValue;
use byteorder::{LittleEndian, WriteBytesExt};
use libc::*;
use nom::IResult;
use nom::number::complete::le_u32;
use nom::IResult;
use serde_json;
use crate::JsonValue;

use crate::rados::*;
#[cfg(feature = "rados_striper")] use crate::rados_striper::*;
#[cfg(feature = "rados_striper")]
use crate::rados_striper::*;
use crate::status::*;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::{ptr, str};

use crate::utils::*;
use std::io::{BufRead, Cursor};
use std::net::IpAddr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::utils::*;

use uuid::Uuid;

Expand All @@ -62,38 +63,49 @@ pub enum CephCommandTypes {
named!(
parse_header<TmapOperation>,
do_parse!(
char!(CEPH_OSD_TMAP_HDR) >> data_len: le_u32 >> data: take!(data_len) >> (TmapOperation::Header {
data: data.to_vec(),
})
char!(CEPH_OSD_TMAP_HDR)
>> data_len: le_u32
>> data: take!(data_len)
>> (TmapOperation::Header { data: data.to_vec() })
)
);

named!(
parse_create<TmapOperation>,
do_parse!(
char!(CEPH_OSD_TMAP_CREATE) >> key_name_len: le_u32 >> key_name: take_str!(key_name_len) >> data_len: le_u32
>> data: take!(data_len) >> (TmapOperation::Create {
name: key_name.to_string(),
data: data.to_vec(),
})
char!(CEPH_OSD_TMAP_CREATE)
>> key_name_len: le_u32
>> key_name: take_str!(key_name_len)
>> data_len: le_u32
>> data: take!(data_len)
>> (TmapOperation::Create {
name: key_name.to_string(),
data: data.to_vec(),
})
)
);

named!(
parse_set<TmapOperation>,
do_parse!(
char!(CEPH_OSD_TMAP_SET) >> key_name_len: le_u32 >> key_name: take_str!(key_name_len) >> data_len: le_u32
>> data: take!(data_len) >> (TmapOperation::Set {
key: key_name.to_string(),
data: data.to_vec(),
})
char!(CEPH_OSD_TMAP_SET)
>> key_name_len: le_u32
>> key_name: take_str!(key_name_len)
>> data_len: le_u32
>> data: take!(data_len)
>> (TmapOperation::Set {
key: key_name.to_string(),
data: data.to_vec(),
})
)
);

named!(
parse_remove<TmapOperation>,
do_parse!(
char!(CEPH_OSD_TMAP_RM) >> key_name_len: le_u32 >> key_name: take_str!(key_name_len)
char!(CEPH_OSD_TMAP_RM)
>> key_name_len: le_u32
>> key_name: take_str!(key_name_len)
>> (TmapOperation::Remove {
name: key_name.to_string(),
})
Expand All @@ -116,26 +128,26 @@ impl TmapOperation {
buffer.push(CEPH_OSD_TMAP_HDR as u8);
buffer.write_u32::<LittleEndian>(data.len() as u32)?;
buffer.extend_from_slice(data);
}
},
TmapOperation::Set { ref key, ref data } => {
buffer.push(CEPH_OSD_TMAP_SET as u8);
buffer.write_u32::<LittleEndian>(key.len() as u32)?;
buffer.extend(key.as_bytes());
buffer.write_u32::<LittleEndian>(data.len() as u32)?;
buffer.extend_from_slice(data);
}
},
TmapOperation::Create { ref name, ref data } => {
buffer.push(CEPH_OSD_TMAP_CREATE as u8);
buffer.write_u32::<LittleEndian>(name.len() as u32)?;
buffer.extend(name.as_bytes());
buffer.write_u32::<LittleEndian>(data.len() as u32)?;
buffer.extend_from_slice(data);
}
},
TmapOperation::Remove { ref name } => {
buffer.push(CEPH_OSD_TMAP_RM as u8);
buffer.write_u32::<LittleEndian>(name.len() as u32)?;
buffer.extend(name.as_bytes());
}
},
}
Ok(buffer)
}
Expand Down Expand Up @@ -280,12 +292,7 @@ impl Iterator for XAttr {
let mut value: *const c_char = ptr::null();
let mut val_length: usize = 0;
unsafe {
let ret_code = rados_getxattrs_next(
self.iter,
&mut name,
&mut value,
&mut val_length,
);
let ret_code = rados_getxattrs_next(self.iter, &mut name, &mut value, &mut val_length);

if ret_code < 0 {
// Something failed, however Iterator doesn't return Result so we return None
Expand Down Expand Up @@ -771,7 +778,6 @@ impl IoCtx {
// unsafe {
// }
// }
//
/// List all the ids of pool snapshots
// pub fn rados_snap_list(ctx: rados_ioctx_t, snaps: *mut rados_snap_t) ->
// RadosResult<()> {
Expand All @@ -792,7 +798,6 @@ impl IoCtx {
// }
// Ok(buffer)
// }
//
/// Get the id of a pool snapshot
pub fn rados_snap_lookup(&self, snap_name: &str) -> RadosResult<u64> {
self.ioctx_guard()?;
Expand Down Expand Up @@ -1537,7 +1542,6 @@ impl Rados {
/// buf_size should be the value used with_capacity
///
/// Returns Ok(Vec<String>) - A list of Strings of the pool names.
///
#[allow(unused_variables)]
pub fn rados_pools(&self) -> RadosResult<Vec<String>> {
self.conn_guard()?;
Expand Down Expand Up @@ -1672,11 +1676,7 @@ pub fn rados_libversion() -> RadosVersion {
unsafe {
rados_version(&mut major, &mut minor, &mut extra);
}
RadosVersion {
major,
minor,
extra,
}
RadosVersion { major, minor, extra }
}

impl Rados {
Expand Down Expand Up @@ -1731,12 +1731,7 @@ impl Rados {
let mut out_str: *mut c_char = ptr::null_mut();
let mut str_length: usize = 0;
unsafe {
let ret_code = rados_ping_monitor(
self.rados,
mon_id_str.as_ptr(),
&mut out_str,
&mut str_length,
);
let ret_code = rados_ping_monitor(self.rados, mon_id_str.as_ptr(), &mut out_str, &mut str_length);
if ret_code < 0 {
return Err(ret_code.into());
}
Expand Down Expand Up @@ -1780,7 +1775,7 @@ pub fn ceph_version_parse() -> Option<String> {
} else {
Some(String::from_utf8_lossy(&output.stderr).to_string())
}
}
},
Err(_) => None,
}
}
Expand All @@ -1800,7 +1795,7 @@ impl Rados {
"The attributes were not found in the output.".to_string(),
))
}
}
},
_ => Err(RadosError::new("JSON data not found.".to_string())),
},
_ => Err(RadosError::new("JSON data not found.".to_string())),
Expand Down Expand Up @@ -1833,7 +1828,7 @@ impl Rados {
} else {
CephHealth::Error
}
}
},
Err(_) => CephHealth::Error,
}
}
Expand Down Expand Up @@ -1861,7 +1856,7 @@ impl Rados {
"The attributes were not found in the output.".to_string(),
))
}
}
},
_ => Err(RadosError::new("JSON data not found.".to_string())),
},
_ => Err(RadosError::new("JSON data not found.".to_string())),
Expand Down Expand Up @@ -1889,7 +1884,7 @@ impl Rados {
} else {
Ok(jsondata)
}
}
},
_ => Err(RadosError::new("JSON data not found.".to_string())),
},
_ => Err(RadosError::new("JSON data not found.".to_string())),
Expand All @@ -1909,10 +1904,7 @@ impl Rados {
self.ceph_mon_command_with_data(name, value, format, data)
}

pub fn ceph_mon_command_without_data(
&self,
cmd: &serde_json::Value,
) -> RadosResult<(Vec<u8>, Option<String>)> {
pub fn ceph_mon_command_without_data(&self, cmd: &serde_json::Value) -> RadosResult<(Vec<u8>, Option<String>)> {
self.conn_guard()?;
let cmd_string = cmd.to_string();
debug!("ceph_mon_command_without_data: {}", cmd_string);
Expand Down Expand Up @@ -2452,7 +2444,8 @@ impl RadosStriper {
let mut xattr_iterator_handle: rados_xattrs_iter_t = ptr::null_mut();

unsafe {
let ret_code = rados_striper_getxattrs(self.rados_striper, object_name_str.as_ptr(), &mut xattr_iterator_handle);
let ret_code =
rados_striper_getxattrs(self.rados_striper, object_name_str.as_ptr(), &mut xattr_iterator_handle);
if ret_code < 0 {
return Err(ret_code.into());
}
Expand Down
4 changes: 2 additions & 2 deletions src/ceph_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl FromStr for CephVersion {
"61" => return Ok(Cuttlefish),
"56" => return Ok(Bobtail),
"48" => return Ok(Argonaut),
_ => {}
_ => {},
},
_ => {}
_ => {},
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion src/ceph_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct LvmTags {
pub wal_device: Option<String>,
#[serde(rename = "ceph.wal_uuid")]
pub wal_uuid: Option<String>,
//Other tags that are not listed here
#[serde(flatten)]
pub other_tags: Option<HashMap<String, String>>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -69,12 +72,35 @@ pub struct LvmMeta {
#[serde(rename = "type")]
pub lv_type: String,
pub vg_name: String,
// other metadata not captured through the above attributes
#[serde(flatten)]
pub other_meta: Option<HashMap<String, String>>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum LvmData {
Osd(LvmMeta),
Journal {
path: Option<String>,
tags: Option<HashMap<String, String>>,
#[serde(rename = "type")]
j_type: Option<String>,
// other metadata not captured through the above attributes
#[serde(flatten)]
other_meta: Option<HashMap<String, String>>,
},
// unknown type of ceph-volume lvm list output
Unknown {
//unknown metadata not captured through the above attributes
#[serde(flatten)]
unknown_meta: Option<HashMap<String, String>>,
},
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Lvm {
#[serde(flatten)]
pub metadata: LvmMeta,
pub metadata: LvmData,
}

// Check the cluster version. If version < Luminous, error out
Expand Down
43 changes: 43 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,17 @@ pub fn osd_crush_remove(cluster_handle: &Rados, osd_id: u64, simulate: bool) ->
Ok(())
}

/// Get a list of all pools in the cluster
pub fn osd_pool_ls(cluster_handle: &Rados) -> RadosResult<Vec<String>> {
let cmd = json!({
"prefix": "osd pool ls",
"format": "json",
});
let result = cluster_handle.ceph_mon_command_without_data(&cmd)?;
let return_data = String::from_utf8(result.0)?;
Ok(serde_json::from_str(&return_data)?)
}

/// Query a ceph pool.
pub fn osd_pool_get(cluster_handle: &Rados, pool: &str, choice: &PoolOption) -> RadosResult<String> {
let cmd = json!({
Expand Down Expand Up @@ -914,6 +925,38 @@ pub fn osd_unset(cluster_handle: &Rados, key: &OsdOption, simulate: bool) -> Rad
Ok(())
}

pub enum CrushNodeStatus {
Up,
Down,
In,
Out,
Destroyed,
}

impl CrushNodeStatus {
pub fn to_string(&self) -> String {
match self {
CrushNodeStatus::Up => "up".to_string(),
CrushNodeStatus::Down => "down".to_string(),
CrushNodeStatus::In => "in".to_string(),
CrushNodeStatus::Out => "out".to_string(),
CrushNodeStatus::Destroyed => "destroyed".to_string(),
}
}
}

/// get a crush tree of all osds that have the given status
pub fn osd_tree_status(cluster_handle: &Rados, status: CrushNodeStatus) -> RadosResult<CrushTree> {
let cmd = json!({
"prefix": "osd tree",
"states" : &[&status.to_string()],
"format": "json-pretty"
});
let result = cluster_handle.ceph_mon_command_without_data(&cmd)?;
let return_data = String::from_utf8(result.0)?;
Ok(serde_json::from_str(&return_data)?)
}

pub fn osd_tree(cluster_handle: &Rados) -> RadosResult<CrushTree> {
let cmd = json!({
"prefix": "osd tree",
Expand Down

0 comments on commit 2610e3f

Please sign in to comment.