Slide 1

Slide 1 text

Plack + Carton deployment simple & awesome Tatsuhiko Miyagawa Perl Mova / YAPC::Russia 2012 Saturday, May 12, 12

Slide 2

Slide 2 text

привет Saturday, May 12, 12

Slide 3

Slide 3 text

Me • Tatsuhiko Miyagawa • CPAN: MIYAGAWA • github.com/miyagawa • Twitter @miyagawa • Lives in San Francisco, CA Saturday, May 12, 12

Slide 4

Slide 4 text

Saturday, May 12, 12

Slide 5

Slide 5 text

Saturday, May 12, 12

Slide 6

Slide 6 text

Saturday, May 12, 12

Slide 7

Slide 7 text

Saturday, May 12, 12

Slide 8

Slide 8 text

Saturday, May 12, 12

Slide 9

Slide 9 text

Saturday, May 12, 12

Slide 10

Slide 10 text

Saturday, May 12, 12

Slide 11

Slide 11 text

Saturday, May 12, 12

Slide 12

Slide 12 text

Saturday, May 12, 12

Slide 13

Slide 13 text

My Software Saturday, May 12, 12

Slide 14

Slide 14 text

Saturday, May 12, 12

Slide 15

Slide 15 text

Saturday, May 12, 12

Slide 16

Slide 16 text

• PSGI • Plack • cpanminus • carton Saturday, May 12, 12

Slide 17

Slide 17 text

PSGI Saturday, May 12, 12

Slide 18

Slide 18 text

Perl Server Gateway Interface Saturday, May 12, 12

Slide 19

Slide 19 text

Inspired by WSGI, Rack Saturday, May 12, 12

Slide 20

Slide 20 text

WSGI (PEP-333) Saturday, May 12, 12

Slide 21

Slide 21 text

Saturday, May 12, 12

Slide 22

Slide 22 text

#  WSGI def  hello(environ,  start_response):    start_response(“200  OK”,  [        (‘Content-­‐Type’,  ‘text/plain’)    ])    return  [“Hello  World”] Saturday, May 12, 12

Slide 23

Slide 23 text

WSGI • Django • Bottle • CherryPy • Tornado • Pylons • Flask • mod_wsgi • Paste • gunicorn • uWSGI • wsgiref • Google AppEngine Saturday, May 12, 12

Slide 24

Slide 24 text

WSGI WSGI middleware Django Bottle Flask Tornado Apache lighttpd nginx mod_wsgi wsgi handlers GAE Saturday, May 12, 12

Slide 25

Slide 25 text

Rack Saturday, May 12, 12

Slide 26

Slide 26 text

Saturday, May 12, 12

Slide 27

Slide 27 text

#  Rack class  Hello    def  call(env)        return  [              200,              {  “Content-­‐Type”  =>  ”text/plain”  },                [“Hello  World”]        ]    end end Saturday, May 12, 12

Slide 28

Slide 28 text

Rack • Rails • Merb • Sinatra • Camping • Ramaze • etc. • Unicorn • Thin • Mongrel • Rainbows! • Phusion Passenger • Heroku Saturday, May 12, 12

Slide 29

Slide 29 text

Rack Rack middleware Rails Merb Sinatra Ramaze Apache lighttpd Thin Unicorn Rack handlers Mongrel Saturday, May 12, 12

Slide 30

Slide 30 text

PSGI Perl Web Server Gateway Interface Saturday, May 12, 12

Slide 31

Slide 31 text

Interface Saturday, May 12, 12

Slide 32

Slide 32 text

#  WSGI def  hello(environ,  start_response):    start_response(“200  OK”,  [        (‘Content-­‐Type’,  ‘text/plain’)    ])    return  [“Hello  World”] Saturday, May 12, 12

Slide 33

Slide 33 text

#  Rack class  Hello    def  call(env)        return  [              200,              {  “Content-­‐Type”  =>  ”text/plain”  },                [“Hello  World”]        ]    end end Saturday, May 12, 12

Slide 34

Slide 34 text

#  PSGI my  $app  =  sub  {        my  $env  =  shift;        return  [                  200,                [  ‘Content-­‐Type’,  ‘text/plain’  ],                [  ‘Hello  World’  ],        ]; }; Saturday, May 12, 12

Slide 35

Slide 35 text

