tool up your lamp stack

67
Tool Up Your LAMP Stack

Upload: lorna-mitchell

Post on 09-May-2015

4.704 views

Category:

Technology


1 download

DESCRIPTION

Presentation from Agile on the Beach covering some tools to use with your lamp stack to Get Things Done!

TRANSCRIPT

Page 1: Tool Up Your LAMP Stack

Tool Up Your LAMP Stack

Page 2: Tool Up Your LAMP Stack

About Me

2

• Lorna Mitchell

• Web Development Consultant

• Speaker and Author

• Website: http://lornajane.net

• Twitter: @lornajane

Page 3: Tool Up Your LAMP Stack

LAMP

3

Page 4: Tool Up Your LAMP Stack

LAMP

4

• Linux

• Apache

• MySQL

• PHP (or Perl, or Python)

Page 5: Tool Up Your LAMP Stack

Technology and Agile

Page 6: Tool Up Your LAMP Stack

6

Technology is not theproblem

Page 7: Tool Up Your LAMP Stack

7

Technology is not thesolution

Page 8: Tool Up Your LAMP Stack

Technology

8

• We need good tools

Page 9: Tool Up Your LAMP Stack

Technology

8

• We need good tools

• They enable our workflow

Page 10: Tool Up Your LAMP Stack

Technology

8

• We need good tools

• They enable our workflow

• They facilitate our achievements

Page 11: Tool Up Your LAMP Stack

Technology

8

• We need good tools

• They enable our workflow

• They facilitate our achievements

• They allow us to meet our deadlines

Page 12: Tool Up Your LAMP Stack

Technology

8

• We need good tools

• They enable our workflow

• They facilitate our achievements

• They allow us to meet our deadlines

• They are not the silver bullet (sorry)

Page 13: Tool Up Your LAMP Stack

Iterative Development

9

develop deploy

Page 14: Tool Up Your LAMP Stack

The Main Ingredients

10

Preparation time: some years

Ingredients:

• Source control

• Development platforms

• Task tracking

• Automated testing

• Static analysis

• Automated deployment

• Continuous integration

Page 15: Tool Up Your LAMP Stack

Source Control

Page 16: Tool Up Your LAMP Stack

Source Control: Key Ingredient

12

• Central, canonical version

• Collaboration point

• Historical information

• what changed

• when

• by whom

• Can include its own config

Page 17: Tool Up Your LAMP Stack

Source Control Tools

13

• Subversion http://subversion.apache.org/

• Git http://git-scm.com/

• Mercurial http://mercurial.selenic.com/

Page 18: Tool Up Your LAMP Stack

Branching Strategies

14

Common patterns:

• Feature branches

• Version branches

• Live/integration branches

Page 19: Tool Up Your LAMP Stack

Traditional Centralised Source Control

15

repo

checkout checkoutcheckout

Page 20: Tool Up Your LAMP Stack

Distributed Source Control

16

repo

repo

repo

repo

repo

Page 21: Tool Up Your LAMP Stack

Database Version Control

17

No silver bullet to keep code and database schema in sync

Strategies:

• All db changes done via script

• Scripts are numbered

• Database knows what numbers it already has

Tools:

• homespun scripts

• DbDeploy http://dbdeploy.com/

• Liquibase http://www.liquibase.org/

Page 22: Tool Up Your LAMP Stack

Development Platforms

Page 23: Tool Up Your LAMP Stack

Development Platforms

19

Requirements:

• Safe area "sandpit" for developers to work

• All software as-live

• Isolated

Page 24: Tool Up Your LAMP Stack

Task Tracking

Page 25: Tool Up Your LAMP Stack

Task Tracking

21

Once called ’bug tracking’.

We can track what status everything is in.

Page 26: Tool Up Your LAMP Stack

Task Tracking

21

Once called ’bug tracking’.

We can track what status everything is in.

Developers understand bug trackers, bug trackers understand yourworkflow .

Page 27: Tool Up Your LAMP Stack

Workflow

22

Complete

Verify

Active

SprintBacklog

Blocked

Page 28: Tool Up Your LAMP Stack

Task Tracking Tools

23

• Pivotal Tracker http://www.pivotaltracker.com/

• GreenHopperhttp://www.atlassian.com/software/greenhopper/

• Trac http://trac.edgewall.org/

Page 29: Tool Up Your LAMP Stack

Automated Testing

Page 30: Tool Up Your LAMP Stack

25

How do you test a website

?

Page 31: Tool Up Your LAMP Stack

26

How do you test a website

repeatedly

?

