Outreachy intern Neerja Narayanappa offers this four-step tutorial.

image

This tutorial was written by Neerja Narayanappa

So far my journey with Outreachy has been incredible! My learning graph has skyrocketed by contributing to the Qinling project with the help of my mentor, Lingxian Kong. I truly appreciate and value everything I’m learning from my mentor and this project. It will forever remain a major contribution to my success and achievements.

In this post, you’ll create a function using the object store service of OpenStack, Swift. Qinling is not only growing tremendously but is now integrated with Swift, where you can create functions and invoke them with not much effort. I believe it’s going to pave new ways in helping user/developer to achieve their tasks.

So let’s get started!

 

OpenStack object storage service Swift can be integrated with Qinling to create functions. You can upload your function package to Swift and create the function by specifying the container name and object name in Swift.

In this example, the function would return **“Hello, Neerja!” ** you can replace the string with the function input. The steps assume there’s already Python 2.7 runtime available in your deployment.

Step 1: Create a function deployment package

$ mkdir ~/qinling_swift_test
$ cd ~/qinling_swift_test
$ cat <<EOF > hello_world.py

def main(name='World',**kwargs):
    ret = 'Hello, %s' % name
    return ret
EOF

$ cd ~/qinling_swift_test && zip -r ~/qinling_swift_test/hello_world.zip ./*

Step 2: Upload the file to Swift

1. Create a container named "functions"

$ openstack container create functions

+---------------------------------------+------------------+------------------------------------+
| account                               | container        | x-trans-id                         |
+---------------------------------------+------------------+------------------------------------+
| AUTH_6ae7142bff0542d8a8f3859ffa184236 | functions        | 9b45bef5ab2658acb9b72ee32f39dbc8   |
+---------------------------------------+------------------+------------------------------------+

2. Add the function deployment package(.zip) to the container

$ openstack object create functions hello_world.zip

+-----------------+-----------+----------------------------------+
| object          | container | etag                             |
+-----------------+-----------+----------------------------------+
| hello_world.zip | functions | 9b45bef5ab2658acb9b72ee32f39dbc8 |
+-----------------+-----------+----------------------------------+

3. Display the container and its object 

$ openstack object show functions hello_world.zip

+----------------+---------------------------------------+
| Field          | Value                                 |
+----------------+---------------------------------------+
| account        | AUTH_6ae7142bff0542d8a8f3859ffa184236 |
| container      | functions                             |
| content-length | 246                                   |
| content-type   | application/zip                       |
| etag           | 9b45bef5ab2658acb9b72ee32f39dbc8      |
| last-modified  | Wed, 18 Jul 2018 17:45:23 GMT         |
| object         | hello_world.zip                       |
+----------------+---------------------------------------+

Step 3: Create a function and get the function ID, replace the runtime_id with the one in your deployment. Also, specify a Swift container and object name.

$ openstack **function** create --name hello_world \
--runtime $runtime_id \
--entry hello_world.main \
--container functions \
--object hello_world.zip

+-------------+----------------------------------------------------------------------------------------------+
| Field       | Value                                                                                        |
+-------------+----------------------------------------------------------------------------------------------+
| id          | f1102bca-fbb4-4baf-874d-ed33bf8251f7                                                         |
| name        | hello_world                                                                                  |
| description | None                                                                                         |
| count       | 0                                                                                            |
| code        | {u'source': u'swift', u'swift': {u'object': u'hello_world.zip', u'container': u'functions'}} |
| runtime_id  | 0d8bcf73-910b-4fec-86b1-38ace8bd0766                                                         |
| entry       | hello_world.main                                                                             |
| project_id  | 6ae7142bff0542d8a8f3859ffa184236                                                             |
| created_at  | 2018-07-18 17:46:29.974506                                                                   |
| updated_at  | None                                                                                         |
| cpu         | 100                                                                                          |
| memory_size | 33554432                                                                                     |
+-------------+----------------------------------------------------------------------------------------------+

Step 4: Invoke the function by specifying function_id

$ function_id=f1102bca-fbb4-4baf-874d-ed33bf8251f7
$ openstack **function** execution create $function_id --input Neerja

+------------------+-----------------------------------------------+
| Field            | Value                                         |
+------------------+-----------------------------------------------+
| id               | 3451393d-60c6-4172-bbdf-c681929fae07          |
| function_id      | f1102bca-fbb4-4baf-874d-ed33bf8251f7          |
| function_version | 0                                             |
| description      | None                                          |
| input            | None                                          |
| result           | {"duration": 0.031,"output": "Hello, Neerja"} |
| status           | success                                       |
| sync             | True                                          |
| project_id       | 6ae7142bff0542d8a8f3859ffa184236              |
| created_at       | 2018-07-18 17:49:46                           |
| updated_at       | 2018-07-18 17:49:48                           |
+------------------+-----------------------------------------------+

Please try out this tutorial, it really makes integrating Qinling and Swift very simple!

 

This post first appeared on Medium. Superuser is always interesting in tutorials, get in touch editorATopenstack.org

Superuser