Commit 3a86ae52 authored by Heinz N. Gies's avatar Heinz N. Gies

Make Destroy destroy zfs dataset and clone snapshot

parent 7ab115ff
......@@ -149,7 +149,7 @@ impl<'a> JDB<'a> {
Some(index) => {
// remove the config file first
let mut path = PathBuf::from(self.config.settings.conf_dir.as_str());
path.join(uuid);
path.push(uuid);
path.set_extension("json");
fs::remove_file(&path)?;
self.index.entries.remove(index);
......@@ -196,9 +196,7 @@ impl<'a> JDB<'a> {
pub fn get(self: &'a JDB<'a>, uuid: &str) -> Option<&IdxEntry> {
match self.find(uuid) {
None => None,
Some(index) => {
Some(&self.index.entries[index])
}
Some(index) => Some(&self.index.entries[index]),
}
}
/// Finds an entry for a given uuid
......
......@@ -120,11 +120,20 @@ fn destroy(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>>
let mut db = JDB::open(conf)?;
let uuid = value_t!(matches, "uuid", String).unwrap();
match db.get(uuid.as_str()) {
Some(entry) =>
match zfs::destroy(entry.root.as_str()){
Ok(_) => debug!(conf.logger, "zfs dataset deleted: {}", entry.root),
Err(e) => warn!(conf.logger, "failed to delete dataset: {}", e)
},
Some(entry) => {
let origin = zfs::origin(entry.root.as_str());
match zfs::destroy(entry.root.as_str()) {
Ok(_) => debug!(conf.logger, "zfs dataset deleted: {}", entry.root),
Err(e) => warn!(conf.logger, "failed to delete dataset: {}", e),
};
match origin {
Ok(origin) => {
zfs::destroy(origin.as_str())?;
debug!(conf.logger, "zfs snapshot deleted: {}", origin)
},
Err(e) => warn!(conf.logger, "failed to delete origin: {}", e),
}
}
None => return Err(NotFoundError::bx("Could not find VM")),
};
db.remove(uuid.as_str())?;
......
......@@ -45,6 +45,26 @@ pub fn get(dataset: &str) -> Result<ZFSEntry, Box<Error>> {
}
}
/// reads the zfs datasets in a pool
pub fn origin(dataset: &str) -> Result<String, Box<Error>> {
let output = Command::new("zfs")
.args(&["get", "-p", "-H", "origin", dataset])
.output()
.expect("zfs list failed");
if output.status.success() {
let out = String::from_utf8_lossy(&output.stdout).to_string();
let mut reply = out.split('\t');
reply.next().ok_or_else(
|| GenericError::bx("NAME field missing"))?;
reply.next().ok_or_else(
|| GenericError::bx("PROPERTY field missing"))?;
let origin = reply.next().ok_or_else(
|| GenericError::bx("PROPERTY field missing"))?;
Ok(String::from(origin))
} else {
Err(GenericError::bx("Failed to get dataset"))
}
}
/// create a zfs datasets in a pool
pub fn create(dataset: &str) -> Result<i32, Box<Error>> {
let output = Command::new("zfs")
......@@ -66,8 +86,7 @@ pub fn snapshot(dataset: &str, snapshot: &str) -> Result<String, Box<Error>> {
let output = Command::new("zfs")
.args(&["snapshot", snap.as_str()])
.output()
.expect("zfs create snapshot");
println!("zfs snapshot {}", snap);
.expect("zfs snapshot failed");
if output.status.success() {
Ok(snap)
} else {
......@@ -75,14 +94,12 @@ pub fn snapshot(dataset: &str, snapshot: &str) -> Result<String, Box<Error>> {
}
}
/// clones a zfs snapshot
pub fn clone(snapshot: &str, dataset: &str) -> Result<i32, Box<Error>> {
let output = Command::new("zfs")
.args(&["clone", snapshot, dataset])
.output()
.expect("zfs create snapshot");
println!("zfs clone {} {}", snapshot, dataset);
.expect("zfs clone failed");
if output.status.success() {
Ok(0)
} else {
......@@ -94,7 +111,7 @@ pub fn destroy(dataset: &str) -> Result<i32, Box<Error>> {
let output = Command::new("zfs")
.args(&["destroy", dataset])
.output()
.expect("zfs create failed");
.expect("zfs destroy failed");
if output.status.success() {
Ok(0)
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment