forked from carljm/django-deployment-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
98 lines (80 loc) · 3.33 KB
/
fabfile.py
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
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
96
97
98
"""
The tutorial's fabfile.
This starts out pretty simple -- just automating deployment on a single
server with a few commands. Then it gets a bit more complex (a basic
provisioning example).
"""
import contextlib
from fabric.api import env, run, cd, sudo, put, require, settings, hide, puts
from fabric.contrib import project, files
# This is a bit more complicated than needed because I'm using Vagrant
# for the examples.
env.hosts = ['pycon-web2']
env.user = 'vagrant'
env.key_filename = '/Library/Ruby/Gems/1.8/gems/vagrant-0.7.2/keys/vagrant'
# Constants for where everything lives on the server.
env.root = "/home/web/myblog"
def deploy():
"Full deploy: push, buildout, and reload."
push()
update_dependencies()
reload()
def push():
"Push out new code to the server."
with cd("%(root)s/django-mingus" % env):
sudo("git pull")
put("mingus-config/local_settings.py",
"%(root)s/django-mingus/mingus/local_settings.py" % env,
use_sudo=True)
put("mingus-config/mingus.wsgi", "%(root)s/mingus.wsgi" % env, use_sudo=True)
def update_dependencies():
"Update Mingus' requirements remotely."
put("mingus-config/requirements.txt", "%s/requirements.txt", use_sudo=True)
sudo("%(root)s/bin/pip install -r %(rot)s/requirements.txt" % env)
def reload():
"Reload Apache to pick up new code changes."
sudo("invoke-rc.d apache2 reload")
#
# OK, simple stuff done. Here's a more complex example: provisioning
# a server the simplistic way.
#
def setup():
"""
Set up (bootstrap) a new server.
This essentially does all the tasks in the script done by hand in one
fell swoop. In the real world this might not be the best way of doing
this -- consider, for example, what the various creation of directories,
git repos, etc. will do if those things already exist. However, it's
a useful example of a more complex Fabric operation.
"""
# Initial setup and package install.
sudo("mkdir -p /home/web/static")
sudo("aptitude update")
sudo("aptitude -y install git-core python-dev python-setuptools "
"postgresql-dev postgresql-client build-essential "
"libpq-dev subversion mercurial apache2 "
"libapache2-mod-wsgi")
# Create the virtualenv.
sudo("easy_install virtualenv")
sudo("virtualenv /home/web/myblog")
sudo("/home/web/myblog/bin/pip install -U pip")
# Check out Mingus
with cd("/home/web/myblog"):
sudo("git clone git://github.com/montylounge/django-mingus.git")
# Set up Apache
with cd("/home/web/"):
sudo("git clone git://github.com/jacobian/django-deployment-workshop.git")
with cd("/etc/apache2"):
sudo("rm -rf apache2.conf conf.d/ httpd.conf magic mods-* sites-* ports.conf")
sudo("ln -s /home/web/django-deployment-workshop/apache/apache2.conf .")
sudo("ln -s /home/web/django-deployment-workshop/mingus-config/mingus.wsgi /home/web/myblog/mingus.wsgi")
sudo("mkdir -m777 -p /var/www/.python-eggs")
# Now do the normal deploy.
deploy()
def run_chef():
"""
Run Chef-solo on the remote server
"""
project.rsync_project(local_dir='chef', remote_dir='/tmp', delete=True)
sudo('rsync -ar --delete /tmp/chef/ /etc/chef/')
sudo('chef-solo')