Sunday 15 November 2015

Python on Cloud Foundry

Cloud Foundry is an open source cloud application platform. It can be installed on an own Server or bought as a service from a company. I myself tried cloud foundry provided by Swisscom. The advantage of the service is on one hand the integrated third party tools like database connections.

This post just describes how to get your python application running on already installed cloud foundry platform, not the installation of the platform. The demo application is a basic cloud interface based on the falcon framework for cloud API.

Preparations

To generate the dependencies for your cloud environment you need pip and virtualenv, which can be installed like this.

sudo apt-get install python-pip, python-virtualenv

Since the demo application uses a falcon cloud API we need falcon. Cython is used to improved the falcon's performance and gunicorn is the http server to run the falcon application on.

sudo pip install cython, falcon, gunicorn

Setup for the project

The whole project for the cloud has to be saved in one folder. To make the installation of the application on the cloud easier are we providing a requirements.txt file and a manifest.yml. Besides this is the folder containing the source for the application, in our case the file server.py.

The manifest.yml has to be written manually. It contains the information about the application. A very simple manifest can you see here.

---
applications:
- name: Example
  memory: 256M
  command: "gunicorn server:app"

The requirements.txt file can be generated automatically using virtualenv. For this open terminal, and create a new virtual environment for our cloud application. Within this virtual environment we install all the required packets to run our application, which is in our case again cython, falcon and gunicorn. With the pip freeze command its possible to print the currently installed packets into the requirements file.

virtualenv env
source env
pip install --upgrade cython falcon gunicorn
pip freeze > example/requirements.txt
deactivate
rm -r env

Upload the application to the cloud

To be able to upload your project to the cloud you need to install the command line interface to the cloud. You can download the one for your system here. Go to the folder containing your application and log into your cloud with the following command and enter your password.

cf login -a https://your-cloud.com -u myusername

Within the cloud you have to change to the space which will contain your app later. For this switch to it with the following command.

cf target -o myusername -s DEVSPACE

Now you can push your application to the cloud using the cf push command. You don't need to specify any more parameters in the push command since we already defined them all in the manifest.yml.

cf push
That's already it. For everyone wanting a starting point for their falcon application is here a simple hello world application for the falcon.
import falcon

class HelloWorld(object):
    def on_get(self, req, resp):
        resp.body = "hello world"
        resp.status = falcon.HTTP_200

app = falcon.API()
helloWorld = HelloWorld()
app.add_route('/helloworld', helloworld)
More support for your first falcon project is available on the official tutorial.

Sources

2 comments:

  1. Infycle Technologies offers the best Data Science Certification in Chennai and is widely known for its excellence in giving the best Data Science training in Chennai. Providing quality software programming training with 100% placement & to build a solid career for every young professional in the software industry is the ultimate aim of Infycle Technologies. Apart from all, the students love the 100% practical training, which is the specialty of Infycle Technologies. To proceed with your career with a solid base, reach Infycle Technologies through 7502633633.
    Get Data Science Certification in Chennai | Infycle Technologies

    ReplyDelete