drush.py 7.01 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, local
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
        h.update_profile()

51
52
53
54
55
    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))):
56

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

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

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

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

68

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

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

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

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

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


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


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

    with h.fab_cd(role, site_root):
127
128
129
        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))

130
        if env.site_languages:
131
            locale = '--locale="{}"'.format(env.site_languages.split(',')[0])
132
133
        else:
            locale = ''
134

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

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

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

149
    h.hook_execute(env.hook_post_install, role)
150
151
152


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

164
165
166
167
        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))
168

169

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

181
182
        print(green('README.html generated in {}'.format(env.docker_workspace)))

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

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