drush.py 9.11 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 datetime import datetime

20
21
from fabric.api import task
from fabric.colors import red
22

23
24
from .core import *
from .git import is_git_dirty
25

26

27
@task
28
29
def make(action='install'):
    """
30
31
    Build the platform by running the Makefile specified in the local_vars.py
     configuration file.
32
    """
33
    if env.get('always_use_pty', True):
34
35
36
37
38
39
40
        if is_git_dirty():
            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))
41

42
    drupal = '{}/drupal'.format(env.workspace)
43
44
    drush_opts = "--prepare-install " if action != 'update' else ''

45
    # Update profile codebase
46
    if env.site_profile_repo and is_custom_profile(env.site_profile):
47
48
49
50
51
52
53
54
55
56
57
        update_profile()

    if env.site_languages:
        if not env.get('always_use_pty', True):
            drush_opts += "--translations={} ".format(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))):
            drush_opts += "--translations= {} ".format(env.site_languages)
58

59
    if env.get('always_use_pty', True):
60
        drush_opts += " --working-copy --no-gitinfofile"
61
62
63
    if not os.path.exists(drupal):
        local("mkdir {}".format(drupal))
    with lcd(drupal):
64
65
66
67
68
        dk_run(env.services['php'], user='root',
               cmd='chown -R {}:{} .'.format(env.local_userid,
                                             env.apache_userid))
        local('drush make {} {} -y'.format(drush_opts, env.makefile))
        fix_permissions()
69

70

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

79
80
    dk_run(env.services['php'], user='root',
           cmd='if [ ! -e {} ]; then mkdir {}; fi;'.format(drush_aliases, drush_aliases))
81

82
    if os.path.exists('{}/conf/aliases.drushrc.php'.format(workspace)):
83
        dk_run(env.services['php'], user='root',
84
               cmd='cp --force {}/conf/aliases.drushrc.php {}/'.format(workspace,
85
86
87
88
89
                                                          drush_aliases))
        print(green('Drush aliases have been copied to {} directory.'
                    ''.format(drush_aliases)))
    else:
        print(green('Drush aliases have not been found'))
90
91
92
93
94
95

@task
def updatedb():
    """
    Run the available database updates. Similar to drush updatedb.
    """
96
97
98
    service = env.services['php']
    dk_run(service, cmd='drush updatedb -y')
    hook_execute(env.hook_post_update, service)
99
100
101
102
103
104
105
106


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

107
    service = env.services['php']
108
109
110
111
112
113
114
115
116
    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
117

118
    profile_opts = ''
119
120
121
    locale = ''
    if env.site_languages:
        locale = '--locale="{}"'.format(env.site_languages.split(',')[0])
122
    if env.site_conf and env.site_profile == "config_installer":
123
124
125
126
127
128
129
130
131
132
        dk_run(
            service,
            user='root',
            cmd='chown -R {}:{} {}'.format(
                env.apache_userid,
                env.local_userid,
                env.site_conf
            )
        )

133
134
135
        profile_opts += " config_installer " \
                      "config_installer_sync_configure_form.sync_directory=" \
                      "{}".format(env.site_conf)
136
137
138
139
140
141
142
    dk_run(service, user='root',
           cmd='chown -R {}:{} .'.format(env.apache_userid, env.local_userid))
    if env.drupal_version == 8:
        dk_run(service, cmd='composer install')

    dk_run(
        service,
143
        cmd="drush site-install {} {} {} --db-url=mysql://{}:{}@{}/{} "
144
145
            "--site-name='{}' --account-name={} --account-pass={} "
            "--sites-subdir={} -y"
146
147
148
            "".format(profile, profile_opts, locale, db_user, db_pass,
                      db_host, db_name, site_name, site_admin_name,
                      site_admin_pass, site_subdir)
149
150
151
152
153
154
155
156
157
158
    )
    fix_permissions()

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

    # Import db_dump if it exists.
    if 'db_dump' in env and env.db_dump is not False:
        import_dump(env.db_dump, service=env.services['db_server'])

    hook_execute(env.hook_post_install, service)
