Skip to content

Commit c46a287

Browse files
authored
Merge pull request #6 from cholcombe973/master
Change example over to safe code calls
2 parents 500c79b + 3147ffb commit c46a287

File tree

2 files changed

+77
-104
lines changed

2 files changed

+77
-104
lines changed

examples/ceph.rs

Lines changed: 67 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,14 @@
1717
extern crate ceph_rust;
1818
extern crate libc;
1919

20-
use libc::*;
21-
use std::ffi::{CStr, CString};
22-
use std::{ptr, str, slice};
2320

21+
use ceph_rust::JsonData;
22+
#[cfg(target_os = "linux")]
23+
use ceph_rust::admin_sockets::*;
2424
#[cfg(target_os = "linux")]
2525
use ceph_rust::ceph as ceph_helpers;
2626
#[cfg(target_os = "linux")]
2727
use ceph_rust::rados as ceph;
28-
#[cfg(target_os = "linux")]
29-
use ceph_rust::admin_sockets::*;
30-
31-
use ceph_rust::JsonData;
32-
33-
macro_rules! zeroed_c_char_buf {
34-
($n:expr) => {
35-
repeat(0).take($n).collect::<Vec<c_char>>();
36-
}
37-
}
3828

3929
#[cfg(not(target_os = "linux"))]
4030
fn main() {}
@@ -43,99 +33,88 @@ fn main() {}
4333

