Developing

web Projects

 with

Fabric



By Tianyi

Full Stack Web Developer


  • Front end (JavaScript, HTML, CSS)
  • Back end (Python, Ruby,  etc.)
  • Data Modeling (Postgresql, Mysql, NoSQL, etc.)
  • Devops (Nginx, uWSGI, Crontabs, Networking, Monitoring. etc.)
  • We even talk to the business people. 
  • A lot more...

YEAH! We're cool!


The processes of a web project


  • Set up the project
  • Develop the project
  • Deploy the project
  • Fix or add new features 
  • Deploy the project again


Set up a Django project


  • Install system packages (Postgresql, Mysql Git, SVN etc.)
  • Install python tools (pip, virtualenv etc.)
  • Create virtual environment
  • Python packages (Django, South etc.)
  • Set up databases
  • More

Developing the application


  • Write tests
  • Write code
  • Run tests
  • Version control the code

Deploy the application


  • Do all the tasks listed in start a Django project.
  • Run tests, make sure no failing tests.
  • Set up Nginx, uWSGI etc.


Boring parts


  • Install system packages
  • Install python tools
  • Create virtual environment
  • Python packages
  • Set up database
  • Run Syncdb, data migration or import data
  • Run tests make sure nothing fails
  • Set up uWSGI
  • Set up Nginx

Fun Parts


  • Write code
  • Watch your application working


We Just Wanna Have Some Fun



Fabric

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks


Okay, it sounds pretty good, but it must be hard to use :(

Fabric Tasks


from fabric.api import env, local, task, settings

env.hosts = ['123.123.1.1'] # Some remote hosts ip

@task
def install_postgres_locally():
    with settings(user='root'):
        local('apt-get install postgresql postgresql-client')

@task 
def install_postgres_remotely(): with settings(user='root'): run('apt-get install postgresql postgresql-client')

fab install_postgres_locally

fab install_postgres_remotely

fabtools

fabtools includes useful functions to help you write your Fabric files.

fabtools makes it easier to manage system users, packages, databases, etc.



from fabric.api import local, task, settings

from fabtools import deb, arch

@task 
install_postgres_deb(): with settings(user='root'): deb.install('postgresql postgresql-client')


@task
def install_postgres_arch():
with settings(user='root')
arch.install('postgresql postgresql-client')

Demo


Talk is cheap. Show me the code

                       -- Torvalds, Linus (2000-08-25)


Deploying a Django application


  • uWSGI
  • Nginx



Configuration files


Normal we can just have uWSGI, Nginx, Postgresql configuration  files ready, and use Fabric command to copy the files over.


put('/path/to/local/uwsgi.ini, '/path/to/remote/uwsgi.ini')  


We can go further than this :)

Jinja2

JJinja2 is a full featured template engine for Python. It has full unicode support, an optional integrated sandboxed execution environment, widely used and BSD licensed.



We can make writing configuration files even easier.

Jinja2


# the base directory (full path)
{% if chdir %}
chdir = {{ chdir }}
{% endif %}
# Django's wsgi file
{% if project_name %}
module = {{ project_name }}.wsgi
{% endif %}
{% if virtualenv_path %}
virtualenv = {{ virtualenv_path }}
{% endif %}
# process-related settings
master = true 
{% if processes %}
processes = {{ processes }}
{% else %}
processes = 4
{% endif %}
...

Fabric Tasks


from fabric.api import env, task, settings, run, cd

from jinja2 import Environment, FileSystemLoader


@task def create_uwsgi_conf(): templateLoader = FileSystemLoader(searchpath="templates") template_env = Environment(loader=templateLoader) template = template_env.get_template('uwsgi.ini') uwsgi_params = {} uwsgi_params['chdir'] = env.project_path uwsgi_params['project_name'] = env.project_name uwsgi_params['virtualenv_path'] = env.virtualenv_path uwsgi_params['processes'] = 10 uwsgi_params['socket_path'] = env.project_path with open('./uwsgi.ini', 'w') as output_file: output_file.write(template.render(uwsgi_params))

Demo


Hope this will work......

Problems with Fabric


  • It's just a thin wrapper, you will still need to write good a bit of code.
  • Local and remote need to use different commands




But it's not all too bad

  • Add one task at a time then put them together
  • Can write some wrapper around local and remote commands

Questions ?


Please ask me something :)

Don't be afraid of embarrassing me :)



@3quarterstack


https://github.com/tianyi33


Developing web projects with Fabric

By Tianyi Wang

Developing web projects with Fabric

Developing web projects with Fabric

  • 2,851