Welcome, guest | Sign In | My Account | Store | Cart

This recipe shows a management command for django project that enables automatic synchronization between the local and remote project. Of course, the deploy exclude to overwrite the settings.py located in the remote server.

This is a clean way to deploy your own project without calling external commands, although this command needs rsync to work.

Python, 61 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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
from django.core.management.base import BaseCommand, CommandError

from django.conf import settings

import commands, os
from optparse import make_option



class Command(BaseCommand):

    option_list = BaseCommand.option_list + (
                make_option('--test', '-t', 
                            dest='test',
                            action='store_true',
                            default=False,
                            help='Simulate the synchronization.'),
    )
    help = 'It allows to sync your develop site project with the remote site repository.'

    def handle(self, *args, **options):
        """
        List of options in rsync command:
        -a:     Preserve the attibutes of the files.
        -v:     Verbose.
        -z:     Enables the compression.
        -u:     Update files.
        -r:     Recursive.
        -E:     Preserve Executability.
        -h:     Human readable.
        -n:     Simulate.
        """
        # Check the rsync command
        st, out = commands.getstatusoutput('rsync --version')
        if st !=0:
            self.stderr.write('Error: To use this command you need the rsync command.\n')
            exit(1)
        
            
        # Check for the settings parameters            
        try:
            # Deploy data
            USER_SERVER = settings.USER_SERVER
            DOMAIN_SERVER = settings.DOMAIN_SERVER
            DIR_SERVER = settings.DIR_SERVER
            DIR_LOCAL = settings.DIR_LOCAL
        except AttributeError:
            self.stderr.write('Error: You have to define the parameters in settings.py.\n')
            exit(1)
            

            
        opts = '-uhzravE --exclude="*.pyc" --exclude=settings.py --exclude="mydata.db"'
        if options['test']:
            opts = opts+' -n'
            
        os.system('rsync '+opts+' '+DIR_LOCAL+' '+USER_SERVER+'@'+DOMAIN_SERVER+':'+DIR_SERVER)
        self.stdout.write('\nWARNING: Consider that the file settings.py must be sent manually.\n'+ \
        'Don\'t apply any changes in the server directory except into settings.py\n')
        
        pass

See https://docs.djangoproject.com/en/1.3/howto/custom-management-commands/ to know how to install this command for your project.

The main variable to deploy are located in settings.py and an example is the following: --- settings.py ---- USER_SERVER = 'feel' DOMAIN_SERVER = 'domain.com' DIR_SERVER = '~/my-django-app/' DIR_LOCAL = '~/my-local-django-app/'

So, in the terminal you just type:

./manage deploy

or if you want to simulate the operation:

./manage deploy --test