2012年10月18日木曜日

[Jenkins] スレーブマシンの再起動をするジョブ

Groovy postbuild plugin はとりあえずインストールしとけ
で、Groovy postbuild plugin を紹介しました。

今回は、Groovy postbuild plugin を使ってスレーブマシンの再起動する例を見つけたので紹介します。

ネタ元はこちら > https://bugreports.qt-project.org/browse/QTQAINFRA-558

以下に紹介されているコードを示します。
import hudson.slaves.OfflineCause.SimpleOfflineCause
import hudson.util.RemotingDiagnostics

/* FIXME: why cannot we import jenkins.util.NonLocalizable ? */
class OfflineMessage extends org.jvnet.localizer.Localizable {
  def message
  OfflineMessage() {
    super(null, null, [])
    def timestr = new Date().format("HH:mm dd/MM/yy z", TimeZone.getTimeZone("UTC"))
    this.message = "automated reboot at end of test at " + timestr
  }
  String toString() {
    this.message
  }
  String toString(java.util.Locale l) {
    toString()
  }
}

def computer = manager.build.getBuiltOn().toComputer()
def channel = computer.getChannel()
computer.setTemporarilyOffline(true, SimpleOfflineCause.create(new OfflineMessage()))
RemotingDiagnostics.executeGroovy( """

    if (Functions.isWindows()) {
      'shutdown /r /t 10 /c "Restarting after Jenkins test completed"'.execute()
    } else {
      "sudo reboot".execute()
    }

""", channel )

何が嬉しいか
クリーンな環境でテストをしたいと思った場合、
仮想マシンや Windows SteadyState(古い?) を使う方法があります。

そういった場合、クリーンな状態に戻すときに再起動する必要があるので、
このスクリプトを使って自動化できると助かるのではないでしょうか。
(Windows の場合 shutdown コマンドに /f オプションも付けたほうがいいかも)

本当に、Groovy postbuild plugin って便利ですね!

0 件のコメント:

コメントを投稿