Commit 03eddeb6 authored by Victor Nikulshin's avatar Victor Nikulshin
Browse files

Merge branch 'feature/coding-style' into '2.x'

Reformat code to match Python coding standards



See merge request !17
parents 4821cbb4 dd737731
Pipeline #1327 failed with stages
in 9 seconds
from fabric.api import task, env, execute
from fabric.colors import red
from fabric.contrib.console import confirm
import behat
import deploy
import docker import docker
from deploy import *
import drush import drush
import behat
import patternlab import patternlab
from .environments import e
from fabric.api import task, env, execute from .environments import e
from deploy import migrate, provision, push
from fabric.colors import red
from fabric.contrib.console import confirm
@task @task
def init(): def init():
""" """
Complete local installation process, used generally when building the docker image for install and configure Drupal. Complete local installation process, used generally when building the docker image for install and configure Drupal.
""" """
execute(docker.image_create) execute(docker.image_create)
execute(docker.container_start) execute(docker.container_start)
execute(drush.make, 'install') execute(drush.make, 'install')
execute(drush.site_install, host='root@{}'.format(env.container_ip)) execute(drush.site_install, host='root@{}'.format(env.container_ip))
execute(drush.aliases) execute(drush.aliases)
execute(behat.init, host='root@{}'.format(env.container_ip))
@task
def test(tags=''):
"""
Setup Behat and run the complete tests suite. Default output formatters: pretty and JUnit.
The JUnit report file is specified in the Behat configuration file. Default: tests/behat/out/behat.junit.xml.
:param tags Specific Behat tests tags to run.
"""
execute(behat.init)
if not tags:
execute(behat.run)
else:
execute(behat.run, tags='{}'.format(tags))
execute(behat.init, host='root@{}'.format(env.container_ip))
@task @task
def install(): def install():
""" """
Run the full installation process. Run the full installation process.
""" """
execute(drush.make, 'install') execute(drush.make, 'install')
execute(drush.site_install) execute(drush.site_install)
execute(behat.init) execute(behat.init)
@task @task
def update(): def release():
""" """
Update the full codebase and run the availabe database updates. Generate all artefacts related to a release process or a deployment process.
""" """
execute(drush.archive_dump)
execute(drush.make, 'update') execute(drush.gen_doc)
execute(drush.updatedb)
execute(behat.init)
@task @task
def release(): def test(tags=''):
""" """
Generate all artefacts related to a release process or a deployment process. Setup Behat and run the complete tests suite. Default output formatters: pretty and JUnit.
The JUnit report file is specified in the Behat configuration file. Default: tests/behat/out/behat.junit.xml.
:param tags Specific Behat tests tags to run.
""" """
execute(behat.init)
execute(drush.archive_dump) if not tags:
execute(drush.gen_doc) execute(behat.run)
else:
execute(behat.run, tags='{}'.format(tags))
@task @task
def deploy(environment): def update():
"""Deploy code and run database updates on a target Drupal environment.
""" """
Update the full codebase and run the availabe database updates.
execute(provision, environment) """
execute(push, environment, hosts=env.hosts) execute(drush.make, 'update')
execute(migrate, environment, hosts=env.hosts) execute(drush.updatedb)
execute(behat.init)
from __future__ import unicode_literals from __future__ import unicode_literals
from fabric.api import task, roles, env from fabric.api import task, roles, env
from fabric.colors import green from fabric.colors import green
import helpers as h import helpers as h
@task @task
@roles('docker') @roles('docker')
def init(rewrite=True): def init(rewrite=True):
...@@ -12,7 +14,6 @@ def init(rewrite=True): ...@@ -12,7 +14,6 @@ def init(rewrite=True):
:param role Default 'role' where to run the task :param role Default 'role' where to run the task
:param rewrite If the behat.yml file should be rewrited or not. :param rewrite If the behat.yml file should be rewrited or not.
""" """
role = 'docker' role = 'docker'
workspace = env.docker_workspace workspace = env.docker_workspace
host = env.site_hostname host = env.site_hostname
...@@ -24,18 +25,18 @@ def init(rewrite=True): ...@@ -24,18 +25,18 @@ def init(rewrite=True):
h.fab_run(role, 'sed -i "s@%DRUPAL_ROOT@{}@g" behat.yml'.format(site_root)) h.fab_run(role, 'sed -i "s@%DRUPAL_ROOT@{}@g" behat.yml'.format(site_root))
h.fab_run(role, 'sed -i "s@%URL@http://{}@g" behat.yml'.format(host)) h.fab_run(role, 'sed -i "s@%URL@http://{}@g" behat.yml'.format(host))
h.fab_run(role, 'echo "127.0.0.1 {}" >> /etc/hosts'.format(host)) h.fab_run(role, 'echo "127.0.0.1 {}" >> /etc/hosts'.format(host))
print green('Behat is now properly configured. The configuration file is {}/tests/behat/behat.yml'.format(workspace)) print green('Behat is now properly configured. The configuration file is {}/tests/behat/behat.yml'.format(workspace))
else: else:
print green('{}/tests/behat/behat.yml is already created.'.format(workspace)) print green('{}/tests/behat/behat.yml is already created.'.format(workspace))
@task() @task
@roles('docker') @roles('docker')
def install(): def install():
""" """
Install behat Install behat
""" """
role = 'docker' role = 'docker'
workspace = env.docker_workspace workspace = env.docker_workspace
...@@ -44,6 +45,7 @@ def install(): ...@@ -44,6 +45,7 @@ def install():
h.fab_run(role, 'curl -s https://getcomposer.org/installer | php') h.fab_run(role, 'curl -s https://getcomposer.org/installer | php')
h.fab_run(role, 'php composer.phar install') h.fab_run(role, 'php composer.phar install')
h.fab_run(role, 'ln -s bin/behat /usr/local/bin/behat') h.fab_run(role, 'ln -s bin/behat /usr/local/bin/behat')
print green('Behat has been properly installed with Composer in /usr/local/bin') print green('Behat has been properly installed with Composer in /usr/local/bin')
else: else:
print(green('Behat is already installed, no need for a new installation')) print(green('Behat is already installed, no need for a new installation'))
...@@ -56,7 +58,6 @@ def run(tags='~@wip&&~@disabled&&~@test'): ...@@ -56,7 +58,6 @@ def run(tags='~@wip&&~@disabled&&~@test'):
Execute the complete Behat tests suite. Execute the complete Behat tests suite.
:param role Default 'role' where to run the task :param role Default 'role' where to run the task
""" """
role = 'docker' role = 'docker'
workspace = env.docker_workspace workspace = env.docker_workspace
...@@ -64,9 +65,10 @@ def run(tags='~@wip&&~@disabled&&~@test'): ...@@ -64,9 +65,10 @@ def run(tags='~@wip&&~@disabled&&~@test'):
# In the container behat is installed globaly, so check before install it inside the tests directory # In the container behat is installed globaly, so check before install it inside the tests directory
if not h.fab_exists(role, '/usr/local/bin/behat') or not h.fab_exists(role, '../tests/behat/bin/behat'): if not h.fab_exists(role, '/usr/local/bin/behat') or not h.fab_exists(role, '../tests/behat/bin/behat'):
install() install()
# If the configuration file behat.yml doesn't exist, call behat_init before run the test. # If the configuration file behat.yml doesn't exist, call behat_init before run the test.
if not h.fab_exists(role, '{}/tests/behat/behat.yml'.format(workspace)): if not h.fab_exists(role, '{}/tests/behat/behat.yml'.format(workspace)):
init() init()
with h.fab_cd(role, '{}/tests/behat'.format(workspace)): with h.fab_cd(role, '{}/tests/behat'.format(workspace)):
h.fab_run(role, 'behat --format junit --format pretty --tags "{}" --colors'.format(tags)) h.fab_run(role, 'behat --format junit --format pretty --tags "{}" --colors'.format(tags))
from __future__ import unicode_literals from __future__ import unicode_literals
from fabric.api import task, roles, env from fabric.api import task, roles, env
from fabric.colors import red, green from fabric.colors import red, green
import helpers as h import helpers as h
@task @task
@roles('docker') @roles('docker')
def db_import(filename, role='docker'): def db_import(filename, role='docker'):
...@@ -13,7 +15,6 @@ def db_import(filename, role='docker'): ...@@ -13,7 +15,6 @@ def db_import(filename, role='docker'):
:param filename: a full path to a gzipped sql dump. :param filename: a full path to a gzipped sql dump.
""" """
if h.fab_exists(role, filename): if h.fab_exists(role, filename):
print green('Database dump {} found.'.format(filename)) print green('Database dump {} found.'.format(filename))
h.fab_run(role, 'zcat {} | mysql -u{} -p{} {}'.format(filename, env.site_db_user, env.site_db_pass, env.site_db_name)) h.fab_run(role, 'zcat {} | mysql -u{} -p{} {}'.format(filename, env.site_db_user, env.site_db_pass, env.site_db_name))
......
...@@ -33,6 +33,7 @@ env.site_languages = 'fr' ...@@ -33,6 +33,7 @@ env.site_languages = 'fr'
# PatternLab # PatternLab
# Specify the PatternLab dir is you want the style guide to be generated # Specify the PatternLab dir is you want the style guide to be generated
env.patternlab_dir = '' env.patternlab_dir = ''
...@@ -41,6 +42,7 @@ env.patternlab_dir = '' ...@@ -41,6 +42,7 @@ env.patternlab_dir = ''
env.db_dump = False env.db_dump = False
# Docker # Docker
env.docker_workspace = '/opt/sfl' env.docker_workspace = '/opt/sfl'
...@@ -49,6 +51,7 @@ env.bind_port = 8001 ...@@ -49,6 +51,7 @@ env.bind_port = 8001
env.apache_user = 'www-data' env.apache_user = 'www-data'
# Docker auto-added container IP # Docker auto-added container IP
env.container_ip = '172.17.0.0' env.container_ip = '172.17.0.0'
......
...@@ -17,13 +17,15 @@ ...@@ -17,13 +17,15 @@
# #
from __future__ import unicode_literals from __future__ import unicode_literals
import glob
import os
from fabric.api import task, env, local, run from fabric.api import task, env, local, run
from fabric.colors import green from fabric.colors import green
from fabric.utils import abort from fabric.utils import abort
import helpers as h import helpers as h
import os
import glob
def _set_hosts(environment): def _set_hosts(environment):
...@@ -51,7 +53,9 @@ def _aegir_platform_name(target, environment): ...@@ -51,7 +53,9 @@ def _aegir_platform_name(target, environment):
""" """
if 'aegir_platform' not in target: if 'aegir_platform' not in target:
abort('Aegir needs a unique platform name to function properly. Check your aegir_platform key in your aliases.') abort('Aegir needs a unique platform name to function properly. Check your aegir_platform key in your aliases.')
aegir_platform = target.get('aegir_platform') aegir_platform = target.get('aegir_platform')
return aegir_platform.format(name=env.project_name, env=environment, build=env.build_number) return aegir_platform.format(name=env.project_name, env=environment, build=env.build_number)
...@@ -61,8 +65,10 @@ def _target_dir(environment): ...@@ -61,8 +65,10 @@ def _target_dir(environment):
:param environment :param environment
""" """
target = env.aliases.get(environment) target = env.aliases.get(environment)
if _is_aegir_deployment(target): if _is_aegir_deployment(target):
return target.get('root') + _aegir_platform_name(target, environment) return target.get('root') + _aegir_platform_name(target, environment)
return target.get('root') return target.get('root')
...@@ -98,7 +104,6 @@ def _clear_site_cache(target, environment): ...@@ -98,7 +104,6 @@ def _clear_site_cache(target, environment):
Helper function to clear site cache. Helper function to clear site cache.
:param environment :param environment
""" """
run('drush --yes --root={} cache-clear all'.format(target.get('root'))) run('drush --yes --root={} cache-clear all'.format(target.get('root')))
print(green('The cache have been cleared on the target environment {}.'.format(environment))) print(green('The cache have been cleared on the target environment {}.'.format(environment)))
...@@ -109,10 +114,13 @@ def _get_archive_from_dir(directory): ...@@ -109,10 +114,13 @@ def _get_archive_from_dir(directory):
:param directory The directory used to untar the artefact. :param directory The directory used to untar the artefact.
""" """
files = glob.glob1(directory, '*.tar.gz') files = glob.glob1(directory, '*.tar.gz')
if len(files) == 0: if len(files) == 0:
abort('No tarball found in {}'.format(directory)) abort('No tarball found in {}'.format(directory))
if len(files) > 1: if len(files) > 1:
abort('More than one tarball has been found in {}. Can not decide which one to deploy.'.format(directory)) abort('More than one tarball has been found in {}. Can not decide which one to deploy.'.format(directory))
return files[0] return files[0]
...@@ -132,6 +140,7 @@ def _aegir_provision_platform(platform, aegir_path, aegir_destsrv): ...@@ -132,6 +140,7 @@ def _aegir_provision_platform(platform, aegir_path, aegir_destsrv):
""" """
run('drush --root="{}/platforms/{}" provision-save "@platform_{}" --context_type="platform" --web_server=@{}' run('drush --root="{}/platforms/{}" provision-save "@platform_{}" --context_type="platform" --web_server=@{}'
.format(aegir_path, platform, platform, aegir_destsrv)) .format(aegir_path, platform, platform, aegir_destsrv))
run('drush @hostmaster hosting-import platform_{}'.format(platform)) run('drush @hostmaster hosting-import platform_{}'.format(platform))
run('drush @hostmaster hosting-dispatch') run('drush @hostmaster hosting-dispatch')
...@@ -156,6 +165,15 @@ def _aegir_remove_platform_without_sites(target, environment, platform): ...@@ -156,6 +165,15 @@ def _aegir_remove_platform_without_sites(target, environment, platform):
run('{}/remove-platforms {} {}'.format(aegir_path, environment, platform)) run('{}/remove-platforms {} {}'.format(aegir_path, environment, platform))
@task(default=True)
def deploy(environment):
"""Deploy code and run database updates on a target Drupal environment.
"""
execute(provision, environment)
execute(push, environment, hosts=env.hosts)
execute(migrate, environment, hosts=env.hosts)
@task @task
def provision(environment, role='local'): def provision(environment, role='local'):
""" """
...@@ -169,10 +187,10 @@ def provision(environment, role='local'): ...@@ -169,10 +187,10 @@ def provision(environment, role='local'):
artefact = _get_archive_from_dir(env.builddir) artefact = _get_archive_from_dir(env.builddir)
with h.fab_cd(role, '{}/src'.format(env.workspace)): with h.fab_cd(role, '{}/src'.format(env.workspace)):
# Clear the currently installed platform # Clear the currently installed platform
if h.fab_exists(role, env.site_root): if h.fab_exists(role, env.site_root):
h.fab_run(role, 'rm -rf {}'.format(env.site_root)) h.fab_run(role, 'rm -rf {}'.format(env.site_root))
# Extract the platform to deploy # Extract the platform to deploy
h.fab_run(role, 'tar -xzf {}/{}'.format(env.builddir, artefact)) h.fab_run(role, 'tar -xzf {}/{}'.format(env.builddir, artefact))
...@@ -186,12 +204,11 @@ def provision(environment, role='local'): ...@@ -186,12 +204,11 @@ def provision(environment, role='local'):
print(green('The platform {} is now ready to be deployed to the target environment {}.'.format(artefact, print(green('The platform {} is now ready to be deployed to the target environment {}.'.format(artefact,
environment))) environment)))
@task @task
def push(environment): def push(environment):
""" """
Push the platform to the target environment. Push the platform to the target environment.
:param environment: The target environment. It must match a valid Drush alias. :param environment: The target environment. It must match a valid Drush alias.
""" """
target = env.aliases.get(environment) target = env.aliases.get(environment)
target_directory = _target_dir(environment) target_directory = _target_dir(environment)
...@@ -217,6 +234,7 @@ def migrate(environment): ...@@ -217,6 +234,7 @@ def migrate(environment):
if _is_aegir_deployment(target): if _is_aegir_deployment(target):
# Deploy to Aegir server. # Deploy to Aegir server.
platform = _aegir_platform_name(target, environment) platform = _aegir_platform_name(target, environment)
if env.get('migrate', "false") == "true": if env.get('migrate', "false") == "true":
_aegir_migrate_sites(target, environment, platform) _aegir_migrate_sites(target, environment, platform)
......
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
from fabric.colors import red, green from fabric.colors import red, green
import helpers as h import helpers as h
########################################################### ###########################################################
# Helper functions to manage docker images and containers # # Helper functions to manage docker images and containers #
########################################################### ###########################################################
def docker_ps(running_only=False): def docker_ps(running_only=False):
args = '' if running_only else '-a' args = '' if running_only else '-a'
result = local('docker ps %s' % args, capture=True) result = local('docker ps %s' % args, capture=True)
...@@ -22,9 +25,11 @@ def docker_tryrun(imgname, containername=None, opts='', mounts=None, cmd='', res ...@@ -22,9 +25,11 @@ def docker_tryrun(imgname, containername=None, opts='', mounts=None, cmd='', res
# Returns True if the container was effectively ran (false if it was restarted or aborted) # Returns True if the container was effectively ran (false if it was restarted or aborted)
if not mounts: if not mounts:
mounts = [] mounts = []
if containername and containername in docker_ps(running_only=True): if containername and containername in docker_ps(running_only=True):
print green("%s already running" % containername) print green("%s already running" % containername)
return False return False
if containername and containername in docker_ps(running_only=False): if containername and containername in docker_ps(running_only=False):
if restart: if restart:
print green("%s already exists and is stopped. Restarting!" % containername) print green("%s already exists and is stopped. Restarting!" % containername)
...@@ -34,16 +39,20 @@ def docker_tryrun(imgname, containername=None, opts='', mounts=None, cmd='', res ...@@ -34,16 +39,20 @@ def docker_tryrun(imgname, containername=None, opts='', mounts=None, cmd='', res
print red("There's a dangling container %s! That's not supposed to happen. Aborting" % containername) print red("There's a dangling container %s! That's not supposed to happen. Aborting" % containername)
print "Run 'docker rm %s' to remove that container" % containername print "Run 'docker rm %s' to remove that container" % containername
return False return False
for from_path, to_path, canwrite in mounts: for from_path, to_path, canwrite in mounts:
abspath = from_path abspath = from_path
opt = ' -v %s:%s' % (abspath, to_path) opt = ' -v %s:%s' % (abspath, to_path)
if not canwrite: if not canwrite:
opt += ':ro' opt += ':ro'
opts += opt opts += opt
if containername: if containername:
containername_opt = '--name %s' % containername containername_opt = '--name %s' % containername
else: else:
containername_opt = '' containername_opt = ''
local('docker run %s %s %s %s' % (opts, containername_opt, imgname, cmd)) local('docker run %s %s %s %s' % (opts, containername_opt, imgname, cmd))
return True return True
...@@ -70,8 +79,10 @@ def docker_ensure_data_container(containername, volume_paths=None, base_image='b ...@@ -70,8 +79,10 @@ def docker_ensure_data_container(containername, volume_paths=None, base_image='b
volume_args = ' '.join('-v %s' % volpath for volpath in volume_paths) volume_args = ' '.join('-v %s' % volpath for volpath in volume_paths)
else: else:
volume_args = '' volume_args = ''
local('docker create %s --name %s %s' % (volume_args, containername, base_image)) local('docker create %s --name %s %s' % (volume_args, containername, base_image))
return True return True
return False return False
...@@ -86,8 +97,10 @@ def docker_isrunning(containername): ...@@ -86,8 +97,10 @@ def docker_isrunning(containername):
def docker_images(): def docker_images():
result = local('docker images', capture=True) result = local('docker images', capture=True)
lines = result.stdout.splitlines() lines = result.stdout.splitlines()
# image name is supposed to be the first column # image name is supposed to be the first column
assert lines[0].strip().startswith('REPOSITORY') assert lines[0].strip().startswith('REPOSITORY')
return [line.strip().split(' ')[0] for line in lines] return [line.strip().split(' ')[0] for line in lines]
...@@ -109,12 +122,10 @@ def connect(role='local'): ...@@ -109,12 +122,10 @@ def connect(role='local'):
@task @task
@roles('local') @roles('local')
def image_create(role='local'): def image_create(role='local'):
""" """
Create docker images Create docker images
:param role Default 'role' where to run the task :param role Default 'role' where to run the task
""" """
with h.fab_cd(role, env.workspace):