1 00:00:00,900 --> 00:00:04,913 I've set up a fresh Ubuntu server with RBM, PostgreSQL and 2 00:00:04,913 --> 00:00:06,540 NGINX at a hosting company. 3 00:00:06,540 --> 00:00:10,370 If you need help setting these packages up see the teacher's notes. 4 00:00:10,370 --> 00:00:13,700 If you'd like to follow along using a virtual machine instead of an actual 5 00:00:13,700 --> 00:00:16,290 server you'll find info in the teacher's notes for that too. 6 00:00:16,290 --> 00:00:19,780 I have not installed Rails or Unicorn, nor 7 00:00:19,780 --> 00:00:23,150 have I manually deployed my Rails app to this server before. 8 00:00:23,150 --> 00:00:26,480 This is to ensure we have a clean environment with no hard to troubleshoot 9 00:00:26,480 --> 00:00:27,780 conflicts. 10 00:00:27,780 --> 00:00:31,700 We're going to let Capistrano handle all of those details for us. 11 00:00:31,700 --> 00:00:33,850 So let's exit back to our host machine. 12 00:00:33,850 --> 00:00:37,480 And first we're going to create a simple Rails app that we can 13 00:00:37,480 --> 00:00:38,940 test Capistrano out with. 14 00:00:39,960 --> 00:00:41,440 So I'm gonna create a new Rails app. 15 00:00:41,440 --> 00:00:46,572 I'll say rails new, and we'll call it captest, as in Capistrano test. 16 00:00:50,424 --> 00:00:52,360 Okay, then I'm gonna change into that directory. 17 00:00:53,840 --> 00:00:55,020 Change directory captest. 18 00:00:58,080 --> 00:00:59,550 And let's bring this up in our editor. 19 00:01:00,680 --> 00:01:03,450 Okay, so first we need to install the Capistrano Gem. 20 00:01:03,450 --> 00:01:05,660 We do that by editing our Gemfile. 21 00:01:05,660 --> 00:01:10,440 And there's already an existing entry in there for 22 00:01:10,440 --> 00:01:14,380 the Capistrano-Rails Gem, so I'm just going to uncomment that. 23 00:01:14,380 --> 00:01:19,750 I'll save that and go back to the terminal and run bundle install. 24 00:01:21,670 --> 00:01:25,420 That will install the Capistrano Rails Gem which has Capistrano itself as 25 00:01:25,420 --> 00:01:26,740 a dependency. 26 00:01:26,740 --> 00:01:29,880 Now, we need to set this new Rails projects up to use Capistrano. 27 00:01:29,880 --> 00:01:35,210 We do that by running bundle exec cap 28 00:01:35,210 --> 00:01:40,780 install, that'll run the cap executable which is included in the Capistrano Gem. 29 00:01:40,780 --> 00:01:45,150 The install sub command that we're running here is going to create a several files 30 00:01:45,150 --> 00:01:48,770 that Capistrano needs to work with our Rails project. 31 00:01:48,770 --> 00:01:51,400 Let's take a look at some of the configuration files that Capistrano 32 00:01:51,400 --> 00:01:53,430 created for us. 33 00:01:53,430 --> 00:01:55,980 The first is named Capfile with a capital C. 34 00:01:55,980 --> 00:01:58,010 It'll be in the rule of our project. 35 00:01:58,010 --> 00:02:02,150 It's mostly used for including y variance that Capistrano requires. 36 00:02:02,150 --> 00:02:04,850 We're just going to use the default set of libraries for now. 37 00:02:04,850 --> 00:02:06,830 So we'll leave this file alone. 38 00:02:06,830 --> 00:02:11,600 Next up is a file within the config subdirectory named deploy.rb. 39 00:02:11,600 --> 00:02:15,380 It's used to set configuration variables that'll be used to deploy your app. 40 00:02:16,420 --> 00:02:19,260 For this quick test, we're not going to need these default variables, so 41 00:02:19,260 --> 00:02:20,430 let's get rid of them. 42 00:02:20,430 --> 00:02:21,040 And instead, 43 00:02:21,040 --> 00:02:26,090 let's create a new variable with a message that we want to set up on our server. 44 00:02:26,090 --> 00:02:33,430 So we'll set message variable up, by calling the set method. 45 00:02:33,430 --> 00:02:36,390 The first argument to set is a symbol with the name of the variable 46 00:02:36,390 --> 00:02:37,090 you want to set up. 47 00:02:38,600 --> 00:02:42,060 And then we'll provide a string with a value to set it to. 48 00:02:42,060 --> 00:02:44,003 Hello, Capistrano. 49 00:02:47,064 --> 00:02:52,250 Let's save that and we'll make use of this variable later on when we set up the task. 50 00:02:52,250 --> 00:02:55,600 Next step, Capistrano created a deployed directory 51 00:02:55,600 --> 00:02:58,860 which contains configuration files for your various stages. 52 00:02:58,860 --> 00:03:03,630 Here we see two stages, the production stage and the staging stage. 53 00:03:03,630 --> 00:03:07,280 The main thing you do in this file is define servers for a stage that you'll 54 00:03:07,280 --> 00:03:11,980 need to deploy to, as well as any variables that are specific to that stage. 55 00:03:11,980 --> 00:03:15,780 So here we're going to call the server method to set up a server 56 00:03:15,780 --> 00:03:17,150 that we're going to deploy to. 57 00:03:17,150 --> 00:03:20,400 It's going to need an IP address to deploy to or 58 00:03:20,400 --> 00:03:23,360 we could use a host name, if we have one configured. 59 00:03:24,880 --> 00:03:28,240 So I'll paste that IP address into a string. 60 00:03:28,240 --> 00:03:29,300 That's the first argument. 61 00:03:30,660 --> 00:03:34,450 Then the server method takes a hash with various keys for 62 00:03:34,450 --> 00:03:36,320 various configuration items. 63 00:03:36,320 --> 00:03:39,230 The first key we're going to wanna use is the user key which 64 00:03:39,230 --> 00:03:43,760 specifies a username that we're going to log in and deploy the app as, so 65 00:03:43,760 --> 00:03:46,370 I'll use our deploy username for that. 66 00:03:46,370 --> 00:03:50,780 The next key we're going to want is an array with a list of roles that this 67 00:03:50,780 --> 00:03:53,220 server plays within our app. 68 00:03:53,220 --> 00:03:57,490 The usual roles Capistrano uses are app, db, and web. 69 00:03:57,490 --> 00:04:01,370 That's app as in the application server, db as in the database server, 70 00:04:01,370 --> 00:04:03,180 and web as in the web server. 71 00:04:03,180 --> 00:04:05,910 A single server can fill all three of those roles. 72 00:04:05,910 --> 00:04:08,400 For this quick demo though, we're gonna create a new role, so 73 00:04:08,400 --> 00:04:10,720 I'll set up an array with just a single item, and 74 00:04:10,720 --> 00:04:13,730 the role this server is going to play is it's going to be a greeter. 75 00:04:15,690 --> 00:04:17,770 It'll store a simple greeting for us. 76 00:04:17,770 --> 00:04:20,310 That's all that we need to define for our production server, so 77 00:04:20,310 --> 00:04:23,190 I'll just save this and close out of that file. 78 00:04:23,190 --> 00:04:26,350 And we won't define anything regarding a staging environment for right now. 79 00:04:26,350 --> 00:04:28,360 So we'll just leave that file alone. 80 00:04:28,360 --> 00:04:37,320 The last thing that Capistrano set up for us is a lib, capistrano, tasks directory. 81 00:04:37,320 --> 00:04:41,430 Here we can create files that define tasks that Capistrano needs to carry out when 82 00:04:41,430 --> 00:04:42,520 it's deploying an app. 83 00:04:42,520 --> 00:04:44,660 So let's create a new file within this directory. 84 00:04:46,440 --> 00:04:52,750 And I'm going to name it write_message.rake. 85 00:04:52,750 --> 00:04:56,230 It's important that the file extension end in dot rake, instead of dot rb, 86 00:04:56,230 --> 00:04:58,356 so that Capistrano detects it. 87 00:04:58,356 --> 00:05:02,840 It uses .rake files because Capistrano is based on another Ruby 88 00:05:02,840 --> 00:05:06,590 utility called Rake which stands for Ruby Make. 89 00:05:06,590 --> 00:05:10,680 Rake is generally used to build applications and Capistrano is just 90 00:05:10,680 --> 00:05:15,140 an extension of the rake syntax that allows for the deployment of applications. 91 00:05:15,140 --> 00:05:18,560 So we've got our file created, let's define a task now. 92 00:05:18,560 --> 00:05:23,350 First you're going to need to call the desc method to define a description for 93 00:05:23,350 --> 00:05:24,360 your task. 94 00:05:24,360 --> 00:05:28,460 The description method takes a string with the description to use cell. 95 00:05:28,460 --> 00:05:32,910 I'm going to say that this task will write a message to a file. 96 00:05:35,400 --> 00:05:39,390 Then we actually define the task by calling the task method. 97 00:05:39,390 --> 00:05:43,550 Task takes its symbols as its first argument with the name of the task. 98 00:05:43,550 --> 00:05:48,610 We'll call it write_message. 99 00:05:48,610 --> 00:05:51,835 And then it takes block with the actual task content. 100 00:05:51,835 --> 00:05:56,065 Normally task should only run on servers that fulfill particular roles. 101 00:05:56,065 --> 00:05:59,415 We set up our production server to fulfill the greater rule so 102 00:05:59,415 --> 00:06:03,930 we're going to specify that this task should be run on greater servers now. 103 00:06:03,930 --> 00:06:09,150 We do that with the on method, then we're going to call the roles method and 104 00:06:09,150 --> 00:06:12,220 pass it to return value to the on method. 105 00:06:12,220 --> 00:06:15,380 We need to pass the symbol argument roles with 106 00:06:15,380 --> 00:06:17,880 the names of the roles that it should work on. 107 00:06:17,880 --> 00:06:21,240 So we'll use a simple greeter and 108 00:06:21,240 --> 00:06:25,490 the return value of roles greeter would then get pass to the on method. 109 00:06:25,490 --> 00:06:29,980 Now we need a block for the on method that specifies what task we should carry out. 110 00:06:29,980 --> 00:06:33,320 Here we're going to use a Capistrano method called execute, 111 00:06:33,320 --> 00:06:37,660 that specifies a command we should execute on the remote server. 112 00:06:37,660 --> 00:06:40,800 Execute takes a string with the command to send and 113 00:06:40,800 --> 00:06:44,390 it'll actually submit that command to the server over SSH. 114 00:06:44,390 --> 00:06:49,300 So we're going to use the echo command on the server, to take a string and 115 00:06:49,300 --> 00:06:55,610 append it to a file within our home directory called message.txt. 116 00:06:55,610 --> 00:06:58,490 As for the content of that string we're gonna write to the file, 117 00:06:58,490 --> 00:07:02,290 we'll get that from a capistrano variable. 118 00:07:02,290 --> 00:07:07,840 So I'm going to interpolate the result of the ruby statement here into the string. 119 00:07:07,840 --> 00:07:11,630 And for that Ruby statement I'm going to call the Capistrano fetch method 120 00:07:11,630 --> 00:07:13,940 which will fetch the value of a variable. 121 00:07:13,940 --> 00:07:17,890 We set the message variable up in another configuration file. 122 00:07:17,890 --> 00:07:20,820 So I'm going to fetch that message variable here. 123 00:07:22,170 --> 00:07:27,410 So the results of this statement is that it will fetch the message variable and 124 00:07:27,410 --> 00:07:30,720 write it out to a file within the home directory on the server. 125 00:07:32,450 --> 00:07:34,990 Let me save that, and we'll go back to our terminal. 126 00:07:36,360 --> 00:07:41,570 We can get a list of all the tasks we have defined by running the bundle 127 00:07:41,570 --> 00:07:45,860 exec cap command, and 128 00:07:45,860 --> 00:07:50,740 passing it a flag of -T, to get a list of the tasks. 129 00:07:50,740 --> 00:07:55,050 There are quite a few tests that have been predefined up here, but 130 00:07:55,050 --> 00:07:58,660 down here at the bottom, we can see our tests defined. 131 00:07:58,660 --> 00:08:02,950 And as it suggests here, we can invoke that with bundle exec 132 00:08:02,950 --> 00:08:07,534 cap write_message. 133 00:08:09,170 --> 00:08:12,920 Each task requires you to specify a stage that you're going to run it on. 134 00:08:12,920 --> 00:08:14,759 And I neglected to do that so, 135 00:08:14,759 --> 00:08:19,040 I'm going to specify that this should run on our production server. 136 00:08:20,640 --> 00:08:22,600 So I'll run the right message task. 137 00:08:24,420 --> 00:08:27,790 It alerts us to the command that it's running on the remote server. 138 00:08:27,790 --> 00:08:32,380 And then it returns us to our shell, indicating that the deployment is done. 139 00:08:32,380 --> 00:08:35,920 Let's go out to that server and take a look at the results of our command. 140 00:08:35,920 --> 00:08:39,620 So I'm gonna copy the IP address here and I'm gonna run ssh. 141 00:08:39,620 --> 00:08:46,720 I'm gonna login as the deployed user at that IP address. 142 00:08:46,720 --> 00:08:49,670 And let's take a look at the contents of our home directory here. 143 00:08:49,670 --> 00:08:52,020 We can see a new message.txt file. 144 00:08:52,020 --> 00:08:53,824 And if we take a look at its contents. 145 00:08:55,872 --> 00:08:59,500 There's the message that we specified in our configuration files. 146 00:08:59,500 --> 00:09:00,050 Now of course, 147 00:09:00,050 --> 00:09:03,470 real deployment tasks that you set up are going to be more complex than this. 148 00:09:03,470 --> 00:09:06,740 But this is a good demonstration of how Capistrano works. 149 00:09:06,740 --> 00:09:09,120 In the next stage we're gonna do something more useful, and 150 00:09:09,120 --> 00:09:11,100 use Capistrano to deploy Rails apps.