92
Symfony in the Cloud Kris Wallsmith February 17, 2010 Wednesday, February 17, 2010

Symfony in the Cloud

Embed Size (px)

DESCRIPTION

One user's experience as Lead Developer on Nebul.us upgrading a symfony application for deployment to Amazon's cloud-based infrastructure. We will discuss the obstacles met along the way, including juggling master and slave database connections and writing uploaded files to S3. Additionally, integration with the symfony configuration and environments system will be demonstrated, allowing one application to be developed locally and seamlessly deployed to the cloud.

Citation preview

Page 1: Symfony in the Cloud

Symfony in the CloudKris Wallsmith

February 17, 2010

Wednesday, February 17, 2010

Page 2: Symfony in the Cloud

Kris Wallsmith

• Freelance web developer, consultant, training, audits...

• 10 years using PHP

• 3 years using symfony

• Author of (too) many plugins

• 1 year on symfony core team

• JavaScript (moo)

• @kriswallsmith

• CTO of Nebul.us

Wednesday, February 17, 2010

Page 3: Symfony in the Cloud

Wednesday, February 17, 2010

Page 4: Symfony in the Cloud

What is Nebul.us ?

• Nebul.us is a vibrant and visual life aggregator. In real time you can seewhat your friends and the public are sharing and discovering online!

• Passively share what you’re doing online

• Show don’t tell

Wednesday, February 17, 2010

Page 5: Symfony in the Cloud

Wednesday, February 17, 2010

Page 6: Symfony in the Cloud

Wednesday, February 17, 2010

Page 7: Symfony in the Cloud

• Symfony Framework

• Doctrine ORM

• Zend Framework

• Swift mailer

• and more…

What is Nebul.us really?

Wednesday, February 17, 2010

Page 8: Symfony in the Cloud

• Web site

• XML services (Flash and Atom)

• JSON services (browser plugins)

• Up to 12 requests per minute when browsing

What is Nebul.us really?

Wednesday, February 17, 2010

Page 9: Symfony in the Cloud

Prototype

Wednesday, February 17, 2010

Page 10: Symfony in the Cloud

HTTP

PHP

MySQ

L

Uploads

ServerGrove VPSSessions

Local Filesystem

Prototype

Wednesday, February 17, 2010

Page 11: Symfony in the Cloud

What is “The Cloud”?

Wednesday, February 17, 2010

Page 12: Symfony in the Cloud

I don’t know.

Wednesday, February 17, 2010

Page 13: Symfony in the Cloud

What is “The Cloud”?

• Adds a (big) black box to your stack

• Several virtualized resources

• Scale based on the level of traffic

• Pay only for what you use

• Elastic!

Wednesday, February 17, 2010

Page 14: Symfony in the Cloud

It’s cloudy out there

• Amazon

• Rackspace

• Microsoft

• Rightscale

Wednesday, February 17, 2010

Page 15: Symfony in the Cloud

Wednesday, February 17, 2010

Page 16: Symfony in the Cloud

Production

Wednesday, February 17, 2010

Page 17: Symfony in the Cloud

Production

HTTP

PHP

MySQ

L

Uploads

ServerGrove VPSSessions

Local Filesystem

Wednesday, February 17, 2010

Page 18: Symfony in the Cloud

Production

HTTP

PHP

MySQ

L

Uploads

ServerGrove VPSSessions

Local Filesystem

Wednesday, February 17, 2010

Page 19: Symfony in the Cloud

No really, it’s that easy.

Wednesday, February 17, 2010

Page 20: Symfony in the Cloud

Writing a scalablesymfony application

Kris Wallsmith

February 17, 2010

Wednesday, February 17, 2010

Page 21: Symfony in the Cloud

Upgrade Points

• Database connections

• File uploads

• Session storage

• Local development

• Deploying

Wednesday, February 17, 2010

Page 22: Symfony in the Cloud

Database Connections

Wednesday, February 17, 2010

Page 23: Symfony in the Cloud

Database Connections

• Typical database configuration

Wednesday, February 17, 2010

Page 24: Symfony in the Cloud

Wednesday, February 17, 2010

Page 25: Symfony in the Cloud

Wednesday, February 17, 2010

Page 26: Symfony in the Cloud

Database Connections

• One writable “master” connection

• One or more read-only “slave” connections

Wednesday, February 17, 2010

Page 27: Symfony in the Cloud

Managing Connections

• Organize read and write connections using a naming convention

• Choose a random read connection

Wednesday, February 17, 2010

Page 28: Symfony in the Cloud

Wednesday, February 17, 2010

Page 29: Symfony in the Cloud

Wednesday, February 17, 2010

Page 30: Symfony in the Cloud

Extend the Doctrine ORM

• Use the appropriate connection

• Doctrine_Query

• Doctrine_Record

• Doctrine_Collection

Wednesday, February 17, 2010

Page 31: Symfony in the Cloud

Wednesday, February 17, 2010

Page 32: Symfony in the Cloud

Wednesday, February 17, 2010

Page 33: Symfony in the Cloud

Wednesday, February 17, 2010

Page 34: Symfony in the Cloud

Configure Doctrine

• Set custom query and collection class attributes in manager

• Set custom record class in builder options

Wednesday, February 17, 2010

Page 35: Symfony in the Cloud

Wednesday, February 17, 2010

Page 36: Symfony in the Cloud

Using the Doctrine DBAL

• Doctrine_Manager::connection()

• $table->getConnection()

Wednesday, February 17, 2010

Page 37: Symfony in the Cloud

Wednesday, February 17, 2010

Page 38: Symfony in the Cloud

Wednesday, February 17, 2010

Page 39: Symfony in the Cloud

Using the Doctrine DBAL

• Doctrine_Manager::getInstance() ->getConnection('master')

• $context->getDatabaseManager() ->getDatabase('master') ->getDoctrineConnection()

• $configuration ->getWriteConnection()

Wednesday, February 17, 2010

Page 40: Symfony in the Cloud

How do we test this?

Wednesday, February 17, 2010

Page 41: Symfony in the Cloud

Connection Listener

• Listen to every connection

• Compare type of query to type of connection

Wednesday, February 17, 2010

Page 42: Symfony in the Cloud

Wednesday, February 17, 2010

Page 43: Symfony in the Cloud

Wednesday, February 17, 2010

Page 44: Symfony in the Cloud

Connection Listener

• Add connection listener to debugmode and test suite

Wednesday, February 17, 2010

Page 45: Symfony in the Cloud

Wednesday, February 17, 2010

Page 46: Symfony in the Cloud

Multiple Connections

• Configure multiple connections to the same database

Wednesday, February 17, 2010

Page 47: Symfony in the Cloud

Wednesday, February 17, 2010

Page 48: Symfony in the Cloud

Do you want it?Introducing sfDoctrineMasterSlavePlugin

Wednesday, February 17, 2010

Page 49: Symfony in the Cloud

File Uploads

Wednesday, February 17, 2010

Page 50: Symfony in the Cloud

File Uploads

• Typical upload form

Wednesday, February 17, 2010

Page 51: Symfony in the Cloud

Wednesday, February 17, 2010

Page 52: Symfony in the Cloud

Wednesday, February 17, 2010

Page 53: Symfony in the Cloud

View Layer

• Render the uploaded image

Wednesday, February 17, 2010

Page 54: Symfony in the Cloud

Wednesday, February 17, 2010

Page 55: Symfony in the Cloud

Amazon S3 Integration

• Must handle file uploads

• Must integrate with the view layer

• Must be disable-able

Wednesday, February 17, 2010

Page 56: Symfony in the Cloud

Stream Wrapper

• Zend Amazon S3 stream wrapper

• s3://mybucket/image.jpg

Wednesday, February 17, 2010

Page 57: Symfony in the Cloud

Wednesday, February 17, 2010

Page 58: Symfony in the Cloud

Read / write paths

• Configure read and write upload paths

Wednesday, February 17, 2010

