drush.py 7.02 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
21
from datetime import datetime

22
from fabric.api import task, roles, env
23
from fabric.colors import red, green
24
from fabric.contrib.console import confirm
25
from fabric.utils import abort
26

27
import core as c
28
import helpers as h
29

30
from git import is_git_dirty
31

32
33
34
35
36
37
38

@task(alias='make')
@roles('local')
def make(action='install'):
    """
    Build the platform by running the Makefile specified in the local_vars.py configuration file.
    """
39
    if env.get('always_use_pty', True):
40
      if (is_git_dirty()):
41
42
43
44
        if (not confirm(red('There are warnings on status of your repositories. '
                        'Do you want to continue and reset all changes to remote repositories'' states?'), default=False)):
          abort('Aborting "drush {}" since there might be a risk of loosing local data.'.format(action))

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

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

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

58
            drush_opts += "--translations=" + env.site_languages + " "
59

60
    if env.get('always_use_pty', True):
61
        drush_opts += " --working-copy --no-gitinfofile"
62

63
64
    if not h.fab_exists('local', env.site_root):
        h.fab_run('local', "mkdir {}".format(env.site_root))
65

66
    with h.fab_cd('local', env.site_root):
67
        h.fab_run('local', 'drush make {} {} -y'.format(drush_opts, env.makefile))
68

69

70
@task
71
@roles('docker')
72
73
74
75
def aliases():
    """
    Copy conf/aliases.drushrc.php in the site environment.
    """
76
    role = 'docker'
77
    drush_aliases = env.site_drush_aliases
78
    workspace = env.docker_workspace
79

80
    if not h.fab_exists(role, drush_aliases):
81
        h.fab_run(role, 'mkdir -p {}'.format(drush_aliases))
82

83
    with h.fab_cd(role, drush_aliases):
84
        # Create aliases
85
        if h.fab_exists(role, '{}/aliases.drushrc.php'.format(drush_aliases)):
86
            h.fab_run(role, 'rm {}/aliases.drushrc.php'.format(drush_aliases))
87

88
        h.fab_run(role, 'cp {}/conf/aliases.drushrc.php .'.format(workspace))
89

90
    print(green('Drush aliases have been copied to {} directory.'.format(drush_aliases)))
91
92
93
94
95
96
97
98
99
100


@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):
101
        h.fab_run(role, 'drush updatedb -y')
102
        h.hook_execute(env.hook_post_update, role)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124


@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
125
    h.init_db('docker')
126
127

    with h.fab_cd(role, site_root):
128
129
130
        h.fab_run(role, 'chown {}:{} ./sites'.format(env.apache_user, env.apache_user))
        h.fab_run(role, 'chmod ug+w ./sites/default'.format(env.docker_site_root))

131
        locale = '--locale="fr"' if env.locale else ''
132

133
        h.fab_run(role, 'sudo -u {} drush site-install {} {} --db-url=mysql://{}:{}@{}/{} --site-name="{}" '
134
135
136
137
138
139
                        '--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))
140

141
        print(green('Site installed successfully!'))
142

Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
143
        # Import db_dump if it exists.
Emmanuel Milou's avatar
Emmanuel Milou committed
144
        if 'db_dump' in env and env.db_dump is not False:
145
            c.db_import(env.db_dump, role)
146

147
    h.hook_execute(env.hook_post_install, role)
148
149
150


@task
151
152
@roles('docker')
def archive_dump(role='docker'):
153
154
155
156
    """
    Archive the platform for release or deployment.
    :param role Default 'role' where to run the task
    """
157
    with h.fab_cd(role, env.docker_site_root):
158
        platform = '{}-{}.tar.gz'.format(env.project_name, datetime.now().strftime('%Y%m%d_%H%M%S'))
159
        h.fab_run(role, 'rm -f {}/build/*.tar.gz'.format(env.docker_workspace))
160
        print(green('All tar.gz archives found in {}/build have been deleted.'.format(env.docker_workspace)))
Emmanuel Milou's avatar
Emmanuel Milou committed
161

162
163
164
165
        h.fab_run(role,
                  '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))
166

167

168
@task
169
170
@roles('docker')
def gen_doc(role='docker'):
171
172
173
174
    """
    Generate README file
    :param role Default 'role' where to run the task
    """
175
    if h.fab_exists(role, '{}/README.adoc'.format(env.docker_workspace)):
176
177
        h.fab_run(role, 'asciidoctor -d book -b html5 -o {}/README.html {}/README.adoc'.
                  format(env.docker_workspace, env.docker_workspace))
178

179
180
        print(green('README.html generated in {}'.format(env.docker_workspace)))

181
    if h.fab_exists(role, '{}/CHANGELOG.adoc'.format(env.docker_workspace)):
182
183
        h.fab_run(role, 'asciidoctor -d book -b html5 -o {}/CHANGELOG.html {}/CHANGELOG.adoc'.
                  format(env.docker_workspace, env.docker_workspace))
184

185
        print(green('CHANGELOG.html generated in {}'.format(env.docker_workspace)))