open whisk quick start guide

61
IBM Bluemix OpenWhisk Joseph Chang| IBM Cloud Architect | [email protected]

Upload: joseph-chang

Post on 13-Apr-2017

208 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Open whisk quick start guide

IBM Bluemix OpenWhisk

Joseph Chang| IBM Cloud Architect | [email protected]

Page 2: Open whisk quick start guide

• Openwhisk Programing Model• Let’s do it

– Install Command Line– Trigger action from Cloudant– Trigger action from Http Request– Define Trigger-Event for Node-Red

2

Page 3: Open whisk quick start guide

Openwhisk Programing Model

3

From:

Page 4: Open whisk quick start guide

Openwhisk Programming model

• Services define the events they emit as triggers, and developers associate the actions to handle the events via rules

• The developer only needs to care about implementing the desired application logic - the system handles the rest

T AR

Page 5: Open whisk quick start guide

Programming model

Trigger: „A class of events that can happen“T

Page 6: Open whisk quick start guide

Programming model

Actions: „An event-handler, i.e. code that runs in response to an event“A

Page 7: Open whisk quick start guide

Programming model

Actions: Multi-runtime support, e.g. JavaScriptA

function main(msg) {return { message: 'Hello, ' + msg.name + ' from ' +

msg.place };};

Page 8: Open whisk quick start guide

Programming model

Actions: Can be chained to create sequences to increase flexibility and foster reuse

A

AA := A1 + A2 + A3

AB := A2 + A1 + A3

AC := A3 + A1 + A2

Page 9: Open whisk quick start guide

Programming model

Rules: „An association of a trigger and an action“R

R := T A

Page 10: Open whisk quick start guide

Want to try it out?

• You want to try OpenWhisk on your own?– Want to try out our IBM Bluemix OpenWhisk offering?

• Sign-up today at: https://new-console.ng.bluemix.net/openwhisk/– Want to try out our open-source OpenWhisk offering?

• Visit: https://developer.ibm.com/openwhisk/

Page 11: Open whisk quick start guide

Whisk 命令列操作練習

11

Page 12: Open whisk quick start guide

Install Openwhisk command line tool

12

開啟 https://new-console.ng.bluemix.net/openwhisk

點選Configure CLI

Page 13: Open whisk quick start guide

Openwhisk command line tool

13

Mac 使用者 依指示完成命令列設定 , Win 使用者請看次頁

Page 14: Open whisk quick start guide

Openwhisk command line tool

14

Windows 使用者請下載安裝 Python 開啟 , 安裝完畢即可使

用 pip

https://www.python.org/downloads/windows/

pip install --upgrade https://new-console.ng.bluemix.net/openwhisk/cli/download

然後執行前頁的1,2,3,4 步驟 , 注意步驟 1 在 windows 是不需要 sudo 的

Page 15: Open whisk quick start guide

當 Cloudant DB 有異動時 , 觸發 Whisk Action

15

Page 16: Open whisk quick start guide

Exercise – 當 Cloudant DB 有異動時 , 觸發 whisk Action

}Event Providers

OpenWhisk

Cloudant

Git

Weather

Data event occurs, e.g.-Commit on a Git Repository-CRUD operation on Cloudant-….

Trigger execution of associated OpenWhisk action

…JS Swift Docker …

Update 一筆Document

顯示那一筆資料被異動 2 1

Page 17: Open whisk quick start guide

Write your whisk application

17

點選 Edit

Page 18: Open whisk quick start guide

Write your whisk application

18

1. 閱讀sample code

2. Click “Create An

Action”

Page 19: Open whisk quick start guide

Write your whisk application

19

填寫 Action 名稱

使用 template

Page 20: Open whisk quick start guide

Write your whisk application

20

範例程式產生

我們要修改output, 讓它顯示被修改的欄位

Page 21: Open whisk quick start guide

Get Cloudant Setting Info

21

1. 點選此處開啟選單2. 用新分頁開啟資料分析 , 我們之後會在 whisk 與 cloudant 交替使用

Page 22: Open whisk quick start guide

Get Cloudant Setting Info

22

2. 撰擇一個 Cloudant

NoSQL DB

1. 若尚未建立請建一個新的 Cloudant DB

Page 23: Open whisk quick start guide

Get Cloudant Setting Info

23

點選服認證

Page 24: Open whisk quick start guide

Get Cloudant Setting Info

24

1. 記下 username, password, host 等會兒要用到

2. 若此頁顯無認識證 , 請新增 一組

3. 回管理頁面

Page 25: Open whisk quick start guide

Create DB

25

點選LAUNCH

