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