TutorialHardcore App LinuxEngine Challenge yourself with advanced projects for power users GAE: Deploy a w enables you to build scalable apps without worrying about scaling details. Dan Frost looks at how to get your first cloud app off the ground.

oogle App Engine is a platform for developing applications on Google’s infrastructure. Like G other hugely scalable platforms, App Engine gives you a way of deploying to the cloud without the expense of running your own server farm. Unlike other cloud computing solutions, Google App Google App Engine SDK 1.1.8 Engine is specialised – it’s purely for building web applications. You can serve pages, store information and interact with external web servers, but you don’t have access to , disks and databases in the same way you would in a normal environment. While this does mean you might have to re-engineer your application, you do have some very powerful tools to work with. Google App Engine supports Google user accounts, image manipulation, huge data stores and, using the Google data library, interaction with several Google data systems. Google App Engine applications are currently only supported by Python. If this isn’t your favourite programming language, keep an eye out for support coming to others platforms near you in the future.

Working with App Engine

Start by working locally using a developer server – dev_ application: mydemoapp appserver.py – which simulates the live environment. This version: 1 environment, provided by the software development kit, gives runtime: python you a working server, data storage, simulated user accounts api_version: 1 Our and pretty much everything else you need to develop your handlers: expert app. Once you’ve made your latest Web 2.0 masterpiece, - url: /.* Dan Frost deploy the application to Google’s servers using appcfg.py, script: main.py is technical which uploads the app to your App Engine account. (Create This tells App Engine that your application is called director of 3ev, a Brighton-based an account at http://appengine.google.com.) mydemoapp and that all requests should be passed to web development It’s now time to install the development environment. main.py. You can set any script to handle any URL, and you agency. He has You’ll need Python 2.5 installed, after which you can can even use patterns in the configuration to use different developed for the TYPO3 CMS download App Engine for your OS from http://code.google. files in your app: project and is com/appengine/downloads.html. For Linux, unzip and add url: /browse/(.*?)/ currently working on the Involve App Engine to your $PATH: script: /listings/\1.py CMS application export PATH=$PATH:/path/to/google_appengine/ The configuration above passes everything to main.py, so the for 3ev. Check this worked by typing dev_appserver.py on the next step is to create this file and add the usual message: command line – you should get the usual page of help print “Hello, World” . Now create a directory for your application: With this modest application ready to go, fire up the mkdir ~/myapp/ development server using dev_appserver.py and point your Next, create the key file for App Engine: app.yaml. This browser at http://localhost:8080. file tells App Engine where in the directory to find your dev_appserver.py ~/myapp/ application and how to treat each file. Create ~/myapp/app. You can use most standard Python expressions within App yaml containing: Engine, with a few exceptions that mostly relate to filesystem

Last month We used Git to manage distributed software versioning.

100 Linux Format April 2009

LXF117.tut_adv 100 11/2/09 12:42:45 pm Google App Engine Tutorial web application

Google App Engine enables you to build scalable apps without worrying about self.response.out.write(template.render(path, template_ Quick scaling details. Dan Frost looks at how to get your first cloud app off the ground. vars)) tip If you want to include stylesheets, JavaScript, images or any other static files, you first need to tell app.yaml. Add the If you’re new to following to the file, above the url: /.* handler: Python, but still want to try out - url: /style Google App Engine, static_dir: style the first pitfall to Next, create a directory called style and then a file called avoid is setting app.css before including it in index.html: your editor to use spaces instead of tabs. If you don’t, Add some style to your CSS file – I’ll leave it for you to do App Engine will so as you see fit. give you all kinds of colourful errors! Saving models Scaling databases is tricky, but Google has found some clever Shopping carts, memo pads and even SQL designers – to ways of doing this that offer a different approach to traditional see some of what’s possible, browse the featured apps. relational databases. Google’s is at the heart of App Engines’s data access. Add some methods, classes, and even create storage system, which means that you can write applications modules and you’ll find that the environment is quite familiar. that can scale to millions of users and page impressions. BigTable is a distributed storage system designed for Using webapp managing “petabytes of data across thousands of commodity The App Engine environment comes pre-loaded with webapp, servers” (http://labs.google.com/papers/bigtable.html), an MVC framework that enables you to build well-structured but getting started with it in App Engine is simple. It all starts apps in just a few lines. Start by importing the framework with with models, which only take a few lines to create. All models the line and creating a handler, which in webapp is just a in App Engine are classes that extend db.Model, whose simple class that extends webapp.RequestHandler: properties are like fields in normal database . For from google.appengine.ext import webapp example, a simple model might consist of: class ExampleApp(webapp.RequestHandler): class MyNote(db.Model): def get(self): thenote = db.StringProperty(multiline=True) self.response.out.write(‘Hello, well structured world’) date = db.DateTimeProperty(auto_now_add=True) The request handler has two important methods – get() The date property is automatically set to the current time and post(). Get is called for all HTTP GET requests, while thanks to auto_now_add, of which thenote is obviously a post is called for all HTTP POST requests. Below the request string. Models can also contain boolean, integer, floats, blob, handler, you need to register it with webapp and then run the emails and many other property types. webapp main() method: You don’t have to do anything else before using this – just application = webapp.WSGIApplication( jump straight in by creating an instance of the class, setting [(‘/’, ExampleApp)], the properties and calling put(): debug=True) note = MyNote() def main(): note.thenote = “Just a quick note” run_wsgi_app(application) note.put() if __name__ == “__main__”: For our example, we start by creating a model for storing main() comments called Comment: Revisit your app’s URL and you’ll see a rather unimpressive message. Let’s improve it by moving the message into a template – create the file index.html: Logging Hi there! Get into the habit of using logger – ‘import logging’ at the

Hello from the template

top of your app and then use logging throughout: logging.info(“Something’s happening...”) You then include this by altering the get() method: You can view logs via the dashboard – navigate to ‘Logs’ def get(self): and drill down into the detail of each item. template_vars = {}

If you missed last issue Call 0870 837 4773 or +44 1858 438795.

April 2009 Linux Format 101

LXF117.tut_adv 101 11/2/09 12:42:46 pm Tutorial Google App Engine

class Comment(db.Model): lot of cases, looks just like SQL. You need to replace the content = db.StringProperty(multiline=True) contents of get() with a call to the Comment’s GQL method, Quick date = db.DateTimeProperty(auto_now_add=True) which you then pass to the template: author = db.UserProperty() comments = Comment.gql(“ORDER BY date DESC “) tip The next step is to get some data into this, list it and then template_vars = { With the Google add some more interesting properties to it. In order to create ‘comments’ : comments Data Services you some comments, we need to create a form, save the data and } can get lots of then display it. Start by adding the form to index.html: self.response.out.write(template.render(‘index.html’, Google’s data into

template_vars)) your apps. Start by installing gdata in content from each: via http://code. {% for comment in comments %} google.com/