Skip to content

Instantly share code, notes, and snippets.

@drscream
Created July 17, 2015 09:30
Show Gist options
  • Save drscream/286151cacb6c0c30efbe to your computer and use it in GitHub Desktop.
Save drscream/286151cacb6c0c30efbe to your computer and use it in GitHub Desktop.
Small hack and workaround to provide some small deploy zone build virtual environment on your local bash environment
# ~/.bash_completion.d/vdz
_vdz() {
local cur
local envs=$( cat ~/.dz/env.json | jq -r '.environments | keys[]' )
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${envs} --deactivate" -- ${cur}) )
}
complete -F _vdz vdz
{
"build_hosts": [
"red.example.com",
"meow.example.com"
],
"environments": {
"production": {
"template_file": "~/.dz/template_prod.json",
"publish_url": "https://production-imgapi.example.com"
},
"development": {
"template_file": "~/.dz/template_test.json",
"publish_url": "https://testing-imgapi.example.com"
}
}
}
# Switch to different dz environments, requires bash, jq, shuf and dz
# Thomas Merkel <tm@core.io>
vdz() {
DZ_ENV=${1}
DZ_ENV_FILE=~/.dz/env.json
if [[ ! -f ${DZ_ENV_FILE} ]]; then
echo "Missing ${DZ_ENV_FILE}"
return 1
fi
# Print environments
if [[ ! -n ${DZ_ENV} ]]; then
echo "Usage: vdz [--deactivate|environment]"
echo
echo "Available environments:"
jq -r ".environments | keys[]" ${DZ_ENV_FILE} | while read env; do
echo " ${env}"
done
echo
echo "Options:"
echo " --deactivate"
return 2
fi
# Unset all env variables we use
if [[ ${DZ_ENV} =~ "deactivate" ]]; then
echo "Unset your current deploy-zone environment"
unset DZ_BUILD_HOST DZ_BUILD_TEMPLATE DZ_BUILD_PUBLISH_URL VIRTUAL_ENV
return 0
fi
# Create DZ_BUILD_HOST
DZ_BUILD_HOST=$(jq -r ".build_hosts[]" ${DZ_ENV_FILE} | shuf -n1)
# Get ENV information
if [[ $(jq ".environments.${DZ_ENV}" ${DZ_ENV_FILE}) == "null" ]]; then
echo "Missing environment \"${DZ_ENV}\", check your ${DZ_ENV_FILE}"
return 3
fi
DZ_BUILD_TEMPLATE=$(jq -r ".environments.${DZ_ENV}.template_file" ${DZ_ENV_FILE})
DZ_BUILD_PUBLISH_URL=$(jq -r ".environments.${DZ_ENV}.publish_url" ${DZ_ENV_FILE})
# Use existing virtual environment variable
VIRTUAL_ENV=${DZ_ENV}
# Export the Information
export DZ_BUILD_HOST DZ_BUILD_TEMPLATE DZ_BUILD_PUBLISH_URL VIRTUAL_ENV
echo "Switching to deploy-zone environment: ${DZ_ENV}"
}
@drscream
Copy link
Author

Setup

Copy the vdz function file to your ~/.bashrc or the location which contains your bash functions. By restarting your shell you will be provided by the new vdz command.

If you're using bash_completion which is really recommended, you could place the bash_completion_vdz content to your ~/.bash_completion file. If you are using Debian, Ubuntu, Gentoo, or any other fancy distribution you maybe have an ~/.bash_completion.d/ folder.

The environment file env.json should be stored in ~/.dz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment