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

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 __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 from fabric.colors import red, green
import helpers as h import helpers as h
...@@ -106,43 +106,40 @@ def docker_images(): ...@@ -106,43 +106,40 @@ def docker_images():
@task @task
@roles('local') @roles('local')
def connect(role='local'): def connect():
""" """
Connect to a docker container using "docker -it exec <name> bash". Connect to a docker container using "docker -it exec <name> bash".
This is a better way to connect to the container than using ssh' 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)): 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: else:
print(red('Docker container {}_container is not running, it should be running to be able to connect.')) print(red('Docker container {}_container is not running, it should be running to be able to connect.'))
@task @task
@roles('local') @roles('local')
def image_create(role='local'): def image_create():
""" """
Create docker images 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(): 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))) print(red('Docker image {}/drupal was found, you has already build this image'.format(env.project_name)))
else: else:
h.copy_public_ssh_keys(role) h.copy_public_ssh_keys('local')
h.fab_run(role, 'docker build -t {}/drupal .'.format(env.project_name)) local('docker build -t {}/drupal .'.format(env.project_name))
print(green('Docker image {}/drupal was build successful'.format(env.project_name))) print(green('Docker image {}/drupal was build successful'.format(env.project_name)))
@task @task
@roles('local') @roles('local')
def container_start(role='local'): def container_start():
""" """
Run docker containers 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 '{}/drupal'.format(env.project_name) in docker_images():
if docker_tryrun('{}/drupal'.format(env.project_name), if docker_tryrun('{}/drupal'.format(env.project_name),
'{}_container'.format(env.project_name), '{}_container'.format(env.project_name),
...@@ -150,7 +147,7 @@ def container_start(role='local'): ...@@ -150,7 +147,7 @@ def container_start(role='local'):
mounts=[(env.workspace, env.docker_workspace, True)]): mounts=[(env.workspace, env.docker_workspace, True)]):
# If container was successful build, get the IP address and show it to the user. # 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) '{}_container'.format(env.project_name), capture=True)
if env.get('always_use_pty', True): if env.get('always_use_pty', True):
...@@ -168,17 +165,16 @@ def container_start(role='local'): ...@@ -168,17 +165,16 @@ def container_start(role='local'):
@task @task
@roles('local') @roles('local')
def container_stop(role='local'): def container_stop():
""" """
Stop docker containers 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 '{}_container'.format(env.project_name) in docker_ps():
if env.get('always_use_pty', True): if env.get('always_use_pty', True):
h.fab_remove_from_hosts(env.site_hostname) 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))) print(green('Docker container {}_container was successful stopped'.format(env.project_name)))
else: else:
print(red('Docker container {}_container was not running or paused'.format(env.project_name))) print(red('Docker container {}_container was not running or paused'.format(env.project_name)))
...@@ -186,18 +182,16 @@ def container_stop(role='local'): ...@@ -186,18 +182,16 @@ def container_stop(role='local'):
@task @task
@roles('local') @roles('local')
def container_remove(role='local'): def container_remove():
""" """
Stop docker containers 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 '{}_container'.format(env.project_name) in docker_ps():
if env.get('always_use_pty', True): if env.get('always_use_pty', True):
h.fab_remove_from_hosts(env.site_hostname) 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))) print(green('Docker container {}_container was successful removed'.format(env.project_name)))
else: else:
print(red('Docker container {}_container was already removed'.format(env.project_name))) print(red('Docker container {}_container was already removed'.format(env.project_name)))
...@@ -205,22 +199,21 @@ def container_remove(role='local'): ...@@ -205,22 +199,21 @@ def container_remove(role='local'):
@task @task
@roles('local') @roles('local')
def image_remove(role='local'): def image_remove():
""" """
Remove docker container and images 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)): if docker_isrunning('{}_container'.format(env.project_name)):
print(red('Docker container {}_container is running, ' print(red('Docker container {}_container is running, '
'you should stopped it after remove the image {}/drupal'.format(env.project_name, 'you should stopped it after remove the image {}/drupal'.format(env.project_name,
env.project_name))) env.project_name)))
if '{}/drupal'.format(env.project_name) in docker_images(): 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. # Remove dangling docker images to free space.
if '<none>' in docker_images(): 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))) print(green('Docker image {}/drupal was successful removed'.format(env.project_name)))
else: else:
......
Supports Markdown
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