Last active
August 29, 2015 14:08
-
-
Save farfanoide/6100419a37ade47cd192 to your computer and use it in GitHub Desktop.
Script to manage a vagrant vm for docker with parallels provider
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 | |
| # --------------------------------------------------------------------------------------- | |
| # Description | |
| # ----------- | |
| # | |
| # | |
| # Script to manage a vagrant vm for docker with parallels provider. If invoked | |
| # from within tmux it'll set environment variables for all new windows | |
| # | |
| # Requirements: | |
| # | |
| # - vagrant: [https://www.vagrantup.com/] | |
| # - vagrant parallels plugin: [https://github.com/Parallels/vagrant-parallels] | |
| # - parallels boot2docker box: [https://vagrantcloud.com/parallels/boxes/boot2docker] | |
| # | |
| # | |
| # --------------------------------------------------------------------------------------- | |
| # Author | |
| # ------- | |
| # | |
| # * Farfanoide (https://github.com/farfanoide) | |
| # | |
| # --------------------------------------------------------------------------------------- | |
| [ -z "$BD_DEBUG" ] && set -e || set -x | |
| cd ${BOOT2DOCKER_VAGRANT_DIR:-"${HOME}/Develop/src/boot2docker"} | |
| SCRIPT_NAME="$(basename $0)" | |
| TAB=' ' | |
| RESET='\033[0m' | |
| G='\033[0;32m' | |
| Y='\033[0;33m' | |
| CONFIG_FILE="/tmp/$(whoami)/env/docker_config" | |
| VM_NOT_RUNNING_MESSAGE="VM not running, start it with '${SCRIPT_NAME} start'" | |
| _usage() { | |
| echo -e "Usage: ${G}${SCRIPT_NAME}${RESET} <OPTION>" | |
| echo | |
| echo -e "Available Options:" | |
| echo -e "${TAB}${Y} config${RESET} -- Export ENV VARS for docker to communicate with the server." | |
| echo -e "${TAB}${Y} edit ${RESET} -- Open vagrant file for editing." | |
| echo -e "${TAB}${Y} reload${RESET} -- Restart docker server VM." | |
| echo -e "${TAB}${Y} start ${RESET} -- Start docker server VM." | |
| echo -e "${TAB}${Y} status${RESET} -- Print status of the docker server VM." | |
| echo -e "${TAB}${Y} stop ${RESET} -- Stop docker server VM." | |
| echo -e "${TAB}${Y} help ${RESET} -- Show this help." | |
| echo | |
| echo -e "Note: output from <config|clean> should be evaled in order to modify current ENV." | |
| } | |
| [ $# -lt 1 ] && _usage && exit | |
| arg=$1 | |
| # Export bd as a shell function to let it change ENV VARS | |
| if [ "$arg" = 'init' ]; then | |
| cat <<EOS | |
| bd() { | |
| arg=\$1; | |
| export SH_ECHO=true | |
| case "\$arg" in | |
| (config|clean) eval "\`command ${SCRIPT_NAME} \$arg\`" ;; | |
| (start) command ${SCRIPT_NAME} \$arg && eval "\`command ${SCRIPT_NAME} config\`" ;; | |
| (stop) command ${SCRIPT_NAME} \$arg && eval "\`command ${SCRIPT_NAME} clean\`" ;; | |
| (*) command ${SCRIPT_NAME} \$arg | |
| esac | |
| } | |
| EOS | |
| exit 0 | |
| fi | |
| # Just in case, we create a temporary config file to be sourced by new terminals | |
| _create_tmp_config() { | |
| mkdir -p $(dirname $CONFIG_FILE) | |
| echo -e $* > $CONFIG_FILE | |
| } | |
| # If we're running from a function, we need to echo the command to be evaled. | |
| _sh_echo() { [ -z "$SH_ECHO" ] && echo -e $* || echo "echo -e $*" ;} | |
| _list_config() { | |
| echo "Docker configurations:" | |
| echo "DOCKER_HOST_IP => ${DOCKER_HOST_IP}" | |
| echo "DOCKER_HOST => ${DOCKER_HOST}" | |
| } | |
| _clean_cfg() { | |
| cat <<EOS | |
| rm -f "/tmp/\$(whoami)/env/docker_config" | |
| unset DOCKER_HOST | |
| unset DOCKER_HOST_IP | |
| if [ -n "\${TMUX}" ]; then | |
| tmux set-environment -g -u DOCKER_HOST_IP | |
| tmux set-environment -g -u DOCKER_HOST | |
| fi | |
| EOS | |
| } | |
| _vm_runnging(){ | |
| local vm_status="$(vagrant status | grep state -2 | tail -1)" | |
| [[ "${vm_status}" =~ "running" ]] | |
| } | |
| _edit_vm() { ${EDITOR:-vi} Vagrantfile ;} | |
| _start() { _vm_runnging && _sh_echo "VM already running." || vagrant up --provider parallels ;} | |
| _reload() { _vm_runnging && vagrant reload || _sh_echo "$VM_NOT_RUNNING_MESSAGE" ;} | |
| _stop() { _vm_runnging && vagrant suspend || _sh_echo "$VM_NOT_RUNNING_MESSAGE" ;} | |
| _status() { vagrant status ;} | |
| _config() { | |
| if _vm_runnging; then | |
| local docker_host_ip=$(vagrant ssh-config | sed -n "s/[ ]*HostName[ ]*//gp") | |
| local docker_host="tcp://${docker_host_ip}:2375" | |
| local docker_host_ip_export="export DOCKER_HOST_IP=${docker_host_ip}" | |
| local docker_host_export="export DOCKER_HOST=\"${docker_host}\"" | |
| if [ ! -z "$TMUX" ]; then | |
| tmux set-environment -g DOCKER_HOST_IP $docker_host_ip | |
| tmux set-environment -g DOCKER_HOST $docker_host | |
| fi | |
| _create_tmp_config "$docker_host_ip_export \n$docker_host_export" | |
| echo "$docker_host_ip_export; $docker_host_export" | |
| else | |
| _sh_echo "$VM_NOT_RUNNING_MESSAGE" | |
| fi | |
| } | |
| case "$arg" in | |
| config) _config ;; | |
| list) _list_config ;; | |
| edit) _edit_vm ;; | |
| reload) _reload ;; | |
| start) _start ;; | |
| status) _status ;; | |
| stop) _stop ;; | |
| clean) _clean_cfg ;; | |
| help|*) _usage ;; | |
| esac |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should be used as a shell function initialized like this:
It creates a config file in
/tmp/<username>/env/docker_configready to be sourced.