fabric/modules/docker.py
Ronny Abraham 2bef12902e new file: api.py
new file:   api.pyc
	new file:   conf/fabric.yml
	new file:   fabfile.py
	new file:   fabfile.pyc
	new file:   modules/__init__.py
	new file:   modules/__init__.pyc
	new file:   modules/conf_setup.py
	new file:   modules/conf_setup.pyc
	new file:   modules/configuration_setup.py
	new file:   modules/database.py
	new file:   modules/database.pyc
	new file:   modules/deploy.py
	new file:   modules/deploy.pyc
	new file:   modules/django.py
	new file:   modules/django.pyc
	new file:   modules/docker.py
	new file:   modules/docker.pyc
	new file:   modules/initialize.py
	new file:   modules/initialize.pyc
	new file:   modules/maintenance.py
	new file:   modules/maintenance.pyc
	new file:   modules/nginx.py
	new file:   modules/nginx.pyc
	new file:   modules/pip.py
	new file:   modules/pip.pyc
	new file:   modules/setup.pyc
	new file:   modules/supervisor.py
	new file:   modules/supervisor.pyc
	new file:   modules/testing/__init__.py
	new file:   modules/testing/__init__.pyc
	new file:   modules/testing/configuration_setup.py
	new file:   modules/testing/maintenance.pyc
	new file:   modules/utils.py
	new file:   modules/utils.pyc
	new file:   templates/conf/database/files/db.drop_all.sql.jinja2
	new file:   templates/conf/database/files/db.drop_db.sql.jinja2
	new file:   templates/conf/database/files/db.init.sql.jinja2
	new file:   templates/conf/database/files/db.re_init.sql.jinja2
	new file:   templates/conf/django/files/gunicorn.jinja2
	new file:   templates/conf/django/files/gunicorn.unixsocket.jinja2
	new file:   templates/conf/django/files/local.jinja2
	new file:   templates/conf/django/files/settings.jinja2
	new file:   templates/conf/django/files/settings18.jinja2
	new file:   templates/conf/django/files/wsgi.jinja2
	new file:   templates/conf/django/files/wsgi.py
	new file:   templates/conf/docker/files/database.jinja2
	new file:   templates/conf/gunicorn/files/gunicorn.jinja2
	new file:   templates/conf/gunicorn/files/gunicorn.unixsocket.jinja2
	new file:   templates/conf/gunicorn/files/local.jinja2
	new file:   templates/conf/gunicorn/files/settings.jinja2
	new file:   templates/conf/gunicorn/files/settings18.jinja2
	new file:   templates/conf/gunicorn/files/wsgi.jinja2
	new file:   templates/conf/gunicorn/files/wsgi.py
	new file:   templates/conf/nginx/files/default.conf.jinja2
	new file:   templates/conf/nginx/files/unixsocket.jinja2
	new file:   templates/conf/supervisor/files/conf_old
	new file:   templates/conf/supervisor/files/supervisor.jinja2
	new file:   templates/meta/development.yml
	new file:   templates/meta/layout.yml
	new file:   templates/meta/staging.yml
	new file:   templates/readmes/aws.md
	new file:   templates/readmes/gandi.md
	new file:   templates/readmes/reset_migrations.md
	new file:   templates/readmes/setup_gandi.md
	new file:   templates/readmes/translations.md
	new file:   templates/readmes/update_images.md
2016-09-06 14:43:49 +03:00

198 lines
5.5 KiB
Python

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)
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
docker_run(docker_start)
@task
def stop(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
docker_run(docker_stop)
if remove:
docker_run(docker_rm)