Python for Entrepreneurs Transcripts
Chapter: Deploying to the cloud
Lecture: Supervisor waitress
0:01 We got our whole application up and running in the last video
0:04 but the wsgi server was only running when we were logged in
0:06 under the deploy a user and running it manually.
0:09 That's not going to suffice for a regular application,
0:11 what we really need is some way to keep our pyramid application up and running
0:15 in the background, all the time, and have it automatically restart
0:18 if for some reason the application shuts down.
0:20 That's where the tool supervisor comes in;
0:23 supervisor is a commonly used tool in Python application deployments
0:27 to start, stop and restart services that you need to run your application.
0:31 We're going to install supervisor, we are going to have it run the p.serve command
0:35 which is using the waitress whiskey server to run our application,
0:39 then when we go back to our application's url everything will be set up
0:43 nginx will serve as a reverse proxy,
0:45 and forward requests on to our Python application
0:48 our Python application will run its code
0:50 supervisor will ensure that the application stays up and running
0:53 and our application will respond back through nginx server
0:56 and we can keep on serving users,
0:58 let's set up the last few bits of our Ansible playbook now.
1:01 We again need to write a couple of new files, one is a yaml file for a supervisor
1:05 that will run the wsgi server, and the second is a template for the supervisor configuration
1:10 we'll start out by writing the yaml file for Ansible.
1:13 Open up roles/common/tasks and start one up that is called wsgi.yml.
1:18 Our first task is to make sure that supervisor is installed via the system package
1:23 this should look familiar at this point, we're going to use the apt module
1:26 search for the package named supervisor, make sure it's installed
1:30 and just in case update that system cache
1:34 and we need to be a Super User to do it.
1:36 Alright Ansible install supervisor, then what-
1:39 well, we need a configuration for supervisor.
1:42 So we use the template module, and we'll specify a supervisor app jinja template
1:50 and it will be put under the supervisor configuration directory on our server.
2:00 And we'll just give it the name of the app name,
2:04 just in case we want to have multiple supervisor configurations out there.
2:07 Often this will come in if you're running a task queue like celery,
2:11 you'll have multiple supervisor configurations
2:14 for every single process that you want to manage.
2:16 So by giving it the app name we should know which application this one is running.
2:19 We load up our configuration, and then what we want to do
2:22 is we actually want to stop the supervisor process,
2:24 we need to be Super User for this,
2:27 and we actually also need to be Super User for the previous command,
2:31 all these should be run with Super User privileges.
2:34 Now there's one quirk that I often run into with supervisor
2:37 which is that if you just restarted quickly,
2:39 it often won't take the configuration files and reload them on Ubuntu,
2:44 it's a bug that's been out there for a while,
2:46 and so the way that I typically get around it is
2:49 I pause during the restart process, I stop supervisor I pause
2:52 and then I restart it, and that gets around the issue.
2:55 You may be able to find a better way to handle this,
2:57 but for now, we want to make sure that we know that this works,
3:00 when we restart supervisor that our configuration takes effect,
3:03 so this will ensure that you won't have any problems with it.
3:06 There is certainly room to optimize this part of the deployment.
3:09 And then finally, we need to restart supervisor
3:12 and then I also restart nginx when this happens,
3:15 that way nginx is not caching any of the results
3:18 everything is restarted from a clean slate.
3:21 Next, we need to write our template file for supervisor,
3:23 it's under roles/common/templates/supervisor_app.conf.j2
3:31 and this is what this supervisor configuration looks like
3:34 the program will be called app name
3:37 or we'll punch in the application name that we've given it
3:41 which is blue yellow in our case, we'll give it the commands to run
3:44 so under our virtual env directory, so we're reusing
3:47 a lot of these variables that we've already created
3:50 we use the pserve command, and we'll give it a production.ini,
3:54 we need to run this from a specific directory,
3:57 and so we can specify the exact directory in which we want to run this command
4:00 that will give it the sub app directory, but of course,
4:06 in your case, if you don't have sub applications folders within folders
4:09 multiple versions of applications, just give it the app directory.
4:13 The user we're running this as is the deploy a user
4:16 so we're not running it as root, we're running it as our deploy user,
4:19 we do want to auto start this application up,
4:22 we don't want to manually start the application, and we also want to auto restart it
4:25 in case supervisor has to restart the application,
4:28 if the app shuts down we want to restart it,
4:30 and we'll redirect any errors to log files, and this should be standard error.
4:38 We've only reused variables that we already defined,
4:41 so we don't need to define any new variables for our deployment.
4:44 The one last step is just to make sure that this new yaml file
4:47 is a part of our playbook, so under rules/common/tasks/main.yml
4:52 make sure wsgi.yml is in there, save that,
4:57 let's try to kick it off-
5:03 it looks like everything ran without a problem,
5:05 but the ultimate test is to check our url, see if our application is live
5:09 we'll restart- boom, blue yellow rockets,
5:13 we got our whole application up and running,
5:17 being served by a waitress on the back end as our wsgi server
5:21 and we're in business.
5:23 So now we have our complete Ansible playbook
5:26 that also can form the foundation in case you want to change any of these pieces
5:29 if you want to swap out the wsgi server, use a different wsgi server
5:32 add new features and functionality to your application,
5:35 if you need to do additional configuration,
5:38 you can do all that by modifying this Ansible playbook.
5:41 We can rerun this over and over again,
5:43 as our code changes, keep our application updated.
5:45 And finally have a repeatable automated way to do our deployment.