That’s it. Saturday, May 12, 12

Slide 36

Slide 36 text

#  PSGI my  $app  =  sub  {        my  $env  =  shift;        return  [                  200,                [  ‘Content-­‐Type’,  ‘text/plain’  ],                [  ‘Hello  World’  ],        ]; }; Saturday, May 12, 12

Slide 37

Slide 37 text

Now you’ve got a PSGI application. Saturday, May 12, 12

Slide 38

Slide 38 text

PSGI makes it so simple to: Saturday, May 12, 12

Slide 39

Slide 39 text

Write a new Perl web app & framework Saturday, May 12, 12

Slide 40

Slide 40 text

Write a new Perl web server Saturday, May 12, 12

Slide 41

Slide 41 text

CGI::Application Apache IIS lighttpd CGI.pm CGI fastcgi mod_perl Jifty Mason Catalyst Mason::CGIHandler Catalyst::Engine nginx HTTP::Server ::Simple Saturday, May 12, 12

Slide 42

Slide 42 text

PSGI Plack::Middleware Catalyst CGI::App Jifty Dancer Apache lighttpd HTTP::Server::PSGI Twiggy Starman Plack::Handler::* (CGI, FCGI, Apache) Saturday, May 12, 12

Slide 43

Slide 43 text

#  Catalyst use  MyApp; MyApp-­‐>setup_engine(‘PSGI’); my  $app  =  sub  {  MyApp-­‐>run(@_)  }; #  $app  is  a  PSGI  app! Saturday, May 12, 12

Slide 44

Slide 44 text

#  Jifty use  MyPonyApp; my  $app  =  MyPonyApp-­‐>psgi_app; #  $app  is  a  PSGI  app! Saturday, May 12, 12

Slide 45

Slide 45 text

#  Dancer use  Dancer; get  ‘/’  =>  sub  {        “Hello  World”; }; dance;  #  returns  a  PSGI  app! Saturday, May 12, 12

Slide 46

Slide 46 text

#  Mojolicious::Lite use  Mojolicious::Lite; get  ‘/:name’  =>  sub  {        my  $self  =  shift;        $self-­‐>render_text(‘Hello!’); }; app-­‐>start;  #  returns  PSGI  app Saturday, May 12, 12

Slide 47

Slide 47 text

#  Web::Simple use  Web::Simple  ‘MyApp’; package  MyApp; dispatch  {    sub(GET)  {        [  200,  [...],  [  ‘Hello’  ]  ];    } }; my  $app  =  MyApp-­‐>as_psgi; #  $app  is  a  PSGI  app! Saturday, May 12, 12

Slide 48

Slide 48 text

Saturday, May 12, 12

Slide 49

Slide 49 text

Recap: PSGI • Inspired by Python, Ruby • Very simple • Code ref, hash ref, array ref • adapted by many frameworks and businesses Saturday, May 12, 12

Slide 50

Slide 50 text

cpanminus Saturday, May 12, 12

Slide 51

Slide 51 text

Saturday, May 12, 12

Slide 52

Slide 52 text

Dead easy to install Sane defaults Ultra-lightweight Saturday, May 12, 12

Slide 53

Slide 53 text

>  curl  -­‐L  cpanmin.us  |\    perl  -­‐  App::cpanminus Saturday, May 12, 12

Slide 54

Slide 54 text

Saturday, May 12, 12

Slide 55

Slide 55 text

-­‐-­‐installdeps Saturday, May 12, 12

Slide 56

Slide 56 text

PSGI/Plack convention for writing apps Saturday, May 12, 12

Slide 57

Slide 57 text

cpanm --installdeps convention for installing deps Saturday, May 12, 12

Slide 58

Slide 58 text

PSGI/Plack + cpanminus = ? Saturday, May 12, 12

Slide 59

Slide 59 text

Saturday, May 12, 12

Slide 60

Slide 60 text

Step 1: Write your app in PSGI (with PSGI-compatible framework) Saturday, May 12, 12

Slide 61

Slide 61 text

Step 2: Maintain dependencies in git with Makefile.PL/Build.PL Saturday, May 12, 12

Slide 62

Slide 62 text

Step 3: Run your code on the cloud via git push! Saturday, May 12, 12

Slide 63

Slide 63 text

Saturday, May 12, 12

Slide 64

Slide 64 text

