This article assumes you are running Linux or Mac OS
- Sign up for a DreamCompute account
- Create an SSH key
- Install a recent version of Python (2.7+)
- Create a virtualenv
- Install the helloworld Python package in this repository,
[user@localhost]$ virtualenv venv && . venv/bin/activate [user@localhost]$ git clone https://github.com/ryanpetrello/dreamcompute-hello-world.git hello-world [user@localhost]$ cd hello-world && pip install -e . shade ansible
Python and WSGI
This tutorial describes how to deploy a very simple Python WSGI application
that responds to all HTTP requests with
Hello, World! WSGI is a Python
protocol that allows a web server to interface with a Python callable (or
function) to handle each request. Our function looks like this:
def application(environ, start_response): data = "Hello, World!\n" start_response("200 OK", [ ("Content-Type", "text/plain"), ("Content-Length", str(len(data))) ]) return data
Deploying with Ansible
This article uses Ansible to create a repeatable “playbook” of commands to set up the Hello World application on a DreamCompute server. These commands include:
- Launching a server in DreamCompute and assigning a public IP address to it
- Installing a web server (this tutorial uses gunicorn)
- Installing our example Python application and configuring it to handle requests
In order for Ansible to run DreamCompute API calls on your behalf, you need to download a small shell script that sets up your API credentials called an openrc file, read the tutorial about openrc files for information on how to download and use it. After the shell script downloads, open your computer’s command line and run the following (substituting the actual location of your downloaded file).
[user@localhost]$ source /path/to/downloaded/file/dhc123456789-openrc.sh
You are prompted for a password - it’s the one you use to log in to the DreamCompute Dashboard.
At this point you should be ready to deploy your application. Do so by running the following commands:
[user@localhost]$ chmod 600 /path/to/keyname.pem [user@localhost]$ ansible-playbook -vvvv -i "localhost," playbooks/deploy.yml --extra-vars "key_name=keyname private_key=/path/to/keyname.pem"
You need to substitute the
keyname key name value for the actual name
you chose earlier, and you also need to replace
with the actual path to the PEM file you downloaded.
If all is well, you are greeted with an instructional message:
Visit http://18.104.22.168/ in your browser!
Example Server Architecture
ssh into your newly created server:
[user@localhost]$ ssh -i /path/to/keyname.pem email@example.com
you find a variety of processes running in the following configuration:
HTTP Request ──> <Production/Proxy Server>, nginx (22.214.171.124:80) │ │ <supervisord> (monitors and keeps gunicorn processes running) ├── <WSGI Server> gunicorn Instance (/tmp/gunicorn.sock) ├── <WSGI Server> gunicorn Instance (/tmp/gunicorn.sock) ├── <WSGI Server> gunicorn Instance (/tmp/gunicorn.sock) ├── <WSGI Server> gunicorn Instance (/tmp/gunicorn.sock)
supervisord is installed and is used to manage multiple
processes, each of which is bound to a Unix domain socket (though you could
also configure them to bind to a TCP port).
NGINX listens on port 80 and
balances incoming HTTP requests across the gunicorn workers processes.