ようへいの日々精進XP

よかろうもん

Rundeck の通知プラグイン二種盛り(Amazon SNS と Fluentd インプット HTTP)

おはげようございます。引き続き、きっと誰も使わないであろう Rundeck のプラグインづくり。

プラグイン二種盛り

AWSSNS(Simple Notification Service) と Fluentd のインプット HTTP に通知するプラグインを作ってみた。

見よう見真似。


それぞれのプラグインのデモっぽいの

rundeck-aws_sns-notification-plugin

Rundeck 上で通知の設定

以下のようにジョブの設定画面にてアクセスキー、シークレットアクセスキー、リージョン、Topic ARN を指定する。

f:id:inokara:20151027080505p:plain

デモなので全てのトリガ(On Failure / On Start / On Success)で通知するように設定した。

f:id:inokara:20151027072054p:plain

Subscribe 先に Email を指定した場合

以下のようにメールが届く。

f:id:inokara:20151027075844p:plain

rundeck-fluentd_http-notification-plugin

Rundeck 上で通知の設定

こちらもジョブの設定画面にてインプット HTTP のエンドポイント、ログに付けるタグの Prefix を指定する。

f:id:inokara:20151027071847p:plain

どちらかと言うと、通知というよりはログ的に使いたいので、全てのトリガ(On Failure / On Start / On Success)で通知するように設定した。

f:id:inokara:20151027072054p:plain

fluentd 側の設定

以下のように設定。

<source>
  type http
  port 8888
  bind 0.0.0.0
  body_size_limit 32m
  keepalive_timeout 10s
</source>

<match rundeck.**>
  type stdout
</match>

設定したら以下のように Fluentd を起動する。

$ fluentd -c test.conf -l debug.log &

ジョブを実行すると...

以下のように debug.log ファイルにログとして出力される。

// OnStart 時に飛ぶ通知
2015-10-27 06:59:02 +0900 rundeck.notification.hello-world: {"execution_id":2676,"execution_href":"http://xxx.xxx.xx.xx:4440/project/hello-world/execution/follow/2676","execution_status":"RUNNING","execution_user":"admin","execution_dateStartedUnixtime":1445896741940,"execution_description":"","execution_argstring":null,"execution_project":"hello-world","execution_loglevel":"INFO","execution_failedNodeListString":null,"execution_failedNodeList":null,"execution_succeededNodeListString":null,"execution_succeededNodeList":null,"execution_nodestatus":null,"execution_dateEndedUnixtime":null,"execution_abortedby":null,"job_id":"fdf1cca5-e729-491b-a1fa-3d8fef373d46","job_href":"http://xxx.xxx.xx.xx:4440/project/hello-world/job/show/fdf1cca5-e729-491b-a1fa-3d8fef373d46","job_name":"hello-world","job_group":"","job_project":"hello-world","job_description":"","job_averageDuration":758}

// OnFailure 時に飛ぶ通知
2015-10-27 06:59:02 +0900 rundeck.notification.hello-world: {"execution_id":2676,"execution_href":"http://xxx.xxx.xx.xx:4440/project/hello-world/execution/follow/2676","execution_status":"FAILED","execution_user":"admin","execution_dateStartedUnixtime":1445896741940,"execution_description":"","execution_argstring":null,"execution_project":"hello-world","execution_loglevel":"INFO","execution_failedNodeListString":"localhost","execution_failedNodeList":["localhost"],"execution_succeededNodeListString":null,"execution_succeededNodeList":null,"execution_nodestatus":{"succeeded":0,"failed":1,"total":1},"execution_dateEndedUnixtime":1445896742912,"execution_abortedby":null,"job_id":"fdf1cca5-e729-491b-a1fa-3d8fef373d46","job_href":"http://xxx.xxx.xx.xx:4440/project/hello-world/job/show/fdf1cca5-e729-491b-a1fa-3d8fef373d46","job_name":"hello-world","job_group":"","job_project":"hello-world","job_description":"","job_averageDuration":758}

