stress free deployment - confoo 2011
TRANSCRIPT
![Page 1: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/1.jpg)
Stress-freeDeployment
Rob Allen
ConFoo March 2011
twitter: @akrabat
![Page 2: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/2.jpg)
Rob Allen?• PHP developer since 1999• Wrote Zend_Config• Tutorial at akrabat.com• Zend Framework in Action!
Next book!
Zend Framework 2 in Action
(with Ryan Mauger)
![Page 3: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/3.jpg)
Why automate deployment?
![Page 4: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/4.jpg)
Getting your house in order
![Page 5: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/5.jpg)
Source code control
![Page 6: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/6.jpg)
Branch!• Branch every new feature• (that includes bug fixes)
• Be ready go live at all times• Trunk deployment• Live branch deployment
![Page 7: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/7.jpg)
Trunk deployment
![Page 8: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/8.jpg)
Live branch deployment
![Page 9: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/9.jpg)
This is what I do
![Page 10: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/10.jpg)
Database considerations
![Page 11: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/11.jpg)
One master database• Live database holds master structure• Copy master to everywhere else• Advantages: • Simple to implement
• Disadvantages: • Backwards compatibility required• Doesnʼt scale for multiple devs well• Easy to make mistakes
![Page 12: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/12.jpg)
Migrations• Versioned schemas• Use delta files with UP and DOWN functions• Advantages: • version controlled• destructive changes possible (but donʼt do it!)
• Disadvantages:• Canʼt think of any!
![Page 13: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/13.jpg)
Migrations tools• DbDeploy• LiquiBase• Framework specific• Doctrine• Akrabat_Db_Schema_Manager• Cake migrations• PEAR: MDB2_Schema
• Home-brew script
![Page 14: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/14.jpg)
For more info“Database version control without the pain”by Harrie Verveer
http://slidesha.re/gwq0aw
Also read http://www.harrieverveer.com/?p=247
![Page 15: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/15.jpg)
Code considerations
![Page 16: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/16.jpg)
Context awareness• Configuration based on where the code has been
deployed• Automatic• Automatic detection based on URL?• Environment variable set in vhost defintion?
• Local configuration file
![Page 17: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/17.jpg)
So whatʼs deployment all about?
![Page 18: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/18.jpg)
Things to think about• Transport to server• FTP? rsync? svn checkout? svn export?
• File permissions• Preserve user uploaded files• Steps after upload• Stale cache?• Cache priming?
![Page 19: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/19.jpg)
Server organisation• Much easier if you control vhosts• For multiple sites on same server:• Use predictable file locations for all sites• e.g:• /home/www/{site name}/live/current• /home/www/{site name}/staging/current
• Multiple servers for one site:• Keep them the same!
![Page 20: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/20.jpg)
The deployment plan
![Page 21: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/21.jpg)
Typical steps• Tag this release• Set “under maintenance” page• Transfer files to server• Set file permissions as required• Delete old cache files• Run database migrations if required• Remove “under maintenance” page
![Page 22: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/22.jpg)
Hereʼs mine:1. Branch trunk to release-{yymmdd-hhmm}2. ssh into server3. Ensure staging is up to date (svn st -u)
If not, stop here!4. svn checkout new release branch to a new
folder in live directory5. Set permissions on the /tmp folder for cache
files
![Page 23: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/23.jpg)
Finally• Switch “current” symlink to new directory
![Page 24: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/24.jpg)
Tools for automation
![Page 25: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/25.jpg)
Simple scripts• PHP, Bash or Bat files• Simple to write and run• Execute command line apps via exec()
![Page 26: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/26.jpg)
Example PHP script$cmd = "svn cp -m \"Tag for automatic deployment\" $baseUrl/$website/trunk $baseUrl/$website/tags/$date";
ob_start();system($cmd, $returnValue);$output = ob_get_clean();
if (0 < $returnValue) { throw new Exception("Tagging failed.\n" . $output);}echo "Tagged to $date\n";
![Page 27: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/27.jpg)
Phing• PHP based build system based on Ant• XML configuration files• PEAR installation• Integration with Subversion and DbDeploy• Expects to run build.xml in current directory• build.properties contains config info
![Page 28: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/28.jpg)
Phing philosophy"• Like make, build scripts consist of targets• Targets can depend on other targets• “live” depends on “tag”, “checkout”, “migrate”
• Each target does the minimum it cane.g.• Create svn tag• checkout files to destination• migrate database
![Page 29: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/29.jpg)
Example build.xml<?xml version="1.0" encoding="UTF-8" ?><project name="BRIBuild" default="deploy" basedir="."> <tstamp> <format property="date" pattern="%Y%m%d-%H%M" /> </tstamp> <property file="build.properties" /> <property name="trunkpath" value="${svnpath}/${website}/trunk" /> <property name="tagpath" value="${svnpath}/${website}/tags/${date}" />
<target name="deploy" depends="tag" /> <target name="tag" description="Tag trunk"> <exec command="svn cp -m 'Tag for automatic deployment' ${trunkpath} ${tagpath}" /> <echo msg="Tagged trunk to ${date}" /> </target></project>
![Page 30: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/30.jpg)
My deployment system
![Page 31: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/31.jpg)
deploy.php~$ deploy.php 23100BRI server side deploy scriptVersion 1.1, 2009
Found /home/domains/bigroom/live/Tagging to 20091030-2303... doneDeploying 20091030-2303 ... doneChanging symlink to new checkoutCleaning up older checkouts23100 successfully deployed to /home/domains/bigroom/live/
![Page 32: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/32.jpg)
Our server layout
![Page 33: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/33.jpg)
deploy.php• Custom PHP script• Relies on environment variable: WWW_DIR• Advantages:• Custom designed to fit our way of working• PHP! Quick and easy to write.
• Disadvantages:• Hard to alter for a specific server• Hard to change methodology
![Page 34: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/34.jpg)
FTP using Phing (1)<project name="project" basedir="." default="deploy"> <property file="build.properties" /> <property name="trunkpath" value="${svnpath}/${website}/trunk" /> <fileset dir="${exportdir}/" id="files"> <include name="**/*" /> </fileset> <target name="deploy" depends="svnexport,ftp-upload" /> <target name="svnexport"> <delete dir="${exportdir}" /> <svnexport username="${username}" password="${password}" nocache="true" force="true" repositoryurl="${trunkpath}" todir="${exportdir}" /> </target>
![Page 35: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/35.jpg)
FTP using Phing (2) <target name="ftp-upload"> <echo msg="Deploying application files" /> <ftpdeploy host="${ftp.host}" port="${ftp.port}" username="${ftp.username}" password="${ftp.password}" dir="${ftp.dir}"> <fileset refid="${files}" /> </ftpdeploy> </target> </project>
![Page 36: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/36.jpg)
FTP with Phing• Per-website build.xml for custom deployments• Advantages:• Leverages other peopleʼs experiences• Was very fast to create• Works where ssh not available!
• Disadvantages:• New technology to be learnt• Phing beta and Pear_Version_SVN alpha
![Page 37: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/37.jpg)
Rollback
![Page 38: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/38.jpg)
Emergency roll-back
Just change the symlink!
![Page 39: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/39.jpg)
Complete roll-back• Write rollback.php or create a Phing build
task
• Put the server back to where it was before• Change the symlink• Delete the deployed directory
• Database rollback• Run down() delta in your migration tool
![Page 40: Stress Free Deployment - Confoo 2011](https://reader033.vdocuments.mx/reader033/viewer/2022052906/558a0f24d8b42a69778b46a4/html5/thumbnails/40.jpg)
To summarise
1. Automated deployment prevents mistakes2. Itʼs not hard3. Easy roll-back is priceless