http://showmetheco.de/articles/2011/8/three-perl-cloud-hosting-platforms.html Saturday, May 12, 12

Slide 65

Slide 65 text

Heroku buildpacks • judofyr/perloku • miyagawa/heroku-buildpack-perl • lstoll/heroku-buildpack-perl Saturday, May 12, 12

Slide 66

Slide 66 text

Saturday, May 12, 12

Slide 67

Slide 67 text

Saturday, May 12, 12

Slide 68

Slide 68 text

Saturday, May 12, 12

Slide 69

Slide 69 text

DIY https://github.com/dagolden/perl-chef Saturday, May 12, 12

Slide 70

Slide 70 text

Recap • PSGI: web app standard • cpanm: dependencies installation • PSGI + cpanm => Deploy to the cloud! Saturday, May 12, 12

Slide 71

Slide 71 text

Problems Saturday, May 12, 12

Slide 72

Slide 72 text

Step 2: Maintain dependencies in git with Makefile.PL/Build.PL Saturday, May 12, 12

Slide 73

Slide 73 text

Makefile.PL Saturday, May 12, 12

Slide 74

Slide 74 text

use  ExtUtils::MakeMaker; WriteMakefile(    NAME  =>  ‘MyModule’,    VERSION  =>  ‘1.22’,    PREREQ_PM  =>  {        ‘JSON’  =>  0,        ‘Hash::MultiValue’  =>  ‘0.11’,    }, ); Saturday, May 12, 12

Slide 75

Slide 75 text

use  Module::Build; my  $build  =  Module::Build-­‐>new(    module_name  =>  ‘MyModule’,    requires  =>  {        ‘JSON’  =>  ‘2.00’,        ‘Hash::MultiValie’  =>  ‘0.20’,    }, ); $build-­‐>create_build_script; Saturday, May 12, 12

Slide 76

Slide 76 text

use  inc::Module::Install; name  ‘MyApp’; all_from  ‘lib/MyApp.pm’; requires  ‘JSON’,  ‘2.00’; requires  ‘Hash::MultiValue’  =>  0.10; WriteAll; Saturday, May 12, 12

Slide 77

Slide 77 text

Problem 1) Lots of boilerplate Saturday, May 12, 12

Slide 78

Slide 78 text

use  ExtUtils::MakeMaker; WriteMakefile(    NAME  =>  ‘MyModule’,    VERSION  =>  ‘1.22’,    PREREQ_PM  =>  {        ‘JSON’  =>  0,        ‘Hash::MultiValue’  =>  ‘0.11’,    }, ); Saturday, May 12, 12

Slide 79

Slide 79 text

use  Module::Build; my  $build  =  Module::Build-­‐>new(    module_name  =>  ‘MyModule’,    requires  =>  {        ‘JSON’  =>  ‘2.00’,        ‘Hash::MultiValie’  =>  ‘0.20’,    }, ); $build-­‐>create_build_script; Saturday, May 12, 12

Slide 80

Slide 80 text

use  inc::Module::Install; name  ‘MyApp’; all_from  ‘lib/MyApp.pm’; requires  ‘JSON’,  ‘2.00’; requires  ‘Hash::MultiValue’  =>  0.10; WriteAll; Saturday, May 12, 12

Slide 81

Slide 81 text

Bootstrapping inc/Module/Install.pm  in  git configure_requires  META.yml Saturday, May 12, 12

Slide 82

Slide 82 text

Problem 2) No way to freeze dep versions Saturday, May 12, 12

Slide 83

Slide 83 text

‘MooseX::Foo’  =>  1.0 means MooseX::Foo with 1.0 or newer Saturday, May 12, 12

Slide 84

Slide 84 text

cpanm  -­‐-­‐installdeps Installs the latest version of modules that satisfies the requirements Saturday, May 12, 12

Slide 85

Slide 85 text

What if ... Saturday, May 12, 12

Slide 86

Slide 86 text

• Jul 2nd: Started working on project • using Web::Framework 1.1 • Jul 9th: Finished version 1.0 • Jul 10-15th: internal beta, QA • Jul 16th: Deploy to the cloud/production Saturday, May 12, 12

Slide 87

Slide 87 text

