Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3769750
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/runner/mod.rs b/src/runner/mod.rs
index 859ca65..e0ad39b 100644
--- a/src/runner/mod.rs
+++ b/src/runner/mod.rs
@@ -1,108 +1,110 @@
// -------------------------------------------------------------
// 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, Stdio};
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)]
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)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output();
match result {
Ok(process_output) => {
let stdout = read_bytes(&process_output.stdout);
let stderr = read_bytes(&process_output.stdout);
if !stdout.is_empty() {
info!("Channel stdout: {}", stdout);
}
if !stderr.is_empty() {
warn!("Channel stderr: {}", stdout);
}
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()
}
diff --git a/src/server/requests.rs b/src/server/requests.rs
index 7d43ccc..a49f48a 100644
--- a/src/server/requests.rs
+++ b/src/server/requests.rs
@@ -1,107 +1,108 @@
// -------------------------------------------------------------
// Alkane :: Server :: Requests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// -------------------------------------------------------------
use limiting_factor::api::replies::{ApiJsonResponse, ApiResponse};
use log::{debug, info, warn};
use rocket::State;
use rocket_codegen::{get, post};
use crate::actions;
use crate::config::AlkaneConfig;
+use crate::runner::RecipeStatus;
// -------------------------------------------------------------
// Monitoring
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[get("/status")]
pub fn status() -> &'static str {
"ALIVE"
}
// -------------------------------------------------------------
// Alkane requests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[get("/is_present/<site_name>")]
pub fn is_present(site_name: String, config: State<AlkaneConfig>) -> ApiJsonResponse<bool> {
actions::is_present(&site_name, &config).into_json_response()
}
#[post("/init/<site_name>", data = "<context>")]
pub fn init(
site_name: String,
context: String,
config: State<AlkaneConfig>,
-) -> ApiJsonResponse<bool> {
+) -> ApiJsonResponse<RecipeStatus> {
info!("Deploying {}", &site_name);
let context = if context.is_empty() {
None
} else {
Some(context)
};
debug!("Context: {:?}", &context);
match actions::initialize(&site_name, context, &config) {
- Ok(_) => true.into_json_response(),
+ Ok(status) => status.into_json_response(),
Err(error) => {
warn!("Deployment error: {:?}", error);
- false.into_json_response()
+ RecipeStatus::Error.into_json_response()
}
}
}
#[post("/update/<site_name>", data = "<context>")]
pub fn update(
site_name: String,
context: String,
config: State<AlkaneConfig>,
-) -> ApiJsonResponse<bool> {
+) -> ApiJsonResponse<RecipeStatus> {
info!("Deploying {}", &site_name);
let context = if context.is_empty() {
None
} else {
Some(context)
};
debug!("Context: {:?}", &context);
match actions::update(&site_name, context, &config) {
- Ok(_) => true.into_json_response(),
+ Ok(status) => status.into_json_response(),
Err(error) => {
warn!("Deployment error: {:?}", error);
- false.into_json_response()
+ RecipeStatus::Error.into_json_response()
}
}
}
#[post("/deploy/<site_name>", data = "<context>")]
pub fn deploy(
site_name: String,
context: String,
config: State<AlkaneConfig>,
-) -> ApiJsonResponse<bool> {
+) -> ApiJsonResponse<RecipeStatus> {
info!("Deploying {}", &site_name);
let context = if context.is_empty() {
None
} else {
Some(context)
};
debug!("Context: {:?}", &context);
match actions::deploy(&site_name, context, &config) {
- Ok(_) => true.into_json_response(),
+ Ok(status) => status.into_json_response(),
Err(error) => {
warn!("Deployment error: {:?}", error);
- false.into_json_response()
+ RecipeStatus::Error.into_json_response()
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 16:46 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260588
Default Alt Text
(6 KB)
Attached To
Mode
rALK Alkane
Attached
Detach File
Event Timeline
Log In to Comment