Commits (6)
......@@ -2,6 +2,7 @@ from .deploy import *
import drush
import behat
import docker
import core
from .environments import e
from fabric.api import lcd, cd, task, roles, env, local, run, runs_once, execute
......@@ -70,3 +71,4 @@ def release():
"""
execute(drush.archive_dump)
execute(drush.gen_doc)
from __future__ import unicode_literals
from fabric.api import lcd, cd, task, roles, env, local, run, runs_once, execute
from fabric.contrib.console import confirm
from fabric.colors import red, green
import helpers as h
import os.path
@task
@roles('local')
def db_import(filename, role='local'):
"""Import and restore the specified database dump.
$ fab core.db_import:/tmp/db_dump.sql.gz
:param filename: a full path to a gzipped sql dump.
"""
if os.path.isfile(filename):
print green('Database dump {} found.'.format(filename))
with h.fab_cd(role, env.site_root):
h.fab_run(role, 'zcat {} | mysql -u{} -p{} -h{} {}'.format(filename, env.site_db_user, env.site_db_pass, env.container_ip, env.site_db_name))
print(green('Database dump successfully restored.'))
else:
print red('Could not find database dump at {}'.format(filename))
......@@ -36,6 +36,9 @@ env.docker_site_root = '{}/src/drupal'.format(env.docker_workspace)
env.bind_port = 8001
env.apache_user = 'www-data'
# Docker auto-added container IP
env.container_ip = '172.17.0.0'
# Hook commands
......
......@@ -141,16 +141,17 @@ def container_start(role='local'):
'-d -p {}:80'.format(env.bind_port),
mounts=[(env.workspace, env.docker_workspace, True)]):
# If container was successful build, get the IP address and show it to the user.
global CONTAINER_IP
CONTAINER_IP = h.fab_run(role, 'docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
env.container_ip = h.fab_run(role, 'docker inspect -f "{{{{.NetworkSettings.IPAddress}}}}" '
'{}_container'.format(env.project_name), capture=True)
if env.interactive_mode:
h.fab_update_hosts(CONTAINER_IP, env.site_hostname)
h.fab_update_hosts(env.container_ip, env.site_hostname)
print(green('Docker container {}_container was build successful. '
print(green('Docker container {}_container was build successful. '
'To visit the Website open a web browser in http://{} or '
'http://localhost:{}.'.format(env.project_name, env.site_hostname, env.bind_port)))
h.fab_update_container_ip(env.container_ip)
else:
print(red('Docker image {}/drupal not found and is a requirement to run the {}_container.'
'Please, run first "fab create" in order to build the {}/drupal '
......@@ -214,3 +215,17 @@ def image_remove(role='local'):
print(red('Docker image {}/drupal was not found'.format(env.project_name)))
@task
@roles('docker')
def update_host():
"""
Update hostname resolution in the container.
"""
site_hostname = run("hostname")
run("sed '/{}/c\{} {} localhost.domainlocal' "
"/etc/hosts > /root/hosts.backup".format(env.container_ip, env.container_ip, site_hostname))
run("cat /root/hosts.backup > /etc/hosts")
h.fab_update_container_ip()
......@@ -24,6 +24,8 @@ from fabric.colors import red, green
# Import datetime
from datetime import datetime
import os.path
import helpers as h
@task(alias='make')
......@@ -135,9 +137,7 @@ def archive_dump(role='docker'):
:param role Default 'role' where to run the task
"""
role = 'docker'
with h.fab_cd(role, env.docker_site_root):
with h.fab_cd(role, env.site_root):
platform = '{}-{}.tar.gz'.format(env.project_name, datetime.now().strftime('%Y%m%d_%H%M%S'))
print(green('Cleaning previous archives'))
h.fab_run(role, 'rm -f {}/*.tar.gz'.format(env.builddir))
......@@ -147,4 +147,20 @@ def archive_dump(role='docker'):
'drush archive-dump --destination={}/{} --tar-options="--exclude=.git"'.format(env.builddir, platform)
)
@task
@roles('local')
def gen_doc(role='local'):
"""
Generate README file
:param role Default 'role' where to run the task
"""
if os.path.isfile('{}/README.adoc'.format(env.workspace)):
h.fab_run(role, 'asciidoctor -b html5 -o {}/README.html {}/README.adoc'.format(env.workspace, env.workspace))
print(green('README.html generated in {}'.format(env.workspace)))
if os.path.isfile('{}/CHANGELOG.adoc'.format(env.workspace)):
h.fab_run(role, 'asciidoctor -b html5 -o {}/CHANGELOG.html {}/CHANGELOG.adoc'.format(env.workspace, env.workspace))
print(green('CHANGELOG.html generated in {}'.format(env.workspace)))
......@@ -26,6 +26,8 @@ from fabric.contrib.files import exists
# Import socket to find the localhost IP address
import socket
import os
# Import datetime
from datetime import datetime
......@@ -47,7 +49,7 @@ host_name = local("hostname", capture=True)
# Set the env dict with the roles and the hosts
env.roledefs['local'] = ["{}@{}".format(user_name, host_name)]
env.roledefs['docker'] = ["root@{}".format(env.site_hostname)]
env.roledefs['docker'] = ["root@{}".format(env.container_ip)]
env.builddir = path.join(env.workspace, 'build')
env.makefile = '{}/{}/{}'.format(env.builddir, env.site_profile, env.site_profile_makefile)
......@@ -121,6 +123,12 @@ def fab_remove_from_hosts(site_hostname):
local('sudo sed -i "/{}/d" /etc/hosts'.format(site_hostname))
def fab_update_container_ip(container_ip):
local('sed -i "/env.container_ip/d" {}/local_vars.py'.format(os.path.dirname(os.path.abspath(__file__))))
local('sed -i "/# Docker auto-added container IP/a env.container_ip = \'{}\'" '
'{}/local_vars.py'.format(''.join(container_ip), os.path.dirname(os.path.abspath(__file__))))
def fab_update_hosts(ip, site_hostname):
"""
Helper function to update the file /etc/hosts
......