The document introduces Google App Engine (GAE). It discusses that GAE allows developers to build applications that run on Google's infrastructure, providing scalability and efficiency. It also overview cloud computing concepts and GAE's features like dynamic web applications, data storage, and additional services. Finally, it provides a toy example of a GAE application and how to develop applications using the Python SDK.
1. Introduction to Google App Engine Andrea Spadaccini - @lupino3 May 22nd, 2010 - Catania – Italy [email_address]
2. Agenda Overview Developing applications with Google App Engine Toy example Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
4. What is GAE? Google App Engine (GAE) is a service, offered by Google, that allows developers to build applications that can run on Google's infrastructure. It is a form of Cloud Computing. Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
5. Cloud computing Abstraction of details like hardware configuration, physical location of data, operating system. Internet-based computing. Cloud : metaphor inspired by the drawing used to represent the Internet (and, earlier, the PSTN) in network diagrams Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
6. Cloud computing Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Traditional client/server paradigm Client N Client 1 Server Internet
7. Cloud computing Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Cloud Computing Client N Client 1 Internet Server Server Server
8. Cloud layers Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Software as a Service Platform as a Service Infrastructure as a Service HW / Net IaaS Paas SaaS
9. Cloud layers Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Software as a Service Platform as a Service Infrastructure as a Service HW / Net IaaS Paas SaaS
10. GAE Features Run dynamic web applications (request / response) Serve static files Data storage (BigTable) Google infrastructure -> scalability, efficiency Additional features (mail, XMPP, Google Accounts, cron, WebServices, memcache, image manip.) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
11. App Engine datastore The App Engine Datastore is a distributed efficient storage designed for web applications. Entity : data object to be stored. Contains attributes and is identified by a key . Queries must be run against indexes . It's not a relational DB. There are no joins (or aggregate queries like count(*)). The schema is enforced by the application, not by the database. Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
14. Which language? Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Frame taken from Matrix (1999)
15. Which language? Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
16. Which language? Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
17. Hello world! # helloworld.py print “Content-Type: text/html” print print “Hello world” # app.yaml application : gtugct-helloworld version : 1 runtime : python api-version : 1 handlers : - uri : /.* script : helloworld.py Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Taken from App Engine Intro – see References
18. The GAE dev cycle 10 Develop - <your favourite tools here> (hint: ViM) 20 Test - dev_appserver.py 30 Deploy - appcfg.py 40 Repeat - GOTO 10 Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine With the Python SDK
19. Paradigm and features Request / response (max 30 seconds!) URL ↔ script mapping Web Frameworks: GAE comes with webapp , a Web Framework developed by Google. You can use Django (but not its ORM!). Web-based administration console Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
21. Answr It! Answr.it by Vanni Rizzo, Pierluigi D'Antrassi, Andrea Spadaccini - http://www.answr.it Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
22. Data modeling (I) # models.py from google.appengine.ext import db class Answr(db.Model): text = db.StringProperty() rand = db.FloatProperty() def to_json(self): return '{"text" : "%s"}' % self.text @staticmethod def get_random(): def add_answr(answr_text, rand = None): Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
23. Data modeling (II) # models.py (continued) class ApplicationData(db.Model): name = db.StringProperty() value = db.IntegerProperty() @staticmethod def getAnswrCounter(): def incrementAnswrCounter(): ... Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
24. Main page code from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from models import Answr class AnswrApp(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'application/json' random_answr = Answr.get_random() self.response.out.write(random_answr.to_json()) application = webapp.WSGIApplication([('/answr', AnswrApp)], debug = True) if __name__ == '__main__': run_wsgi_app(application) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
25. app.yaml application: answr-it version: 10 runtime: python api_version: 1 handlers: - url: /answr script: main.py - url: / static_files: static/index.html upload: static/index.html - url: /css (same for /img e /js) static_dir: static/css Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
26. Users API from models import Answr from google.appengine.api import users [.. omissis ..] class PopulatePage(webapp.RequestHandler): def get(self): user = users.get_current_user() if not user: print "<a href = '%s'>Log in with your Google Account</a>" % users.create_login_url(self.request.path) elif users.is_current_user_admin(): print "Populating the DB..." answrs = [ .. omissis .. ] for answr_text in answrs: Answr.add_answr(answr_text) Answr.add_answr("Something strange is going on..", 1.0) else : logging.warning('Unauthorized access to /populate') Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
27. Live demo http://answr-it.appspot.com .. or .. http://www.answr.it Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
28. Twitter bot (cron + WS) # twitterbot.py import tweepy from models import ApplicationData, Answr # 1. Login auth = tweepy.BasicAuthHandler("answrit", "the_password") api = tweepy.API(auth) logging.info('Login done!') # 2. Follow back who follows me followers = api.followers() friends = api.friends() to_follow = [x for x in followers if x not in friends] for user in to_follow: try : user.follow() except tweepy.TweepError, e: logging.warning(e) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
29. Twitter bot (cron + WS) # twitterbot.py (cont.) # 3. Read last mentions last_id = ApplicationData.getLastAnswredTweetId() if not last_id: mentions = api.mentions(count = 200) else : mentions = api.mentions(count = 200, since_id = last_id) # 4. Random answr for status in mentions: answr = Answr.get_random() api.update_status("@%s %s" % (status.author.screen_name, answr.text), status.id) if status.id > last_id: last_id = status.id # 5. Save the last ID if last_id: ApplicationData.setLastAnswredTweetId(last_id) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
30. Twitter bot (cron + WS) # cron.yaml cron: - description: answr to people url: /twitterbot schedule: every 1 mins # (part of) app.yaml - url: /twitterbot login: admin script: twitterbot.py Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine DEMO! (if Murphy allows it..) Tweet something to @answrit (like: @answrit: are you ok?)
31. Chat bot (XMPP) # (part of) xmpp.py from google.appengine.api import xmpp from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from models import Answr class XMPPHandler(webapp.RequestHandler): def post(self): message = xmpp.Message(self.request.POST) message.reply(Answr.get_random().text) application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)], debug=True) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
32. Chat bot (XMPP) # (significant part of) app.yaml - url: /_ah/xmpp/message/chat/ script: xmpp.py inbound_services: - xmpp_message Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine DEMO! Add to your XMPP buddy list [email_address] (GTalk is fine), and ask him questions!
33. References http://en.wikipedia.org - Articles on Cloud Computing, Affero GPL and others Google Inc – aa.vv. - “Bigtable: a distributed storage system for structured data” - OSDI 2006 Dan Sanderson - “Programming Google App Engine” - O' Reilly / Google Press – 2009 Google App Engine home page - http://www.appspot.com Simon Willison - “I've (probably) been using Google App Engine for a week longer than you have” - Slides from Barcamp London 4 – 2008” (referred in this document as App Engine Intro ) Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
37. KVM Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
38. Cloud Freedom - AGPL Software licenses like the General Public License state rights of those who get the program, not of those who use the program via a network The GNU Affero General Public License (AGPL) require the availability of source code when the licensed code is deployed as a network service. http://www.gnu.org/licenses/agpl-3.0.html Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine
39. Python vs. Java Image taken from stacktrace.it: http://stacktrace.it/2009/05/google-app-engine/ Andrea Spadaccini - @lupino3 – Introduzione a Google App Engine