add retrun err on transaction, refactor the cmd terraform

This commit is contained in:
marc
2020-11-23 17:40:25 +01:00
parent d7dff20a4a
commit bfd9fb47fa
2 changed files with 71 additions and 61 deletions

View File

@@ -1,83 +1,93 @@
use dirs::home_dir;
use crate::cmd::utilities::exec_with_envs_and_output;
use crate::constants::{TF_PLUGIN_CACHE_DIR};
use crate::error::{SimpleError};
use crate::constants::TF_PLUGIN_CACHE_DIR;
use crate::error::{SimpleError, SimpleErrorKind};
use retry::delay::Fixed;
use retry::OperationResult;
// fn terraform_exec_with_init_validate(root_dir: &str) -> Result<(), SimpleError> {
// // terraform init
// let result = retry::retry(Fixed::from_millis(3000).take(5), || {
// let try_result = terraform_exec(root_dir, vec!["init"]);
// match try_result {
// Ok(out) => OperationResult::Ok(out),
// Err(err) => OperationResult::Err(format!("Command error: {:?}", err)),
// }
// });
//
// match result {
// Err(err) => match err {
// retry::Error::Operation {
// error: _,
// total_delay: _,
// tries: _,
// } => Ok(Some(false)),
// retry::Error::Internal(err) => Err(SimpleError::new(SimpleErrorKind::Other, Some(err))),
// },
// Ok(_) => Ok(Some(true)),
// };
//
// // terraform validate config
// terraform_exec(root_dir, vec!["validate"])?;
//
// Ok(())
// }
fn terraform_exec_with_init_validate_plan(
root_dir: &str,
first_time_init_terraform: bool,
) -> Result<(), SimpleError> {
fn terraform_exec_with_init_validate_plan(root_dir: &str) -> Result<(), SimpleError> {
// terraform init
let init_args = if first_time_init_terraform {
vec!["init"]
} else {
vec!["init"]
let result = retry::retry(Fixed::from_millis(3000).take(5), || {
let try_result = terraform_exec(root_dir, vec!["init"]);
match try_result {
Ok(out) => OperationResult::Ok(out),
Err(err) => OperationResult::Err(format!("Command error: {:?}", err)),
}
});
match result {
Err(err) => match err {
retry::Error::Operation {
error: _,
total_delay: _,
tries: _,
} => Ok(Some(false)),
retry::Error::Internal(err) => Err(SimpleError::new(SimpleErrorKind::Other, Some(err))),
},
Ok(_) => Ok(Some(true)),
};
//TODO print
terraform_exec(root_dir, init_args)?;
// terraform validate config
terraform_exec(root_dir, vec!["validate"])?;
// terraform plan
terraform_exec(root_dir, vec!["plan", "-out", "tf_plan"])?;
match terraform_exec(root_dir, vec!["validate"]) {
Err(e) => {
error!("While trying to Terraform validate the rendered templates");
return Err(e);
}
_ => {
match terraform_exec(root_dir, vec!["plan", "-out", "tf_plan"]) {
Err(e) => {
error!("While trying to Terraform plan the rendered templates");
return Err(e);
}
Ok(rs) => {}
};
}
};
Ok(())
}
pub fn terraform_exec_with_init_validate_plan_apply(
root_dir: &str,
first_time_init_terraform: bool,
dry_run: bool,
) -> Result<(), SimpleError> {
// terraform init and plan
terraform_exec_with_init_validate_plan(root_dir, first_time_init_terraform);
// terraform apply
if !dry_run {
terraform_exec(root_dir, vec!["apply", "-auto-approve", "tf_plan"])?;
}
match terraform_exec_with_init_validate_plan(root_dir) {
Ok(_) => match dry_run {
true => {
warn!("dry run flag is true, no terraform apply will happens");
}
false => match terraform_exec(root_dir, vec!["apply", "-auto-approve", "tf_plan"]) {
Ok(_) => {}
Err(e) => {
error!("While trying to Terraform apply the rendered templates");
return Err(e);
}
},
},
Err(e) => return Err(e),
};
Ok(())
}
pub fn terraform_exec_with_init_plan_apply_destroy(root_dir: &str) -> Result<(), SimpleError> {
// terraform init and plan
// should apply before destroy to be sure destroy will compute on all ressources
terraform_exec_with_init_validate_plan_apply(root_dir, false, false);
match terraform_exec_with_init_validate_plan_apply(root_dir, false) {
Ok(_) => {}
Err(e) => {
return Err(e);
}
}
// terraform destroy
terraform_exec(root_dir, vec!["destroy", "-auto-approve"])
match terraform_exec(root_dir, vec!["destroy", "-auto-approve"]) {
Ok(_) => {}
Err(e) => {
error!("While trying to Terraform destroy the rendered templates");
return Err(e);
}
};
Ok(())
}
pub fn terraform_exec(root_dir: &str, args: Vec<&str>) -> Result<(), SimpleError> {

View File

@@ -437,7 +437,7 @@ impl<'a> Transaction<'a> {
Ok(_) => TransactionResult::Rollback(err),
Err(e) => {
error!("ROLLBACK FAILED! fatal error: {:?}", e);
TransactionResult::UnrecoverableError(err, e)
return TransactionResult::UnrecoverableError(err, e);
}
}
}
@@ -453,7 +453,7 @@ impl<'a> Transaction<'a> {
Ok(_) => TransactionResult::Rollback(err),
Err(e) => {
error!("ROLLBACK FAILED! fatal error: {:?}", e);
TransactionResult::UnrecoverableError(err, e)
return TransactionResult::UnrecoverableError(err, e);
}
}
}
@@ -488,7 +488,7 @@ impl<'a> Transaction<'a> {
Ok(_) => TransactionResult::Rollback(commit_error),
Err(err) => {
error!("ROLLBACK FAILED! fatal error: {:?}", err);
TransactionResult::UnrecoverableError(commit_error, err)
return TransactionResult::UnrecoverableError(commit_error, err);
}
};
}