Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling #39

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion common/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::ffi::OsStr;
use thiserror::Error;

pub trait CommandExtTrait {
/// Execute command via output() and return:
/// Execute command via output() using sudo and return:
///
/// 1. An IO Error if the command could not be run
/// 2. An ExecutionError if the Command was not successfull
Expand Down Expand Up @@ -109,6 +109,7 @@ impl CommandError {
impl Display for CommandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_char('"')?;
f.write_str("sudo ")?;
for arg in self.args.iter() {
f.write_str(arg)?;
f.write_char(' ')?;
Expand Down
10 changes: 6 additions & 4 deletions common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ impl Termination for FlakeError {
/// All other errors are represented as Failure
fn report(self) -> std::process::ExitCode {
match self {
FlakeError::CommandError(CommandError {
base: ProcessError::ExecutionError(Output { status, ..}),
..
}) => match status.code() {
FlakeError::CommandError(
CommandError {
base: ProcessError::ExecutionError(Output { status, .. }),
..
}
) => match status.code() {
Some(code) => (code as u8).into(),
None => ExitCode::FAILURE,
},
Expand Down
4 changes: 1 addition & 3 deletions firecracker-pilot/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
use flakes::user::User;
use lazy_static::lazy_static;
use serde::Deserialize;
use strum::Display;
Expand Down Expand Up @@ -156,8 +155,7 @@ pub struct RuntimeSection<'a> {
/// of the VM engine is performed by sudo.
/// The behavior of sudo can be controlled via the
/// file /etc/sudoers
#[serde(borrow, flatten)]
pub runas: User<'a>,
pub runas: &'a str,

/// Resume the VM from previous execution.
/// If the VM is still running, the app will be
Expand Down
24 changes: 14 additions & 10 deletions firecracker-pilot/src/firecracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ pub fn create(program_name: &String) -> Result<(String, String), FlakeError> {
runas, resume, force_vsock, firecracker: engine_section, ..
} = config().runtime();

let user = User::from(runas);

// check for includes
let tar_includes = config().tars();
let path_includes = config().paths();
Expand All @@ -197,7 +199,7 @@ pub fn create(program_name: &String) -> Result<(String, String), FlakeError> {

// Check early return condition
if Path::new(&vm_id_file_path).exists() && gc_meta_files(
&vm_id_file_path, runas, program_name, resume
&vm_id_file_path, user, program_name, resume
)? && (resume || force_vsock) {
// VM exists
// report ID value and its ID file name
Expand All @@ -206,7 +208,7 @@ pub fn create(program_name: &String) -> Result<(String, String), FlakeError> {
}

// Garbage collect occasionally
gc(runas, program_name).ok();
gc(user, program_name).ok();

// Sanity check
if Path::new(&vm_id_file_path).exists() {
Expand All @@ -229,7 +231,7 @@ pub fn create(program_name: &String) -> Result<(String, String), FlakeError> {

match run_creation(
&vm_id_file_path, program_name, engine_section,
resume, runas, has_includes
resume, user, has_includes
) {
Ok(result) => {
if let Some(spinner) = spinner {
Expand All @@ -251,7 +253,7 @@ fn run_creation(
program_name: &String,
engine_section: EngineSection,
resume: bool,
runas: User,
user: User,
has_includes: bool
) -> Result<(String, String), FlakeError> {
// Create initial vm_id_file with process ID set to 0
Expand All @@ -272,7 +274,7 @@ fn run_creation(
vm_overlay_file_fd.write_all(&[0])?;

// Create filesystem
let mut mkfs = runas.run("mkfs.ext2");
let mut mkfs = user.run("mkfs.ext2");
mkfs.arg("-F")
.arg(&vm_overlay_file);
if Lookup::is_debug() {
Expand Down Expand Up @@ -318,11 +320,13 @@ pub fn start(
!*/
let RuntimeSection { runas, resume, force_vsock, .. } = config().runtime();

let user = User::from(runas);

let mut is_blocking: bool = true;

if vm_running(&vm_id, runas)? {
if vm_running(&vm_id, user)? {
// 1. Execute app in running VM
execute_command_at_instance(program_name, runas)?;
execute_command_at_instance(program_name, user)?;
} else {
let firecracker_config = NamedTempFile::new()?;
create_firecracker_config(
Expand All @@ -332,13 +336,13 @@ pub fn start(
// 2. Startup VM as background job and execute app through vsock
is_blocking = false;
call_instance(
&firecracker_config, &vm_id_file, runas, is_blocking
&firecracker_config, &vm_id_file, user, is_blocking
)?;
execute_command_at_instance(program_name, runas)?;
execute_command_at_instance(program_name, user)?;
} else {
// 3. Startup VM and execute app
call_instance(
&firecracker_config, &vm_id_file, runas, is_blocking
&firecracker_config, &vm_id_file, user, is_blocking
)?;
}
}
Expand Down
25 changes: 14 additions & 11 deletions flake-ctl/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub fn create_vm_config(
}
}

pub fn remove(app: &str, engine: &str, silent: bool) {
pub fn remove(app: &str, engine: &str, silent: bool) -> bool {
/*!
Delete application link and config files
!*/
Expand All @@ -211,8 +211,8 @@ pub fn remove(app: &str, engine: &str, silent: bool) {
"Application {:?} must be specified with an absolute path",
app
);
}
return;
};
return false
}
if !silent {
info!("Removing application: {}", app);
Expand All @@ -226,22 +226,22 @@ pub fn remove(app: &str, engine: &str, silent: bool) {
Err(error) => {
if !silent {
error!("Error removing pilot link: {}: {:?}", app, error);
}
return;
};
return false
}
}
} else {
if !silent {
error!("Symlink not pointing to {}: {}", engine, app);
}
return;
};
return false
}
}
Err(error) => {
if !silent {
error!("Failed to read as symlink: {}: {:?}", app, error);
}
return;
};
return false
}
}
// remove config file and config directory
Expand All @@ -254,7 +254,8 @@ pub fn remove(app: &str, engine: &str, silent: bool) {
Err(error) => {
if !silent {
error!("Error removing config file: {}: {:?}", config_file, error)
}
};
return false
}
}
}
Expand All @@ -266,11 +267,13 @@ pub fn remove(app: &str, engine: &str, silent: bool) {
error!(
"Error removing config directory: {}: {:?}",
app_config_dir, error
)
);
return false
}
}
}
}
true
}

pub fn basename(program_path: &String) -> String {
Expand Down
2 changes: 2 additions & 0 deletions flake-ctl/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl AppConfig {
let config = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(config_file)
.unwrap_or_else(|_| panic!("Failed to open {:?}", config_file));
serde_yaml::to_writer(config, &yaml_config).unwrap();
Expand Down Expand Up @@ -304,6 +305,7 @@ impl AppConfig {
let config = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(config_file)
.unwrap_or_else(|_| panic!("Failed to open {:?}", config_file));
serde_yaml::to_writer(config, &yaml_config).unwrap();
Expand Down
20 changes: 10 additions & 10 deletions flake-ctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
},
// remove
cli::Firecracker::Remove { vm, app } => {
if ! app.is_none() {
app::remove(
app.as_ref().map(String::as_str).unwrap(),
defaults::FIRECRACKER_PILOT, false
);
if ! app.is_none() && ! app::remove(
app.as_ref().map(String::as_str).unwrap(),
defaults::FIRECRACKER_PILOT, false
) {
return Ok(ExitCode::FAILURE)
}
if ! vm.is_none() {
app::purge(
Expand Down Expand Up @@ -180,11 +180,11 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
},
// remove
cli::Podman::Remove { container, app } => {
if ! app.is_none() {
app::remove(
app.as_ref().map(String::as_str).unwrap(),
defaults::PODMAN_PILOT, false
);
if ! app.is_none() && ! app::remove(
app.as_ref().map(String::as_str).unwrap(),
defaults::PODMAN_PILOT, false
) {
return Ok(ExitCode::FAILURE)
}
if ! container.is_none() {
app::purge(
Expand Down
2 changes: 1 addition & 1 deletion flake-ctl/src/podman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn rm(container: &String){
Call podman image rm with force option to remove all running containers
!*/
info!("Removing image and all running containers...");
info!("podman rm -f {}", container);
info!("podman rm -f {}", container);
let status = Command::new(defaults::PODMAN_PATH)
.arg("image")
.arg("rm")
Expand Down
4 changes: 1 addition & 3 deletions podman-pilot/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
use flakes::user::User;
use lazy_static::lazy_static;
use serde::Deserialize;
use std::{env, path::PathBuf, fs};
Expand Down Expand Up @@ -176,8 +175,7 @@ pub struct RuntimeSection<'a> {
/// of the container engine is performed by sudo.
/// The behavior of sudo can be controlled via the
/// file /etc/sudoers
#[serde(borrow, flatten)]
pub runas: User<'a>,
pub runas: &'a str,

/// Resume the container from previous execution.
///
Expand Down
Loading
Loading