In this Code Pattern, we will demonstrate the use of OpenWhish by creating a podcast downloader. A light weight Flask application is set up to request and download the content, which is then retained in OpenStack Swift object storage.
When the reader has completed this Code Pattern, they will understand how to:
- compose a microservices style app
- utilize the power of OpenWhisk
- make use of OpenStack Swift object storage
- A local python app with MySQL backend is set up.
- User packages Python Actions with a virtual environment in a zip file and uploads to IBM Cloud Functions.
- User interacts with local app to invoke Action.
- Data is stored in Object Storage OpenStack Swift.
- IBM Cloud Object Storage: Build and deliver cost effective apps and services with high reliability and fast speed to market in an unstructured cloud data store.
- OpenWhisk: Execute code on demand in a highly scalable, serverless environment.
- Microservices: Collection of fine-grained, loosely coupled services using a lightweight protocol to provide building blocks in modern application composition in the cloud.
- Clone the repo
- Create database
- Server side
- Create user, podcast, and subscription
- Create a zip python action
- Deploy Downloader action to OpenWhisk platform
- Invoke downloader action from podcast manager
Make sure you have a Bluemix account. It also helpful if you are slightly familiar with basic OpenWhisk commands Model and idea and have installed both the Bluemix CLI and the Cloud Functions Plugin.
- Python2.7 Installed.
- pip install -r requirements.txt
- Mysql DB server instance installed locally or provisioned on remote cloud server.
- curl command tool or similar tools available.
Clone the PodcastDownloader locally. In a terminal, run:
git clone https://github.com/IBM/PodcastDownloader
cd PodcastDownloader
Provision a mysql dabatase instance, using the provided podcasts_downloader.sql to create the database tables.
mysql -u user -p < podcasts_downloader.sql
Start the podcasts manager api server.
export FLASK_APP=podcasts_manager.py
export FLASK_DEBUG=1
flask run
Use curl to create the users, subscriptions, and podcasts. For example, suppose your podcast manager api server runs locally and listen on port 5000.
- create a user.
curl -X POST "http://localhost:5000/customer?name=liu"
- create a podcast.
curl -X POST "http://localhost:5000/podcast?podname=google&url=https://www.ted.com/talks/rss"
- create a subscription.
curl -X POST "http://localhost:5000/sub?customerid=1&podid=1"
Make sure you have docker installed locally and you have openwhisk/python2action docker images already built successfully.
cp DownloaderAction.py __main__.py
In order to persist the downloaded podcast content on storage, we now support integration with OpenStack swift client, to persist the downloaded content on IBM Object Storage.
First, you need to provision an object storage service, and copy all your storage service
authentication information to VCAP_SERVICES.json
, the Downloader Action will read the VCAP_SERVICES.json
file, to pass the storage services authentication.
Make the zip python action.
docker run --rm -v "$PWD:/tmp" openwhisk/python2action sh -c "cd tmp; virtualenv virtualenv; source virtualenv/bin/activate; pip install -r requirements.txt;"
zip -r wgetPython.zip virtualenv __main__.py VCAP_SERVICES.json
bx wsk action create wgetPython --kind python:2 wgetPython.zip
curl -X POST -m 50 "http://localhost:5000/download?customername=liu&downloader_url=https://openwhisk.ng.bluemix.net/api/v1/namespaces/<cf_org>_<cf_space>/actions/wgetPython"
This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.