0:00:17.160,0:00:19.800 BEN DIXON: OK. So, I'm Ben. 0:00:19.800,0:00:22.170 I'm a Rails developer and the author 0:00:22.170,0:00:26.150 of a book called Reliably Deploying Rails[br]Applications. 0:00:26.180,0:00:28.090 I spend a lot of people - a lot of people? 0:00:28.100,0:00:30.810 A lot of time teaching people Ruby at 0:00:30.810,0:00:32.259 MakeItWithCode dot com, 0:00:32.259,0:00:35.120 which is an online mentor-driven class 0:00:35.120,0:00:36.780 for people who are new to Ruby 0:00:36.780,0:00:39.000 but want to get started making things quickly. 0:00:39.000,0:00:41.200 I spend the rest of my time working with 0:00:41.200,0:00:43.680 the awesome guys at HelloSwimio, where we[br]make cool 0:00:43.680,0:00:46.110 tech for swimmers. So we make things like[br]the 0:00:46.110,0:00:49.310 time tabling system for the Olympic pool in[br]London, 0:00:49.310,0:00:52.010 the speedo swim tracking app, and the first[br]automatic 0:00:52.020,0:00:54.359 lap tracker for the Pebble smart watch. 0:00:54.360,0:00:57.750 So, before we get started, could you put your 0:00:57.750,0:01:00.451 hands up if you've deployed a Rails app of 0:01:00.460,0:01:05.209 some sort to Heroku. Awesome. Now keep your[br]hand 0:01:05.209,0:01:07.341 up if you've deployed a Rails app to a 0:01:07.341,0:01:11.610 server that you've set up yourself. Good.[br]That's less 0:01:11.640,0:01:12.570 people. 0:01:12.570,0:01:14.770 So if someone came to me and said, I 0:01:14.770,0:01:16.279 want to deploy my first Rails app, how should 0:01:16.280,0:01:18.571 I do it? I wouldn't think about it, I'd 0:01:18.580,0:01:22.150 just say use Heroku. And I'd say use Heroku, 0:01:22.150,0:01:25.530 because Heroku makes deploying apps incredibly[br]simple. You just 0:01:25.530,0:01:29.750 commit your code, Heroku create, git push[br]Heroku, and 0:01:29.780,0:01:32.660 magically, a couple of minutes later, your[br]application is 0:01:32.660,0:01:36.010 live, on the internet, for the world to see. 0:01:36.010,0:01:38.630 And because Heroku has made this process so[br]simple 0:01:38.630,0:01:40.860 and so easy, it's really easy to take that 0:01:40.860,0:01:42.220 server stuff and kind of put it in a 0:01:42.240,0:01:44.990 bucket of things we don't look at. Because[br]it's 0:01:44.990,0:01:48.500 complicated and Heroku makes it really easy,[br]so why 0:01:48.500,0:01:50.030 would we bother? 0:01:50.040,0:01:51.750 And what I'd like to demonstrate in this talk 0:01:51.750,0:01:53.850 is that, in the same way Rails makes building 0:01:53.850,0:01:57.070 web applications really, really easy, there[br]are lots of 0:01:57.080,0:01:59.080 great tools out there that make setting up[br]servers 0:01:59.080,0:02:02.050 for them and deploying to those servers really[br]easy. 0:02:02.060,0:02:03.600 And once you get the hang of these tools, 0:02:03.600,0:02:06.170 you can do some really cool stuff. 0:02:06.170,0:02:09.758 Before we get started, two important bits[br]of terminology, 0:02:09.759,0:02:12.931 which probably you already know. A PaaS, a[br]platform 0:02:12.940,0:02:15.260 as a service, and by that I mean something 0:02:15.260,0:02:18.030 like Heroku. So something that just abstracts[br]all of 0:02:18.030,0:02:19.420 your infrastructure away so that you don't[br]have to 0:02:19.420,0:02:20.610 worry about it. 0:02:20.610,0:02:23.060 On the other scale, you've got a VPS, a 0:02:23.060,0:02:26.120 Virtual Private Server, and for the purposes[br]of this 0:02:26.140,0:02:28.480 talk, I just mean some sort of Linux server 0:02:28.480,0:02:30.560 running the cloud. So the type of thing you 0:02:30.560,0:02:33.630 get from Linode or RackSpace or DigitalOcean. 0:02:33.630,0:02:36.370 So, to get started, I want to take a 0:02:36.380,0:02:38.730 little look at what's going on behind the[br]scenes 0:02:38.730,0:02:41.790 when you deploy a typical Rails application.[br]How do 0:02:41.790,0:02:44.570 we go from Rails S on our development machine 0:02:44.570,0:02:47.280 to a Rails app on a VPS somewhere serving 0:02:47.280,0:02:49.160 our application to the world? 0:02:49.160,0:02:52.450 So, here we've got a beautiful picture that[br]I've 0:02:52.460,0:02:55.660 drawn, because I'm new to Keynote. And the[br]black 0:02:55.660,0:02:57.900 box around the outside is our VPS. It's a 0:02:57.920,0:03:01.010 single VPS. And this is the first thing that 0:03:01.010,0:03:03.920 seemed to surprise lots of people. For a simple 0:03:03.920,0:03:07.510 production configuration, you only need one[br]VPS. In the 0:03:07.520,0:03:10.360 same way, in development, you can run your[br]entire 0:03:10.360,0:03:14.430 Rails application on your development machine.[br]For simple production 0:03:14.430,0:03:17.210 application, you can run everything on a single[br]VPS. 0:03:17.220,0:03:20.670 And that VPS will have three main components[br]on 0:03:20.670,0:03:23.339 it for almost all Rails applications. You're[br]gonna have 0:03:23.340,0:03:25.761 a web server, you're gonna have an app server, 0:03:25.761,0:03:29.650 and you're gonna have a database server. So,[br]when 0:03:29.660,0:03:31.830 someone types in the address of our website,[br]a 0:03:31.830,0:03:35.199 request comes in. It hits our VPS, and the 0:03:35.200,0:03:36.931 first place that request is gonna go is our 0:03:36.940,0:03:40.319 web server. Now, if that request was for a 0:03:40.320,0:03:42.781 static file, and by a static file, I basically 0:03:42.781,0:03:45.510 mean, anything that goes in the Rails asset[br]pipeline 0:03:45.510,0:03:47.520 or anything that you might find in the public 0:03:47.520,0:03:49.640 folder, the web server's gonna deal with that[br]directly. 0:03:49.640,0:03:51.880 It's gonna send that file straight back to[br]the 0:03:51.880,0:03:54.220 user for display in their browser and that's[br]the 0:03:54.220,0:03:55.390 end of it. 0:03:55.400,0:03:58.760 If, however, that request is for a dynamic[br]page, 0:03:58.760,0:04:02.970 so for a page in our Rails app, essentially, 0:04:02.970,0:04:05.830 that's gonna get passed back to an app server. 0:04:05.840,0:04:09.209 Now, when we run Rails S in development, what 0:04:09.209,0:04:12.840 we're starting is an instance of an app server. 0:04:12.840,0:04:15.491 So, the app server here is Rails S running 0:04:15.491,0:04:17.630 on our VPS. So the web server passes the 0:04:17.630,0:04:21.079 request back. The app server generates the[br]response and 0:04:21.079,0:04:24.041 then passes it back to the web server, which 0:04:24.041,0:04:26.220 then returns it to the user. 0:04:26.220,0:04:28.329 And in development, we're probably used to[br]the web, 0:04:28.329,0:04:31.271 the app server being something like Thin or[br]WebRick. 0:04:31.280,0:04:33.109 In production it might also be Thin or Webrick. 0:04:33.109,0:04:35.051 It might also be something a bit more advanced 0:04:35.060,0:04:37.470 like Puma or Unicorn. 0:04:37.470,0:04:40.880 Finally, for pretty much all Rails applications,[br]we're gonna 0:04:40.880,0:04:43.370 want some sort of database server. And that's[br]probably 0:04:43.380,0:04:47.230 gonna be PostGres or mySQL or Mongo. And,[br]again, 0:04:47.230,0:04:49.960 we don't need a separate database server.[br]This all 0:04:49.980,0:04:52.370 runs on our single VPS. 0:04:52.380,0:04:55.440 So, to recap, a request comes in and it 0:04:55.440,0:04:58.350 hits our web server. Probably EngineX or Apache.[br]If 0:04:58.350,0:05:00.520 it's for a static file, the web server deals 0:05:00.520,0:05:02.680 with it. If it's for part of our Rails 0:05:02.680,0:05:05.110 application, it gets passed back to the app[br]server, 0:05:05.110,0:05:07.640 which is basically Rails S, and the app server 0:05:07.640,0:05:10.420 may communicate with the database server,[br]read some data, 0:05:10.420,0:05:13.110 write some data, and then the app server constructs 0:05:13.110,0:05:14.810 the response, sends it back to the web server, 0:05:14.820,0:05:16.420 which serves it to the user. 0:05:16.420,0:05:19.600 So, you'd be forgiven for looking at that[br]and 0:05:19.600,0:05:22.890 thinking, well, that already looks more complicated[br]than Heroku. 0:05:22.890,0:05:24.880 Why on earth would you ever do that? Why 0:05:24.880,0:05:26.359 not just use Heroku? 0:05:26.360,0:05:28.351 Well, I started doing this, if I'm honest,[br]because 0:05:28.360,0:05:30.610 of cost. I had quite a lot of side 0:05:30.620,0:05:35.770 projects, and as you probably already know,[br]most past 0:05:35.780,0:05:38.050 solutions charge by the process. And a lot[br]of 0:05:38.050,0:05:41.890 my personal projects and side projects had[br]background jobs. 0:05:41.890,0:05:45.190 Now when you've got five or six side projects, 0:05:45.190,0:05:47.609 all of which would need two Heroku dynos,[br]suddenly 0:05:47.620,0:05:48.930 that bill at the end of the month starts 0:05:48.930,0:05:52.481 to get quite substantial. 0:05:52.481,0:05:53.960 And so I wanted to save money by deploying 0:05:53.960,0:05:57.210 to my own servers. And I did. It's worth 0:05:57.240,0:05:59.510 bearing in mind, if that's also your motivation,[br]that, 0:05:59.510,0:06:03.590 as with any new skill, you have to invert. 0:06:03.590,0:06:05.200 Invert? You have to put in a certain amount 0:06:05.220,0:06:07.850 of time to learn it. If you were to 0:06:07.850,0:06:09.510 account for the time I put into learning this 0:06:09.510,0:06:12.390 to begin with, at market rates, I'm fairly[br]sure 0:06:12.390,0:06:14.320 there would have been no saving at all. Personally, 0:06:14.340,0:06:16.740 they're my own projects. I wasn't paying myself[br]market 0:06:16.740,0:06:19.080 rates. And I was happy to treat it as 0:06:19.080,0:06:20.770 an investment which would pay off in the long 0:06:20.770,0:06:23.190 term, and it has. 0:06:23.200,0:06:26.800 Actually, it turned out, the biggest benefit[br]of learning 0:06:26.800,0:06:28.300 how to do this was nothing to do with 0:06:28.300,0:06:31.640 money. It was the infrastructure went from[br]being this 0:06:31.640,0:06:33.770 kind of necessary evil that I have to have 0:06:33.770,0:06:36.250 in order to run my Rails apps, to just 0:06:36.250,0:06:38.640 another tool that I could use for building[br]interesting 0:06:38.660,0:06:42.020 stuff. And the best example of that, for me, 0:06:42.040,0:06:44.830 is Make It With Code. We provide our students 0:06:44.830,0:06:47.840 with a cloud IDE that has Ruby pre-installed.[br]And 0:06:47.840,0:06:50.060 that means they can hit the ground running.[br]They 0:06:50.060,0:06:53.200 can start writing Ruby code really quickly. 0:06:53.200,0:06:54.700 And the techniques that we use setting those[br]up 0:06:54.700,0:06:57.480 automatically for all of our students are[br]exactly the 0:06:57.480,0:06:59.540 same techniques that I'm gonna cover in this[br]presentation 0:06:59.540,0:07:01.879 for setting up Rails apps. So it means that 0:07:01.880,0:07:04.481 our infrastructure has gone from being this[br]thing that 0:07:04.481,0:07:05.999 we have to have in the background, as like 0:07:06.000,0:07:09.201 a supporting thing, to something that actually[br]adds value 0:07:09.201,0:07:11.439 to our product in itself. It's a part of 0:07:11.440,0:07:13.171 our product. 0:07:13.180,0:07:14.830 So if you decide to give this a go, 0:07:14.830,0:07:16.920 I really, really urge you not to do it 0:07:16.920,0:07:19.580 even slightly like I did it originally. Because[br]it 0:07:19.580,0:07:21.450 was terrible. I started off by getting a VPS. 0:07:21.450,0:07:23.560 That bit was good. That bit worked. I Googled, 0:07:23.580,0:07:26.740 how do I set up a Rails VPS. I 0:07:26.740,0:07:28.810 found all these great tutorials that said,[br]type in 0:07:28.810,0:07:31.189 this command, then that command, then edit[br]this config 0:07:31.200,0:07:33.241 file. And I found loads of things on StackOverflow 0:07:33.241,0:07:34.759 that said, here's what to do when that goes 0:07:34.760,0:07:37.211 wrong and that goes wrong. And eventually[br]I had 0:07:37.211,0:07:38.439 this working server. And it was great. 0:07:38.440,0:07:40.671 And it broke a few times in the first 0:07:40.680,0:07:43.419 few weeks. But we fixed that when it happened. 0:07:43.420,0:07:47.700 And, by and large, it worked for quite awhile. 0:07:47.700,0:07:49.920 And it worked until it didn't work, and when 0:07:49.920,0:07:51.931 it didn't work, I was in France, on a 0:07:51.931,0:07:53.710 holiday with my family, and it was a fairly 0:07:53.720,0:07:56.610 rural bit of France, and we had, we had 0:07:56.610,0:07:58.409 a wifi connection. But it was only one wifi 0:07:58.409,0:08:02.701 connection for, basically, the village. And[br]to access that, 0:08:02.720,0:08:04.699 we, we could access it by sitting half in 0:08:04.700,0:08:08.181 a wardrobe on the top floor of the house, 0:08:08.181,0:08:09.979 which was nice. 0:08:09.980,0:08:12.461 And, so one morning I woke up and I 0:08:12.461,0:08:14.979 was sitting in the wardrobe with a coffee[br]and 0:08:14.980,0:08:17.230 checking my email, and I had a wonderful,[br]a 0:08:17.230,0:08:19.220 lovely email from my host to say, Dear Valued 0:08:19.220,0:08:20.931 Customer, your server is dead, you need to[br]make 0:08:20.940,0:08:23.580 a new one, Lots of love, from your hosts. 0:08:23.580,0:08:25.940 And I thought, good. Right. So I set up 0:08:25.940,0:08:28.250 a little desk in this wardrobe and for the 0:08:28.250,0:08:30.180 rest of the day, my family sat by a 0:08:30.180,0:08:35.490 river. They drank wine, ate cheese, mocked[br]me slightly, 0:08:35.490,0:08:37.590 and I sat in a roof, in a wardrobe, 0:08:37.600,0:08:40.009 in a heat wave, and pretty much followed the 0:08:40.009,0:08:42.601 same tutorials I employed the first time.[br]Made the 0:08:42.601,0:08:45.140 same mistakes that I'd made the first time,[br]and 0:08:45.160,0:08:47.110 I got the server back up, but it really 0:08:47.110,0:08:49.740 wasn't any easier the second time. And that[br]seemed 0:08:49.740,0:08:52.130 kind of wrong to me. That it seemed really 0:08:52.130,0:08:52.430 frustrating. 0:08:52.430,0:08:53.710 So I figured there must be a better way 0:08:53.740,0:08:56.290 of doing this. If I'm honest, actually what[br]I 0:08:56.290,0:08:58.320 thought was, I can't believe there isn't already[br]a 0:08:58.320,0:09:00.540 better way of doing this, I should invent[br]it 0:09:00.540,0:09:01.970 and then I'll be rich and famous and everyone 0:09:01.980,0:09:05.670 will love me. Luckily I took a step back 0:09:05.670,0:09:07.510 and thought, OK, this probably already exists. 0:09:07.520,0:09:11.180 And, of course, it did. And what I wanted 0:09:11.180,0:09:15.080 was a configuration management tool. And a[br]configuration management 0:09:15.080,0:09:17.360 tool is really simple. It allows you to define 0:09:17.360,0:09:19.330 and automate the commands that it takes to[br]set 0:09:19.330,0:09:22.300 up the server to do a particular thing. And 0:09:22.300,0:09:25.079 what's amazing about a configuration management[br]tool is once 0:09:25.080,0:09:27.281 you've done it once, doing it again and again 0:09:27.281,0:09:29.980 and again is completely trivial. 0:09:29.980,0:09:33.530 So, that time you put in to start with, 0:09:33.530,0:09:34.870 it will probably only be one command to set 0:09:34.920,0:09:37.639 up an identical server the next time. And[br]to 0:09:37.640,0:09:38.890 give you an idea of just how simple it 0:09:38.890,0:09:40.940 can be, I've got a brief demonstration of[br]the 0:09:40.960,0:09:43.101 process that I actually move, at the moment,[br]on 0:09:43.101,0:09:45.390 a pretty much weekly basis for setting up[br]new 0:09:45.390,0:09:45.960 servers. 0:09:45.960,0:09:49.120 At the end of this presentation, I'll link[br]to 0:09:49.120,0:09:51.060 tutorials of how to do this yourself and the 0:09:51.060,0:09:53.490 sample code. So please don't worry about following[br]it 0:09:53.500,0:09:54.730 step by step, I just want to give an 0:09:54.730,0:09:57.220 idea about how simple it can be. 0:09:57.220,0:10:02.210 So, here you can see, on the left, your 0:10:02.220,0:10:04.300 left, there is a list of servers that I've 0:10:04.300,0:10:07.779 deployed in the past. And I'm using my configuration 0:10:07.780,0:10:11.161 management tool of choice, Chef Solo, which[br]uses JSON 0:10:11.161,0:10:14.910 files for defining what should go onto a particular 0:10:14.910,0:10:15.500 type of server. 0:10:15.500,0:10:17.100 And on the bottom you can see I've got 0:10:17.100,0:10:18.470 a couple of web servers for Make It With 0:10:18.470,0:10:20.650 Code, which at the moment is running a selection 0:10:20.650,0:10:23.429 of Rails and Sinatra apps. And I want to 0:10:23.440,0:10:25.781 write a new one. And to do this, I 0:10:25.781,0:10:28.390 just create a new JSON file, web3 dot makeitwithcode 0:10:28.390,0:10:31.510 dot com. I save it, and then this server, 0:10:31.520,0:10:33.400 it's gonna be pretty much the same as web2. 0:10:33.400,0:10:35.069 It's just running Rails apps. 0:10:35.080,0:10:36.311 So I just copy all the JSON from my 0:10:36.320,0:10:39.720 previous server, but it into the new one,[br]and 0:10:39.720,0:10:40.899 the bit I want to draw your attention to 0:10:40.900,0:10:43.351 is, if you look at the bottom of that, 0:10:43.351,0:10:44.699 you've got this run list with a list of 0:10:44.700,0:10:47.311 things that are being called roles. And pretty[br]much 0:10:47.320,0:10:50.820 all configuration management systems have[br]some sort of analogous 0:10:50.820,0:10:55.940 concept to this, which, kind of individual[br]components that 0:10:55.940,0:10:58.260 you can mix and match to define what should 0:10:58.260,0:11:00.490 go onto this server that you're setting up. 0:11:00.490,0:11:02.980 So I've defined in the past, what goes on 0:11:02.980,0:11:04.850 an internet server. What goes on a PostGres[br]server. 0:11:04.850,0:11:06.700 And now if I want that, I can just 0:11:06.720,0:11:08.320 drop that role in, and I know that it 0:11:08.320,0:11:09.769 will be set up in exactly the way that 0:11:09.769,0:11:12.491 I want it. So on this server I want 0:11:12.500,0:11:14.450 Redis. I didn't on the previous one. So I 0:11:14.450,0:11:17.930 can just uncomment it. Uncomment it, yeah.[br]And I'll 0:11:17.940,0:11:20.870 get exactly the Redis configuration I want. 0:11:20.870,0:11:22.780 The good bit, the fun bit, is once I've 0:11:22.780,0:11:25.310 done this, I can type on command - knife 0:11:25.310,0:11:27.069 solo bootstrap - and then the address of the, 0:11:27.069,0:11:31.051 the VPS I've created. Presenter. And then[br]leave it 0:11:31.060,0:11:33.640 for about twenty minutes, go and get a coffee, 0:11:33.660,0:11:36.420 and the output that will scroll through, it[br]will 0:11:36.420,0:11:39.269 tell me what commands it's applying. It will[br]show 0:11:39.269,0:11:42.010 me diffs of any configuration files that it's[br]changing. 0:11:42.020,0:11:43.611 And if it goes wrong, which it doesn't very 0:11:43.611,0:11:45.400 often, it will tell me exactly what it was 0:11:45.400,0:11:47.000 doing when it went wrong, so I can fix 0:11:47.000,0:11:47.509 it. 0:11:47.509,0:11:49.771 And at the end of this process, I will 0:11:49.780,0:11:52.710 have a Rails server which is identical the[br]last 0:11:52.710,0:11:54.590 one I set up, and the one before that, 0:11:54.600,0:11:56.360 and I can check that this works. I can 0:11:56.360,0:11:58.639 fire up a web browser. I can go to 0:11:58.640,0:12:00.760 web3 and the address that I gave it, and 0:12:00.760,0:12:03.421 I'll get this wonderful Welcome to EngineX[br]page, which 0:12:03.421,0:12:05.910 says, that server is set up and it's ready 0:12:05.910,0:12:06.759 to use. 0:12:06.780,0:12:10.261 So, what I'm trying to demonstrate with that[br]figure 0:12:10.261,0:12:14.790 is that configuration management is basically,[br]don't repeat yourself 0:12:14.790,0:12:17.050 for setting up servers. If you think back[br]to 0:12:17.060,0:12:20.050 the first time you used, say, Devise, for[br]example, 0:12:20.050,0:12:21.590 the first time I used Devise, it took me 0:12:21.590,0:12:24.210 ages to get all_auth and things like that[br]working. 0:12:24.210,0:12:26.619 And then, six months later, I came back to 0:12:26.640,0:12:28.441 it, and didn't really remember any of the[br]stuff 0:12:28.441,0:12:31.069 that I'd done to get it to work. But 0:12:31.080,0:12:32.330 I could go back to the code I wrote 0:12:32.330,0:12:34.381 the first time, I could look at that code, 0:12:34.381,0:12:37.220 copy bits of it, and it was much quicker. 0:12:37.220,0:12:41.470 Now, the problem with just copying commands[br]or entering 0:12:41.470,0:12:44.140 commands manually when setting up the server[br]is there 0:12:44.160,0:12:46.769 is no natural audit trail of what did I 0:12:46.769,0:12:49.091 do to make this work the first time. So 0:12:49.100,0:12:50.810 there's a good chance, the second time you're[br]gonna 0:12:50.810,0:12:52.630 do it, you'll make the same mistakes as the 0:12:52.640,0:12:53.680 first time. 0:12:53.680,0:12:56.680 With a configuration management utility, you[br]naturally create this 0:12:56.680,0:12:59.959 audit trail as you go along. So that, in 0:12:59.960,0:13:02.301 time, that time you invest up front, it goes 0:13:02.301,0:13:05.250 much, much further in the long run. 0:13:05.250,0:13:08.920 So, hopefully that's shown that the bit where[br]people 0:13:08.920,0:13:11.400 often trip up, getting a server setup, it[br]doesn't 0:13:11.400,0:13:12.889 need to be that difficult, and I'll give you 0:13:12.889,0:13:14.801 the exact sample code at the end, which you 0:13:14.820,0:13:17.240 can use if you want a head start. 0:13:17.240,0:13:20.180 But something like Heroku, it doesn't just[br]make setting 0:13:20.180,0:13:23.290 up a server really easy. It makes deploying[br]to 0:13:23.290,0:13:25.590 one incredibly easy. Git push heroku. It pretty[br]much 0:13:25.600,0:13:28.290 couldn't be easier. And I think it's fair[br]to 0:13:28.290,0:13:29.889 say that the first time I used Heroku, it 0:13:29.889,0:13:31.760 was completely revolutionary. 0:13:31.780,0:13:34.411 So I came to Rails from Python/Jengo, and[br]before 0:13:34.411,0:13:37.380 that, PHP, and I'd used a whole range of, 0:13:37.380,0:13:40.819 kind of, home grown deployment systems, which[br]ranged from 0:13:40.820,0:13:42.861 trying to remember what files you've changed[br]and FTP 0:13:42.861,0:13:47.230 them across to all sync things and custom[br]shell 0:13:47.230,0:13:49.160 scripts. And the one thing they all had in 0:13:49.180,0:13:52.320 common was they were flaky and deployment[br]was something 0:13:52.320,0:13:54.339 you didn't want to do, because it would probably 0:13:54.340,0:13:56.701 break. So you maybe deployed a couple of times 0:13:56.701,0:13:57.709 a week. 0:13:57.709,0:13:59.361 So I used Heroku for the first time and 0:13:59.361,0:14:02.370 it was amazing. Suddenly I could apploy. Apploy?[br]Deploy 0:14:02.370,0:14:05.619 ten times a day. And it was just fitted 0:14:05.620,0:14:06.711 into my normal workflow. 0:14:06.720,0:14:09.060 So I was really keen that if I was 0:14:09.060,0:14:11.060 deploying to my own servers, it had to be 0:14:11.060,0:14:12.600 as simple as that. It had to be one 0:14:12.600,0:14:15.660 command to deploy. And luckily, as is often[br]the 0:14:15.660,0:14:18.040 case in the Rails community, loads of very[br]clever 0:14:18.040,0:14:20.360 people wanted exactly the same thing, and[br]so have 0:14:20.360,0:14:23.220 developed some awesome tools, some awesome[br]gems, to make 0:14:23.220,0:14:25.720 it really simple to set that up. 0:14:25.720,0:14:27.569 In the Rails community, I think the best known 0:14:27.580,0:14:30.411 of these is probably Capistrano, which I'll[br]talk about 0:14:30.411,0:14:34.680 a bit more towards the end of the presentation. 0:14:34.680,0:14:37.210 But, first, another brief demo of how simple[br]it 0:14:37.220,0:14:40.200 is to add Capistrano into an existing, in[br]this 0:14:40.200,0:14:43.769 case, Rails 4.1 application. Again, at the[br]end, I'll 0:14:43.769,0:14:46.880 give details of step by, a step by step 0:14:46.880,0:14:49.351 tutorial and the ensemble code for doing this.[br]So 0:14:49.360,0:14:51.190 please don't worry about the individual steps.[br]I just 0:14:51.190,0:14:55.030 want to give an idea of what's involved. 0:14:55.040,0:15:00.970 So, to get started, Rails 4.1 application.[br]I just 0:15:00.970,0:15:04.780 drop in the Capistrano gem and a few supporting 0:15:04.780,0:15:08.019 helper gems. Save that, and then add it to 0:15:08.020,0:15:11.520 git and, sorry, bundle and add it to Git. 0:15:11.520,0:15:13.941 Under the hood we're still using Git to deploy 0:15:13.941,0:15:15.900 in the same way Heroku does, so it's important 0:15:15.900,0:15:18.259 to make sure changes like that are committed. 0:15:18.260,0:15:20.411 And then running cap install, which just generates[br]some 0:15:20.420,0:15:23.339 basic configuration files. The first one of[br]these is 0:15:23.340,0:15:25.661 a cap file. This is just a rake file 0:15:25.661,0:15:28.540 for capistrano. And here, I'm just telling[br]it that 0:15:28.540,0:15:30.720 I want to use those helper gems that I 0:15:30.720,0:15:32.490 included in the first step. So I'm telling[br]it 0:15:32.490,0:15:34.319 that I want to use rbenv to manage what 0:15:34.320,0:15:36.461 Ruby version I'm using. I want it to install 0:15:36.461,0:15:39.600 gems for me, compile assets for me, and apply 0:15:39.600,0:15:42.079 migrations. All the normal stuff you do when[br]setting 0:15:42.080,0:15:43.140 up a Rails app. 0:15:43.140,0:15:45.940 We've then got our main config file, just[br]deploy 0:15:45.940,0:15:48.471 dot rb. And here I'm just giving the app 0:15:48.480,0:15:50.580 a name, telling it which Git repository to[br]deploy 0:15:50.580,0:15:53.190 from, and getting rid of the load of the 0:15:53.190,0:15:56.831 boilerplate for the simple application. I'm[br]not gonna use 0:15:56.840,0:15:58.920 it. Uncommenting out a few other bits of boilerplate. 0:15:58.920,0:16:02.480 And then finally I'm gonna add an rbenv specific 0:16:02.480,0:16:05.290 block that tells it to always use a particular 0:16:05.290,0:16:08.100 Ruby version. I think, in this case, 2 point 0:16:08.100,0:16:10.360 1 point 1, when deploying this app. 0:16:10.360,0:16:13.270 Finally, we have stage configuration files,[br]so if you 0:16:13.270,0:16:15.679 have a production server and a staging server,[br]and 0:16:15.700,0:16:17.931 here I'm just telling it that the production[br]server 0:16:17.940,0:16:20.630 is going to be web3 dot makeitwithcode dot[br]com, 0:16:20.630,0:16:21.710 which is the server we set up in the 0:16:21.720,0:16:24.279 first demo. And then that should be made available 0:16:24.280,0:16:28.371 to the public at, imaginatively, web3 dot[br]makeitwithcode dot 0:16:28.371,0:16:28.970 com. 0:16:28.970,0:16:32.990 Then, firing up a console, running a command[br]called 0:16:32.990,0:16:35.640 setup config, and you can think of this like 0:16:35.660,0:16:38.379 heroku create. This is just telling that server,[br]or 0:16:38.380,0:16:41.330 preparing that server for a new application.[br]It's generating 0:16:41.330,0:16:44.190 a few config files, it's creating a new database 0:16:44.190,0:16:48.211 yml and telling EngineX that it needs to serve 0:16:48.220,0:16:50.920 a new site. Then running another helper task[br]that 0:16:50.940,0:16:55.639 I added this time. Create database. This is[br]just, 0:16:55.640,0:16:57.770 create a new database on that server. Making[br]sure 0:16:57.770,0:17:00.130 there's a user that has permission to access[br]that 0:17:00.130,0:17:00.570 server. 0:17:00.570,0:17:03.651 And this is the important command. Cap production[br]deploy. 0:17:03.660,0:17:06.970 And this, if we're using capistrano, is our[br]equivalent 0:17:06.980,0:17:10.449 to git push heroku. And if you look at 0:17:10.449,0:17:12.780 the output, it's doing some pretty standard[br]stuff. It's 0:17:12.780,0:17:16.910 applying migrations, compiling assets and[br]restarting the app server. 0:17:16.920,0:17:18.871 And once that completes, we can fire up the 0:17:18.880,0:17:21.089 web browser, visit it, and you can see we've 0:17:21.089,0:17:23.420 got a simple, if utterly useless Rails application. 0:17:23.420,0:17:26.020 And it has access to the database, it can 0:17:26.020,0:17:27.980 read and write, and now we can iterate on 0:17:27.980,0:17:30.420 that, and every time we want to make a 0:17:30.420,0:17:34.410 change, we can just make that change, commit[br]it, 0:17:34.410,0:17:37.811 cap production deploy, and it really is as[br]simple 0:17:37.820,0:17:40.710 as deploying it to heroku. 0:17:40.720,0:17:43.080 So one thing that we haven't looked at yet 0:17:43.080,0:17:46.389 is, it's very easy to set this stuff up, 0:17:46.389,0:17:48.191 but the other great thing about using a path 0:17:48.200,0:17:50.720 like heroku is that when it breaks at 2 0:17:50.720,0:17:53.239 AM, it kind of isn't our problem. They have 0:17:53.240,0:17:55.260 engineers and they get woken up at 2 AM. 0:17:55.260,0:17:57.821 And they go and fix it. And our application 0:17:57.821,0:17:59.759 just comes back up online again. 0:17:59.760,0:18:04.050 And we can't get around that completely. It's[br]now 0:18:04.050,0:18:06.380 our problem when it breaks at 2 AM. But 0:18:06.380,0:18:09.610 luckily there are lots of tools out there[br]which 0:18:09.620,0:18:11.510 mean we can minimize the number of times this 0:18:11.510,0:18:15.201 will happen. If you think in a typical production 0:18:15.201,0:18:19.999 Rails application, you probably got some exception[br]handling. You 0:18:20.000,0:18:22.100 know some exceptions will happen from time[br]to time, 0:18:22.100,0:18:23.691 and so you've got to have your begin and 0:18:23.700,0:18:26.359 rescues in that. And then hopefully you've[br]also got 0:18:26.360,0:18:29.351 some sort of exception notification. And that[br]means that 0:18:29.351,0:18:32.219 when an exception that you haven't planned[br]for happens, 0:18:32.220,0:18:34.050 you get an email and you know that you 0:18:34.050,0:18:35.441 can go in, you should go in and fix 0:18:35.441,0:18:36.080 it. 0:18:36.080,0:18:38.959 So, there are an analogous set of tools when 0:18:38.980,0:18:41.601 you're setting up your own servers. And these[br]are 0:18:41.601,0:18:45.009 called monitoring tools, and they're really,[br]really simple. They're 0:18:45.009,0:18:47.431 essentially just a list of checks that say,[br]here's 0:18:47.440,0:18:50.029 how something ought to behave. If it's not[br]behaving, 0:18:50.029,0:18:52.441 here's a command that you can run to try 0:18:52.441,0:18:55.879 and fix it. If it's still not working, here's 0:18:55.880,0:18:57.660 the person to notify and how to notify them 0:18:57.660,0:18:58.890 there's a problem. 0:18:58.890,0:19:02.051 And that might sound a little bit daunting.[br]So 0:19:02.060,0:19:03.499 every time you set up an application you're[br]gonna 0:19:03.500,0:19:05.990 have to think about all these checks. In practice, 0:19:06.000,0:19:08.640 this is where configuration management comes[br]in. So, if 0:19:08.640,0:19:10.941 you remember in that first demonstration,[br]when I uncommented 0:19:10.941,0:19:14.330 that Redis role, as part of that Redis role, 0:19:14.330,0:19:16.639 when I originally wrote it, probably a year[br]or 0:19:16.640,0:19:19.380 a couple of years ago, I included the monitoring 0:19:19.380,0:19:22.980 configuration for it, which basically says,[br]is Redis working? 0:19:22.980,0:19:25.331 If no, restart it. If it's still not working, 0:19:25.340,0:19:25.840 email me. 0:19:25.840,0:19:28.950 And now that will get automatically setup[br]every time 0:19:28.950,0:19:31.210 I include that Redis role. So there really[br]isn't 0:19:31.210,0:19:33.889 that much duplication. Configuration management[br]just does all of 0:19:33.900,0:19:35.931 this for us. 0:19:35.940,0:19:38.919 So to get a bit more practical, if you 0:19:38.920,0:19:41.831 decide to, to go ahead and try this. This 0:19:41.831,0:19:44.060 is the stack that I end up using, probably 0:19:44.060,0:19:48.729 99% of the time. For a web server, EngineX, 0:19:48.729,0:19:50.571 really popular in the Rails community. There's[br]lots of 0:19:50.580,0:19:54.060 really great Rails-specific documentation[br]out there. 0:19:54.060,0:19:56.550 For an app server, Unicorn. This probably[br]isn't what 0:19:56.560,0:19:58.359 you're using in development at the moment.[br]That's more 0:19:58.380,0:20:01.191 likely to be Thin or Webrick. Unicorn's a[br]little 0:20:01.191,0:20:04.259 bit more complicated. But, for that little[br]bit of 0:20:04.260,0:20:07.760 extra complexity, you get some really nice[br]features. In 0:20:07.760,0:20:09.900 particular, it's very easy to set up what's[br]called 0:20:09.900,0:20:13.650 zero-downtime deployment, which you may already[br]be familiar with. 0:20:13.650,0:20:16.860 Essentially what this means is, the simplest[br]way to 0:20:16.880,0:20:21.120 deploy an application is to deploy your new[br]code, 0:20:21.120,0:20:23.560 stop the existing application, and then start[br]it again 0:20:23.560,0:20:25.260 with the new code. The problem is, if you've 0:20:25.260,0:20:27.060 got a big Rails application, it can take two 0:20:27.060,0:20:29.211 or three minutes to start up. And that means 0:20:29.211,0:20:30.680 that, for two or three minutes, every time[br]you 0:20:30.680,0:20:33.969 deploy, your app's offline, which is pretty[br]bad for 0:20:33.980,0:20:36.750 your customers if you're doing this five,[br]six times 0:20:36.750,0:20:37.571 a day. 0:20:37.580,0:20:39.309 With Unicorn, you can set it up so that 0:20:39.309,0:20:42.260 when you deploy new code, it will start up 0:20:42.260,0:20:44.090 your app in the background, and only once[br]it's 0:20:44.090,0:20:46.410 ready will it switch it over and stop the 0:20:46.410,0:20:51.041 previous version, so your customers don't[br]notice an interruption. 0:20:51.060,0:20:53.599 For a database, I typ, by default go with 0:20:53.600,0:20:56.420 PostGres. Again, you've probably all heard[br]of it. Very 0:20:56.420,0:20:59.260 popular in the Rails community. I recommend[br]it to 0:20:59.260,0:21:01.420 people, because if you're on the Heroku free[br]plan, 0:21:01.420,0:21:05.500 it's probably what you're using already, because[br]it's their 0:21:05.500,0:21:06.590 default database. 0:21:06.600,0:21:09.010 The only controversial item on this list,[br]because all 0:21:09.010,0:21:11.081 lists should have at least one controversial[br]item, is 0:21:11.081,0:21:14.239 probably Monit. And it's a bit controversial[br]because there 0:21:14.240,0:21:16.321 are some amazing monitoring tools out there[br]which are 0:21:16.321,0:21:20.710 written using Ruby syntax for defining rules.[br]I keep 0:21:20.710,0:21:24.560 coming back to Monit because it's tiny. Its[br]syntax 0:21:24.580,0:21:25.980 isn't quite as nice to work with as some 0:21:26.000,0:21:27.721 of the others, some of the others, but it 0:21:27.721,0:21:30.450 uses absolutely no memory, and once you set[br]it 0:21:30.450,0:21:32.169 up, it just stays out of the way and 0:21:32.169,0:21:35.500 works, which is pretty much what I look for 0:21:35.520,0:21:37.290 in a monitoring tool. 0:21:37.290,0:21:39.211 For setting up your server, so your server[br]provisioning 0:21:39.220,0:21:43.259 tool, I recommend getting started with Chef[br]Solo. Chef 0:21:43.260,0:21:44.721 Solo is the tool you saw me using in 0:21:44.721,0:21:49.989 the first demonstration. And it's designed[br]for deploying individual 0:21:49.989,0:21:53.211 servers from your development machine. What's[br]really nice about 0:21:53.220,0:21:56.499 Chef Solo is it's built on top of a 0:21:56.500,0:21:58.941 much bigger piece of software called Chef[br]Server. And 0:21:58.941,0:22:00.509 Chef Server is designed for when you need[br]to 0:22:00.509,0:22:03.831 deploy lots of servers in complicated configurations. 0:22:03.840,0:22:06.190 So, if you need, eventually, to move on from 0:22:06.190,0:22:10.619 your simple single-box configuration to much[br]more complicated ones, 0:22:10.620,0:22:13.811 almost everything you've learnt using Chef[br]Solo will be 0:22:13.820,0:22:15.399 applicable to Chef Server. 0:22:15.400,0:22:21.020 I mentioned, previously, there were lots of[br]deployment tools, 0:22:21.020,0:22:24.221 automation tools out there for Rails. Probably[br]the best-known 0:22:24.221,0:22:28.239 one is Capistrano. At the moment, there are[br]two 0:22:28.240,0:22:31.331 stable versions of Capistrano out in the wild.[br]There 0:22:31.331,0:22:36.129 is Capistrano 2 and Capistrano 3. Most of[br]the 0:22:36.140,0:22:39.760 documentation and tutorials that you find[br]will probably be 0:22:39.760,0:22:43.201 for Capistrano 2. 0:22:43.201,0:22:45.950 Despite that, I recommend starting off with[br]Capistrano 3, 0:22:45.950,0:22:48.359 if you are starting from a blank slate. The 0:22:48.360,0:22:50.961 reason for this is that version 3 was a 0:22:50.961,0:22:55.080 complete ground-up rewrite of version 2. In[br]particular, it's 0:22:55.080,0:22:58.700 just a rake application. So Capistrano 3 is[br]just 0:22:58.700,0:23:03.549 a, a layer on top of rake that provides 0:23:03.549,0:23:07.020 deployment-specific functionality. And that[br]means that any knowledge you've 0:23:07.040,0:23:09.000 already got of how to write rake tasks and 0:23:09.000,0:23:10.640 how to interact with rake tasks and how to 0:23:10.640,0:23:13.920 debug them is directly applicable to Capistrano[br]3. And 0:23:13.920,0:23:16.071 that just makes the learning curve that little[br]bit 0:23:16.071,0:23:19.129 shallower. 0:23:19.129,0:23:21.311 So next steps, if you're gonna give it a 0:23:21.320,0:23:23.830 go. I suggest you go and get a VPS. 0:23:23.830,0:23:26.779 I suggest starting off with a one gig one. 0:23:26.800,0:23:28.640 You can run a Rails app on a 512 0:23:28.640,0:23:31.250 meg VPS. You can also spend a huge amount 0:23:31.250,0:23:33.340 of time troubleshooting out of memory hours,[br]which I 0:23:33.340,0:23:36.711 think is the least enjoyable thing possible.[br]You can 0:23:36.720,0:23:39.289 get a one gig VPS for about ten dollars 0:23:39.289,0:23:42.970 a month at DigitalOcean, I think. They're[br]also a 0:23:42.970,0:23:45.760 sponsor, and they're really generous with[br]free credit. So 0:23:45.780,0:23:47.370 if you corner them in a booth, I imagine 0:23:47.370,0:23:48.800 you can get at least a few months of 0:23:48.800,0:23:50.331 experimentation for free. 0:23:50.340,0:23:54.009 So, the url at the top there, talkingquickly[br]dot 0:23:54.009,0:23:58.850 co dot uk slash deploying_rails. That's a[br]page specific 0:23:58.850,0:24:02.250 to this presentation, and it contains links[br]to two 0:24:02.250,0:24:06.110 key tutorials. The first one goes through[br]step-by-step how 0:24:06.110,0:24:08.430 to set up a Rails server in the way 0:24:08.460,0:24:10.650 I did in the first video, and the second 0:24:10.660,0:24:13.430 one is how to set up Capistrano with a 0:24:13.430,0:24:16.170 new or existing Rails application, as per[br]the second 0:24:16.180,0:24:17.591 video. 0:24:17.591,0:24:20.619 They also include the complete sample code[br]that I'm 0:24:20.620,0:24:23.270 actually using in production, pretty much[br]every day, for 0:24:23.280,0:24:27.571 deploying new Rails apps. So, hopefully, the[br]fairly large 0:24:27.571,0:24:29.119 amount of time I put in originally trying[br]to 0:24:29.120,0:24:31.290 work out how to get a working configuration[br]management 0:24:31.290,0:24:33.030 set up, you can take that as a starting 0:24:33.030,0:24:35.880 point and get it started very quickly. It[br]should 0:24:35.900,0:24:37.821 take maybe an hour and a half to get 0:24:37.821,0:24:40.169 your first server set up and an app deployed 0:24:40.180,0:24:41.010 to it. 0:24:41.010,0:24:43.470 I mentioned at start, I've also written a[br]book 0:24:43.480,0:24:46.081 about this. Reliably Deploying Rails Applications. 0:24:46.081,0:24:46.960 This was basically 0:24:46.960,0:24:48.879 the reference that I wish I'd had when I 0:24:48.880,0:24:51.961 started doing this. It uses exactly the same[br]sample 0:24:51.961,0:24:56.269 code as those tutorials. So if you start off 0:24:56.269,0:24:58.441 using those and find you want to do more 0:24:58.441,0:24:59.789 complicated things, 0:24:59.800,0:25:01.010 hopefully that will be a useful reference. 0:25:01.010,0:25:03.740 And there's a discount code, et cetera, for[br]RailsConf 0:25:03.740,0:25:06.811 attendees on that link. 0:25:06.820,0:25:10.619 Finally, I'll be in the AirPad Pit at 4:30 0:25:10.620,0:25:12.150 today. So if anyone wants to give this a 0:25:12.150,0:25:14.821 go for real and wants a hand setting stuff 0:25:14.821,0:25:17.219 up, I'd be very happy to help out. Yeah. 0:25:17.220,0:25:19.720 Thank you so much for coming to the talk. 0:25:19.720,0:25:22.920 I've really enjoyed doing this. Let's have[br]some questions.