Skip to content

Instantly share code, notes, and snippets.

@rk295
Created November 29, 2016 13:39
Show Gist options
  • Save rk295/df8dc4fa98cbb83e00c6a4f5ab0b2d87 to your computer and use it in GitHub Desktop.
Save rk295/df8dc4fa98cbb83e00c6a4f5ab0b2d87 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
export AWS_KEYPAIR_NAME="robin RSA"
export SSH_PRIVATE_KEY="$HOME/.ssh/id_rsa"
export INSTANCE_NAME="Robin Vagrant"
export AMI_ID="ami-d48532a3"
# OLD_PS1="$PS1"
ENV_DIR="${HOME}/.aws_environments"
function listEnvironments(){
for file in "${ENV_DIR}"/*.asc ; do
fileName="${file##*/}"
echo "${fileName%.asc}"
done
}
function cws-edit(){
local environment
environment="$1"
if [[ -z "$environment" ]]; then
echo "Need to provide an environment name as the first arg. List follows:"
echo ""
for env in $(listEnvironments); do
echo "* $env"
done
return
fi
varFile="${ENV_DIR}/${environment}"
if [ -e "${varFile}.asc" ] ; then
tempFile=$(mktemp "$varFile.XXXXX")
gpg --decrypt "${varFile}.asc" > "$tempFile"
vim "$tempFile"
gpg --encrypt \
--armor \
--recipient robin@riviera.org.uk \
--output "${varFile}.asc" \
--batch \
--yes \
"$tempFile"
rm -f "$tempFile"
echo "$environment updated, you now need to source it:"
echo ""
echo "cws $environment"
else
echo "$environment is not a valid environment, please select from one of these:"
echo ""
listEnvironments
fi
}
function cws(){
environment=$1
if [ -z "$environment" ]; then
echo "Need to provide an environment name as the first arg. List follows:"
echo ""
for env in $(listEnvironments); do
echo "* $env"
done
else
varFile="${ENV_DIR}/${environment}"
if [ -e "${varFile}.asc" ] ; then
# shellcheck source=$HOME/.aws_environments/
eval "$(gpg --decrypt "${varFile}.asc")"
# shellcheck source=$HOME/.virtualenvs/generate/bin/activate/
source ~/.virtualenvs/generate/bin/activate
export ENVIRONMENT="${environment}"
export AWS_ACCOUNT="${environment}"
else
echo "$environment is not a valid environment, please select from one of these:"
echo ""
listEnvironments
fi
fi
}
# Nice to have auto-complete
_cws_aws_environments(){
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
export prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(listEnvironments)
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _cws_aws_environments cws
complete -F _cws_aws_environments cws-edit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment