Permalink
Browse files

Improvements to the script and a bunch of fabfile changes.

  • Loading branch information...
1 parent 4b33cf7 commit 135b3956736a9d7482b139af439ff0ecb65e3cb6 @jacobian jacobian committed Mar 10, 2011
Showing with 42 additions and 163 deletions.
  1. +34 −35 fabfile.py
  2. +0 −83 fabfiles/multiple-webs.py
  3. +0 −42 fabfiles/single-server.py
  4. +8 −3 notes/pycon2011/script.txt
View
@@ -1,41 +1,40 @@
"""
-This is my private fabfile I use to automate parts of the teaching of the
-class. It doesn't really serve any pedagogical purpose; see the fabiles in
-``fabfiles/`` for better examples.
+The tutorial's first fabfile: automate deployment onto a single server.
"""
import os
-import cloudservers
from fabric.api import *
-from fabric.contrib import files
-from unipath import FSPath as Path
-CS = cloudservers.CloudServers(os.environ['CLOUD_SERVERS_USERNAME'],
- os.environ['CLOUD_SERVERS_API_KEY'])
-
-env.hosts = ['oscon-web1', 'oscon-web2', 'oscon-db1']
-env.user = 'root'
-
-def bootem():
- servers = []
- flavor = CS.flavors.find(ram=256)
- image = CS.images.find(name="Ubuntu 10.04 LTS (lucid)")
- for name in env.hosts:
- server = CS.servers.create(name, flavor=flavor, image=image)
- servers.append(server)
- print "%s: ips: %s/%s pass: %s" % (name, server.public_ip, server.private_ip, server.adminPass)
-
-def copyid():
- for name in env.hosts:
- local('ssh-copy-id %s' % name)
-
-def setup():
- run('aptitude update && aptitude -y safe-upgrade')
- run('aptitude -y install bash-completion language-pack-en')
- run('echo ". /etc/bash_completion" >> .bashrc')
-
-def killem():
- for name in env.hosts:
- CS.servers.find(name=name).delete()
-
-del Path
+# This is a bit more complicated than needed because I'm using Vagrant
+# for the examples.
+env.hosts = ['pycon-web1']
+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 push():
+ "Push out new code to the server."
+ with cd("%(root)s/django-mingus" % env):
+ sudo("git pull")
+
+ put("mingus-config/web1.py",
+ "%(root)s/django-mingus/mingus/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")
+
+def deploy():
+ "Full deploy: push, buildout, and reload."
+ push()
+ update_dependencies()
+ reload()
View
@@ -1,83 +0,0 @@
-"""
-The tutorial's second fabfile: automate deployment onto multiple web servers.
-
-See ``single-server.py`` for notes on how to run these fabfiles, and for more
-comments.
-"""
-
-from __future__ import with_statement
-
-import os
-from fabric.api import *
-from fabric.contrib import files
-
-env.hosts = ['oscon-web1', 'oscon-web2']
-env.user = 'root'
-env.web_root = "/home/web"
-env.code_root = os.path.join(env.web_root, "django-deployment-workshop")
-env.buildout_root = os.path.join(env.code_root, "fumblerooski-site")
-
-def setup():
- "Set up and bootstrap a new web server."
-
- # Install needed packages
- run('aptitude -y install subversion git-core python-dev postgresql-client build-essential libpq-dev apache2 libapache2-mod-wsgi')
-
- # Make the code directories, and go get the code
- run('mkdir -p %s' % os.path.join(env.web_root, "static"))
- with cd(env.web_root):
- run('git clone git://github.com/jacobian/django-deployment-workshop.git')
-
- # Bootstrap and buildout
- with cd(env.buildout_root):
- run('python bootstrap.py')
- run('./bin/buildout')
-
- # In the real world, the settings file would already contain the correct
- # DATABASE_HOST. Since this is a demo, we'll do something somewhat
- # hackish -- but cool -- and swap in the correct DATABASE_HOST at
- # bootstrap time.
- dbip = prompt("What's the private IP for DB1?")
- files.sed(os.path.join(env.buildout_root, "fski", "settings.py"),
- before = r"^DATABASE_HOST.*$",
- after = r'DATABASE_HOST = "%s"' % dbip.strip())
-
- # Install the Apache conf
- with cd("/etc/apache2"):
- run('rm -rf apache2.conf conf.d/ httpd.conf magic mods-* sites-* ports.conf ')
- run('ln -s /home/web/django-deployment-workshop/apache/apache2.conf .')
-
- # Make the Python eggs directory.
- run('mkdir -m777 -p /var/www/.python-eggs')
-
- # Done - where! Now restart Apache.
- run('invoke-rc.d apache2 restart')
-
-def push():
- "Push out new code to the server."
- with cd(env.code_root):
- run("git pull")
-
-def buildout():
- "Run the buildout remotely."
- with cd(env.buildout_root):
- run("./bin/buildout")
-
-def reload():
- "Reload Apache to pick up new code changes."
- run("invoke-rc.d apache2 reload")
-
-def restart_apache():
- "Restart Apache."
- run("invoke-rc.d apache2 restart")
-
-def flush_cache():
- "Flush memcached."
- run("invoke-rc.d memcached restart")
-
-def deploy():
- "Full deploy: push, buildout, and reload."
- push()
- buildout()
- reload()
- flush_cache()
View
@@ -1,42 +0,0 @@
-"""
-The tutorial's first fabfile: automate deployment onto a single server.
-
-Run with::
-
- fab -f fabfiles/single-server.py <command>
-
-Expects to be run from the parent directory.
-"""
-
-import os
-from fabric.api import *
-
-# I have entries in /etc/hosts which make these names work.
-# If I didn't, I'd just use IP addresses.
-env.hosts = ['pyweb-web1']
-env.user = 'root'
-
-# Constants for where everything lives on the server.
-env.root = "/home/web/myblog"
-
-def push():
- "Push out new code to the server."
- with cd(env.code_root):
- run("git pull")
- put("mingus-config/web1.py", "%(root)s/django-mingus/mingus/settings.py" % env)
- put("mingus-config/mingus.wsgi", "%(root)s/mingus.wsgi" % env)
-
-def update_dependencies():
- "Update Mingus' requirements remotely."
- put("mingus-config/requirements.txt", "%s/requirements.txt")
- run("%(root)s/bin/pip install -r %(rot)s/requirements.txt" % env)
-
-def reload():
- "Reload Apache to pick up new code changes."
- run("invoke-rc.d apache2 reload")
-
-def deploy():
- "Full deploy: push, buildout, and reload."
- push()
- buildout()
- reload()
View
@@ -86,8 +86,11 @@ And create users and database. Root user for convenience::
Wire up the app to PostgreSQL, load data, take a look::
+ web1$ pip install psycopg2
web1$ cd /home/web/myblog/django-mingus/mingus
web1$ vim local_settings.py # DATABASE_HOST = '...'
+ web1$ ./manage.py syncdb
+ web1$ ./manage.py loaddata test_data.json
web1$ ./bin/django runserver 0.0.0.0:8000
Hit ``http://pycon-web1:8000/`` -- *boom*.
@@ -98,6 +101,7 @@ Hit ``http://pycon-web1:8000/`` -- *boom*.
Install Apache and mod_wsgi, and zap away all the Ubuntu cruft::
web1$ aptitude install apache2 libapache2-mod-wsgi
+ web1$ mkdir /home/web/static # DocumentRoot
web1$ cd /etc/apache2/
web1$ rm -rf apache2.conf conf.d/ httpd.conf magic mods-* sites-* ports.conf
@@ -115,9 +119,10 @@ Hit ``http://pycon-web1/`` -- *boom*.
Now try it with Gunicorn::
web1$ pip install gunicorn
- web1$ cd /home/web/myblog/django-mingus/mingus
- web1$ vim settings.py # INSTALLED_APPS += "gunicorn"
- web1$ ./manage.py run_gunicorn
+ web1$ cd /home/web/myblog/django-mingus
+ web1$ gunicorn_django -b 0.0.0.0 mingus/settings.py
+
+Show off a few features of Gunicorn (TTIN, TTOU, etc.).
4. Automation
-------------

0 comments on commit 135b395

Please sign in to comment.