Page 32: Tool Up Your LAMP Stack

Automated Testing

27

Gives repeatable results

TDD Test-Driven Development

BDD Behaviour-Driven Development

Page 33: Tool Up Your LAMP Stack

Automated Testing Tools

28

• Selenium: browser-based record and play of tests

• Selenium IDE http://seleniumhq.org/projects/ide/

• Selenium RChttp://seleniumhq.org/projects/remote-control/

• PHPUnit: unit testing and automation

• http://phpunit.de

• Also generates code coverage graphs

Page 34: Tool Up Your LAMP Stack

My First Unit Test

29

require_once '../src/models/MathUtilModel.php' ;

class MathUtilModelTest extends PHPUnit_Framework_TestCase {public function testAddNumbersWithNumbers() {

$util = new MathUtilModel();$result = $util->addNumbers(3,5);$this->assertEquals(8, $result);

}}

Page 35: Tool Up Your LAMP Stack

Running One Test

30

To run our tests, from the tests directory do:

phpunit models/MathUtilModel

Output:

PHPUnit 3.5.13 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.00Mb

OK (1 test, 1 assertion)

Page 36: Tool Up Your LAMP Stack

Testable Code

31

• Testable code is clean and modular

• Need to be able to separate elements to test

• Each function does one thing

• Not too many paths through the code

• Dependencies are dangerous

Page 37: Tool Up Your LAMP Stack

Dependency Injection

32

Passing things in or looking them up.

function getData() {$db = new MyDatabaseObject();// sql and query

}

function getData($db) {// sql and query

}

Page 38: Tool Up Your LAMP Stack

Code Coverage

33

What percentage of your code is tested?

• Summary view

• Drill in to see which lines are run by tests

• Beware: 100% code coverage does not mean fully tested

Use phpunit -coverage-html and specify where PHPUnit shouldwrite the report files

Examples from http://jenkins.joind.in

Page 39: Tool Up Your LAMP Stack

Code Coverage

34

Page 40: Tool Up Your LAMP Stack

Code Coverage

35

Page 41: Tool Up Your LAMP Stack

Static Analysis

Page 42: Tool Up Your LAMP Stack

Static Analysis

37

Evaluating code without running it

Allows us to check for quality, commenting, coding standards

Page 43: Tool Up Your LAMP Stack

Static Analysis Tools

38

• PHP Code Sniffer: checks for coding standards

• http://pear.php.net/PHP_CodeSniffer

• PHP Mess Detector: detects ’bad smells’

• http://phpmd.org/

• PHP Lines of Code: project size, class count

• https://github.com/sebastianbergmann/phploc

Page 44: Tool Up Your LAMP Stack

phploc Sample Output (joind.in)

39

Directories: 32Files: 213

Lines of Code (LOC): 21339Cyclomatic Complexity / Lines of Code: 0.10

Comment Lines of Code (CLOC): 4908Non-Comment Lines of Code (NCLOC): 16431

Namespaces: 0Interfaces: 0Classes: 87

Abstract: 1 (1.15%)Concrete: 86 (98.85%)Average Class Length (NCLOC): 116

Methods: 532Scope:

Non-Static: 532 (100.00%)Static: 0 (0.00%)

Visibility:Public: 501 (94.17%)Non-Public: 31 (5.83%)

Average Method Length (NCLOC): 18Cyclomatic Complexity / Number of Methods: 2.91

Page 45: Tool Up Your LAMP Stack

API Documentation

40

Another form of static analysis is to generate documentation

• Commented documentation in each file, class, function

• Automatically generate into readable documents

• Tools:

• PHPDocumentor http://www.phpdoc.org/

• DocBlox http://www.docblox-project.org/

Page 46: Tool Up Your LAMP Stack

API Documentation

41

Page 47: Tool Up Your LAMP Stack

PHPCS Examples

42

Install:

pear install PHP_CodeSniffer

Run:

phpcs --standard=PEAR example.php

Examples taken from http://bit.ly/kedQrU

Page 48: Tool Up Your LAMP Stack

PHPCS Examples

43

Source code:

class recipe{

protected $_id;

public $name;

public $prep_time;

function getIngredients() {$ingredients = Ingredients::fetchAllById($this->_id);return $ingredients;

}}

Page 49: Tool Up Your LAMP Stack

PHPCS Examples

44

Sniffer output:

FILE: /home/lorna/phpcs/recipe. class .php--------------------------------------------------- --------------------FOUND 8 ERROR(S) AND 0 WARNING(S) AFFECTING 5 LINE(S)--------------------------------------------------- --------------------

