Python for Entrepreneurs Transcripts
Chapter: Deploying to the cloud
Lecture: Application dependencies
0:01 Our application source code is on the server
0:03 so it seems like a good time just to take one more look at our map
0:06 and see where we are and where we're going to go in the last few videos.
0:09 We've already gone through setting up our domain name
0:12 getting our ssl certificate and setting up the web server,
0:14 and now we also have our source code pulled down from github,
0:17 along with any static files that are in our git repository on our server.
0:20 The final missing piece of the puzzle is our wsgi server
0:23 and that's what we're going to set up now.
0:26 The wsgi server along with using a virtual environment
0:28 to store our application dependencies is what's going to run our Python code
0:31 and have our application run from end to end.
0:34 Of course, this is going to involve a few more changes to our Ansible playbook
0:38 but once this is all done, our entire deployment is going to be automated
0:41 so we can run it again and again as the code continues to change.
0:44 We are going to start out by creating a new dependences.yml file
0:47 that will keep under common tasks, go ahead and open that up
1:00 the first one is going to be Python 3 virtual environments,
1:09 and the pip command specifically for Python 3 on our new Ubuntu system
1:14 because we're going to run a whole application with Python 3 rather than Python 2.
1:17 The name here we've seen this before, we're just going to specify item
1:21 and then we'll have a longer list of items, with the with items command.
1:28 Update our cache, use the super user command and our with items list
1:32 is only going to have two items, Python 3 venv,
1:37 that will install the appropriate system packages,
1:41 next up, we want to check if the virtual env should be created,
1:45 so if it's already been created, we'll just skip the step of creating a virtual env.
1:49 so as we continue to repeat running our deployment
1:52 it doesn't matter whether the virtual env exists or not,
1:55 we'll create it if it's necessary, and if it's already there, we'll just skip that step.
1:59 And we've seen this module before stat, and we'll check the venv directory
2:07 so this will just be a variable and we'll register venv created.
2:11 Now we actually want to create the venv directory,
2:14 we'll just do this from the shell module
2:17 and we only want to do this when the venv,
2:20 which we just checked if it's been created, when it does not exist.
2:28 Finally, we want to use the pip 3 command to install our project dependencies
2:32 and again, we'll just use the shell command,
2:36 we're going to combine two commands together,
2:39 we're going to move into the sub app directory, I'll explain that in just a moment
2:44 and we'll take a look at our venv directory, use the pip 3 command,
2:50 install upgrade, so if there's any new dependencies
2:56 they get added as we change our code,
2:59 we'll automatically update to the latest versions,
3:02 if we take a look at the code, we have many different subdirectories
3:05 with versions of the application,
3:07 and our is the one that we're deploying in this chapter
3:10 under chapter 15 deployment, and this is actually where our application lives;
3:14 in most projects you're just going to use the application directory
3:17 because that's where your application is going to live,
3:20 but we're separating out in this deployment, the application directory
3:23 from a sub application directory, because we have all these sub directories.
3:26 In your case, you probably don't have to do that in your own project.
3:29 So, now we need to create these two variables,
3:32 the first one being venv directory, and the second one being sub app directory.
3:35 First one, venv directory, this one will be under the deploy users home directory
3:44 I'll just call it venv, and the sub app directory
3:49 will be the app directory plus a sub directory.
3:54 Save that, and then the final step, make sure
4:01 that we add to our main.yml dependencies.yml, we should be able to run this now,
4:15 it looks like we had a little typo there, we should be able to run this now,
4:24 so yours will likely take several minutes because
4:27 the pip 3 command is going to PyPi, pulling down all the dependencies
4:30 what we are going to do, is we're going to ssh over to our remote server,
4:39 and there's nothing running, our wsgi server is not actually running here
4:42 but if we activate our virtual environment, we go under the directory
4:47 where our application lives, we can now type p serve for the serve command
4:53 production.ini, we're running on port 6543,
5:01 let's go to the pythondeploymentexample.com
5:06 nginx serves as a reverse proxy, hits the upstream server
5:10 and now the wsgi server is executing our Python code.
5:13 But, we see that there is an issue here,
5:16 if we take a look at this network tab, we're getting 404,
5:19 that's why we don't have any design or css is not there, images are not there,
5:23 so we need to slightly change our nginx configuration in order to handle this.
5:27 The problem here is that when we set up our nginx configuration,
5:31 we didn't set the right static directory, so I'm going to create a new shell here,
5:35 we're going to go to our local course demos,
5:38 we need to modify our template for nginx,
5:42 which is under rules/common/templates/nginx
5:45 and the big issue here is that we didn't specify the appropriate static directory
5:51 so our app directory is not actually serving up static files
5:55 it's not the correct location, if we take a look under blue yellow app
6:01 there we have the static directory,
6:05 so right now nginx is trying to serve up static assets
6:08 from an app directory, when it should serve up from the sub app directory,
6:13 now what is our sub app directory, that we just defined.
6:17 That's going to go to blue yellow app deployment,
6:21 and then I'll go to blue yellow app, so in our case,
6:23 we're just going to specify blue yellow app, and save that.
6:28 Rerun or deployment playbook, what this is going to do
6:31 is refresh the configuration for nginx, let's see what happens.
6:39 We hit refresh, and now we get actually back the files that we need
6:45 and our application is up and running,
6:51 We're manually running the wsgi server right now,
6:55 so if we stop this and we exit out of our connection,
6:59 then we refresh the page, we get that 502 bad gateway still.
7:03 So in the next video, we're going to stand up a Deamon process
7:06 that will run the wsgi server in the background,
7:09 automatically restart it if any issues arise,
7:11 and then our application deployment will finally be complete.