とりあえず、Rundeck Notification プラグインで取得出来るデータを全てダンプしてみた。


それぞれのプラグインを作る過程で得た知見

とりあえず列挙

具体的な内容については随時書いていく。

  • AWS SDK for Java の簡単な使い方
  • Gradle の簡単な使い方

AWS SDK for Javaの簡単な使い方

Credential

SDK で何かしようとすると避けては通れない認証周り。今回、rundeck-aws_sns-notification-plugin を作るにあたっては以下のコードが認証周りの定義になる。

(略)
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;

(略)

  public boolean postNotification(String trigger, Map executionData, Map config) {
    BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_access_key, aws_secret_access_key);
    AmazonSNSClient snsClient = new AmazonSNSClient(awsCreds);
    snsClient.setRegion(Region.getRegion(Regions.fromName(aws_region)));

今回は Rundeck のジョブ設定画面から入力したアクセスキー(変数:aws_access_key)、シークレットアクセスキー(変数:aws_secret_access_key)を扱う為に com.amazonaws.auth.BasicAWSCredentials クラスを事前に import しておく。次にアクセスキーとシークレットアクセスキーを引数として BasicAWSCredentials からオブジェクトを生成、更にそのオブジェクトを引数として AmazonSNSClient クラスからオブジェクトを作成し認証の手続きが完了となる。

尚、この場合にはアクセスキーとシークレットアクセスキーを用いた認証のみ対応する実装になってしまっている為、IAM role や $HOME/.aws/credentials での認証には対応出来ない。(今後の改善点の一つ)

Gradle の簡単な使い方

Gradle とは

ザックリ言うと Groovy で書かれたビルドツールとのこと。build.gradle というファイルにビルドに必要な定義を記述して、以下のように実行すると jar ファイルや war ファイルを生成してくれる。個人的には make コマンドっぽい印象を受けている。

$ gradle build

gradle 自体のインストールは GVM というツールを利用してインストールするのが簡単だった。

build.gradle サンプル

rundeck-aws_sns-notification-plugin の build.gradle のサンプル。

version = '0.0.1'
defaultTasks 'clean','build'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.5
ext.rundeckPluginVersion= '1.1'

configurations{
    pluginLibs
    compile{
        extendsFrom pluginLibs
    }
}

repositories {
  mavenLocal()
  mavenCentral()
}

dependencies {
  compile(group:'org.rundeck', name: 'rundeck-core', version: '2.6.1')
  pluginLibs group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.10.28'
}

task copyToLib(type: Copy) {
  into "$buildDir/output/lib"
  from configurations.pluginLibs
}

jar {
  //include contents of output dir
  from "$buildDir/output"
  manifest {
    attributes 'Rundeck-Plugin-Version': rundeckPluginVersion, 'Rundeck-Plugin-Archive': 'true', 'Rundeck-Plugin-Libs-Load-First':'false'
    //create space-separated list of pluginLibs
    def libList = configurations.pluginLibs.collect{'lib/'+it.name}.join(' ')
    attributes 'Rundeck-Plugin-Classnames': 'com.inokara.rundeck.plugin.AwsSnsNotificationPlugin', 'Rundeck-Plugin-Libs': "${libList}"
  }
}

jar.dependsOn(copyToLib)

task wrapper(type: Wrapper) {
    gradleVersion = '1.8'
}

それぞれの設定については勉強中(纏まったら随時書いていく。)

全体を通して感じたこと、気付いたこと

  • Java は怖くないし悪くない、Java が怖いという先入観と触らないのにアレコレ言うのが一番怖いし、悪い
  • Groovy ならちょっとした JVM 上で動くスクリプトを割と簡単に作れそう(な気がするだけ)
  • プログラミングの基礎が無いのは辛い
  • ドキュメントをちゃんと読もう(但し、Java Doc の読み方が解らない...)

以上。