drush.py 7.09 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
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
    if not h.fab_exists('local', env.site_root):
64
        local('mkdir {}'.format(env.site_root))
65

66
    with h.fab_cd('local', env.site_root):
67
        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
        if env.site_languages:
132
            locale = '--locale="{}"'.format(env.site_languages.split(',')[0])
133
134
        else:
            locale = ''
135

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

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

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

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


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

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

170

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

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

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

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