Deploying a Python/Django webapp to SAP HANA Cloud Platform, starter edition for Cloud Foundry services
Cloud Foundry services on the SAP Hana Cloud Platform (HCP) have recently changed to beta status. This post documents my experience deploying a basic Python based web app to it.
The fact that it is based on an open standard backed by many big companies besides SAP increases innovation speed and hopefully creates a very strong foundation to implement for.
I’m very excited with these news and decided to give it a go by using the awesome Django framework to build a trivial web app as a proof of concept and deploy it there.
It stores “Tasks” in a table and lists them:
Not the prettiest web app
Data is stored in a PostgreSQL database provided out-of-the box on this Cloud Foundry service.
Cloud Foundry 101
Cloud Foundry, which is now fully supported on SAP HCP, is a platform as a service. This requires a slightly different mindset than the one used for managing a full server. In Cloud Foundry you have “aplications” which run with the support of buildpacks and “services” which your code can use. In the case of Cloud Foundry, you manage the platform with a command line tool called cf.
Download the file for Windows: https://github.com/cloudfoundry/cli#downloadsInformation
Unpack the zip file, and run the cf executable file. When prompted, select Install.
Download the file for Mac OS: https://github.com/cloudfoundry/cli#downloadsInformation
Open the pkg file. In the Installation Wizard, select Continue, and then select the destination folder for the cf CLI software. Select Continue again, and then select Install Software.
I did this on a linux box so installed with the following command:
curl -L “https://cli.run.pivotal.io/stable?release=linux64-binary&source=github” | tar -zx
YaaS trial subscription
Follow Rui Nogueira’s post on how to setup a trial account on YaaS. Hint: In the builder section, create an organization and a space. These are needed in a proper setup to support billing.
After that you’ll be able to login into the platform with the cf tool:
You’ll be required to enter the credentials. For me these are the same as in the HANA Cloud Platform trial. You’ll see the project space and organization you’ve set up earlier in the YaaS builder.
Our app is going to need a database to store the data. The platform gives us some options out of the box with the cf marketplace command:
We’ll use the postgresql because it is well supported by Django. To use it, just start the service:
cf create-service postgresql v9.4-container postgres-service
There is an important detail here. We didn’t specify a user/password or port number for the database. These are automagically created by the platform and will be provided to the application in runtime after you declare that it uses that service. More on that latter. Let’s first build a mini web app based on Django.
Django is a great python based classical web framework. I say classical because it follows a kind of Model-Template-View paradigm where html is rendered server side. Of course, there is nothing there stopping you from using client-side rendering frameworks, such as SAPUI5, but nevertheless, the framework is good and has a huge community.
To start quickly you can clone my repository ( https://github.com/jumpifzero/django-on-sap-hcp-cloudfoundry ) I’ll just point out the important bits to make it run on this platform.
Looking at the project structure:
We have an app folder called “tasks” and a project called “tasks_django”. (I know the names could be better, bear with me). Manifest.yml, Procfile and runtime.txt are crucial to make it run on Cloud Foundry:
- runtime.txt – lists the python version that will be used.
- manifest.yml lists the apps and their services. In our case an app called “djangotasks” with a dependency on a service called “postgres-service”.
- Procfile contains python and django-specific commands that are executed when the app is deployed.
The tasks folder defines the views, models and html templates our app will use. It also defines the urls this app exposes. There is nothing Cloud Foundry or SAP specific there. models.py define the structure of the tables the app needs, views.py the http request handlers and templates the html templates. This is just scratching django’s surface but enough for our purposes.
Tasks_django is the project folder and has a very important file: settings.py as It contains all django settings. The most important of which, the connection to the database.
In Cloud Foundry, the details for the services used by the app are available in an environment variable called VCAP_SERVICES. It is a json-formatted string. We’ll parse it to extract the database credentials and provide that to django:
Please use more robust code in production
We also check if that environment variable is set. If not we use a sqlite database as this allows us to test locally.
And that’s it! Really.
Use the push command (cf push). It will push the code, assign a url for the app, execute the initialization commands in Procfile (creating/migrating the database schema) and start the app and service.
Open the browser for that url and hopefully see your app running.