Created
July 17, 2015 09:30
-
-
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
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
| # ~/.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 |
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
| { | |
| "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" | |
| } | |
| } | |
| } |
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
| # Switch to different dz environments, requires bash, jq, shuf and dz | |
| # Thomas Merkel <[email protected]> | |
| 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}" | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup
Copy the
vdzfunction file to your~/.bashrcor the location which contains your bash functions. By restarting your shell you will be provided by the newvdzcommand.If you're using
bash_completionwhich is really recommended, you could place thebash_completion_vdzcontent to your~/.bash_completionfile. If you are using Debian, Ubuntu, Gentoo, or any other fancy distribution you maybe have an~/.bash_completion.d/folder.The environment file
env.jsonshould be stored in~/.dz.