-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
2,639 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env bash ; | ||
|
||
read -p "Enter Target-Profile [ e.g.: default ] ?: " aws_default_profile ; | ||
read -p "Enter Target-Region [ e.g.: us-east-1 ] ?: " aws_default_region ; | ||
|
||
export terraform_restore="{{ console.Restore_Folder }}" ; | ||
|
||
mkdir -p ${terraform_restore} ; | ||
cd ${terraform_restore} ; | ||
|
||
git clone {{ console.Remote_Origin }}.git ${terraform_restore} ; | ||
git checkout -b restore {{ console.Commit_SHAID }} ; | ||
|
||
[[ -d {{ github.workspace }} ]] && export HOME="{{ github.workspace }}" ; | ||
cp -pr ${HOME}/.ssh ${terraform_restore} ; | ||
|
||
export AWS_PROFILE="${aws_default_profile}"; | ||
export AWS_DEFAULT_REGION="${aws_default_region}"; | ||
|
||
echo -e "\nFetching Terraform components ... \n"; | ||
aws --profile ${AWS_PROFILE} \ | ||
--region ${AWS_DEFAULT_REGION} \ | ||
s3 cp s3://{{ console.S3Bucket_Name }}/{{ console.Remote_Path }} \ | ||
${terraform_restore} \ | ||
--recursive \ | ||
; | ||
|
||
echo -e "\nDisplaying Terraform file-structure ...\n" ; | ||
tree -FCla --prune -I .git $(pwd) ; | ||
|
||
if [[ -f ${terraform_restore}/terraform.tfstate.d/dev/terraform.tfplan ]]; then | ||
echo -e "\nInitializing Terraform ... \n" ; | ||
eval {{ console.Verbosity }} \ | ||
terraform init ; echo -e ; | ||
echo -e "\nTerraform Create|Select Workspace [{{ console.Target_Workspace }}] ... \n" ; | ||
eval {{ console.Verbosity }} \ | ||
terraform workspace select {{ console.Target_Workspace }} || terraform workspace new {{ console.Target_Workspace }} ; | ||
echo -e "\nTerraform Listing Workspaces ... \n" ; | ||
eval {{ console.Verbosity }} \ | ||
terraform workspace list ; | ||
echo -e "\nExecuting Terraform ..." ; | ||
eval {{ console.Verbosity }} \ | ||
TF_VAR_region=${AWS_DEFAULT_REGION} \ | ||
terraform {{ console.Terraform_Action }} ; | ||
else echo -e "\nWarning: Unable to download Terraform components! \n" ; | ||
exit 1 ; | ||
fi ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
name: GitHub Actions - Terraform Controller | ||
on: | ||
|
||
####---------------------------------------------------------------------------- | ||
workflow_dispatch: | ||
name: Manual Deployment | ||
description: 'Triggering Manual Deployment' | ||
inputs: | ||
accesskey: | ||
description: 'Target Access Key-ID' | ||
required: false | ||
default: '' | ||
account: | ||
description: 'Target AWS Account' | ||
required: false | ||
default: '' | ||
destroy-terraform: | ||
description: 'Terraform Destroy Request' | ||
required: false | ||
default: true | ||
keypair-name: | ||
description: 'Private Key-Pair Name' | ||
required: false | ||
default: '' | ||
keypair-secret: | ||
description: 'Private Key-Pair Secret' | ||
required: false | ||
default: '' | ||
region: | ||
description: 'Target AWS Region' | ||
required: false | ||
default: '' | ||
secretkey: | ||
description: 'Target Secret Access-Key' | ||
required: false | ||
default: '' | ||
workspace: | ||
description: 'Terraform Workspace' | ||
required: false | ||
default: 'dev' | ||
####---------------------------------------------------------------------- | ||
# logLevel: | ||
# description: 'Log level' | ||
# required: true | ||
# default: 'warning' | ||
# tags: | ||
# description: 'Terraform Controller' | ||
####---------------------------------------------------------------------------- | ||
push: | ||
branches: [ master ] | ||
paths: | ||
- action.yaml | ||
####---------------------------------------------------------------------------- | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_DEFAULT_ACCOUNT: 738054984624 ## ${{ secrets.AWS_DEFAULT_ACCOUNT }} | ||
AWS_DEFAULT_PROFILE: default ## ${{ secrets.AWS_DEFAULT_PROFILE }} | ||
AWS_DEFAULT_REGION: us-east-1 ## ${{ secrets.AWS_DEFAULT_REGION }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
## Terraform Operations: Deploy, Destroy | ||
BACKUP_TERRAFORM: ${{ secrets.BACKUP_TERRAFORM }} | ||
DEPLOY_TERRAFORM: ${{ secrets.DEPLOY_TERRAFORM }} | ||
DESTROY_TERRAFORM: ${{ secrets.DESTROY_TERRAFORM }} | ||
## DEVOPS_ASSUMEROLE_POLICY | ||
## DEVOPS_BOUNDARIES_POLICY | ||
## DEVOPS_ACCESS_POLICY | ||
DEVOPS_ACCESS_ROLE: ${{ secrets.DEVOPS_ACCESS_ROLE }} | ||
DEVOPS_ACCOUNT_NAME: devops ## ${{ secrets.DEVOPS_ACCOUNT_NAME }} | ||
DYNAMODB_DEFAULT_REGION: us-east-1 ## ${{ secrets.DYNAMODB_DEFAULT_REGION }} | ||
## INSPECT_DEPLOYMENT | ||
PRIVATE_KEYPAIR_FILE: .ssh/id_rsa ## ${{ secrets.PRIVATE_KEYPAIR_FILE }} | ||
PRIVATE_KEYPAIR_NAME: devops ## ${{ secrets.PRIVATE_KEYPAIR_NAME }} | ||
PRIVATE_KEYPAIR_SECRET: ${{ secrets.PRIVATE_KEYPAIR_SECRET }} | ||
PROVISION_TERRAFORM: ${{ secrets.PROVISION_TERRAFORM }} | ||
S3BUCKET_CONTAINER: pipelines | ||
S3BUCKET_DEFAULT_REGION: us-east-1 ## ${{ secrets.S3BUCKET_DEFAULT_REGION }} | ||
TARGET_WORKSPACE: dev ## ${{ secrets.TARGET_WORKSPACE }} | ||
## UPDATE_PYTHON_LATEST | ||
## UPDATE_SYSTEM_LATEST | ||
## | ||
terraform_input_params: '' | ||
####---------------------------------------------------------------------------- | ||
jobs: | ||
terraform-controller: | ||
runs-on: ubuntu-latest | ||
steps: | ||
####---------------------------------------------------------------------------- | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
####---------------------------------------------------------------------------- | ||
## Environment Variables | ||
- name: Environment Variables | ||
id: environment-variables | ||
run: | | ||
####------------------------------------------------------------------ | ||
## Parsing GitHub Action - Workflow dispatch (limited to 10 input-params) | ||
echo -e "Processing File|Input-based Parameters ... [ 1-10 ]\n" ; | ||
####------------------------------------------------------------------ | ||
eval "echo '::set-env name=TARGET_WORKSPACE::$( | ||
cat ${{ github.workspace }}/workspace \ | ||
| grep -vxE '[[:blank:]]*([#;].*)?' \ | ||
| tr -d "[:space:]" | ||
)'" ; | ||
####------------------------------------------------------------------ | ||
eval "echo '::set-env name=SESSION_TIMESTAMP::$(date +"%y%m%d%H%M%S")'" ; | ||
echo '::set-env name=AWS_ACCESS_KEY_ID::${{ secrets.AWS_ACCESS_KEY_ID }}' | ||
echo '::set-env name=AWS_SECRET_ACCESS_KEY::${{ secrets.AWS_SECRET_ACCESS_KEY }}' | ||
####------------------------------------------------------------------ | ||
custom_workspace="${{ github.event.inputs.workspace }}" ; | ||
if [[ (${#custom_workspace} -gt 0) && (${custom_workspace} != '') ]]; then | ||
echo -e " Target Workspace [input-based]: '${custom_workspace}'" ; | ||
eval "echo '::set-env name=TARGET_WORKSPACE::${custom_workspace}'" ; | ||
fi ; | ||
####------------------------------------------------------------------ | ||
cloud_region="${{ github.event.inputs.region }}" ; | ||
if [[ (${#cloud_region} -gt 0 ) && (${cloud_region} != '') ]]; then | ||
echo -e " Target Cloud Region [input-based]: '${cloud_region}'" ; | ||
eval "echo '::set-env name=AWS_DEFAULT_REGION::${cloud_region}'" ; | ||
fi ; | ||
####------------------------------------------------------------------ | ||
cloud_account="${{ github.event.inputs.account }}" ; | ||
if [[ (${#cloud_account} -gt 0 ) && (${cloud_account} != '') ]]; then | ||
echo -e " Target Cloud Account [input-based]: '${cloud_account}'" ; | ||
eval "echo '::set-env name=AWS_DEFAULT_ACCOUNT::${cloud_account}'" ; | ||
fi; | ||
####------------------------------------------------------------------ | ||
access_keyid="${{ github.event.inputs.accesskey }}" ; | ||
if [[ (${#access_keyid} -gt 0 ) && (${access_keyid} != '') ]]; then | ||
echo -e " Target Access Key-ID [input-based]: '${access_keyid}'" ; | ||
eval "echo '::set-env name=AWS_ACCESS_KEY_ID::${access_keyid}'" ; | ||
fi; | ||
####------------------------------------------------------------------ | ||
secret_keyid="${{ github.event.inputs.secretkey }}" ; | ||
if [[ (${#secret_keyid} -gt 0 ) && (${secret_keyid} != '') ]]; then | ||
echo -e " Target Secret Key-ID [input-based]: '${secret_keyid}'" ; | ||
eval "echo '::set-env name=AWS_SECRET_ACCESS_KEY::${secret_keyid}'" ; | ||
fi; | ||
####------------------------------------------------------------------ | ||
keypair_name="${{ github.event.inputs.keypair-name }}" ; | ||
if [[ (${#keypair_name} -gt 0 ) && (${keypair_name} != '') ]]; then | ||
echo -e " Private Key-Pair Name [input-based]: '${keypair_name}'" ; | ||
eval "echo '::set-env name=PRIVATE_KEYPAIR_NAME::${keypair_name}'" ; | ||
fi; | ||
####------------------------------------------------------------------ | ||
keypair_secret="${{ github.event.inputs.keypair-secret }}" ; | ||
if [[ (${#keypair_secret} -gt 0 ) && (${keypair_secret} != '') ]]; then | ||
private_keypair_secret="$(echo -e "${keypair_secret}" | sed -e "s|;$||" | tr ';' '\n')"; | ||
echo -e "Private Key-Pair Secret [input-based]: \n'***'" ; | ||
eval "echo '::set-env name=PRIVATE_KEYPAIR_SECRET::${private_keypair_secret}'" ; | ||
fi; | ||
####------------------------------------------------------------------ | ||
destroy_terraform="${{ github.event.inputs.destroy-terraform }}" ; | ||
if [[ (${#destroy_terraform} -gt 0 ) && (${destroy_terraform} != true) ]]; then | ||
echo -e " Destroy Terraform [input-based]: \n'${destroy_terraform}'" ; | ||
eval "echo '::set-env name=DESTROY_TERRAFORM::${destroy_terraform}'" ; | ||
fi; | ||
####---------------------------------------------------------------------------- | ||
## System Requirements | ||
- name: System Requirements | ||
uses: emvaldes/system-requirements@master | ||
id: system-requirements | ||
with: | ||
install-awscli-tool: true | ||
install-custom-tools: 'netcat' | ||
install-default-tools: true | ||
install-terraform-cli: latest | ||
update-operating-system: ${UPDATE_SYSTEM_LATEST} | ||
update-python-version: ${UPDATE_PYTHON_LATEST} | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Installed Packages | ||
- name: Installed Packages | ||
id: installed-packages | ||
shell: bash | ||
run: | | ||
####------------------------------------------------------------------ | ||
jq --version; | ||
tree --version; | ||
aws --version; | ||
terraform --version; | ||
####---------------------------------------------------------------------------- | ||
## Terraform Parameters | ||
- name: Terraform Parameters | ||
id: terraform-parameters | ||
shell: bash | ||
run: | | ||
####------------------------------------------------------------------ | ||
remote_origin="$(git config --get remote.origin.url)" ; | ||
route53_record="${remote_origin##*\/}" ; | ||
oIFS="${IFS}" ; IFS=$'\n' ; | ||
declare -a custom_params=( | ||
custom_timestamp="${SESSION_TIMESTAMP}" | ||
custom_engineer='Eduardo Valdes' | ||
custom_contact='[email protected]' | ||
custom_listset='["ami-abc123","ami-def456"]' | ||
custom_mapset='{"us-east-1":"ami-abc123","us-east-2":"ami-def456"}' | ||
route53_record="${SESSION_TIMESTAMP}.${route53_record}" | ||
) ; | ||
## echo -e "\nListing Encoding entries: ..." ; | ||
## for xitem in ${custom_params[@]}; do | ||
## encrypted=$(echo -en ${xitem} | base64 -w0 | tr -d '\n\r') ; | ||
## decrypted=$(echo -en "${encrypted}" | base64 --decode) ; | ||
## echo -e "${encrypted} -> ${decrypted}" ; | ||
## done ; | ||
eval "echo '::set-env name=terraform_input_params::$( | ||
for xitem in ${custom_params[@]}; do | ||
echo -en "`echo -en ${xitem} | base64 -w0 | tr -d '\n\r'`_" ; | ||
done | sed -e 's|\(.*\)\(\_\)$|\1|' ; | ||
)'" ; | ||
IFS="${oIFS}" ; | ||
eval "echo '::set-env name=terraform_input_tfvars::configs/${TARGET_WORKSPACE}-configs.tfvars'" ; | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Requesting Credentials | ||
- name: Requesting Credentials | ||
uses: emvaldes/generate-credentials@master | ||
id: request-credentials | ||
with: | ||
aws-access-key-id: ${AWS_ACCESS_KEY_ID} | ||
aws-default-account: ${AWS_DEFAULT_ACCOUNT} | ||
aws-default-profile: ${AWS_DEFAULT_PROFILE} | ||
aws-default-region: ${AWS_DEFAULT_REGION} | ||
aws-secret-access-key: ${AWS_SECRET_ACCESS_KEY} | ||
devops-access-role: ${DEVOPS_ACCESS_ROLE} | ||
devops-account-name: ${DEVOPS_ACCOUNT_NAME} | ||
session-timestamp: "DevOpsPipeline--${SESSION_TIMESTAMP}" | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Provisioning Access | ||
- name: Provisioning Access | ||
uses: emvaldes/configure-access@master | ||
id: provision-access | ||
with: | ||
private-keypair-file: ${PRIVATE_KEYPAIR_FILE} | ||
private-keypair-secret: "${PRIVATE_KEYPAIR_SECRET}" | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Provision Terraform | ||
- name: Provision Terraform | ||
uses: ./ | ||
id: provision-terraform | ||
with: | ||
provision-terraform: ${PROVISION_TERRAFORM} | ||
terraform-input-params: "${terraform_input_params}" | ||
terraform-input-tfvars: "${terraform_input_tfvars}" | ||
## Terraform Log-levels: TRACE, DEBUG, INFO, WARN or ERROR | ||
terraform-loglevel: false | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Deploy Terraform | ||
- name: Deploy Terraform | ||
uses: ./ | ||
id: deploy-terraform | ||
with: | ||
deploy-terraform: ${DEPLOY_TERRAFORM} | ||
## Terraform Log-levels: TRACE, DEBUG, INFO, WARN or ERROR | ||
terraform-loglevel: false | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Backup Terraform | ||
- name: Backup Terraform | ||
uses: emvaldes/provision-terraform@master | ||
id: backup-terraform | ||
with: | ||
backup-terraform: ${BACKUP_TERRAFORM} | ||
continue-on-error: false | ||
####---------------------------------------------------------------------------- | ||
## Destroy Terraform | ||
- name: Destroy Terraform | ||
uses: ./ | ||
id: destroy-terraform | ||
with: | ||
destroy-terraform: ${DESTROY_TERRAFORM} | ||
## Terraform Log-levels: TRACE, DEBUG, INFO, WARN or ERROR | ||
terraform-loglevel: false | ||
continue-on-error: false | ||
###---------------------------------------------------------------------------- |
Oops, something went wrong.