Page 59: Symfony in the Cloud

Wednesday, February 17, 2010

Page 60: Symfony in the Cloud

Configure Amazon S3

• Toggle integration on/off

• Environment buckets

Wednesday, February 17, 2010

Page 61: Symfony in the Cloud

Wednesday, February 17, 2010

Page 62: Symfony in the Cloud

Configure File Uploads

• Upload to the write directory

• Custom validated file class

Wednesday, February 17, 2010

Page 63: Symfony in the Cloud

Wednesday, February 17, 2010

Page 64: Symfony in the Cloud

Wednesday, February 17, 2010

Page 65: Symfony in the Cloud

Amazon S3 ACL

• ACL defaults to private

• Extend and change to public-read

Wednesday, February 17, 2010

Page 66: Symfony in the Cloud

Pssst,

Matthew

...

Wednesday, February 17, 2010

Page 67: Symfony in the Cloud

Wednesday, February 17, 2010

Page 68: Symfony in the Cloud

Wednesday, February 17, 2010

Page 69: Symfony in the Cloud

View Layer

• Nothing magic

• Custom helper function that uses the configured upload read path

• Search and replace…

Wednesday, February 17, 2010

Page 70: Symfony in the Cloud

Wednesday, February 17, 2010

Page 71: Symfony in the Cloud

Wednesday, February 17, 2010

Page 72: Symfony in the Cloud

Wednesday, February 17, 2010

Page 73: Symfony in the Cloud

Session Storage

Wednesday, February 17, 2010

Page 74: Symfony in the Cloud

Session Storage

• Default factories configuration

Wednesday, February 17, 2010

Page 75: Symfony in the Cloud

Wednesday, February 17, 2010

Page 76: Symfony in the Cloud

Wednesday, February 17, 2010

Page 77: Symfony in the Cloud

Session Storage

• Store session data in the database

Wednesday, February 17, 2010

Page 78: Symfony in the Cloud

Database Session Storage

• Migrate the database

Wednesday, February 17, 2010

Page 79: Symfony in the Cloud

Wednesday, February 17, 2010

Page 80: Symfony in the Cloud

Local Development

Wednesday, February 17, 2010

Page 81: Symfony in the Cloud

Local Development

• One database

• Save uploads to the local filesystem

• Present correct upload path in view layer

Wednesday, February 17, 2010

Page 82: Symfony in the Cloud

Deploying

Wednesday, February 17, 2010

Page 83: Symfony in the Cloud

Deploying

• Subversion

• svn update

• svn checkout + symlink

Wednesday, February 17, 2010

Page 84: Symfony in the Cloud

Deploy with svn update

1. Disable

2. Update the working copy

3. Rebuild model files

4. Migrate the database

5. Enable

Wednesday, February 17, 2010

Page 85: Symfony in the Cloud

Deploy with svn checkout

1. Checkout a fresh working copy

2. Setup files not in the repository

3. Build model files

4. Disable current working copy

5. Migrate the database

6. Update symlink

Wednesday, February 17, 2010

Page 86: Symfony in the Cloud

Deploy with svn update

1. Disable

2. Update the working copy

3. Build model files

4. Migrate the database

5. Enable

Wednesday, February 17, 2010

Page 87: Symfony in the Cloud

Deploy with svn checkout

1. Checkout a fresh working copy

2. Setup files not in the repository

3. Build model files

4. Disable current working copy

5. Migrate the database

6. Symlink the new working copy

Wednesday, February 17, 2010

Page 88: Symfony in the Cloud

Deployment Strategies

• svn update

• fewer steps

• svn checkout + symlink

• less downtime

Wednesday, February 17, 2010

Page 89: Symfony in the Cloud

Automated Deployment

• Rightscale API

Wednesday, February 17, 2010

Page 90: Symfony in the Cloud

Wednesday, February 17, 2010

Page 91: Symfony in the Cloud

Wednesday, February 17, 2010

Page 92: Symfony in the Cloud

Questions?

Wednesday, February 17, 2010