• Jul 2nd: Started working on project • using Web::Framework 1.1 • Jul 9th: Finished version 1.0 • Jul 10-15th: internal beta, QA • Jul 16th: Web::Framework 1.2 is released • Jul 16th: Deploy to the cloud/production Saturday, May 12, 12

Slide 88

Slide 88 text

Web::Framework 1.2 broke my code! Saturday, May 12, 12

Slide 89

Slide 89 text

Saturday, May 12, 12

Slide 90

Slide 90 text

Makefile.PL Recap • Not so easy to write • Could have deps, bootstrapping issue • No way to freeze versions Saturday, May 12, 12

Slide 91

Slide 91 text

A solution Saturday, May 12, 12

Slide 92

Slide 92 text

cpanfile  + Carton Saturday, May 12, 12

Slide 93

Slide 93 text

requires  ‘Plack’,  0.9980; requires  ‘Catalyst’,  ‘>=  5.8,  <  5.9’; conflicts  ‘MooseX::Foo’,  ‘<  0.10’; on  ‘test’  =>  sub  {        requires  ‘Test::More’,  0.80; }; on  ‘develop’  =>  sub  {        requires  ‘Devel::NYTProf’; }; Saturday, May 12, 12

Slide 94

Slide 94 text

inspired by: gemfile(5) Saturday, May 12, 12

Slide 95

Slide 95 text

Backward compatible to: Module::Install(::DSL) Saturday, May 12, 12

Slide 96

Slide 96 text

Converted to to: CPAN::Meta::Prereqs Saturday, May 12, 12

Slide 97

Slide 97 text

Toolset Module::CPANfile Module::Install::CPANfile Saturday, May 12, 12

Slide 98

Slide 98 text

Supported by Dist::Zilla & Carton Saturday, May 12, 12

Slide 99

Slide 99 text

Carton https://github.com/miyagawa/carton Saturday, May 12, 12

Slide 100

Slide 100 text

Manages CPAN deps Saturday, May 12, 12

Slide 101

Slide 101 text

Inspired by... Saturday, May 12, 12

Slide 102

Slide 102 text

Saturday, May 12, 12

Slide 103

Slide 103 text

• App-specific local environment • Fast and safe install • Dep-tree analysis, including versions • Locking module versions • Easy Redeployment • Single-file, VCS friendly • Safe and easy rollback Saturday, May 12, 12

Slide 104

Slide 104 text

Local perl environment Using local::lib and cpanm -L Each app has an isolated local library path Saturday, May 12, 12

Slide 105

Slide 105 text

Fast and safe install cpanm 1.5 Saves MYMETA.json and install meta info Saturday, May 12, 12

Slide 106

Slide 106 text

Dep tree analysis Rebuild the dependency tree from meta info Checks if anything is missing/superfluous Saturday, May 12, 12

Slide 107

Slide 107 text

Locking versions Versions are saved in carton.lock including dependencies Saturday, May 12, 12

Slide 108

Slide 108 text

Easy Redeployment Reinstall exactly the same set of modules on another prod/development machines. Saturday, May 12, 12

Slide 109

Slide 109 text

Single-file, VCS friendly You can add carton.lock to git update whenever you update modules Saturday, May 12, 12

Slide 110

Slide 110 text

Safe and easy rollback revert the lock file and redeploy Saturday, May 12, 12

Slide 111

Slide 111 text

DEMO Saturday, May 12, 12

Slide 112

Slide 112 text

>  cpanm  Carton Saturday, May 12, 12

Slide 113

Slide 113 text

WARNING It is pre-1.0 beta software, some features are missing or not working correctly (yet). Saturday, May 12, 12

Slide 114

Slide 114 text

TODO / Roadmap • Update specific modules • Make install and bundle faster • Inject old versions / patched versions • Install from git SHA Saturday, May 12, 12

Slide 115

Slide 115 text

Recap • Write app in PSGI/Plack • Specify CPAN dependencies with cpanfile • Manage them and deploy with carton • Inspired by Ruby :) Saturday, May 12, 12

Slide 116

Slide 116 text

github.com/miyagawa/carton irc.perl.org #carton Saturday, May 12, 12

Slide 117

Slide 117 text

Saturday, May 12, 12

Slide 118

Slide 118 text

Questions? Saturday, May 12, 12

Slide 119

Slide 119 text

спасибо! speakerdeck.com/u/miyagawa Saturday, May 12, 12