Skip to content

3kwa/cherrypy-dotcloud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using CherryPy on dotCloud

We are using tags to iterate through sections of an exploratory tutorial using CherryPy to discover dotCloud:

  1. quickstart from zero to hello in no time
  2. static serving static content
  3. local dev server (made spunkier using Fabric)

Now to explore dotCloud a bit further let's look at the all mighty sessions, central to most web apps and an opportunity to roll out a new scallable service.

The Zen of ...

CherryPy has its own Zen (video). It's first two koans state that:

Common tasks should be fast and easy
Doing nothing should be easier and faster

You want to use sessions in CherryPy, turn them on in the configuration:

'tools.sessions.on' : True

That's all!

The default backend for sessions storage is memory. It works well for most web applications but we want to be ready to scale beyond one CherryPy instance hence will use something schnazier : Redis

Redis on dotCloud

To create a new redis instance on dotCloud we need to add a few line to our build file:

session:
    type: redis

Redis is notorious for its ease of deployement but that is trivial :P How do we connect to that service you may wonder.

$ dotcloud info tutorial.session
cluster: wolverine
config:
    redis_password: lshYSDfQDe
created_at: 1310511988.2404289
ports:
-   name: ssh
    url: ssh://[email protected]:7473
-   name: redis
    url: redis://redis:[email protected]:7474
state: running
type: redis

But we know better than to hard code such things (by the way this are not the real values, we borrowed the output from the dotCloud Redis doc ).

dotCloud environment

When dotCloud builds your application it create a file in the home directory of your services named environment.json. For a Redis service it will contain the following keys:

  • DOTCLOUD_SESSION_REDIS_HOST
  • DOTCLOUD_SESSION_REDIS_LOGIN
  • DOTCLOUD_SESSION_REDIS_PASSWORD
  • DOTCLOUD_SESSION_REDIS_PORT
  • DOTCLOUD_SESSION_REDIS_URL

Note that SESSION is the name of the Redis service we specified in the build file. It can be whatever you want.

with open('/home/dotcloud/environment.json') as f:
    environment = json.load(f)

Is all you need to load the content of environment.json in the aptly name environment variable :P

We can use this information to configure CherryPy but first ...

Dependencies

CherryPy doesn't support Redis as a backend out of the box. The cherrys python package comes to the rescue ;) First let's add 2 lines to our requirements.txt

cherrys>=0.3
hiredis

We talked about it before, straightforward pip freeze format.

Putting it all together

Now we plug in the redis backend into CherryPy (explicit is better than implicit cf PEP20):

import cherrys
cherrypy.lib.sessions.RedisSession = cherrys.RedisSession

And configure our application:

config = {'/' :{
    'tools.sessions.on' : True,
    'tools.sessions.storage_type' : 'redis',
    'tools.sessions.host' : environment['DOTCLOUD_SESSION_REDIS_HOST'],
    'tools.sessions.port' : environment['DOTCLOUD_SESSION_REDIS_PORT'],
    'tools.sessions.password' : environment['DOTCLOUD_SESSION_REDIS_PASSWORD']
}}

Voila : a dummy web application that counts the number of visits :P

Note that we have removed the Fabric file to concentrate on the session subject rather than the environment gymkana.

What's next?

More than likely the last iteration of this tutorial, probably involving a datastore/database of some description (stay tuned). Something a bit more involved that showing the number of time a person visit the page but not much more ;)

About

growing cherries in the cloud

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages