1 00:00:00,000 --> 00:00:04,472 [MUSIC] 2 00:00:04,472 --> 00:00:07,370 Here in my editor, I have our Guest Book Rails app open. 3 00:00:07,370 --> 00:00:10,160 It's a simple app we created in a prior workshop that lets 4 00:00:10,160 --> 00:00:12,090 visitors leave signatures. 5 00:00:12,090 --> 00:00:13,180 See the teachers notes for 6 00:00:13,180 --> 00:00:16,160 info on obtaining a copy of the latest code for yourself. 7 00:00:16,160 --> 00:00:19,940 We're going to set this app up to use Capistrano to deploy the production. 8 00:00:19,940 --> 00:00:23,670 So the first thing we're going to need to do is install Capistrano itself. 9 00:00:23,670 --> 00:00:26,777 So I'm gonna go into the Gemfile just like we did on the prior stage. 10 00:00:29,174 --> 00:00:33,295 And there's a line here that lets you use Capistrano for deployment. 11 00:00:33,295 --> 00:00:36,155 It enables the Capistrano rails gem, but 12 00:00:36,155 --> 00:00:39,935 Capistrano rails all by itself isn't going to work for this app. 13 00:00:39,935 --> 00:00:44,025 So I'm gonna delete this line, and instead here at the top of our development 14 00:00:44,025 --> 00:00:47,690 gem group, I'm gonna add three different gems. 15 00:00:47,690 --> 00:00:50,080 I'm just going to paste in the lines that require them. 16 00:00:50,080 --> 00:00:54,940 First is the capistrano-bundler gem which will use bundler on the remote 17 00:00:54,940 --> 00:00:58,960 server to install other gems that our app depends on. 18 00:00:58,960 --> 00:01:03,890 Next up is capistrano-rails itself, which will deploy our rails app, and 19 00:01:03,890 --> 00:01:06,230 the third is capistrano-rbenv. 20 00:01:06,230 --> 00:01:08,400 Since we're using rbenv on the remote server, 21 00:01:08,400 --> 00:01:12,130 we're going to need to invoke all our commands using the rbenv environment. 22 00:01:12,130 --> 00:01:15,020 Having this gem installed will automatically take care of using that 23 00:01:15,020 --> 00:01:16,440 environment for us. 24 00:01:16,440 --> 00:01:18,270 We've also got particular versions for 25 00:01:18,270 --> 00:01:21,480 each of these gems that we're locking them down to. 26 00:01:21,480 --> 00:01:23,910 Okay, let's save our changes to the gem file and 27 00:01:23,910 --> 00:01:27,700 go back to our directory with the rails app here in the terminal and 28 00:01:27,700 --> 00:01:30,130 we're going to run bundle install to install everything. 29 00:01:32,660 --> 00:01:36,180 Now we need to set up all the Capistrano configuration files just like we did in 30 00:01:36,180 --> 00:01:37,260 the previous stage. 31 00:01:37,260 --> 00:01:39,940 So I'm going to run bundle exec 32 00:01:41,340 --> 00:01:45,650 cap install to set up those configuration files. 33 00:01:45,650 --> 00:01:48,840 Okay, Capistrano has added its configuration files to 34 00:01:48,840 --> 00:01:50,360 our rails at forest. 35 00:01:50,360 --> 00:01:53,390 Now I'm going to go into one of those configuration files that 36 00:01:53,390 --> 00:01:54,910 can't file here in the brewed of our project. 37 00:01:56,790 --> 00:02:01,960 Within the Capfile you can require other libraries that Capistrano might need 38 00:02:01,960 --> 00:02:03,310 to deploy your app. 39 00:02:03,310 --> 00:02:05,620 There's a bunch of require statements already present here but 40 00:02:05,620 --> 00:02:06,820 they been common to doubt. 41 00:02:06,820 --> 00:02:08,540 I'm gonna uncommon the ones we need. 42 00:02:10,160 --> 00:02:14,063 So we're gonna need capistrano/rbenv, capistrano/bundler, 43 00:02:14,063 --> 00:02:18,545 capistrano/rails/assets for pre compiling our CSS and 44 00:02:18,545 --> 00:02:21,860 JavaScript and capistrano rails migrations for 45 00:02:21,860 --> 00:02:26,430 running our database migrations I'll save my changes there and close that. 46 00:02:26,430 --> 00:02:30,700 Next we need to edit the main Capistrano deployment file here in the config 47 00:02:30,700 --> 00:02:31,830 directory. 48 00:02:31,830 --> 00:02:34,780 This is where we're going to set the variables that get used else where in 49 00:02:34,780 --> 00:02:36,180 the deployment scripts. 50 00:02:36,180 --> 00:02:39,030 It's already got one for the application name which i'm going to go ahead and 51 00:02:39,030 --> 00:02:40,300 change here. 52 00:02:40,300 --> 00:02:42,380 I'll change that from its default to guest book. 53 00:02:43,690 --> 00:02:46,700 Next it wants the URL of a git repository that 54 00:02:46,700 --> 00:02:50,280 the remote server is going to download the rails code from. 55 00:02:50,280 --> 00:02:51,820 I'll just paste that in here. 56 00:02:51,820 --> 00:02:55,710 This URL refers to my personal git repo that holds the guest book code. 57 00:02:55,710 --> 00:02:59,040 You'll need to set up a repo of your own on GitHub or some other git server and 58 00:02:59,040 --> 00:03:00,570 use that URL instead. 59 00:03:00,570 --> 00:03:02,590 See the teacher's notes for more info. 60 00:03:02,590 --> 00:03:07,260 Next I'm going to set up a new variable called deploy_user, and 61 00:03:07,260 --> 00:03:12,780 that's gonna have the username that we're going to use to deploy the actual app. 62 00:03:12,780 --> 00:03:14,963 That will become deploy. 63 00:03:17,460 --> 00:03:21,910 Then down here, it has an example of a value for the deploy_to variable. 64 00:03:23,460 --> 00:03:27,490 The default Capistrano scripts use this variable as the path 65 00:03:27,490 --> 00:03:31,260 that it should actually install the Rails app code to. 66 00:03:31,260 --> 00:03:33,900 So, I'm gonna create a double quoted string here, and 67 00:03:33,900 --> 00:03:37,480 am going to interpolate a couple variable values into it. 68 00:03:37,480 --> 00:03:40,450 So, it's going to be home, within the root directory. 69 00:03:41,460 --> 00:03:43,746 Then am gonna interpolate in the value fetch. 70 00:03:46,767 --> 00:03:48,534 With the symbol, deploy user. 71 00:03:52,033 --> 00:03:56,540 What the fetch method does is it fetches the value of a Capistrano variable. 72 00:03:56,540 --> 00:04:01,520 So, we have deploy_user set up here, and this will actually retrieve that value and 73 00:04:01,520 --> 00:04:04,570 include it down here in the path we're deploying to. 74 00:04:04,570 --> 00:04:08,820 So this will create a path to the deployed users' home directory and then within that 75 00:04:08,820 --> 00:04:13,920 directory, I'm going to create a sub directory with the application name. 76 00:04:13,920 --> 00:04:17,790 So, I'll fetch the value of the application variable from above, 77 00:04:17,790 --> 00:04:22,410 which will be guest book, and then I'll create a guest book sub directory 78 00:04:22,410 --> 00:04:25,140 within my deployed users' home directory. 79 00:04:25,140 --> 00:04:26,310 I'll save and close this. 80 00:04:27,380 --> 00:04:32,080 The last file we need to configure is the parameters specific to 81 00:04:32,080 --> 00:04:36,100 the production server which is here config subdirectory, 82 00:04:36,100 --> 00:04:41,280 deploy subdirectory, in a file named production.rb. 83 00:04:41,280 --> 00:04:44,440 Here we have several example configuration lines common to doubt. 84 00:04:44,440 --> 00:04:48,100 Below that we need to add a new line where we call the server method and 85 00:04:48,100 --> 00:04:52,570 pass it a string with the address, whether that be a domain name or 86 00:04:52,570 --> 00:04:56,100 an IP address, for a server that we're going to deploy to. 87 00:04:57,880 --> 00:04:59,310 Following that string with the address, 88 00:04:59,310 --> 00:05:03,640 we can pass several key word arguments with several values we need to set up. 89 00:05:04,660 --> 00:05:07,460 Here we set up the name of the user that we're going to be 90 00:05:07,460 --> 00:05:11,600 using on the remote server, again this is going to be the deploy user. 91 00:05:11,600 --> 00:05:14,890 And then we have several roles that the server is going to fulfill. 92 00:05:14,890 --> 00:05:19,320 Here we use the standard roles that Capistrano supports. 93 00:05:19,320 --> 00:05:24,110 This is going to be both an app server, a database server and a web server. 94 00:05:24,110 --> 00:05:27,410 So we include a rake and a rake with all three of those values. 95 00:05:27,410 --> 00:05:29,970 So with that set up we can close out of that. 96 00:05:29,970 --> 00:05:31,620 Okay, we're just about ready to deploy. 97 00:05:31,620 --> 00:05:35,390 But there's one more detail that you may need to take care of. 98 00:05:35,390 --> 00:05:38,950 If your server is brand new it may not have bundler installed on it just yet. 99 00:05:38,950 --> 00:05:44,064 So lets SSH out to that server then I'll log in as my deployed user and 100 00:05:44,064 --> 00:05:47,671 send the IP address that we're connecting to. 101 00:05:51,021 --> 00:05:54,340 And I just need to run the gem install bundler command. 102 00:05:56,330 --> 00:05:59,440 That'll ensure that bundler is installed in case it's not already. 103 00:05:59,440 --> 00:06:02,640 And it'll be ready for Capistrano to use during the deployment, 104 00:06:02,640 --> 00:06:04,460 so we don't get any errors. 105 00:06:04,460 --> 00:06:08,750 Okay, so let's exit off the server and back to our local machine. 106 00:06:08,750 --> 00:06:14,040 And now we're ready to run the command for the deployment, bundle exec 107 00:06:14,040 --> 00:06:18,440 cap, the stage that we're going to deploy to, 108 00:06:18,440 --> 00:06:23,180 that's production and the command we're going to run which is deploy. 109 00:06:25,280 --> 00:06:28,760 It'll print off the names of the tasks as it completes them. 110 00:06:28,760 --> 00:06:31,260 So it'll download the Rails app from GitHub for 111 00:06:31,260 --> 00:06:35,740 me, then it'll run bundle install within that app on the remote server to ensure we 112 00:06:35,740 --> 00:06:36,990 have all the gems we need. 113 00:06:36,990 --> 00:06:38,700 This step can take quite a while. 114 00:06:40,420 --> 00:06:44,060 When it returns you to your shell prompt you'll know your deployment is complete. 115 00:06:44,060 --> 00:06:47,490 Unfortunately there's an error at the end of the deployment. 116 00:06:47,490 --> 00:06:49,940 If we'll scroll up we can see what the problem is. 117 00:06:53,060 --> 00:06:56,470 It looks like there was an error while I was trying to run the database migrations. 118 00:06:56,470 --> 00:07:01,580 The problem is that the database guestbook_production doesn't exist. 119 00:07:01,580 --> 00:07:03,490 So, that's something we are going to need to fix, but 120 00:07:03,490 --> 00:07:07,520 before we do let's look what they get the point successfully under the server. 121 00:07:07,520 --> 00:07:10,000 So, I'm gonna ssh back to the server there. 122 00:07:11,220 --> 00:07:13,321 And change into the guestbook directory. 123 00:07:18,159 --> 00:07:21,990 And you can see that our app deployment directory did get created successfully. 124 00:07:21,990 --> 00:07:26,560 Because that guestbook directory is here in our deploy users home directory. 125 00:07:26,560 --> 00:07:29,980 Capistrano has also created several sub-directories for us. 126 00:07:29,980 --> 00:07:32,820 Here in the releases directory subsequent releases 127 00:07:32,820 --> 00:07:35,770 will get downloaded each time you deploy a new version of your app. 128 00:07:37,150 --> 00:07:42,710 The repo directory is the getrepo that your app will be downloaded to. 129 00:07:42,710 --> 00:07:47,070 So, new code is downloaded to repo sub-directory and staged there prior to 130 00:07:47,070 --> 00:07:52,120 copying to the releases sub-directory The shared subdirectory is where configuration 131 00:07:52,120 --> 00:07:56,520 files that will be used across multiple versions of your app will be stored. 132 00:07:56,520 --> 00:08:00,880 And finally after a successful deployment there'll be a fourth subdirectory here 133 00:08:00,880 --> 00:08:04,750 named current, that'll be a link to the most current version of your app. 134 00:08:04,750 --> 00:08:07,630 So our app is getting cloned at the server successfully but 135 00:08:07,630 --> 00:08:10,290 the deployment fails while setting up the database. 136 00:08:10,290 --> 00:08:11,570 We'll look at how to fix this next.