Python for Entrepreneurs Transcripts
Chapter: Deploying to the cloud
Lecture: git-ting our source code

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We got nginx working, ssl configured, got that https connection and DNS is pointing to the correct IP address,
0:08 but what about our code, we need to run our wsgi server, to execute our Python code and respond to requests.
0:14 In this video, we're going to create a deployment read only key, add it to github, then pull down our code onto the server
0:22 and make sure that every time we run our deployment playbook Ansible is always pulling down the latest code
0:29 so that we can constantly update our application as we continue to build it. And we already have a deployment key for Ansible,
0:34 but we're going to create another deployment key that we're going to use for read only purposes,
0:38 this will be stored on the server, and it will only be used to pull down code as opposed to both push and pull from remote git repositories.
0:47 We're going to pull out our handy ssh keygen command once again 248, specified the type of RSA, once again store this somewhere safe
0:58 outside your git repository, call this read only key, no pass phrase on this one because we're going to automate the deployment
1:08 and we don't want to have to type in the pass phrase when our server is trying to pull down our code.
1:14 Okay, so now we've got both a public and a private key let's take a look at what's inside the public key,
1:19 we're going to copy and paste that put it on github. Alright, so copy the contents of this key
1:27 and now move over into the github repository that you're deploying, Now if that's the Python for Entrepreneurs course demos
1:35 that you're using to follow along, what you're going to need to do because you don't have the settings where you can add a deploy key
1:42 you need to fork the repository, I'm going to fork it to my username, and now with the fork repository, we can go into settings.
1:51 Within settings, there is a special section for deploy keys, now with deploy keys you're going to click add deploy key
1:59 we'll call this read only for prod server, and then paste in the contents of that key
2:05 and don't allow right access, make sure this is not checked, click add key. Punching your password, all right, now we're all set up,
2:18 where we can pull the contents of this repository onto a remote server, but we can't push code back up to the repository,
2:25 and that's exactly the set up that we want, because we don't ever want to take code from production and put it back into our development environment,
2:32 we want it to be a one way pass from development to github, to the production server, or test server,
2:40 or whatever other servers we have that are running our code. With that in place, we're ready to get back to writing our Ansible playbook.
2:47 Once again, you are going to write a new yaml file, and this one is going to be under rules/common tasks, and we're going to write git.yml.
3:01 We'll just add a little note, in this playbook we're going to have four tasks
3:06 the first one is to ensure that git is actually installed on the remote machine because right now it's not, we'll use the apt module
3:14 and the name for this package is git-core we want it to be present of course, and we'll say yes to update cache,
3:22 we always want to update the cache to make sure we have the latest version and become true Super User.
3:30 Alright, the next task that we need to write is to create a directory to store our deployment key, and this will be created on the remote machine
3:38 so we're going to use the file module, even though we're creating a directory this is still going to use the file module,
3:46 the path here is going to be the home directory of our deployed_user and we'll just create a directory called git_deploy_key
3:57 and the state is going to be a directory, we're not creating a file we're going to create a directory,
4:03 we don't need to use Super User privileges for this one we want this to be accessible by the deploy_user;
4:10 the third task is to ensure that the deploy key is on the remote server so if it's not there, we'll copy it over using the copy module
4:20 the source is going to be the local deploy key directory and then the name of our deploy key, so in our case, that was read only key
4:32 and we'll make sure that that's a variable as well. So that's the source file, then the location that we want to store that on
4:43 is going to be the remote machine, which will be under the deploy_user
4:47 and actually that directory that we just created up above, git_deploy_key directory. And we want to make sure it has the appropriate permissions
4:57 and that the owner is not set to root but set to the deploy_user with deployed group. With that deploy key in place
5:04 and we've configured it on github, now we can clone or we can pull any updates from the remote repository on github onto the server.
5:14 Now there's a special module for this, and it's just named git, the repository is going to be- we'll use the code repository variable
5:23 and the destination will be the app directory that we've already specified in an earlier video;
5:30 the key file that we're going to use is that key file that we specified above, deploy_user, git_deploy_key, and the read only key name.
5:41 One more thing, we want to check, or git is going to check whether we want to accept the prompt that will pop up
5:50 when we're trying to access a new remote server so we want to say yes to that except the host key,
5:56 we can save this file and then we just need to make sure that this file is in the main.yml, so we'll include git.yml,
6:06 save the file, now let's just update our variables here. We got just a couple of new variables, we've already got deploy user
6:13 but we do need a local deploy key directory, now this is for our local file system, so this is just going to be for me home/matt
6:23 for you- whatever directory you decided to store that deploy key in. And then the one other bit is just the code repository,
6:31 and this code repository is that remote git repository that we're trying to clone. Now, in my case, that's going to be mattmakai,
6:39 and then Python for Entrepreneurs course demos, but obviously for you, you're going to specify your git user name
6:46 wherever you fork the repository to, and then .git Now, when it clones that repository, it's going to put it
6:54 under the app directory that we've specified up here, which is the home/deploy user, and then it'll put them under
7:01 put the code under blue yellow, so that's exactly what we want let's rerun this playbook- now it's going to go through all the same steps
7:08 for configuring ssl, https, but it's going to skip a lot of those steps because they've already been done in previous deployments,
7:18 okay, so we got to our git.yml file, but it looks like in one of the tasks we forgot to specify one of the variable names, local deployed directory.
7:26 So let's just take a look at that file real quick, yeah, right here, local deploy directory
7:36 and it may just have been a case where we messed up on one of the names. So we said local deploy key directory,
7:46 so now we have a decision, we can modify that file itself and that's probably going to be a better name, local deploy key directory,
7:54 and it looks like we didn't include read only deploy key name so we're going to need to include that as well.
8:02 And in this case, it's going to be read only deploy key that's the private key that we are going to need on the remote machine.
8:11 Now, that's important because we need the private key, not the public key on that remote machine,
8:17 so this private key is going to be stored on that remote machine, so that it can connect to github to pull down the code,
8:23 all right, let's give this one more try- now it's important to note that this is the name of the private key, not the public key,
8:31 because the private key is going to be stored on that production server that will allow it to connect to github, pull down that source code,
8:39 so don't use the name of the public key, use the name of the private key here.
8:43 Let's try this one more time, so everything looks good from what we can tell here, but we can double check this if we log into our remote server,
8:54 we'll do that by specifying our private key, and we're going to be deployer@ and then the IP address of our remote server,
9:06 let's see if we've got our source code; well that looks good, we've got our git deploy key,
9:11 and we've got blue yellow, all right, we've got our source code and now we can use this source code to install our application dependencies
9:18 and get that wsgi server up and running.


Talk Python's Mastodon Michael Kennedy's Mastodon