Do not throw error if service_base_dir already exists (#18)

This commit is contained in:
folex 2020-08-18 22:58:41 +03:00 committed by GitHub
parent a670ec76cd
commit 22d6c6b6c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 25 deletions

View File

@ -18,17 +18,22 @@ use fluence_faas::FaaSError;
use std::io::Error as IOError;
use std::error::Error;
use std::path::PathBuf;
#[derive(Debug)]
pub enum AppServiceError {
/// An error related to config parsing.
InvalidConfig(String),
/// Various errors related to file i/o.
IOError(String),
/// FaaS errors.
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 {}
@ -37,18 +42,17 @@ impl std::fmt::Display for AppServiceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
AppServiceError::InvalidConfig(err_msg) => write!(f, "{}", err_msg),
AppServiceError::IOError(err_msg) => write!(f, "{}", err_msg),
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))
}
}
impl From<FaaSError> for AppServiceError {
fn from(err: FaaSError) -> Self {
AppServiceError::FaaSError(err)

View File

@ -22,6 +22,8 @@ use fluence_faas::FluenceFaaS;
use fluence_faas::ModulesConfig;
use std::convert::TryInto;
use std::path::{Path, PathBuf};
use std::io::ErrorKind;
const SERVICE_ID_ENV_NAME: &str = "service_id";
const SERVICE_LOCAL_DIR_NAME: &str = "local";
@ -81,26 +83,32 @@ impl AppService {
service_id: &str,
mut envs: Vec<String>,
) -> Result<ModulesConfig> {
let base_dir = match config.service_base_dir.as_ref() {
Some(base_dir) => base_dir,
_ => {
return Err(AppServiceError::IOError(String::from(
"service_base_dir should be specified",
)))
}
let base_dir: &Path = config
.service_base_dir
.as_ref()
.ok_or(AppServiceError::MissingServiceDir)?
.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);
std::fs::create_dir(service_dir_path.clone())?; // will return an error if dir is already exists
let service_dir = base_dir.join(service_id);
create(&service_dir)?;
let local_dir_path = service_dir_path.join(SERVICE_LOCAL_DIR_NAME);
std::fs::create_dir(local_dir_path.clone())?; // will return an error if dir is already exists
let local_dir = service_dir.join(SERVICE_LOCAL_DIR_NAME);
create(&local_dir)?;
let tmp_dir_path = service_dir_path.join(SERVICE_TMP_DIR_NAME);
std::fs::create_dir(tmp_dir_path.clone())?; // will return an error if dir is already exists
let tmp_dir = service_dir.join(SERVICE_TMP_DIR_NAME);
create(&tmp_dir)?;
let local_dir: String = local_dir_path.to_string_lossy().into();
let tmp_dir: String = tmp_dir_path.to_string_lossy().into();
let local_dir = local_dir.to_string_lossy().to_string();
let tmp_dir = tmp_dir.to_string_lossy().to_string();
let preopened_files = vec![local_dir.clone(), tmp_dir.clone()];
let mapped_dirs = vec![

View File

@ -133,6 +133,7 @@ pub struct RawWASIConfig {
#[derive(Debug, Clone, Default)]
pub struct ModulesConfig {
/// 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>,
/// Path to a dir where compiled Wasm modules are located.