159
160
161


@task
162
def fix_permissions():
163
    """
164
    Securing file permissions and ownership
165
    """
166
    fix_files_owner_and_permissions()
167

Emmanuel Milou's avatar
Emmanuel Milou committed
168

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
@task
def archive_dump(service=env.services['php']):
    """
    Archive the platform for release or deployment.
    :param service Default 'service' where to run the task
    """
    platform = '{}-{}.tar.gz'.format(env.project_name,
                                     datetime.now().strftime('%Y%m%d_%H%M%S'))
    dk_run(service, user=env.local_userid,
           cmd='rm -f {}/build/*.tar.gz'.format(env.code_workspace))
    print(green('All tar.gz archives found in {}/build have been deleted.'
                ''.format(env.code_workspace)))

    dk_run(
        service,
        user=env.local_userid,
        cmd="drush archive-dump --destination={}/build/{} --tags='sflinux {}' "
            "--generatorversion='3.x' "
            "--generator='Drupalizer::fab drush.archive_dump' "
            "--tar-options='--exclude=.git'"
            "".format(env.code_workspace, platform, env.project_name)
    )
191

192

193
@task
194
def gen_doc(service=env.services['php']):
195
196
    """
    Generate README file
197
    :param service Default 'service' where to run the task
198
    """
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    if os.path.exists('{}/README.adoc'.format(env.code_workspace)):
        dk_run(service, user=env.local_userid,
               cmd='asciidoctor -d book -b html5 -o {}/README.html '
                   '{}/README.adoc'.format(env.code_workspace,
                                           env.code_workspace))
        print(green('README.html generated in {}'.format(env.code_workspace)))

    if os.path.exists('{}/CHANGELOG.adoc'.format(env.code_workspace)):
        dk_run(service, user=env.local_userid,
               cmd='asciidoctor -d book -b html5 -o {}/CHANGELOG.html '
                   '{}/CHANGELOG.adoc'.format(env.code_workspace,
                                              env.code_workspace))
        print(green('CHANGELOG.html generated in {}'
                    ''.format(env.code_workspace)))
213

214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
@task
def export_dump():
    """
    Import and restore the specified database dump.
    :param dump: a full path to a gzipped sql dump.
    """
    dump = '{}/build/{}.sql'.format(env.code_workspace, env.project_name)
    if os.path.exists(dump):
        local('rm {}'.format(dump))
    dk_run(env.services['php'], user=env.local_userid,
           cmd='drush sql-dump > {}'.format(dump))
    print(green('Database dump successfully exported.'))


@task
def import_dump(dump=False):
    """
    Import and restore the specified database dump.
    :param dump: name of sql dump inside build/.
    """
    if dump:
        dump = '{}/{}'.format(env.builddir, dump)
    else:
        dump = '{}/{}.sql'.format(env.builddir, env.project_name)
    if os.path.exists(dump):
        dump = '{}/build/{}.sql'.format(env.code_workspace, env.project_name)
        dk_run(env.services['php'], user=env.local_userid,
               cmd="drush sql-cli < {}".format(dump))
        print(green('Database dump successfully restored.'))
    else:
        print(red('Could not find database dump at {}'.format(dump)))
Samuel Sirois's avatar
Samuel Sirois committed
246
247


Samuel Sirois's avatar
Samuel Sirois committed
248
249
250
251
252
253
254
255
256
257
@task(alias='cex')
def config_export():
  """
  Export configurations in a config directory.
  """
  dk_run(env.services['php'], user=env.local_userid,
         cmd="drush config-export -y")
  print(green('Configurations exported.'))


258
@task(alias='cim')
Samuel Sirois's avatar
Samuel Sirois committed
259
260
261
262
263
264
265
def config_import():
  """
  Import configurations from a config directory.
  """
  dk_run(env.services['php'], user=env.local_userid,
         cmd="drush config-import -y")
  print(green('Configurations imported.'))