mirror of
https://github.com/fluencelabs/marine.git
synced 2024-12-12 14:55:32 +00:00
Do not throw error if service_base_dir already exists (#18)
This commit is contained in:
parent
a670ec76cd
commit
22d6c6b6c8
@ -18,17 +18,22 @@ use fluence_faas::FaaSError;
|
|||||||
|
|
||||||
use std::io::Error as IOError;
|
use std::io::Error as IOError;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum AppServiceError {
|
pub enum AppServiceError {
|
||||||
/// An error related to config parsing.
|
/// An error related to config parsing.
|
||||||
InvalidConfig(String),
|
InvalidConfig(String),
|
||||||
|
|
||||||
/// Various errors related to file i/o.
|
|
||||||
IOError(String),
|
|
||||||
|
|
||||||
/// FaaS errors.
|
/// FaaS errors.
|
||||||
FaaSError(FaaSError),
|
FaaSError(FaaSError),
|
||||||
|
|
||||||
|
/// Directory creation failed
|
||||||
|
CreateDir { err: IOError, path: PathBuf },
|
||||||
|
|
||||||
|
/// Base service dir wasn't specified in config
|
||||||
|
/// TODO: do we need that dir to be optional?
|
||||||
|
MissingServiceDir,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for AppServiceError {}
|
impl Error for AppServiceError {}
|
||||||
@ -37,15 +42,14 @@ impl std::fmt::Display for AppServiceError {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||||
match self {
|
match self {
|
||||||
AppServiceError::InvalidConfig(err_msg) => write!(f, "{}", err_msg),
|
AppServiceError::InvalidConfig(err_msg) => write!(f, "{}", err_msg),
|
||||||
AppServiceError::IOError(err_msg) => write!(f, "{}", err_msg),
|
|
||||||
AppServiceError::FaaSError(err) => write!(f, "{}", err),
|
AppServiceError::FaaSError(err) => write!(f, "{}", err),
|
||||||
|
AppServiceError::CreateDir { err, path } => {
|
||||||
|
write!(f, "Failed to create dir {:?}: {:?}", path, err)
|
||||||
|
}
|
||||||
|
AppServiceError::MissingServiceDir => {
|
||||||
|
write!(f, "service base dir should be specified in config")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl From<IOError> for AppServiceError {
|
|
||||||
fn from(err: IOError) -> Self {
|
|
||||||
AppServiceError::IOError(format!("{}", err))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ use fluence_faas::FluenceFaaS;
|
|||||||
use fluence_faas::ModulesConfig;
|
use fluence_faas::ModulesConfig;
|
||||||
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::io::ErrorKind;
|
||||||
|
|
||||||
const SERVICE_ID_ENV_NAME: &str = "service_id";
|
const SERVICE_ID_ENV_NAME: &str = "service_id";
|
||||||
const SERVICE_LOCAL_DIR_NAME: &str = "local";
|
const SERVICE_LOCAL_DIR_NAME: &str = "local";
|
||||||
@ -81,26 +83,32 @@ impl AppService {
|
|||||||
service_id: &str,
|
service_id: &str,
|
||||||
mut envs: Vec<String>,
|
mut envs: Vec<String>,
|
||||||
) -> Result<ModulesConfig> {
|
) -> Result<ModulesConfig> {
|
||||||
let base_dir = match config.service_base_dir.as_ref() {
|
let base_dir: &Path = config
|
||||||
Some(base_dir) => base_dir,
|
.service_base_dir
|
||||||
_ => {
|
.as_ref()
|
||||||
return Err(AppServiceError::IOError(String::from(
|
.ok_or(AppServiceError::MissingServiceDir)?
|
||||||
"service_base_dir should be specified",
|
.as_ref();
|
||||||
)))
|
|
||||||
}
|
let create = |dir: &PathBuf| match std::fs::create_dir(dir) {
|
||||||
|
Err(e) if e.kind() == ErrorKind::AlreadyExists => Ok(()),
|
||||||
|
Err(err) => Err(AppServiceError::CreateDir {
|
||||||
|
err,
|
||||||
|
path: dir.clone(),
|
||||||
|
}),
|
||||||
|
_ => Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let service_dir_path = std::path::Path::new(base_dir).join(service_id);
|
let service_dir = base_dir.join(service_id);
|
||||||
std::fs::create_dir(service_dir_path.clone())?; // will return an error if dir is already exists
|
create(&service_dir)?;
|
||||||
|
|
||||||
let local_dir_path = service_dir_path.join(SERVICE_LOCAL_DIR_NAME);
|
let local_dir = service_dir.join(SERVICE_LOCAL_DIR_NAME);
|
||||||
std::fs::create_dir(local_dir_path.clone())?; // will return an error if dir is already exists
|
create(&local_dir)?;
|
||||||
|
|
||||||
let tmp_dir_path = service_dir_path.join(SERVICE_TMP_DIR_NAME);
|
let tmp_dir = service_dir.join(SERVICE_TMP_DIR_NAME);
|
||||||
std::fs::create_dir(tmp_dir_path.clone())?; // will return an error if dir is already exists
|
create(&tmp_dir)?;
|
||||||
|
|
||||||
let local_dir: String = local_dir_path.to_string_lossy().into();
|
let local_dir = local_dir.to_string_lossy().to_string();
|
||||||
let tmp_dir: String = tmp_dir_path.to_string_lossy().into();
|
let tmp_dir = tmp_dir.to_string_lossy().to_string();
|
||||||
|
|
||||||
let preopened_files = vec![local_dir.clone(), tmp_dir.clone()];
|
let preopened_files = vec![local_dir.clone(), tmp_dir.clone()];
|
||||||
let mapped_dirs = vec![
|
let mapped_dirs = vec![
|
||||||
|
@ -133,6 +133,7 @@ pub struct RawWASIConfig {
|
|||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct ModulesConfig {
|
pub struct ModulesConfig {
|
||||||
/// Used for preparing filesystem on the service initialization stage.
|
/// Used for preparing filesystem on the service initialization stage.
|
||||||
|
/// TODO: do we need that dir to be optional? We require it on creation anyways
|
||||||
pub service_base_dir: Option<String>,
|
pub service_base_dir: Option<String>,
|
||||||
|
|
||||||
/// Path to a dir where compiled Wasm modules are located.
|
/// Path to a dir where compiled Wasm modules are located.
|
||||||
|
Loading…
Reference in New Issue
Block a user