Serverless computing based on OpenWhisk, part 2







This article continues the series of translated notes on OpenWhisk by Priti Desai . Today we will look at examples of GitHub webhook deployment, periodic signals, as well as the simplest application for sending messages to Slack.







Github webhook



OpenWhisk functions can be triggered by various events (for example, push, pull request, etc.) from the GitHub repository .







Let's see how you can deploy a function to handle a GitHub webhook using wskdeploy .







Step one



Create a manifest file:







To handle events with GitHub, you need to specify the event source /whisk.system/github/webhook



as a condition for triggering:







 packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! triggers: GitHubWebhookTrigger: feed: /whisk.system/github/webhook rules: helloworldOnWebhook: action: helloworld trigger: GitHubWebhookTrigger
      
      





Second step



We create a file for deployment, in which we set the values ​​of the input parameters username



, repository



, accessToken



and events



for conditional processing of the event source GitHubWebhookTrigger :







 application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: GitHubWebhookTrigger: inputs: username: pritidesai repository: pritidesai/helloworld accessToken: <accessToken> events: push
      
      





Step three



Expand the function:







 ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld bindings: * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: GitHubWebhookTrigger bindings: - name: accessToken value: **** - name: events value: push - name: username value: pritidesai - name: repository value: pritidesai/helloworld annotations: - name: feed value: /whisk.system/github/webhook Rules * rule: helloworldOnWebhook - trigger: GitHubWebhookTrigger - action: helloworld Do you really want to deploy this? (y/N): y Deploying package helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger feed GitHubWebhookTrigger ... Done! Deploying rule helloworldOnWebhook ... Done! Deployment completed successfully.
      
      





We deployed the hello world function, called from GitHub every time we update the code in the GitHub repository, and used the webhook for this:













We can check the deployed function by creating new code changes in the GitHub repository. In this case, simple welcome messages will be returned. Let's try to use the payload when updating the code sent from GitHub via a POST request. This data is available as function parameters, for example:







 function main(params) { console.log("GitHub repository is at ", params.repository.url); return {commits: params.commits}; }
      
      





Here is a snippet of the payload obtained by the normal editing of README.md:







 "commits" : [ { "author" : { "name" : Priti Desai, "username" : pritidesai }, "timestamp" : 2017-03-20T12:54:41-07:00, "removed" : [ ], "modified" : [ README.md ], "added" : [ ], "message" : Update README.md, "committer" : { "name" : GitHub, "email" : noreply@github.com, "username" : web-flow } } ],
      
      





You can study the detailed description of the GitHub webhook operation here







Signals



OpenWhisk functions may periodically be triggered by an internal signal (something like cron tasks). Let's try to add a conditional trigger on a signal through wskdeploy



.







Step one



To process signals, you must specify the event source /whisk.system/alarms/alarm



as a condition for triggering in the manifest:







 packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 inputs: name: type: string description: name of a person place: type: string description: location of a person outputs: payload: type: string description: a simple greeting message, Hello World! triggers: Every12Hours: feed: /whisk.system/alarms/alarm rules: hellowroldOnCron: action: helloworld trigger: Every12Hours
      
      





Second step



We create a file for deployment, we specify cron as an input condition, with value Every12Hours . You can also use both options used in cron, traditional and enhanced:







 <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>  <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>
      
      





Contents of deployment.yaml file:







 application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: Every12Hours: inputs: cron: "0 */12 * * *"
      
      





It is possible to specify the payload transmitted when the signal is triggered (transmitted as a function parameter every time it is triggered) as the trigger_payload parameter:







 application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris triggers: Every12Hours: inputs: cron: "0 */12 * * * *" trigger_payload: "{\"name\":\"Mark\", \"place\":\"Barcelona\"}"
      
      





Step three



Expand the function:







 ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld bindings: * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: * trigger: Every12Hours bindings: - name: cron value: 0 */12 * * * * annotations: - name: feed value: /whisk.system/alarms/alarm Rules * rule: helloworldEvery12Hours - trigger: Every12Hours - action: helloworld Do you really want to deploy this? (y/N): y Deploying package helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deploying trigger feed Every12Hours ... Done! Deploying rule helloworldEvery12Hours ... Done! Deployment completed successfully.
      
      





From now on, ace has a hello world function that runs every 12 hours. We can check it by activating the condition, as shown earlier







Package for Slack



To send messages to Slack , a package for Slack is offered. Let's look at package binding using wskdeploy



.







Step one



Create a manifest file with a package binding for Slack. To do this, specify it as a dependency, setting the location value to /whisk.system/slack



:







 packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL
      
      





First of all, we need to configure support for the incoming webhook in our Slack work environment. You can configure a new webhook to send messages to the Slack channel using this step-by-step instruction .







Now we add the package for Slack to our application for sending messages to the Slack channel every hour using this manifest:







 packages: SlackPackage: dependencies: slack-package-to-post-messages: location: /whisk.system/slack inputs: username: $SLACK_USERNAME url: $SLACK_URL channel: $SLACK_CHANNEL actions: post-to-slack: function: actions/post-to-slack.js runtime: nodejs:6 inputs: message: type: string description: message to post on slack slack_package: type: string description: slack package name triggers: everyhour: feed: /whisk.system/alarms/alarm rules: post-to-slack-every-hour: action: post-to-slack trigger: everyhour
      
      





The contents of the post-to-slack.js function can be viewed here .







Second step



Create a file for deployment:







 application: name: AppToPostToSlack packages: SlackPackage: actions: post-to-slack: inputs: message: "Hello from WskDeploy!" slack_package: slack-package-to-post-messages triggers: everyhour: inputs: cron: "0 */1 * * *"
      
      





Step three



Expand the function:







 ./wskdeploy -p ~/AppToPostToSlack/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \ __\/ |_| Packages: Name: SlackPackage bindings: * dependency: slack-package-to-post-messages location: /whisk.system/slack * action: post-to-slack bindings: - message : "Hello from WskDeploy!" - slack_package : "slack-package-to-post-messages" annotations: Triggers: * trigger: everyhour bindings: - cron : "0 */1 * * *" annotations: - name: feed value: /whisk.system/alarms/alarm Rules * rule: post-to-slack-every-hour - trigger: everyhour - action: SlackPackage/post-to-slack Do you really want to deploy this? (y/N): y Deployment completed successfully.
      
      





At this step, we already have a post-to-slack function that runs 1 time per hour. You can verify it by activating the condition, as shown earlier . After activation, we should receive a new message on the Slack channel:







 Activation: post-to-slack (9909dd5229e84526bff9902a2cd860df) [ "2017-09-12T23:05:17.17872899Z stdout: Hello from WskDeploy!", "2017-09-12T23:05:17.549177677Z stdout: Posted message to slack" ]
      
      





Other cycle articles



Serverless computing based on OpenWhisk, part 1

Serverless computing based on OpenWhisk, part 2

OpenWhisk Serverless Computing, Part 3

Serverless computing based on OpenWhisk, part 4








All Articles