Page 26: Open whisk quick start guide

Create DB

26

點選 Create Database

為 DB 取個名字

記住 Database 名稱 , 等會兒要用到

Page 27: Open whisk quick start guide

Insert New Doc

27

點選 + , 再選 +New Doc

Page 28: Open whisk quick start guide

Insert New Doc

28

修改一下文件內容 , 加一些 欄位值可參考如下內容 ,完成後按

“ Create Document”

Page 29: Open whisk quick start guide

Insert New Doc

29

系統自動 新 增 _rev 欄位 回到OpenWhisk 頁面

Enable Docs

Page 30: Open whisk quick start guide

Modify the Action

30

1. 修改程式碼 , 讓被異動 的 doc id , 版本 顯示出來

function main(params) {return { "message": "The doc is changed " + params._id+":"+params._rev };

}

2. 按 Run it Now

Page 31: Open whisk quick start guide

Modify the Action

31

點選 Run With These

Values

Page 32: Open whisk quick start guide

Modify the Action

32

點選 Close

看到綠色邊及此訊息表示測試成功

Page 33: Open whisk quick start guide

Create a Trigger

33

點選Automate This

Action Create a Rule

Page 34: Open whisk quick start guide

Create A Trigger

34

點選CLOUDANT

changes

Page 35: Open whisk quick start guide

Create a Trigger

35

點選 NEW TRIGGER

Page 36: Open whisk quick start guide

Create a Trigger

36

完成後點選Save

Configuration

為 Trigger 取名字

Page 37: Open whisk quick start guide

Create a Rule

37點選 Next

檢查Trigger 是否新增 成功

檢查Trigger 是否新增 成功

Page 38: Open whisk quick start guide

Create a Rule

38

點選 This Looks Good

系統將建立此 Rule

Page 39: Open whisk quick start guide

Create a Rule

39

可修改Rule 名稱

點選 Save Rule

Page 40: Open whisk quick start guide

Create a Rule

40

點選Enable Rule

Rule 已產生 , 但是處於 disable 狀態

Page 41: Open whisk quick start guide

Create a Rule

41

1. 確認 Rule 是在 Enable 狀態

2. 點選Dashboard

Page 42: Open whisk quick start guide

View Dashboard

42

Page 43: Open whisk quick start guide

Do some changes in Cloudant DB

43

點選編輯

Page 44: Open whisk quick start guide

Do some changes in Cloudant DB

44

修改一下Note, 或其它欄位

點選 Save Changes, 回到 Whisk DashBoard

Page 45: Open whisk quick start guide

View Dashboard Again

45

點選勾勾可檢視完整 內容

Page 46: Open whisk quick start guide

由 Http Request 觸發 Whisk Action

46

Page 47: Open whisk quick start guide

OpenWhisk: How does it work?

OpenWhisk

JS Swift Docker …

Incoming HTTP request, e.g.HTTP GET mynewcoolapp.com/customers

1 2 Invoke associated OpenWhisk action „getCustomers“

Browser

Mobile App

Web App

Variety of languages

Page 48: Open whisk quick start guide

開啟 Openwhisk 編輯器

48

1. 選擇 Hello World With

Params 範例程式2. 點選 View

REST Endpoint

Page 49: Open whisk quick start guide

查看 Action 的 REST Endpoint

49

端點及範例在此

點選 Show Full

Command

Page 50: Open whisk quick start guide

可以直接將內容複製到程式中使用

50

Page 51: Open whisk quick start guide

Test Run

51

Page 52: Open whisk quick start guide

由 Node-RED 觸發 Event

52

Page 53: Open whisk quick start guide

從 Node-RED Trigger Openwhisk Event

53

Page 54: Open whisk quick start guide

節點說明

54

皆為預設值 , 無需調整

Page 55: Open whisk quick start guide

節點說明

55

由 payload 回傳參數 , 需為 JSON Object 型式

Page 56: Open whisk quick start guide

Define trigger with wsk

56

請先在 Command line 建立所需的 Triggerwsk trigger create <you trigger name>

Namespace Trigger name

Page 57: Open whisk quick start guide

Define trigger with wsk

57

在 OpenWhisk – Command CLI 說明可以找到 Auth Key

Auth key

Page 58: Open whisk quick start guide

節點說明

58

請將以上資訊填入

Page 59: Open whisk quick start guide

Test trigger

59

點選 timstamp 以觸發 Openwhisk trigger

Page 60: Open whisk quick start guide

View Result in Dashboard

60

收到由 Node-RED 觸發 的 Openwhisk Event

Page 61: Open whisk quick start guide

本單元結束