Monday 18 June 2018

Getting Started with Python on SAP HANA XSA

Currently, XSA only distributes a build pack for Python while the runtime needs to be installed manually by the developer. This is different from Node.js and Java for which XSA provides runtime as well. Similarly, the SAP Web IDE for SAP HANA, the main development IDE for XSA, only supports Java and Node.js development. All Python development needs to be done and deployed through command line interface (CLI).

Setting up Python Runtime


First off, let’s start with deploying the Python runtime to XSA. Using the Terminal on your VM, navigate to a local directory and download and unzip the latest Python source release.

cd /local/dir
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
tar xzvf Python-3.6.5.tgz
cd Python-3.6.5
Create a new directory to hold compiled Python files and run build.

md name_of_new_dir
./configure --prefix=/path/to/new/directory --exec-prefix=/path/to/new/directory --enable-optimizations
make -j4 && make altinstall

If the process is successful, there should be bin, include, lib, lib64, and share folders created within your new directory.


Figure 1: Command Line Output for Successful Build

At this point, you should be all set to deploy the Python runtime to XSA. However, when I tried doing this the first time, the runtime was getting deployed fine but every time I try deploying and running an application, I was getting the following error:


Figure 2: Error Received on Application Deployment

It turns out that if you are using openSUSE LEAP, like myself, instead of the Enterprise SUSE, you have to do a bit more work to have the runtime deployed properly. You need to go into the new directory where you stored Python build and copy contents of the lib64 folder to the lib folder. This step should help prevent errors in deploying your Python applications later and hopefully, save you from the trouble I had to go through!

cp -avp lib64/* lib

Now you are ready to deploy the Python runtime to XSA, for real this time! If you have not done already, start XSA and login as admin using the user XSA_ADMIN and the HANA Express master password you chose when you installed HANA Express. When prompted for a space, choose development.

cd /usr/sap/HXE/HDB00
./HDB start
xs login


Figure 3: Expected Output After Login

Once you are logged in, deploy the Python runtime using xs create-runtime. The path specified should be to the new directory you created to store compiled Python files.

xs create-runtime -p /path/to/compiled/python/directory

To confirm the runtime has been deployed correctly, execute xs runtimes. The output list should include a python3.


Figure 4: Console Output if Runtime Deployed Properly

Ta da! If that’s the output you get, you have successfully deployed a Python runtime to XSA and are ready to start making some awesome, powerful, and yet relatively simple Python applications!

Hello World Application


Now that your Python runtime is all set up, you are ready to build your first application to test the runtime. I will cover everything from now on using Windows, though if you prefer to use Linux instead, all the steps and commands are similar. I will point out any differences as needed.

Create a new directory called pyapp and use this as the root directory for your application. Create a new file called runtime.txt and specify the version of Python runtime you have deployed.

>>> runtime.txt
python-3.6.5

Create a new file called requirements.txt. This file is used to specify all the dependencies for your python application, similar to the package.json file used in Node.js. As your application gets deployed, XSA goes through this file and gathers the dependent packages using pip, unless the packages are already installed and included in the application folder. Pip is the Python equivalent of npm in Node.js. For this example, we only need to import Flask which is a micro web framework for Python. It is quite simple to use, yet very powerful and useful in executing HTTP operations and designing RESTful APIs.

>>> requirements.txt
Flask==0.12.2

Create another file called manifest.yml, which outlines details about your application and its deployment in XSA. This is the application descriptor file similar to the mta.yaml file used for development on the SAP Web IDE for SAP HANA. As I mentioned earlier, you cannot use the Web IDE to build or run Python applications. All Python development has to be done locally and deployed to the XSA server using xs push from the CLI. The downside of this approach is that xs push command cannot run Multi-Target Applications (MTA). It follows the pure Cloud Foundry approach where the manifest is required and only a single module is run at a time. The MTA concept has been added to the Cloud Foundry by SAP, but is not yet available for Python. In short, you have to use a manifest.yml file as opposed to a mta.yaml file for developing Python applications. 

>>> manifest.yml
---
applications:
- name: pyapp
  host: pyapp
  path: .
  command: python server.py

Be extremely careful with indentation in the manifest.yml file. Use spaces instead of tabs and make sure similar sections are indented equally. The file conventionally starts with three dashes. The “applications:” line is not indented. The application name is preceded by preceded by a dash and a space. Every subsequent line is indented two spaces and every nested block is indented another two spaces.

Create a file called server.py, which will contain the application logic. Copy and paste the following code into the file. This code snippet initializes a Flask application, assigns it a port to run on, and binds the hello function to the default URL using the route() decorator. You can use @app.route(<URL (without hostname)>) to redirect users to a specific function in your Python application whenever that URL is requested. 

>>> server.py
import os
from flask import Flask

#create an instance of Flask
app = Flask(__name__)

#assign the port that the flask application runs on
port = int(os.environ.get('PORT', 3000))

#execute hello function when page URL is loaded
@app.route('/')
def hello():
    return "Hello Python World"

if __name__ == '__main__':
    app.run(port=port) 

Save all files and open the command line tool (on Windows or Linux – depending on where you are saving all the files). Login to XSA, make sure you are in the development space, and push the pyapp application to XSA server as shown.

xs login
xs target -s development     //to change space to development
cd C:/path/to/app/directory
xs push pyapp

If the application is staged and started successfully, the output should display information about the application including the URL to access it.


Figure 5: Console Output on Successful Application Deployment

If you are using hostname based routing, like myself, you will have to set up a host alias to be able to access the application URL (highlighted in Figure 5 above). I should mention here that we, at SAP, do highly recommend using hostname based routing as opposed to port based routing to make applications more user friendly. To set up a host alias, copy the URL for the application. Open the hosts file usually located in C:\Windows\System32\drivers\etc with Visual Studio Code as an administrator. Map the application’s URL to your machine’s IP address as shown below. Again, this step is not necessary if you are using a port based routing.


Figure 6: Host File Containing Hostname Aliases

Open the application URL on a web browser and the output should be “Hello Python World”.


Figure 7: Application Output

Congratulations, you have successfully deployed a Python runtime to XSA and used it to run your first Python application! If you have any trouble following along with any of the steps or run into errors that haven’t been accounted for in this post, feel free to comment below! I really hope you find this post helpful.

No comments:

Post a Comment