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