Skip to content

Commit

Permalink
allow definition of environment to run in
Browse files Browse the repository at this point in the history
  • Loading branch information
derklaro committed Feb 28, 2024
1 parent 0425f9a commit de59e5a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
17 changes: 16 additions & 1 deletion src/entity/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use std::path::PathBuf;

use clap::Parser;

#[derive(Debug, Clone)]
pub(crate) struct Symlink {
pub link_name: String,
Expand Down Expand Up @@ -61,6 +62,8 @@ pub(crate) struct Options {
default_value = "REVISION"
)]
pub git_revision_file: String,
#[arg(long = "environment", env = "EASYDEP_ENV", default_value = "")]
pub environment: String,
// parsed internally, not exposed
#[arg(
long = "symlinks",
Expand All @@ -85,4 +88,16 @@ impl Options {
})
.collect()
}

pub fn prod_environment(&self) -> bool {
self.environment.is_empty() || self.environment == "prod"
}

pub fn environment_suffix(&self) -> String {
if self.environment.is_empty() || self.environment == "prod" {
String::from("")
} else {
format!("-{}", self.environment.clone())
}
}
}
2 changes: 1 addition & 1 deletion src/handler/finish_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) async fn finish_deployment(
let deploy_base_dir = info.base_directory();
let result = internal_finish_deployment(options, info).await;
let finish_script_result =
call_followup_lifecycle_script(&deploy_base_dir, "publish", result).await;
call_followup_lifecycle_script(options, &deploy_base_dir, "publish", result).await;

// cleanup (by removing the oldest release)
info!("Published one release, trying to discord the oldest release");
Expand Down
20 changes: 14 additions & 6 deletions src/handler/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@ pub(crate) async fn read_latest_release(
let installation = find_installation(&octocrab, options).await?;
let app_scoped_octocrab = octocrab.installation(installation.id);

// list the last 10 releases and find the latest non-draft one
// list the last 100 releases and find the latest for the current env
let repo_handler =
app_scoped_octocrab.repos(&options.github_repo_org, &options.github_repo_name);
let last_releases = repo_handler
.releases()
.list()
.per_page(10)
.per_page(100)
.send()
.await?
.items;
let mut non_draft_releases: Vec<Release> = last_releases
let mut possible_releases: Vec<Release> = last_releases
.into_iter()
.filter(|release| !release.prerelease && !release.draft)
.filter(|release| !release.draft)
.filter(|release| {
// pre-release + prod -> false
// pre-release + staging -> true
// release + prod -> true
// release + staging -> false
let prod = options.prod_environment();
release.prerelease != prod
})
.collect();

// sort the releases by id, descending
non_draft_releases.sort_by(|left, right| right.id.cmp(&left.id));
Ok(non_draft_releases.first().cloned())
possible_releases.sort_by(|left, right| right.id.cmp(&left.id));
Ok(possible_releases.first().cloned())
}

async fn authenticated_github_client(options: &Options) -> anyhow::Result<Octocrab, anyhow::Error> {
Expand Down
7 changes: 4 additions & 3 deletions src/handler/init_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) async fn init_deployment(
) -> anyhow::Result<Vec<CommandResult>, anyhow::Error> {
let deploy_base_dir = info.base_directory();
let result = internal_init_deployment(options, info).await;
call_and_aggregate_lifecycle_script(&deploy_base_dir, "init", result).await
call_and_aggregate_lifecycle_script(options, &deploy_base_dir, "init", result).await
}

async fn internal_init_deployment(
Expand Down Expand Up @@ -162,11 +162,12 @@ async fn internal_init_deployment(
"Executing deployment script in {:?} ({})",
deploy_repo_dir, info.tag_name
);
let deploy_script_path = deploy_repo_dir.join(".easydep").join("execute.sh");
let script_dir = format!(".easydep{}", options.environment_suffix());
let deploy_script_path = deploy_repo_dir.join(&script_dir).join("execute.sh");
if deploy_script_path.exists() {
let mut script_execute_command = Command::new("bash");
script_execute_command
.arg(".easydep/execute.sh")
.arg(format!("{}/execute.sh", script_dir))
.current_dir(deploy_repo_dir);
command_results.push(run_command(script_execute_command).await?);
}
Expand Down
24 changes: 15 additions & 9 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::fmt::Debug;
use std::path::PathBuf;
use std::process::Command;

use crate::entity::options::Options;
use crate::helper::process_helper::{run_command, CommandResult};

pub(crate) mod cancel_handler;
pub(crate) mod finish_handler;
pub(crate) mod github;
pub(crate) mod init_handler;
pub(crate) mod initial_handler;
pub(crate) mod release_discard;

use crate::helper::process_helper::{run_command, CommandResult};
use std::fmt::Debug;
use std::path::PathBuf;
use std::process::Command;

#[derive(PartialEq, Debug, Clone)]
pub(crate) enum LifecycleState {
Success,
Expand All @@ -27,26 +29,28 @@ impl LifecycleState {
}

pub(crate) async fn call_followup_lifecycle_script<T: Debug>(
options: &Options,
deploy_base_directory: &PathBuf,
lifecycle_event_name: &str,
previous_result: anyhow::Result<T, anyhow::Error>,
) -> anyhow::Result<Option<CommandResult>, anyhow::Error> {
let state = LifecycleState::from_result(&previous_result);
let command_result =
call_lifecycle_script(deploy_base_directory, lifecycle_event_name, state).await?;
call_lifecycle_script(options, deploy_base_directory, lifecycle_event_name, state).await?;

previous_result?;
Ok(command_result)
}

pub(crate) async fn call_and_aggregate_lifecycle_script(
options: &Options,
deploy_base_directory: &PathBuf,
lifecycle_event_name: &str,
previous_result: Result<Vec<CommandResult>, anyhow::Error>,
) -> anyhow::Result<Vec<CommandResult>, anyhow::Error> {
let state = LifecycleState::from_result(&previous_result);
let command_result =
call_lifecycle_script(deploy_base_directory, lifecycle_event_name, state).await?;
call_lifecycle_script(options, deploy_base_directory, lifecycle_event_name, state).await?;

// return the previous result if there was an error
#[allow(clippy::question_mark)]
Expand All @@ -64,17 +68,19 @@ pub(crate) async fn call_and_aggregate_lifecycle_script(
}

pub(crate) async fn call_lifecycle_script(
options: &Options,
deploy_base_directory: &PathBuf,
lifecycle_event_name: &str,
state: LifecycleState,
) -> anyhow::Result<Option<CommandResult>, anyhow::Error> {
// resolve the target script path
let script_dir = format!(".easydep{}", options.environment_suffix());
let script_name = format!("{}_{:?}.sh", lifecycle_event_name, state).to_lowercase();
let script_path = deploy_base_directory.join(".easydep").join(&script_name);
let script_path = deploy_base_directory.join(&script_dir).join(&script_name);

// run the script if it exists
if script_path.exists() {
let script_name_for_command = format!(".easydep/{}", script_name);
let script_name_for_command = format!("{}/{}", script_dir, script_name);
let mut script_command = Command::new("bash");
script_command
.arg(script_name_for_command)
Expand Down

0 comments on commit de59e5a

Please sign in to comment.