diff --git a/common/src/io.rs b/common/src/io.rs new file mode 100644 index 0000000..b8c2d2f --- /dev/null +++ b/common/src/io.rs @@ -0,0 +1,89 @@ +// +// Copyright (c) 2023 SUSE Software Solutions Germany GmbH +// +// This file is part of flake-pilot +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +use crate::flakelog::FlakeLog; +use crate::error::FlakeError; +use crate::user::User; +use crate::command::CommandExtTrait; + +#[derive(Debug, Default, Clone, Copy)] +pub struct IO { +} + +impl IO { + pub fn sync_includes( + target: &String, tar_includes: Vec<&str>, path_includes: Vec<&str>, user: User + ) -> Result<(), FlakeError> { + /*! + Sync custom include data to target path + !*/ + for tar in tar_includes { + FlakeLog::debug(&format!("Provision tar archive: [{}]", tar)); + let mut call = user.run("tar"); + call.arg("-C").arg(target) + .arg("-xf").arg(tar); + FlakeLog::debug(&format!("{:?}", call.get_args())); + let output = call.perform()?; + FlakeLog::debug( + &format!("{}", &String::from_utf8_lossy(&output.stdout)) + ); + FlakeLog::debug( + &format!("{}", &String::from_utf8_lossy(&output.stderr)) + ); + } + for path in path_includes { + FlakeLog::debug(&format!("Provision path: [{}]", path)); + Self::sync_data( + path, &format!("{}/{}", target, path), + ["--mkpath"].to_vec(), user + )?; + } + Ok(()) + } + + pub fn sync_data( + source: &str, target: &str, options: Vec<&str>, user: User + ) -> Result<(), FlakeError> { + /*! + Sync data from source path to target path + !*/ + let mut call = user.run("rsync"); + call.arg("-av"); + for option in options { + call.arg(option); + } + call.arg(source).arg(target); + FlakeLog::debug(&format!("{:?}", call.get_args())); + let output = call.output()?; + FlakeLog::debug( + &format!("{}", &String::from_utf8_lossy(&output.stdout)) + ); + FlakeLog::debug( + &format!("{}", &String::from_utf8_lossy(&output.stderr)) + ); + if !output.status.success() { + return Err(FlakeError::SyncFailed) + } + Ok(()) + } +} diff --git a/common/src/lib.rs b/common/src/lib.rs index 60bbf0e..68527ee 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -31,3 +31,4 @@ pub mod error; pub mod config; pub mod flakelog; pub mod defaults; +pub mod io; diff --git a/firecracker-pilot/src/firecracker.rs b/firecracker-pilot/src/firecracker.rs index 24e402b..54e683e 100644 --- a/firecracker-pilot/src/firecracker.rs +++ b/firecracker-pilot/src/firecracker.rs @@ -23,6 +23,7 @@ // use std::ffi::OsStr; use std::{thread, time}; +use flakes::io::IO; use flakes::command::{CommandError, handle_output, CommandExtTrait}; use flakes::error::{FlakeError, OperationError}; use flakes::user::{User, mkdir}; @@ -150,9 +151,15 @@ pub fn create(program_name: &String) -> Result<(String, String), FlakeError> { # Optional path to initrd image done by app registration initrd_path: /var/lib/firecracker/images/NAME/initrd - Calling this method returns a vector including a placeholder - for the later VM process ID and and the name of - the VM ID file. + include: + tar: + - tar-archive-file-name-to-include + path: + - file-or-directory-to-include + + Calling this method returns a vector including a placeholder + for the later VM process ID and and the name of + the VM ID file. !*/ if ! Lookup::which(defaults::FIRECRACKER) { return Err(FlakeError::IOError { @@ -274,7 +281,9 @@ fn run_creation( if Lookup::is_debug() { debug!("Syncing includes..."); } - sync_includes(&vm_mount_point, User::ROOT)?; + IO::sync_includes( + &vm_mount_point, config().tars(), config().paths(), User::ROOT + )?; } umount_vm(tmp_dir, User::ROOT)?; } @@ -822,69 +831,6 @@ pub fn delete_file(filename: &String, user: User) -> bool { true } -pub fn sync_includes( - target: &String, user: User -) -> Result<(), FlakeError> { - /*! - Sync custom include data to target path - !*/ - let tar_includes = &config().tars(); - let path_includes = &config().paths(); - - for tar in tar_includes { - if Lookup::is_debug() { - debug!("Provision tar archive: [{}]", tar); - } - let mut call = user.run("tar"); - call.arg("-C").arg(target) - .arg("-xf").arg(tar); - if Lookup::is_debug() { - debug!("{:?}", call.get_args()); - } - let output = call.perform()?; - if Lookup::is_debug() { - debug!("{}", &String::from_utf8_lossy(&output.stdout)); - debug!("{}", &String::from_utf8_lossy(&output.stderr)); - } - } - for path in path_includes { - if Lookup::is_debug() { - debug!("Provision path: [{}]", path); - } - sync_data( - path, &format!("{}/{}", target, path), - ["--mkpath"].to_vec(), user - )?; - } - Ok(()) -} - -pub fn sync_data( - source: &str, target: &str, options: Vec<&str>, user: User -) -> Result<(), FlakeError> { - /*! - Sync data from source path to target path - !*/ - let mut call = user.run("rsync"); - call.arg("-av"); - for option in options { - call.arg(option); - } - call.arg(source).arg(target); - if Lookup::is_debug() { - debug!("{:?}", call.get_args()); - } - let output = call.output()?; - if Lookup::is_debug() { - debug!("{}", &String::from_utf8_lossy(&output.stdout)); - debug!("{}", &String::from_utf8_lossy(&output.stderr)); - } - if !output.status.success() { - return Err(FlakeError::SyncFailed) - } - Ok(()) -} - pub fn mount_vm( sub_dir: &str, rootfs_image_path: &str, overlay_path: &str, user: User diff --git a/podman-pilot/src/podman.rs b/podman-pilot/src/podman.rs index 97d6d53..546a244 100644 --- a/podman-pilot/src/podman.rs +++ b/podman-pilot/src/podman.rs @@ -23,6 +23,7 @@ // use flakes::user::{User, chmod, mkdir}; use flakes::lookup::Lookup; +use flakes::io::IO; use spinoff::{Spinner, spinners, Color}; use std::path::Path; use std::process::{Command, Stdio}; @@ -106,15 +107,15 @@ pub fn create( - --rm - -ti - Calling this method returns a vector including the - container ID and and the name of the container ID - file. - include: tar: - tar-archive-file-name-to-include path: - file-or-directory-to-include + + Calling this method returns a vector including the + container ID and and the name of the container ID + file. !*/ // Read optional @NAME pilot argument to differentiate // simultaneous instances of the same container application @@ -273,7 +274,7 @@ fn run_podman_creation(mut app: Command) -> Result { } let app_mount_point = mount_container(layer, runas, true)?; update_removed_files(&app_mount_point, &removed_files)?; - sync_data( + IO::sync_data( &format!("{}/", app_mount_point), &format!("{}/", instance_mount_point), [].to_vec(), @@ -294,7 +295,9 @@ fn run_podman_creation(mut app: Command) -> Result { if Lookup::is_debug() { debug!("Syncing includes..."); } - match sync_includes(&instance_mount_point, runas) { + match IO::sync_includes( + &instance_mount_point, config().tars(), config().paths(), runas + ) { Ok(_) => { }, Err(error) => { call_instance("rm", &cid, "none", runas)?; @@ -433,69 +436,6 @@ pub fn umount_container( Ok(()) } -pub fn sync_includes( - target: &String, user: User -) -> Result<(), FlakeError> { - /*! - Sync custom include data to target path - !*/ - let tar_includes = &config().tars(); - let path_includes = &config().paths(); - - for tar in tar_includes { - if Lookup::is_debug() { - debug!("Provision tar archive: [{}]", tar); - } - let mut call = user.run("tar"); - call.arg("-C").arg(target) - .arg("-xf").arg(tar); - if Lookup::is_debug() { - debug!("{:?}", call.get_args()); - } - let output = call.perform()?; - if Lookup::is_debug() { - debug!("{}", &String::from_utf8_lossy(&output.stdout)); - debug!("{}", &String::from_utf8_lossy(&output.stderr)); - } - } - for path in path_includes { - if Lookup::is_debug() { - debug!("Provision path: [{}]", path); - } - sync_data( - path, &format!("{}/{}", target, path), - ["--mkpath"].to_vec(), user - )?; - } - Ok(()) -} - -pub fn sync_data( - source: &str, target: &str, options: Vec<&str>, user: User -) -> Result<(), FlakeError> { - /*! - Sync data from source path to target path - !*/ - let mut call = user.run("rsync"); - call.arg("-av"); - for option in options { - call.arg(option); - } - call.arg(source).arg(target); - if Lookup::is_debug() { - debug!("{:?}", call.get_args()); - } - let output = call.output()?; - if Lookup::is_debug() { - debug!("{}", &String::from_utf8_lossy(&output.stdout)); - debug!("{}", &String::from_utf8_lossy(&output.stderr)); - } - if !output.status.success() { - return Err(FlakeError::SyncFailed) - } - Ok(()) -} - pub fn sync_host( target: &String, mut removed_files: &File, user: User ) -> Result<(), FlakeError> {