Skip to content

Commit

Permalink
Consolidate common code to IO mod
Browse files Browse the repository at this point in the history
  • Loading branch information
schaefi committed Sep 14, 2023
1 parent 78fd9fa commit 47956ef
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 136 deletions.
89 changes: 89 additions & 0 deletions common/src/io.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ pub mod error;
pub mod config;
pub mod flakelog;
pub mod defaults;
pub mod io;
80 changes: 13 additions & 67 deletions firecracker-pilot/src/firecracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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
Expand Down
78 changes: 9 additions & 69 deletions podman-pilot/src/podman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -273,7 +274,7 @@ fn run_podman_creation(mut app: Command) -> Result<String, FlakeError> {
}
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(),
Expand All @@ -294,7 +295,9 @@ fn run_podman_creation(mut app: Command) -> Result<String, FlakeError> {
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)?;
Expand Down Expand Up @@ -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> {
Expand Down

0 comments on commit 47956ef

Please sign in to comment.