Last active
August 29, 2015 14:04
-
-
Save jchysk/294d187dcb22d55be9a4 to your computer and use it in GitHub Desktop.
Fabfile that can interact with Dockers from any OS
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
# Docker controller | |
# example usage | |
# fab local_os restart_env | |
from fabric.api import * | |
def _vagrant(): | |
env.user = "vagrant" | |
env.hosts = ["127.0.0.1"] | |
env.port = 2222 | |
#host-vm is the directory where the docker-host vagrantfile exists | |
env.key_filename = local("cd host-vm && vagrant ssh-config | grep IdentityFile | awk '{print $2}'", True) | |
def _doit(command): | |
if env.os == "linux": | |
local(command) | |
elif env.os != "unknown": | |
run(command) | |
def local_os(): | |
from sys import platform as _platform | |
env.os = "unknown" | |
if _platform == "linux" or _platform == "linux2": | |
env.os = "linux" | |
elif _platform == "darwin": | |
_vagrant() | |
env.os = "mac" | |
elif "win" in _platform: | |
_vagrant() | |
env.os = "windows" | |
def _start_env(): | |
local("vagrant up --no-parallel") | |
def start_env(): | |
''' Will clean out any currently stopped containers and then start everything up ''' | |
if env.os != "linux" and env.key_filename != "": | |
rm_containers() | |
_start_env() | |
def restart_env(): | |
stop_env() | |
rm_containers() | |
_start_env() | |
def stop_env(): | |
with settings(warn_only=True): | |
cmd = "docker stop $(docker ps -q)" | |
_doit(cmd) | |
def rm_containers(): | |
with settings(warn_only=True): | |
cmd = "docker rm $(docker ps -a -q)" | |
_doit(cmd) | |
def kill_everything(): | |
''' Stops the containers, removes them, and then turns off the VM ''' | |
stop_env() | |
rm_containers() | |
if env.os != "linux": | |
local("cd host-vm && vagrant halt") | |
def vagrant_destroy(): | |
kill_everything() | |
if env.os != "linux": | |
local("cd host-vm && vagrant destroy") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Ubuntu 12.04, 14.04, and OS X 10.9.4