From 5f84b381e39e3b3f41190f50c69be27b8fa81283 Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Sat, 10 Mar 2018 16:06:48 -0500 Subject: [PATCH] implement current command --- citadel-tools/citadel-appimg/src/main.rs | 16 +++++++++++ citadel-tools/citadel-appimg/src/manager.rs | 31 +++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/citadel-tools/citadel-appimg/src/main.rs b/citadel-tools/citadel-appimg/src/main.rs index 9ef393f..13221f2 100644 --- a/citadel-tools/citadel-appimg/src/main.rs +++ b/citadel-tools/citadel-appimg/src/main.rs @@ -44,6 +44,10 @@ fn main() { .about("Set an application image as the default image to boot") .arg(Arg::with_name("name").required(true))) + .subcommand(SubCommand::with_name("current") + .about("Set an application image as 'current'") + .arg(Arg::with_name("name").required(true))) + .get_matches(); let result = match matches.subcommand() { @@ -51,6 +55,7 @@ fn main() { ("start", Some(m)) => start_cmd(m), ("stop", Some(m)) => stop_cmd(m), ("default", Some(m)) => default_cmd(m), + ("current", Some(m)) => current_cmd(m), _ => Ok(()), }; if let Err(e) = result { @@ -92,3 +97,14 @@ fn default_cmd(matches: &ArgMatches) -> Result<()> { } Ok(()) } + +fn current_cmd(matches: &ArgMatches) -> Result<()> { + let name = matches.value_of("name").unwrap(); + let mut manager = ImageManager::load()?; + if manager.image_exists(name) { + manager.set_current(name)?; + } else { + warn!("No image '{}' exists", name); + } + Ok(()) +} diff --git a/citadel-tools/citadel-appimg/src/manager.rs b/citadel-tools/citadel-appimg/src/manager.rs index ffbbad6..ba798a2 100644 --- a/citadel-tools/citadel-appimg/src/manager.rs +++ b/citadel-tools/citadel-appimg/src/manager.rs @@ -55,7 +55,6 @@ impl ImageManager { } let appimg = AppImg::new(name)?; - println!("adding: {}", appimg.name()); self.images.insert(appimg.name().to_string(), appimg); Ok(()) } @@ -84,7 +83,7 @@ impl ImageManager { // if current is not set, set it to this instance let set_as_current = self.current.is_none(); if set_as_current { - self.set_current(name)?; + self.set_current_target(name)?; } match self.images.get(name) { @@ -123,7 +122,7 @@ impl ImageManager { fs::remove_file(&path)?; } if let Some(img_name) = self.find_running_image_name() { - self.set_current(&img_name)?; + self.set_current_target(&img_name)?; systemd::systemctl_start(DESKTOPD_SERVICE); } } @@ -182,6 +181,32 @@ impl ImageManager { } pub fn set_current(&mut self, name: &str) -> Result<()> { + { + let img = match self.images.get(name) { + Some(img) => img, + None => { + warn!("Cannot set {} as current, no image with that name exists", name); + return Ok(()); + }, + }; + + if self.is_current(img) { + warn!("Image {} is already current image", name); + return Ok(()); + } + + if !img.is_running() { + img.start()?; + } + } + self.set_current_target(name)?; + systemd::systemctl_restart(DESKTOPD_SERVICE); + Ok(()) + } + + + + fn set_current_target(&mut self, name: &str) -> Result<()> { if !is_valid_name(name) { warn!("{} is not a valid image name", name); return Ok(())