Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3766683
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
9 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/actions.rs b/src/actions.rs
index 969f212..e23d046 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -1,111 +1,109 @@
// -------------------------------------------------------------
// 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 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" && 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/deploy.rs b/src/deploy.rs
index f5022f3..611bbc7 100644
--- a/src/deploy.rs
+++ b/src/deploy.rs
@@ -1,63 +1,67 @@
// -------------------------------------------------------------
// Alkane :: Deploy
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// -------------------------------------------------------------
use std::error::Error;
use std::fmt::{Display, Formatter};
// -------------------------------------------------------------
// Errors during our own workflow deployment
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/// Represents an error during the workflow to run a deployment
#[derive(Debug)]
pub struct AlkaneDeployError {
pub message: String,
/// The name of the site to deploy
pub site_name: String,
/// The deployment action, "init" or "update"
pub action: String,
}
impl Display for AlkaneDeployError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- write!(f, "Can't run deployment action '{}' for site '{}': {}", self.action, self.site_name, self.message)
+ write!(
+ f,
+ "Can't run deployment action '{}' for site '{}': {}",
+ self.action, self.site_name, self.message
+ )
}
}
impl Error for AlkaneDeployError {}
impl AlkaneDeployError {
pub fn new<S>(message: S, site_name: S, action: S) -> Self
where
S: AsRef<str>,
{
Self {
message: message.as_ref().to_string(),
site_name: site_name.as_ref().to_string(),
action: action.as_ref().to_string(),
}
}
}
// -------------------------------------------------------------
// Errors that can occur during a deployment
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[derive(Debug)]
pub enum DeployError {
Alkane(AlkaneDeployError),
}
impl Display for DeployError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
DeployError::Alkane(error) => error.fmt(f),
}
}
}
diff --git a/src/runner/mod.rs b/src/runner/mod.rs
index 9ad68ce..4e602e1 100644
--- a/src/runner/mod.rs
+++ b/src/runner/mod.rs
@@ -1,108 +1,105 @@
// -------------------------------------------------------------
// 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, 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();
+ 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
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Nov 24, 19:36 (2 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258778
Default Alt Text
(9 KB)
Attached To
Mode
rALK Alkane
Attached
Detach File
Event Timeline
Log In to Comment