Page MenuHomeDevCentral

No OneTemporary

diff --git a/src/actions.rs b/src/actions.rs
index a4f85f7..969f212 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -1,111 +1,111 @@
// -------------------------------------------------------------
// Alkane :: Actions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// -------------------------------------------------------------
use crate::command::ServerArgs;
use crate::config::AlkaneConfig;
use crate::db::Database;
use crate::deploy::AlkaneDeployError;
use crate::deploy::DeployError;
use crate::runner::store::RecipesStore;
use crate::runner::RecipeStatus;
use crate::server::kernel::run;
// -------------------------------------------------------------
// Actions only available in CLI
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pub fn serve(args: ServerArgs, config: AlkaneConfig) {
run(config, &args.mounting_point);
}
// -------------------------------------------------------------
// Actions available both for CLI and HTTP
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fn run_deployment_action(
site_name: &str,
context: Option<String>,
config: &AlkaneConfig,
action: &str,
) -> Result<RecipeStatus, DeployError> {
let db = Database::from_config(config).ok_or_else(|| {
let error = AlkaneDeployError::new("Can't initialize database", site_name, action);
DeployError::Alkane(error)
})?;
let recipes = RecipesStore::from_config(config).ok_or_else(|| {
let error = AlkaneDeployError::new("Can't initialize recipes store", site_name, action);
DeployError::Alkane(error)
})?;
let site = config
.get_site(site_name, context)
.ok_or_else(|| {
let error = AlkaneDeployError::new("Can't resolve site path", site_name, action);
DeployError::Alkane(error)
})?;
let status = recipes.run_recipe(&site, action);
- if action == "init" {
+ if action == "init" && status == RecipeStatus::Success {
db.set_initialized(&site.name);
}
Ok(status)
}
pub fn initialize(
site_name: &str,
context: Option<String>,
config: &AlkaneConfig,
) -> Result<RecipeStatus, DeployError> {
run_deployment_action(site_name, context, config, "init")
}
pub fn update(
site_name: &str,
context: Option<String>,
config: &AlkaneConfig,
) -> Result<RecipeStatus, DeployError> {
run_deployment_action(site_name, context, config, "update")
}
pub fn deploy(
site_name: &str,
context: Option<String>,
config: &AlkaneConfig,
) -> Result<RecipeStatus, DeployError> {
if is_present(site_name, config) {
run_deployment_action(site_name, context, config, "update")
} else {
run_deployment_action(site_name, context, config, "init")
}
}
pub fn is_present(site_name: &str, config: &AlkaneConfig) -> bool {
match Database::from_config(&config) {
None => false,
Some(db) => db.is_initialized(site_name),
}
}
// -------------------------------------------------------------
// Tests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_is_present() {
let config = AlkaneConfig::load().unwrap();
assert_eq!(true, is_present("foo.acme.tld", &config));
assert_eq!(false, is_present("notexisting.acme.tld", &config));
}
}
diff --git a/src/runner/mod.rs b/src/runner/mod.rs
index 2d54e2c..9ad68ce 100644
--- a/src/runner/mod.rs
+++ b/src/runner/mod.rs
@@ -1,108 +1,108 @@
// -------------------------------------------------------------
// Alkane :: Runner
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// Description: Run a recipe to initialize or update a site
// -------------------------------------------------------------
use std::ffi::OsStr;
use std::fmt::{Debug, Display};
use std::process::Command;
use log::{error, info, warn};
use serde::Serialize;
// -------------------------------------------------------------
// Modules
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pub mod site;
pub mod store;
// -------------------------------------------------------------
// Exit status of a recipe.
//
// The executable called to build the site should use
// those exit code inspired by the Nagios one.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-#[derive(Debug, Serialize)]
+#[derive(Debug, Serialize, PartialEq)]
pub enum RecipeStatus {
Success,
Warning,
Error,
Unknown,
}
impl RecipeStatus {
pub fn from_status_code(code: i32) -> Self {
match code {
0 => RecipeStatus::Success,
1 => RecipeStatus::Warning,
2 => RecipeStatus::Error,
_ => RecipeStatus::Unknown,
}
}
pub fn to_status_code(&self) -> i32 {
match self {
RecipeStatus::Success => 0,
RecipeStatus::Warning => 1,
RecipeStatus::Error => 2,
RecipeStatus::Unknown => 3,
}
}
}
// -------------------------------------------------------------
// Run an executable, returns the recipe status
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pub fn run<E, I, S>(command: S, args: I, environment: E) -> RecipeStatus
where
E: IntoIterator<Item = (S, S)>,
I: IntoIterator<Item = S> + Debug,
S: AsRef<OsStr> + Display,
{
info!("Running command {} with args {:?}", command, args);
let result = Command::new(command)
.args(args)
.envs(environment)
.output();
match result {
Ok(process_output) => {
let stdout = read_bytes(&process_output.stdout);
let stderr = read_bytes(&process_output.stderr);
if !stdout.is_empty() {
info!("Channel stdout: {}", stdout);
}
if !stderr.is_empty() {
warn!("Channel stderr: {}", stderr);
}
match process_output.status.code() {
None => {
warn!("Process terminated by signal.");
RecipeStatus::Unknown
}
Some(code) => RecipeStatus::from_status_code(code),
}
}
Err(error) => {
error!("Process can't spawn: {:?}", error);
RecipeStatus::Error
}
}
}
fn read_bytes(bytes: &Vec<u8>) -> String {
String::from_utf8_lossy(bytes).to_string()
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 25, 07:07 (1 d, 14 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2256316
Default Alt Text
(6 KB)

Event Timeline