4434
#[cfg(target_os = "linux")]
4535
fn main() {
46-
let mut major: i32 = 0;
47-
let mut minor: i32 = 0;
48-
let mut extra: i32 = 0;
49-
50-
let config_file = CString::new("/etc/ceph/ceph.conf").unwrap();
51-
let pool_name = CString::new("lsio").unwrap();
52-
let mut cluster: ceph::rados_t = std::ptr::null_mut();
53-
let mut ret_code: i32;
54-
36+
let pool_name = "lsio";
5537
// NB: These examples (except for a few) are low level examples that require the unsafe block.
5638
// However, work for the higher level pur Rust is being worked on in the ceph.rs module of
5739
// the library. A few of those are present below. We will create a common Result or Option
5840
// return and allow for pattern matching.
5941

6042
// Example of accessing the `Admin Socket` for mon
61-
match admin_socket_command("help", "/var/run/ceph/ceph-mon.ceph-vm1.asok") {
43+
match admin_socket_command("help", "/var/run/ceph/ceph-mon.ip-172-31-31-247.asok") {
6244
Ok(json) => {
6345
println!("{}", json);
6446
},
65-
Err(e) => { println!("{}", e); },
47+
Err(e) => {
48+
println!("{}", e);
49+
},
6650
}
6751

68-
unsafe {
69-
ceph::rados_version(&mut major, &mut minor, &mut extra);
70-
ret_code = ceph::rados_create(&mut cluster, std::ptr::null());
71-
println!("Return code: {} - {:?}", ret_code, cluster);
72-
73-
ret_code = ceph::rados_conf_read_file(cluster, config_file.as_ptr());
74-
println!("Return code: {} - {:?}", ret_code, cluster);
52+
let rados_version = ceph_helpers::rados_libversion();
53+
println!("Librados version: {:?}", rados_version);
7554

76-
ret_code = ceph::rados_connect(cluster);
77-
println!("Return code: {} - {:?}", ret_code, cluster);
55+
println!("Connecting to ceph");
56+
let cluster = ceph_helpers::connect_to_ceph("admin", "/etc/ceph/ceph.conf").unwrap();
57+
println!("Creating pool {}", pool_name);
58+
ceph_helpers::rados_create_pool(cluster, pool_name).unwrap();
7859

79-
ret_code = ceph::rados_pool_create(cluster, pool_name.as_ptr());
80-
println!("Return code: {}", ret_code);
60+
println!("Listing pools");
61+
let pools_list = ceph_helpers::rados_pools(cluster).unwrap();
62+
for pool in pools_list{
63+
println!("pool: {}", pool);
64+
}
8165

82-
let pools_list = ceph_helpers::rados_pools(cluster).unwrap();
83-
println!("{:?}", pools_list);
66+
println!("Deleting pool: {}", pool_name);
67+
ceph_helpers::rados_delete_pool(cluster, pool_name).unwrap();
68+
69+
println!("Getting cluster fsid");
70+
let fsid = ceph_helpers::rados_fsid(cluster);
71+
println!("rados_cluster_fsid {:?}", fsid);
72+
73+
let cluster_stat = ceph_helpers::rados_stat_cluster(cluster).unwrap();
74+
println!("Cluster stat: {:?}", cluster_stat);
75+
76+
// Print CephHealth of cluster
77+
println!("{}", ceph_helpers::ceph_health_string(cluster).unwrap_or("".to_string()));
78+
79+
// Print Status of cluster health a different way
80+
println!("{}", ceph_helpers::ceph_status(cluster, &["health", "overall_status"]).unwrap());
81+
// Currently - parses the `ceph --version` call. The admin socket commands `version`
82+
// and `git_version`
83+
// will be called soon to replace the string parse.
84+
// Change to the real mon admin socket name
85+
let ceph_ver = ceph_helpers::ceph_version("/var/run/ceph/ceph-mon.ip-172-31-31-247.asok");
86+
println!("Ceph Version - {:?}", ceph_ver);
87+
// Mon command to check the health. Same as `ceph -s`
88+
match ceph_helpers::ceph_mon_command(cluster, "prefix", "status", None) {
89+
Ok((outbuf, outs)) => {
90+
match outbuf {
91+
Some(output) => println!("Ceph mon command (outbuf):\n{}", output),
92+
None => {},
93+
}
94+
match outs {
95+
Some(output) => println!("Ceph mon command (outs):\n{}", output),
96+
None => {},
97+
}
98+
},
99+
Err(e) => {
100+
println!("{:?}", e);
101+
},
102+
}
84103

85-
ret_code = ceph::rados_pool_delete(cluster, pool_name.as_ptr());
86-
println!("Return code: {}", ret_code);
104+
// This command encapsulates the lower level mon, osd, pg commands and returns JsonData
105+
// objects based on the key path
106+
println!("{:?}",
107+
ceph_helpers::ceph_command(cluster, "prefix", "status", ceph_helpers::CephCommandTypes::Mon, &["health"]));
87108

109+
// Get a list of Ceph librados commands in JSON format.
110+
// It's very long so it's commented out.
111+
// println!("{}", ceph_helpers::ceph_commands(cluster, None).unwrap().pretty());
112+
unsafe {
113+
println!("Getting rados instance id");
88114
let instance_id = ceph::rados_get_instance_id(cluster);
89115
println!("Instance ID: {}", instance_id);
90-
91-
let buf_size: usize = 37; // 36 is the constant size +1 for null.
92-
let mut fs_id: Vec<u8> = Vec::with_capacity(buf_size);
93-
94-
let len = ceph::rados_cluster_fsid(cluster, fs_id.as_mut_ptr() as *mut c_char, buf_size);
95-
let slice = slice::from_raw_parts(fs_id.as_mut_ptr(), buf_size - 1);
96-
let s: &str = str::from_utf8(slice).unwrap();
97-
println!("rados_cluster_fsid len: {} - {}", len, s);
98-
99-
// Rust specific example...
100-
let cluster_stat = ceph_helpers::rados_stat_cluster(cluster);
101-
println!("Cluster stat: {:?}", cluster_stat);
102-
103-
// Mon command to check the health. Same as `ceph -s`
104-
match ceph_helpers::ceph_mon_command(cluster, "prefix", "status", None) {
105-
Ok((outbuf, outs)) => {
106-
match outbuf {
107-
Some(output) => println!("Ceph mon command (outbuf):\n{}", output),
108-
None => {},
109-
}
110-
match outs {
111-
Some(output) => println!("Ceph mon command (outs):\n{}", output),
112-
None => {},
113-
}
114-
},
115-
Err(e) => {println!("{:?}", e);},
116-
}
117-
118-
// Print CephHealth of cluster
119-
println!("{}", ceph_helpers::ceph_health_string(cluster).unwrap_or("".to_string()));
120-
121-
// Print Status of cluster health a different way
122-
println!("{}", ceph_helpers::ceph_status(cluster, &["health", "overall_status"]).unwrap());
123-
124-
// This command encapsulates the lower level mon, osd, pg commands and returns JsonData objects based on the key path
125-
println!("{:?}", ceph_helpers::ceph_command(cluster, "prefix", "status", ceph_helpers::CephCommandTypes::Mon, &["health"]));
126-
127-
// Get a list of Ceph librados commands in JSON format.
128-
// It's very long so it's commented out.
129-
// println!("{}", ceph_helpers::ceph_commands(cluster, None).unwrap().pretty());
130-
131-
// Currently - parses the `ceph --version` call. The admin socket commands `version` and `git_version`
132-
// will be called soon to replace the string parse.
133-
let ceph_ver = ceph_helpers::ceph_version("/var/run/ceph/ceph-mon.ceph-vm1.asok"); // Change to the real mon admin socket name
134-
println!("Ceph Version - {:?}", ceph_ver);
135-
136-
ceph::rados_shutdown(cluster);
137116
}
138117

139-
println!("RADOS Version - v{}.{}.{}", major, minor, extra);
140-
118+
ceph_helpers::disconnect_from_ceph(cluster);
119+
println!("RADOS Version - v{}.{}.{}", rados_version.major, rados_version.minor, rados_version.extra);
141120
}

src/ceph.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
#![cfg(target_os = "linux")]
1515
#![allow(unused_imports)]
1616

17-
use std::error::Error as StdError;
18-
use std::ffi::{CStr, CString, IntoStringError, NulError};
17+
use std::ffi::{CStr, CString};
1918

20-
use std::io::{BufRead, Cursor, Error};
19+
use std::io::{BufRead, Cursor};
2120
use std::net::IpAddr;
22-
use std::iter::FromIterator;
23-
use std::{ptr, fmt, slice, str};
21+
use std::{ptr, str};
2422
use std::time::{Duration, SystemTime, UNIX_EPOCH};
2523

26-
use uuid::{ParseError, Uuid};
24+
use uuid::{Uuid};
2725
use byteorder::{LittleEndian, WriteBytesExt};
2826
use nom::{IResult, le_u32};
2927
use libc::*;
@@ -1557,22 +1555,18 @@ pub fn rados_fsid(cluster: rados_t) -> Result<Uuid, RadosError> {
15571555
return Err(RadosError::new("Rados not connected. Please initialize cluster".to_string()));
15581556
}
15591557
let mut fsid_buffer: Vec<u8> = Vec::with_capacity(37);
1560-
let fsid: Uuid;
15611558
unsafe {
15621559
let ret_code = rados_cluster_fsid(cluster, fsid_buffer.as_mut_ptr() as *mut c_char, fsid_buffer.capacity());
15631560
if ret_code < 0 {
15641561
return Err(RadosError::new(try!(get_error(ret_code))));
15651562
}
1566-
if ret_code > 0 {
1567-
// Length of buffer required for fsid
1568-
fsid_buffer.reserve(ret_code as usize);
1569-
rados_cluster_fsid(cluster, fsid_buffer.as_mut_ptr() as *mut c_char, fsid_buffer.capacity());
1570-
// Tell the Vec how much Ceph read into the buffer
1571-
fsid_buffer.set_len(ret_code as usize);
1572-
}
1563+
// Tell the Vec how much Ceph read into the buffer
1564+
fsid_buffer.set_len(ret_code as usize);
15731565
}
1574-
fsid = try!(Uuid::from_bytes(&fsid_buffer));
1575-
Ok(fsid)
1566+
//Ceph actually returns the fsid as a uuid string
1567+
let fsid_str = String::from_utf8(fsid_buffer)?;
1568+
// Parse into a UUID and return
1569+
Ok(fsid_str.parse()?)
15761570
}
15771571

15781572
/// Ping a monitor to assess liveness

0 commit comments

Comments
 (0)