インストールなどは
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)も用意されていますのでこれをつかいます。
---
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が正しく実行されていることが確認できました。