#!/usr/bin/env python2.7

import subprocess
import pprint

from docker import Client

IMAGE_NAME='mxs-devbox'
CONTAINER_NAME='mxs-devbox'

def get_container():
     cli = Client(base_url='unix://var/run/docker.sock')
     found = False
     for container in cli.containers(all=True):
          if '/%s' % CONTAINER_NAME in container['Names']:
               found = True
     if not found:
          cli.create_container(image=IMAGE_NAME, hostname=CONTAINER_NAME, ports=[20000], name=CONTAINER_NAME)
     devbox = cli.inspect_container(CONTAINER_NAME)
     if not devbox['State']['Running']:
          cli.start(IMAGE_NAME, privileged=True, port_bindings={20000: ('127.0.0.1', 20000)}, binds={'/home/mxs/.ssh': {'bind': '/home/mxs/.ssh', 'ro': True}})
          devbox = cli.inspect_container(CONTAINER_NAME)
     return devbox['NetworkSettings']['IPAddress']

def main():
     subprocess.call("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s" % get_container(), shell=True)

if __name__ == '__main__':
     main()
