django-vagrant-chef-simple
django-vagrant-chef-simple copied to clipboard
Simple configuration for setting up a development environment for django with vagrant and chef
django-vagrant-chef-simple
This project provides a template for a simple configuration for setting up a development environment for django with vagrant and chef. Honza's django-chef provided much of the code for starting this project. Cookbooks are cloned directly from opscode.
After following the instructions below, you will have a simple bare-bones configuration for your project running. This project uses apache2 with mod_wsgi and mysql as the database. Static files are served directly with apache. This configuration may not be perfect for performance, but it is easy to understand and fairly easy to set up.
Right now this project is only tested with the precise32 box provided on vagrant's website. I plan to expand the project to work with other flavors of Ubuntu as well as CentOS and RedHat soon.
Dependencies
Chef cookbooks included
These are directly cloned into the project instead of included as sub-modules, mostly because of [this suggestion] 9 for chef-solo users.
- apache2 apache2
- apt apt
- build-essential build-essential
- git git
- vim vim
- python python
- mysql mysql
- openssl openssl
- [wkhtmltopdf] wkhtmltopdf-cookbook
Basic Use
-
Start by cloning your django project a folder named "appname" in the main project directory (how to).
cd django-vagrant-chef-simple git clone /path/to/your/project appnameThis project expects manage.py to live one directory below
appname. Thus, the location from the top directory would bedjango-vagrant-chef-simple/appname/myprojectname/manage.py. -
Create a deployment specific requirements file. This will be called via pip to install any python packages required for your project. Mine looks like this:
Django==1.3.1 Pinax==0.9a2 South==0.7.6 django-appconf==0.5 django-compressor==1.0.1 django-debug-toolbar==0.8.5 django-extensions==0.9 django-staticfiles==1.1.2 django-tastypie==0.9.11 ipython==0.13 mimeparse==0.1.3 mysql-python==1.2.3 pinax-theme-bootstrap==0.1.2
Don't forget mysql-python!
- Edit the main VagrantFile under the "django_settings" variable to change:
project_namefromonpointto the name of your project, e.g.myprojectnamepip_requirements_fileto the location of your requirements file, relative to the directoryproject_folder_namesouth_appsto a list of the apps that you want to migrate using South.fixturesto a list of the fixtures you want to be installed in your new database. Thedescriptionfield is only used by vagrant to to display information about what data is being loaded.
-
We suggest you create a fixture called
user_session.jsonto load initial superuser data. You can place this in any fixture directory in your project. You can dump this out of your current installation by running the command:python manage.py dumpdata --indent=2 auth.user
Delete all but the first entry from this file, which should be the superuser account you created when first running manage.py syncdb
-
Create a file called
production_settings.pyat the same level asmanage.py; use this to over-ride anythin insettings.pythat you want changed in production. For example, mine has the following:DEBUG = False TEMPLATE_DEBUG = DEBUG SERVE_MEDIA = DEBUG DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", # Add "postgresql_psycopg2", "postgresql", "mysql", "sqlite3" or "oracle". "NAME": "appname", # Or path to database file if using sqlite3. "USER": "root", # Not used with sqlite3. "PASSWORD": "iloverandompasswordsbutthiswilldo", # Not used with sqlite3. "HOST": "", # Set to empty string for localhost. Not used with sqlite3. "PORT": "3306", # Set to empty string for default. Not used with sqlite3. } } -
Start vagrant box (Windows command console).
vagrant up
After executing this command you should see your site [here] 4
-
When finished, destroy box (Windows command console)
vagrant destroy
More commands specific to dealing with Vagrant boxes can be in [the Vagrant tutorial] 8.
Setting up Fabric
Fabric fabric is a python module that allows you to execute commands remotely on multiple remote locations simultaneously, simplifying server administration.
Using Fabric requires installation of the pycrypto modules. These are compiled C modules, so the easiest way to get them working for Windows is to download the exe directly and install this into your virtual_env using easy_install.
- Download the latest exe of pycrypto [here] pycrypto
- make sure to use the 32 bit version if using 32 bit python, even if you're running 64 bit Windows
-
Install (make sure you activated the correct virtual_env)
easy_install pycrypto-2.6.win32-py3.3.exe -
Install fabric with pip
pip install fabric -
Test your installation by trying [the tutorial] fabric_tutorial
Tips
-
Additional cookbooks can be found [here] 5
-
To ssh into the box, use this command with cygwin on Windows. Your project files will be found in the
/vagrant/appnamedirectory.ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no [email protected] -p 2222 -
Try using a different distro if you experience problems when adding a recipe to this project
-
For me, the mysql recipe would not install correctly with lucid64, but worked fine with precise32
-
More boxes can be found here
-
Check apache cookbook attributes to see where logfiles are created and other similar details
TODO
-
Check compatability with other distros, esp. CentOS
-
Boxes [here] (http://www.vagrantbox.es/)
-
Brach to use either
yumoraptas package manager as seen in apache cookbook -
If yum, make sure epel is installed for RHEL derivatives
-
Refactor to use
attributesfolder to define default params forappnamerecipe -
Refactor to define functions to hook into to make code re-use easier
-
See [example from pip cookbook] (https://github.com/turtlemonvh/django-vagrant-chef-simple/blob/master/deploy/cookbooks/python/providers/pip.rb)
-
Change to [use pip2pi for installing pip packages] (http://stackoverflow.com/questions/12521552/installing-pip-packages-to-a-virtualenv-using-a-download-cache)
-
useful when network is slow, or you don't have any access to internet
-
Get rid of hardcoding of
vagrantdirectory inappnamerecipe -
Document process for using chef without vagrant for installation on a "real" server