from fabric.api import env, task from fabric.operations import run from fabric.contrib.files import upload_template from utils import loggify, generate_template_files_path, booleanize from utils import generate_template_build_path, print_console @task def docker_ip(): configuration = env.config if configuration.docker.database.host == 'local': docker_cmd = 'docker-machine ip default' return run(docker_cmd) else: return configuration.docker.database.host def docker_run(cmd): from fabric.context_managers import prefix docker_eval = "eval $(docker-machine env default)" with prefix(docker_eval): run(cmd) @task def generate(): """ generates and uploads the docker.yml configuration file based on the settings in the yml file for the current branch. e.g. if we are using development.yml then it will check for the docker settings in there to find out what conf values we want to use when creating whatever docker containers we are usign for this branch currently, only the development branch is using docker, but I might change that in the future. """ configuration = env.config if env.debug: logger = loggify('docker', 'generate') build_path = generate_template_build_path('docker', 'database') files_path = generate_template_files_path('docker') context = dict() context['docker_service_name'] = \ configuration.docker.database.service_name context['docker_container_name'] = \ configuration.docker.database.container_name context['docker_database_env_user'] = \ configuration.docker.database.env.user context['docker_database_env_pass'] = \ configuration.docker.database.env.password context['docker_database_env_db'] = \ configuration.docker.database.env.dbname context['docker_database_image'] = configuration.docker.database.image context['docker_database_port_external'] = \ configuration.server.database.port context['docker_database_port_internal'] = \ configuration.docker.database.port context['database_user'] = configuration.server.database.admin.user context['database_pass'] = configuration.server.database.admin.password context['database_name'] = configuration.server.database.name if env.debug: for key in context.keys(): logger.debug("context[{key}] : {value}".format( key=key, value=context[key])) upload_msg = "upload_template(" \ "\n\tfilename={filename}," \ "\n\tdestination={destination}," \ "\n\tcontext={context}," \ "\n\tuse_jinja=True," \ "\n\tuse_sudo=False," \ "\n\tbackup=False," \ "\n\ttemplate_dir={template_dir})".format( filename=configuration.templates.docker.database.src, destination=build_path, context=context, template_dir=files_path) logger.debug("upload_msg : %s" % upload_msg) import pprint pp = pprint.PrettyPrinter(indent=4) print("context:\n") pp.pprint(context) else: config_src = configuration.templates.docker.database.src upload_template( filename=config_src, destination=build_path, context=context, use_jinja=True, use_sudo=False, backup=False, template_dir=files_path) @task def create(container='database'): """ helper function to create a docker-based database container container - specifies the type of container being built NOTE: "container" must have a corresponding value in configuration file """ # configuration = env.config if env.debug: logger = loggify("docker", 'create') build_path = generate_template_build_path('docker', container) info_msg = """ Generating container template for {container}, note that the container paramter of "{container}" must have a corresponding value in the {branch} configuration file under "docker" """.format(container=container, branch="dev") print_console(info_msg, numsep=60) dockercompose_cmd = \ "docker-compose -f {build_path} up -d".format(build_path=build_path) if env.debug: logger.debug("build_path : %s" % build_path) logger.debug("dockercompose_cmd : %s" % dockercompose_cmd) else: docker_run(dockercompose_cmd) @task def status(): docker_run("docker ps -a") @task def start(create=False): """ this will start the docker container referenced by container_type NOTE: you should have created it with the docker.create method above first! container_type - the type of container to start create - craete if container has not yet been created """ configuration = env.config create = booleanize(create) docker_start = 'docker start %s' % \ configuration.docker.database.container_name if env.debug: print("docker_run(%s)" % docker_start) else: docker_run(docker_start) @task def stop(remove=False): """ remove=False this will start the docker container referenced by container_type NOTE: you should have created it with the docker.create method above first! container_type - the type of container to start create - craete if container has not yet been created """ configuration = env.config remove = booleanize(remove) docker_stop = 'docker stop %s' % \ configuration.docker.database.container_name docker_rm = 'docker rm %s' % configuration.docker.database.container_name if env.debug: print("docker_run(%s)" % docker_stop) print("docker_run(%s)" % docker_rm) else: docker_run(docker_stop) if remove: docker_run(docker_rm)