Created
April 3, 2019 14:08
-
-
Save efimk-lu/a965cfd4f8666a4988bb3c743b98d808 to your computer and use it in GitHub Desktop.
Deployment script
This file contains hidden or 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
#!/usr/bin/env bash | |
set -eo pipefail | |
bold=$(tput bold 2>/dev/null || true) | |
normal=$(tput sgr0 2>/dev/null || true) | |
echo ".____ .__ .__ "; | |
echo "| | __ __ _____ |__| ____ ____ |__| ____ "; | |
echo "| | | | \/ \| |/ ___\ / _ \ | |/ _ \ "; | |
echo "| |___| | / Y Y \ / /_/ > <_> ) | ( <_> )"; | |
echo "|_______ \____/|__|_| /__\___ / \____/ /\ |__|\____/ "; | |
echo " \/ \/ /_____/ \/ "; | |
echo | |
echo "${bold}General deployment script${normal}" | |
function usage() { | |
cat <<EOM | |
Usage: | |
If no parameters are used then local deployment is being chosen. | |
$(basename $0) [options] | |
[--encrypted-file - Optional. Encrypted file under encrypted_files directory. If not given then using default credential file. | |
[--env] - Optional. Env to deploy to. Default is environment variable of USER. | |
[--region] - Optional. Deploy on this aws region. Default is us-west-2 or if .env is defined use the value defined there. | |
[--termination-protection] - Optional. If set then the stack will be protected by termination protection | |
EOM | |
exit 0 | |
} | |
function deploy() { | |
echo "${bold}Deploying${normal}" | |
npm i > /dev/null 2>&1 | |
sls deploy --env $env --region $region | |
if [[ ! -z "$termination_protection" ]] | |
then | |
echo "Updating termination protection" | |
stack_name=$(sls info --env $env --region $region|grep stack:|awk '{print $2}') | |
aws cloudformation update-termination-protection --region $region --enable-termination-protection --stack-name ${stack_name} | |
fi | |
} | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
--help) | |
usage | |
;; | |
--env) | |
opt_env="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--region) | |
opt_region="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--encrypted-file) | |
encrypted_file="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--termination-protection) | |
termination_protection="True" | |
shift # past argument | |
;; | |
*) | |
echo "Unknown argument ${1}. Aborting." | |
exit 1 | |
esac | |
done | |
env=${opt_env:-${USER}} | |
region=${opt_region:-us-west-2} | |
echo "Env: ${env}" | |
echo "Region: ${region}" | |
# If no arguments were given then assume local deployment. | |
if [[ -z "$encrypted_file" ]] | |
then | |
echo "Using local AWS credentials file" | |
deploy | |
else | |
enc_location=./encrypted_files/$encrypted_file | |
if [[ ! -f ${enc_location} ]] | |
then | |
echo "$enc_location not found" | |
exit 1 | |
fi | |
echo "Creating new credential files" | |
mkdir ~/.aws | |
echo ${KEY} | gpg --batch -d --passphrase-fd 0 ${enc_location} > ~/.aws/credentials | |
deploy | |
fi | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment