drush.py 6.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding: utf-8
#
# Copyright (C) 2016 Savoir-faire Linux Inc. (<www.savoirfairelinux.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from __future__ import unicode_literals
20
from fabric.api import task, roles, env
21
22
23
24
25
26
from fabric.contrib.console import confirm
from fabric.colors import red, green

# Import datetime
from datetime import datetime

27
28
import os.path

29
import helpers as h
30
31
import core as c

32
33
34
35
36
37
38
39
40
41

@task(alias='make')
@roles('local')
def make(action='install'):
    """
    Build the platform by running the Makefile specified in the local_vars.py configuration file.
    """

    drush_opts = "--prepare-install " if action != 'update' else ''

42
43
44
45
46
47
48
49
50
    # Update profile codebase
    if env.site_profile and env.site_profile != '':
        drush_opts += "--contrib-destination=profiles/{} ".format(env.site_profile)
        h.update_profile()

    if not env.interactive_mode:
        drush_opts += "--translations=fr "
    elif confirm(red('Say [Y] to {} the site at {} with the French translation, if you say [n] '
                     'the site will be installed in English only'.format(action, env.site_root))):
51
52
53
54
55
56
57
        drush_opts += "--translations=fr "

    if env.interactive_mode:
        drush_opts += " --working-copy --no-gitinfofile"
    if not h.fab_exists('local', env.site_root):
        h.fab_run('local', "mkdir {}".format(env.site_root))
    with h.fab_cd('local', env.site_root):
58
        h.fab_run('local', 'drush make {} {} -y'.format(drush_opts, env.makefile))
59
60
61
62
63
64
65
66
67
68


@task
@roles('local')
def aliases():
    """
    Copy conf/aliases.drushrc.php in the site environment.
    """

    role = 'local'
69
    drush_aliases = env.site_drush_aliases
70
71
    workspace = env.workspace

72
73
74
    if not h.fab_exists(role, drush_aliases):
        h.fab_run(role, 'mkdir {}'.format(drush_aliases))
    with h.fab_cd(role, drush_aliases):
75
        # Create aliases
76
        if h.fab_exists(role, '{}/aliases.drushrc.php'.format(drush_aliases)):
77
78
79
            h.fab_run(role, 'rm aliases.drushrc.php')
        h.fab_run(role, 'ln -s {}/conf/aliases.drushrc.php .'.format(workspace))

80
    print green('Drush aliases have been copied to {} directory.'.format(drush_aliases))
81
82
83
84
85
86
87
88
89
90
91
92


@task
@roles('docker')
def updatedb():
    """
    Run the available database updates. Similar to drush updatedb.
    """

    role = 'docker'

    with h.fab_cd(role, env.docker_site_root):
93
        h.fab_run(role, 'drush updatedb -y')
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116


@task
@roles('docker')
def site_install():
    """
    Run the site installation procedure.
    """

    role = 'docker'
    site_root = env.docker_site_root
    apache = env.apache_user
    profile = env.site_profile
    db_user = env.site_db_user
    db_pass = env.site_db_pass
    db_host = env.site_db_host
    db_name = env.site_db_name
    site_name = env.site_name
    site_admin_name = env.site_admin_user
    site_admin_pass = env.site_admin_pass
    site_subdir = env.site_subdir

    # Create first the database if necessary
117
    h.init_db('docker')
118
119
120

    with h.fab_cd(role, site_root):
        locale = '--locale="fr"' if env.locale else ''
121

122
        h.fab_run(role, 'sudo -u {} drush site-install {} {} --db-url=mysql://{}:{}@{}/{} --site-name={} '
123
124
125
126
127
128
                        '--account-name={} --account-pass={} --sites-subdir={} -y'.format(apache, profile, locale,
                                                                                          db_user, db_pass,
                                                                                          db_host, db_name, site_name,
                                                                                          site_admin_name,
                                                                                          site_admin_pass,
                                                                                          site_subdir))
129
130
131

        print green('Site installed successfully!')

Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
132
        # Import db_dump if it exists.
Emmanuel Milou's avatar
Emmanuel Milou committed
133
        if 'db_dump' in env and env.db_dump is not False:
134
            c.db_import(env.db_dump, role)
135
136
137
138
139

    h.hook_execute('post_install', role)


@task
140
141
@roles('docker')
def archive_dump(role='docker'):
142
143
144
145
146
    """
    Archive the platform for release or deployment.
    :param role Default 'role' where to run the task
    """

147
    with h.fab_cd(role, env.docker_site_root):
148
        platform = '{}-{}.tar.gz'.format(env.project_name, datetime.now().strftime('%Y%m%d_%H%M%S'))
149
150
        h.fab_run(role, 'rm -f {}/build/*.tar.gz'.format(env.docker_workspace))
        print green('All tar.gz archives found in {}/build have been deleted.'.format(env.docker_workspace))
Emmanuel Milou's avatar
Emmanuel Milou committed
151

152
153
        h.fab_run(
            role,
154
155
156
            'drush archive-dump --destination={}/build/{} --tags="sflinux {}" --generatorversion="2.x" '
            '--generator="Drupalizer::fab drush.archive_dump" --tar-options="--exclude=.git"'
            ''.format(env.docker_workspace, platform, env.project_name)
157
158
        )

159

160
@task
161
162
@roles('docker')
def gen_doc(role='docker'):
163
164
165
166
167
    """
    Generate README file
    :param role Default 'role' where to run the task
    """

168
169
170
171
172
173
174
175
176
    if os.path.isfile('{}/README.adoc'.format(env.docker_workspace)):
        h.fab_run(role, 'asciidoctor -b html5 -o {}/README.html {}/README.adoc'.format(env.docker_workspace,
                                                                                       env.docker_workspace))
        print(green('README.html generated in {}'.format(env.docker_workspace)))

    if os.path.isfile('{}/CHANGELOG.adoc'.format(env.docker_workspace)):
        h.fab_run(role, 'asciidoctor -b html5 -o {}/CHANGELOG.html {}/CHANGELOG.adoc'.format(env.docker_workspace,
                                                                                             env.docker_workspace))
        print(green('CHANGELOG.html generated in {}'.format(env.docker_workspace)))