drush.py 8.82 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
    if not os.path.exists(drush_aliases):
80
81
        dk_run(env.services['php'], user='root',
               cmd='mkdir {}'.format(drush_aliases))
82

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

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


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

111
    service = env.services['php']
112
113
114
115
116
117
118
119
120
    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
121

122
    profile_opts = ''
123
124
125
    locale = ''
    if env.site_languages:
        locale = '--locale="{}"'.format(env.site_languages.split(',')[0])
126
127
128
129
    if env.site_conf and env.site_profile == "config_installer":
        profile_opts += " config_installer " \
                      "config_installer_sync_configure_form.sync_directory=" \
                      "{}".format(env.site_conf)
130
131
132
133
134
135
136
    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,
137
        cmd="drush site-install {} {} {} --db-url=mysql://{}:{}@{}/{} "
138
139
            "--site-name='{}' --account-name={} --account-pass={} "
            "--sites-subdir={} -y"
140
141
142
            "".format(profile, profile_opts, locale, db_user, db_pass,
                      db_host, db_name, site_name, site_admin_name,
                      site_admin_pass, site_subdir)
143
144
145
146
147
148
149
150
151
152
    )
    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)
153
154
155


@task
156
def fix_permissions():
157
    """
158
    Securing file permissions and ownership
159
    """
160
    fix_files_owner_and_permissions()
161

Emmanuel Milou's avatar
Emmanuel Milou committed
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
@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)
    )
185

186

187
@task
188
def gen_doc(service=env.services['php']):
189
190
    """
    Generate README file
191
    :param service Default 'service' where to run the task
192
    """
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    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)))
207

208

209
210
211
212
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
@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
240
241
242
243
244
245
246
247
248
249


@task
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.'))