Commit 08884967 authored by 20th (Victor Nikulshin)'s avatar 20th (Victor Nikulshin) Committed by Victor Nikulshin

Avoid `fab_run()` for Docker tasks

It makes sense (and even possible at all) to controll Docker containers from
local environment. Replacing `fab_run()` directly with `local()` calls ensures
that container management will not be performed inside of Docker instance or on
remote server because of a configuration error.
parent 8ee7032f
from __future__ import unicode_literals
from fabric.api import task, roles, env, local, run
from fabric.api import task, roles, env, local, run, lcd
from fabric.colors import red, green
import helpers as h
......@@ -106,43 +106,40 @@ def docker_images():
@task
@roles('local')
def connect(role='local'):
def connect():
"""
Connect to a docker container using "docker -it exec <name> bash".
This is a better way to connect to the container than using ssh'
:param role Default 'role' where to run the task
"""
with h.fab_cd(role, env.workspace):
with lcd(env.workspace):
if docker_isrunning('{}_container'.format(env.project_name)):
h.fab_run(role, 'docker exec -it {}_container bash'.format(env.project_name))
local('docker exec -it {}_container bash'.format(env.project_name))
else:
print(red('Docker container {}_container is not running, it should be running to be able to connect.'))
@task
@roles('local')
def image_create(role='local'):
def image_create():
"""
Create docker images
:param role Default 'role' where to run the task
"""
with h.fab_cd(role, env.workspace):
with lcd(env.workspace):
if '{}/drupal'.format(env.project_name) in docker_images():
print(red('Docker image {}/drupal was found, you has already build this image'.format(env.project_name)))
else:
h.copy_public_ssh_keys(role)
h.fab_run(role, 'docker build -t {}/drupal .'.format(env.project_name))
h.copy_public_ssh_keys('local')
local('docker build -t {}/drupal .'.format(env.project_name))
print(green('Docker image {}/drupal was build successful'.format(env.project_name)))
@task
@roles('local')
def container_start(role='local'):
def container_start():
"""
Run docker containers
:param role Default 'role' where to run the task
"""
with h.fab_cd(role, env.workspace):
with lcd(env.workspace):
if '{}/drupal'.format(env.project_name) in docker_images():
if docker_tryrun('{}/drupal'.format(env.project_name),
'{}_container'.format(env.project_name),
......@@ -150,7 +147,7 @@ def container_start(role='local'):
mounts=[(env.workspace, env.docker_workspace, True)]):
# If container was successful build, get the IP address and show it to the user.
env.container_ip = h.fab_run(role, 'docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
env.container_ip = local('docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
'{}_container'.format(env.project_name), capture=True)
if env.get('always_use_pty', True):
......@@ -168,17 +165,16 @@ def container_start(role='local'):
@task
@roles('local')
def container_stop(role='local'):
def container_stop():
"""
Stop docker containers
:param role Default 'role' where to run the task
"""
with h.fab_cd(role, env.workspace):
with lcd(env.workspace):
if '{}_container'.format(env.project_name) in docker_ps():
if env.get('always_use_pty', True):
h.fab_remove_from_hosts(env.site_hostname)
h.fab_run(role, 'docker stop {}_container'.format(env.project_name))
local('docker stop {}_container'.format(env.project_name))
print(green('Docker container {}_container was successful stopped'.format(env.project_name)))
else:
print(red('Docker container {}_container was not running or paused'.format(env.project_name)))
......@@ -186,18 +182,16 @@ def container_stop(role='local'):
@task
@roles('local')
def container_remove(role='local'):
def container_remove():
"""
Stop docker containers
:param role Default 'role' where to run the task
"""
with h.fab_cd(role, env.workspace):
with lcd(env.workspace):
if '{}_container'.format(env.project_name) in docker_ps():
if env.get('always_use_pty', True):
h.fab_remove_from_hosts(env.site_hostname)
h.fab_run(role, 'docker rm -f {}_container'.format(env.project_name))
local('docker rm -f {}_container'.format(env.project_name))
print(green('Docker container {}_container was successful removed'.format(env.project_name)))
else:
print(red('Docker container {}_container was already removed'.format(env.project_name)))
......@@ -205,22 +199,21 @@ def container_remove(role='local'):
@task
@roles('local')
def image_remove(role='local'):
def image_remove():
"""
Remove docker container and images
:param role Default 'role' where to run the task
"""
with h.fab_cd(role, env.workspace):
with lcd(env.workspace):
if docker_isrunning('{}_container'.format(env.project_name)):
print(red('Docker container {}_container is running, '
'you should stopped it after remove the image {}/drupal'.format(env.project_name,
env.project_name)))
if '{}/drupal'.format(env.project_name) in docker_images():
h.fab_run(role, 'docker rmi -f {}/drupal'.format(env.project_name))
local('docker rmi -f {}/drupal'.format(env.project_name))
# Remove dangling docker images to free space.
if '<none>' in docker_images():
h.fab_run(role, 'docker images --filter="dangling=true" -q | xargs docker rmi -f')
local('docker images --filter="dangling=true" -q | xargs docker rmi -f')
print(green('Docker image {}/drupal was successful removed'.format(env.project_name)))
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment