Commit f8fee175 authored by Victor Nikulshin's avatar Victor Nikulshin

Merge branch 'feature/no-local-fab-run' into '2.x'

Do not use `fab_run()` helper for local tasks

Le `fab_run()` helper n'ajoute aucun fonctionnalité à l'exécution des commandes locales, mais il fair plus difficile à comprendre où un commande sera exécuter.

Si on minimalise utilisation de `fab_run()`, le	workflow de Drupalizer deviens plus transparent.

See merge request !22
parents 669cfc07 adb5acd7
Pipeline #1335 failed with stages
in 9 seconds
......@@ -175,27 +175,26 @@ def deploy(environment):
@task
def provision(environment, role='local'):
def provision(environment):
"""
Provision a Jenkins deployment.
This task loads the target environment and extract the archive to deploy.
:param environment The environment to deploy the site DEV, STAGE, PROD
:param role Tha fabric role to run the task.
"""
_set_hosts(environment)
artefact = _get_archive_from_dir(env.builddir)
with h.fab_cd(role, '{}/src'.format(env.workspace)):
with lcd('{}/src'.format(env.workspace)):
# Clear the currently installed platform
if h.fab_exists(role, env.site_root):
h.fab_run(role, 'rm -rf {}'.format(env.site_root))
if os.path.exists(env.site_root):
local('rm -rf {}'.format(env.site_root))
# Extract the platform to deploy
h.fab_run(role, 'tar -xzf {}/{}'.format(env.builddir, artefact))
local('tar -xzf {}/{}'.format(env.builddir, artefact))
# Fast-check if the archive looks like a Drupal installation
if not h.fab_exists(role, '{}/src/drupal'.format(env.workspace)):
if not os.path.exists('{}/src/drupal'.format(env.workspace)):
abort('The archive to deploy does not contain a drupal directory.')
if not os.path.isfile('{}/src/drupal/cron.php'.format(env.workspace)):
......
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:
......
......@@ -19,7 +19,7 @@
from __future__ import unicode_literals
from datetime import datetime
from fabric.api import task, roles, env
from fabric.api import task, roles, env, local
from fabric.colors import red, green
from fabric.contrib.console import confirm
from fabric.utils import abort
......@@ -61,10 +61,10 @@ def make(action='install'):
drush_opts += " --working-copy --no-gitinfofile"
if not h.fab_exists('local', env.site_root):
h.fab_run('local', "mkdir {}".format(env.site_root))
local('mkdir {}'.format(env.site_root))
with h.fab_cd('local', env.site_root):
h.fab_run('local', 'drush make {} {} -y'.format(drush_opts, env.makefile))
local('drush make {} {} -y'.format(drush_opts, env.makefile))
@task
......
......@@ -58,16 +58,15 @@ else:
env.site_drush_aliases = path.join(env.docker_site_root, 'sites/all/drush')
def fab_run(role="local", cmd="", capture=False):
def fab_run(role="local", cmd=""):
"""
Helper function to run the task locally or remotely
:param role: the role to use for define the host
:param cmd: the command to execute
:param capture: if it should return or not the output of the command
:return: the function to execute the command locally or remotely
"""
if role == "local":
return local(cmd, capture)
return local(cmd)
else:
return run(cmd)
......@@ -191,7 +190,7 @@ def init_db(role='docker'):
"""
Create a database and a user that can access it.
"""
container_ip = fab_run('local', 'docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
container_ip = local('docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
'{}_container'.format(env.project_name), capture=True)
docker_iface_ip = [(s.connect((container_ip, 80)), s.getsockname()[0], s.close())
......
from __future__ import unicode_literals
from fabric.api import task, roles, env
from fabric.api import task, roles, env, local, lcd
from fabric.colors import green
import helpers as h
......@@ -12,11 +12,10 @@ def build():
"""
Generate the PatternLab static site
"""
role = 'local'
workspace = env.docker_workspace
host = env.site_hostname
site_root = env.docker_site_root
with h.fab_cd(role, env.patternlab_dir):
h.fab_run(role, 'touch public/styleguide/html/styleguide.html')
h.fab_run(role, 'php core/builder.php -g')
with lcd(env.patternlab_dir):
local('touch public/styleguide/html/styleguide.html')
local('php core/builder.php -g')
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