Posts tagged Fabric
As a follow up post of Automated deployment with Ubuntu, Fabric and Django here are the slides from my presentation on topic “Automation, Fabric and Django”. Unfortunately there is no audio podcast but if there is interest I can add some comments about each slide as part of this post.
If there is anything that need explanation feel free to ask.
A few months ago I started to play with Fabric and the result was a simple script that automates the creation of a new Django project. In the last months I continued my experiments and extended the script to a full stack for creation and deployment of Django projects.
As the details behind the script like the project structure that I use and the server setup are a bit long I will keep this post only on the script usage and I will write a follow up one describing the project structure and server.
So in a brief the setup that I use consist of Ubuntu as OS, Nginx as web server and uWSGI as application server. The last one is controlled by Upstart. The script is available for download at GitHub.
In a wait for more detailed documentation here is a short description of the main tasks and what they do:
- Creates a new virtual environment
- Installs the predefined packages(uWSGI, Django and South))
- Creates a new Django project from the predefined template
- Creates different configuration files for development and production environment
- Initializes new git repository
- Prompts the user to choose a database type. Then installs database required packages, creates a new database and user with the name of the project and generates the local settings file
- Runs syncdb(if database is selected) and collectstatic
- installs the required packages such as Nginx, PIP, GCC etc
- prompts for a database type and install it
- reboots the server as some of the packages may require it
Once you have a ready server and a project just use this the following task to deploy it to the server. Please have in mind that it should be used only for initial deployment.
- Creates a new virtual environment
- Clones the project from the repository
- Installs the required packages
- Creates symbolic links to the nginx/uwsgi configuration files
- Asks for database engine, creates new DB/User with the project name and updates the settings file
- Calls the update_project task
- Updates the source code
- Installs the required packages(if there are new)
- Runs syncdb, migrate and collect static
- Restart the Upstart job that runs the uWSGI and the Nginx server
The script is still in development so use it on your own risk. Also it reflects my own idea of server/application(I am planning to describe it deeper in a follow up post) setup but I would really like if you try it and give a feedback. Feel free to fork, comment and advice.
Or how automate the creation of new projects with simple script
Preface: Do you remember all this tiny little steps that you have to perform every time when you start new project – create virtual environment, install packages, start and setup Django project? Kind of annoying repetition, isn’t it? How about to automate it a bit.
Solution: Recently I started learning Fabric and thought “What better way to test it in practice than automating a simple, repetitive task?”. So, lets mark the tasks that I want the script to perform:
- Create virtual environment with the project name
- Activate the virtual environment
- Download list of packages and install them
- Make ‘src’ directory where the project source will reside
- Create new Django project in source directory
- Update the settings
Thanks to the local command the first one was easy. The problem was with the second one. Obviously each local command is run autonomously so I had to find some way have activated virtual environment for each task after this. Fortunately the prefix context manager works like a charm. I had some issues making it read and write in the paths I wants and voilà it was working exactly as I want.
The script is too long to place it here but is publicly available at https://gist.github.com/2818562
It is quite simple to use, you only need python, fabric and virtual environment. Then just use the following code.
To Do: Here are few things that can be improved:
- Read the packages from a file
- Update urls.py to enable admin
- Generate Nginx server block file
So this is my first try with Fabric, I hope that you will like it and find it useful. As always any comments, questions and/or improvement ideas are welcome.