deploy.py 6.03 KB
Newer Older
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
1
2
3
4
5
6
7
8
9
10
11
12
from __future__ import unicode_literals
from fabric.api import lcd, cd, task, roles, env, local, run, runs_once, execute
from fabric.contrib.project import rsync_project
from fabric.colors import red, green

import helpers as h
import drush as d
import os, glob

@task
@roles('local')
def to_aegir(environement, build='0', role='local', migrate_sites=False, delete_old_platforms=False):
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
13
    """Deploy the artifact to the aegir server.
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
14

Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
15
    $ fab deploy.to_aegir
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
16

Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
17
18
19
20
21
    :param environement: the environnement (dev, stage, production)
    :param build: the number of the build in Jenkins
    :param role: the role to run the task in fabric
    :param migrate_sites: if aegir should migrate the sites to the new platform
    :param delete_old_platforms: if aegir should remove old platforms
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    """
    aegir_server = env.aegir_server
    aegir_user = env.aegir_user
    aegir_path = env.aegir_path
    aegir_dest_server = env.aegir_dest_server
    build_dir = '{}/build'.format(env.workspace)
    if environement == 'dev' and aegir_server and aegir_user and aegir_path and aegir_dest_server:
        os.chdir(build_dir)
        for artifact in glob.glob("*.tar.gz"):
            # Get platform name
            platform_name = '{}-{}-{}'.format(environement, artifact.split('.')[0], build)

            # Remove old platforms
            if h.fab_exists(role, env.site_root):
                h.fab_run(role, 'rm -rf {}'.format(env.site_root))

            # Untar new platform
            with h.fab_cd(role, '{}/src'.format(env.workspace)):
                h.fab_run(role, 'tar -xvzf {}/build/{}'.format(env.workspace, artifact))

            # Rsync new platform
            with h.fab_cd(role, env.workspace):
                print green('Rsync {} to aegir'.format(platform_name))
                h.fab_run(role, 'rsync -a src/drupal/ {}@{}:{}/platforms/{}'.format(aegir_user, aegir_server,
                                                                                    aegir_path, platform_name))

            # Declare platform in aegir
            print green('Declaring {} in aegir'.format(platform_name))
            h.fab_run(
                role,
                """
                ssh {}@{} "drush --root='{}/platforms/{}' provision-save '@platform_{}' --context_type='platform' --web_server=@{}"
                """.format(aegir_user, aegir_server, aegir_path, platform_name, platform_name, aegir_dest_server)
            )
            h.fab_run(
                role,
                """
                ssh {}@{} "drush @hostmaster hosting-import platform_{}"
                """.format(aegir_user, aegir_server, platform_name)
            )
            h.fab_run(
                role,
                """
                ssh {}@{} "drush @hostmaster hosting-dispatch"
                """.format(aegir_user, aegir_server)
            )
            if migrate_sites == 'true':
                print green("Migrating all websites currently on {} to {}".format(environement, platform_name))
                h.fab_run(
                    role,
                    """
                    ssh {}@{} "./migrateS.drush {} {}"
                    """.format(aegir_user, aegir_server, environement, platform_name))

            # Remove old platforms
            h.fab_run(role, 'rm -rf {}'.format(env.site_root))
            h.fab_run(role, 'rm -rf {}/build/{}'.format(env.workspace, artifact))

        # Delete old platforms in aegir
        if delete_old_platforms == 'true':
            print green("Deleting old platforms on {}".format(environement))
            h.fab_run(
                    role,
                    """
                    ssh {}@{} "./deleteP.drush {} {}"
                    """.format(aegir_user, aegir_server, environement, platform_name))

    else:
        print red('There are not enought information to deploy the platformt')


@task
@roles('local')
def to_server(environement, role='local'):
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
96
97
98
99
100
101
102
    """Deploy the artifact to the aegir server.

        $ fab deploy.to_aegir

        :param environement: the environnement (dev, stage, production)
        :param role: the role to run the task in fabric
    """
Ernesto Rodriguez Ortiz's avatar
Ernesto Rodriguez Ortiz committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    build_dir = '{}/build'.format(env.workspace)
    os.chdir(build_dir)
    for artifact in glob.glob("*.tar.gz"):
        # Remove old platforms
        if h.fab_exists(role, env.site_root):
            h.fab_run(role, 'rm -rf {}'.format(env.site_root))

        # Untar new platform
        print green('Untar new platform')
        with h.fab_cd(role, '{}/src'.format(env.workspace)):
            h.fab_run(role, 'tar -xvzf {}/build/{}'.format(env.workspace, artifact))
            d.aliases()

        with h.fab_cd(role, env.site_root):
            # Set the site in maintenance mode
            print green('Set the site in maitenance mode')
            h.fab_run(
                role,
                'drush --yes @{} vset site_offline 1'
                    .format(environement)
            )
            # rsync
            print green('Rsync the site source code')
            h.fab_run(
                role,
                'drush --yes rsync --exclude=sites/all/files --exclude-files --exclude=.htaccess --delete --verbose @self @{}'
                    .format(environement)
            )
            # update data base
            print green('Update site database')
            h.fab_run(
                role,
                'drush --yes @{} updatedb'
                    .format(environement)
            )
            # Set site online
            print green('Set the site online')
            h.fab_run(
                role,
                'drush --yes @{} vset site_offline 0'
                    .format(environement)
            )
            # clean cache
            print green('Clear site cache')
            h.fab_run(
                role,
                'drush --yes @{} cache-clear all'
                    .format(environement)
            )
        # Remove old platforms
        h.fab_run(role, 'rm -rf {}'.format(env.site_root))
        h.fab_run(role, 'rm -rf {}/build/{}'.format(env.workspace, artifact))
        print green('Deployment in {} finished'.format(environement))