LoginSignup
1
1

More than 5 years have passed since last update.

StackStormを使う Webhookを使って見る

Posted at

インストールなどは
http://qiita.com/ko-he-8/items/953e1eb5267f43789861

Webhook

StackStormのTriggerには「Webhook」が用意されています。これを使うことでpackが用意されていないツールとも比較的簡単に連携できます。

WebhookのRuleを作成する。

サンプル(/usr/share/doc/st2/examples/rules/sample_rule_with_webhook.yaml)も用意されていますのでこれをつかいます。

/usr/share/doc/st2/examples/rules/sample_rule_with_webhook.yaml
---
name: "sample_rule_with_webhook"
pack: "examples"
description: "Sample rule dumping webhook payload to a file."
enabled: true

trigger:
    type: "core.st2.webhook"
        parameters:
        url: "sample"

criteria:
    trigger.body.name:
        pattern: "st2"
        type: "equals"

action:
    ref: "core.local"
    parameters:
        cmd: "echo \"{{trigger.body}}\" >> ~/st2.webhook_sample.out"

---

このRulehが
* trigger
* webhookが受けつけるurlをhttps://localhost/api/v1/webhooks/sampleにしていしています。
* criteria
* 受け付けたpost requestのbodyのnameにst2という文字が含まれていればActionを実行します。
* action
* 実行ユーザがstanleyになるので/home/stanley/st2.webhook_sample.outにpostのbody部分を出力します。

操作

まずはトークンを取得します。StackStormのインストールで指定したuserとpasswordでトークンを取得します。

st2 auth <user> -p <password>

次に先ほど作成したRuleを登録します。

$ st2 rule create /usr/share/doc/st2/examples/rules/sample_rule_with_webhook.yaml

+-------------+--------------------------------------------------------------+
| Property    | Value                                                        |
+-------------+--------------------------------------------------------------+
| id          | 577b4eb45d698e258335e50b                                     |
| name        | sample_rule_with_webhook                                     |
| pack        | examples                                                     |
| description | Sample rule dumping webhook payload to a file.               |
| action      | {                                                            |
|             |     "ref": "core.local",                                     |
|             |     "parameters": {                                          |
|             |         "cmd": "echo "{{trigger.body}}" >>                   |
|             | ~/st2.webhook_sample.out"                                    |
|             |     }                                                        |
|             | }                                                            |
| criteria    | {                                                            |
|             |     "trigger.body.name": {                                   |
|             |         "pattern": "st2",                                    |
|             |         "type": "equals"                                     |
|             |     }                                                        |
|             | }                                                            |
| enabled     | True                                                         |
| ref         | examples.sample_rule_with_webhook                            |
| tags        |                                                              |
| trigger     | {                                                            |
|             |     "type": "core.st2.webhook",                              |
|             |     "ref": "core.1e4f1702-5213-4de0-a42c-001158753d9f",      |
|             |     "parameters": {                                          |
|             |         "url": "sample"                                      |
|             |     }                                                        |
|             | }                                                            |
| type        | {                                                            |
|             |     "ref": "standard",                                       |
|             |     "parameters": {}                                         |
|             | }                                                            |
| uid         | rule:examples:sample_rule_with_webhook                       |
+-------------+--------------------------------------------------------------+

ではルールで指定したurlにpostリクエストを投げてみましょう。'token'は先ほど取得したものに書き換えてください。

$ curl -k https://localhost/api/v1/webhooks/sample -d '{"foo": "bar", "name": "st2"}' -H 'Content-Type: application/json' -H 'X-Auth-Token: <token>'

確認

登録されているwebhookの確認します。

 $ st2 webhook list
 +------------------+------+------------------+-------------+------------------+
| type             | pack | name             | description | parameters       |
+------------------+------+------------------+-------------+------------------+
| core.st2.webhook | core | 1093a1f1-2c9a-   |             | {u'url':         |
|                  |      | 44aa-be3c-       |             | u'sample'}       |
|                  |      | 360c7f68a0f4     |             |                  |
+------------------+------+------------------+-------------+------------------+

実行結果を確認します。

$ cat /home/stanley/st2.webhook_sample.out 
{'foo': 'bar', 'name': 'st2'}

これでwebhookが正しく実行されていることが確認できました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1