2 | ERROR | Missing file doc comment3 | ERROR | Class name must begin with a capital letter3 | ERROR | Missing class doc comment6 | ERROR | Protected member variable "_id" must not be prefixed with

| | underscore12 | ERROR | Missing function doc comment12 | ERROR | Opening brace should be on a new line13 | ERROR | Line indented incorrectly; expected at least 8 sp aces, found13 | ERROR | Spaces must be used to indent lines; tabs are not al lowed

--------------------------------------------------- --------------------

Page 50: Tool Up Your LAMP Stack

Automated Deployment

Page 51: Tool Up Your LAMP Stack

Automated Deployment

46

• How many times do you deploy an agile project?

Page 52: Tool Up Your LAMP Stack

Automated Deployment

46

• How many times do you deploy an agile project?

• Fast

• Hardened

• Painless

• Repeatable

Page 53: Tool Up Your LAMP Stack

Automated Deployment Tools

47

• Phing/Ant: easy automated build scripts

• http://phing.info/

• http://ant.apache.org/

• Capistrano (or Webistrano): scripted releases (with web interface)

• https://github.com/capistrano/capistrano

Page 54: Tool Up Your LAMP Stack

Automating Deployment: Why

48

• Minimise mistakes

• Save time on each deploy

• Better than documentation

• Reliable process - use for different platforms

• Scope for rollback

Page 55: Tool Up Your LAMP Stack

Automating Deployment: What

49

• Application code

• minimal downtime or time in an inconsistent state

• easy rollback

• additional setup steps (upload files, etc) also automated

• Database

• apply database patches

• include rollback patches

• Config changes

• useful for large or complex sites

• config deploys separately, can update quickly and easily

Page 56: Tool Up Your LAMP Stack

Code Deployment

50

• Get a clean copy of code

• Place in new directory on server

• Perform any other preparation tasks

• Change symlink in web directory to point to new version

• Tools: shell script or ant/phing

Page 57: Tool Up Your LAMP Stack

Config Deployment

51

• Exactly like code deployment

• Application needs to be designed with this in mind

• Default to live config

• Environment variables set in vhost

Page 58: Tool Up Your LAMP Stack

Phing Example

52

<?xml version ="1.0" encoding= "UTF-8" ?><project name= "example" basedir= "." default ="deploy" >

<property name= "builddir" value= "./build" /><property name= "appdir" value= "./build/code" /><tstamp><format property= "date" pattern= "%Y%m%d-%H%M"/></tstamp

<target name= "deploy" depends= "clean, prepare, export, putlive"

<target name= "export" ><exec command="svn export ${repo} ${appdir}/${date}" />

</target>

<target name= "putlive" ><exec command="scp -r ${appdir}/${date} ${destination}

> ${builddir}/logs/scp.log" /></target>

Page 59: Tool Up Your LAMP Stack

Phing Example Cont’d

53

<target name= "clean" ><delete dir= "${builddir}" />

</target>

<target name= "prepare" ><mkdir dir= "${builddir}" /><mkdir dir= "${builddir}/logs" />

</target></project>

Phing can also handle upload directories, database versioning, otherdeployment recipe steps and post deploy tasks

Page 60: Tool Up Your LAMP Stack

Continuous Integration

Page 61: Tool Up Your LAMP Stack

Continuous Integration

55

The glue that holds everything together!

• Source control commit triggers:

• Static analysis tools

• Automated tests

• Document generation

• CI system centralises:

• Deployment (to various platforms)

• Other tasks, cron jobs

• Centralised dashboard and reporting

Page 62: Tool Up Your LAMP Stack

Continuous Integration Tools

56

• Jenkins (née Hudson)

• http://jenkins-ci.org/

• PHPUnderControl (PHP-specific CruiseControl project)

• http://phpundercontrol.org/

Page 63: Tool Up Your LAMP Stack

Tool Up Your LAMP Stack

Page 64: Tool Up Your LAMP Stack

The Main Ingredients for LAMP

58

Preparation time: some years

Ingredients:

• Source control

• Development platforms

• Task tracking

• Automated testing

• Static analysis

• Automated deployment

• Continuous integration

Page 65: Tool Up Your LAMP Stack

Questions?

Page 66: Tool Up Your LAMP Stack

Thanks

60

• Website: http://lornajane.net

• Twitter: @lornajane

Page 67: Tool Up Your LAMP Stack

Image Credits

61

• LAMP http://www.flickr.com/photos/sewpixie/2059308961

• Sandpithttp://www.flickr.com/photos/giltastic/3159081924