Upload
kohsuke-kawaguchi
View
7.282
Download
3
Embed Size (px)
DESCRIPTION
JGGUGで2011/2/24にした発表です
Citation preview
©2010 CloudBees, Inc. All Rights Reserved
©2010 CloudBees, Inc. All Rights Reserved
Jenkins と Groovy川口耕介CloudBees, Inc.
©2010 CloudBees, Inc. All Rights Reserved
2©2010 CloudBees, Inc. All Rights Reserved
©2010 CloudBees, Inc. All Rights Reserved
3©2010 CloudBees, Inc. All Rights Reserved
スクリプトコンソール• 任意の Groovy スクリプトを eval– 実行中のアプリケーションの内部を操作– VMWare ESXi の ssh アクセスみたいなもの
• トラブル対応– デバッグフラグを on/off したり
• バッチ処理– ジョブの設定を一括で変えたり
• 情報収集– 実行中のスレッドの一覧
©2010 CloudBees, Inc. All Rights Reserved
4©2010 CloudBees, Inc. All Rights Reserved
スクリプトコンソール• 以前は運用中の Jenkins にデバッガを接
続してやっていた– デバッガでしかできない事もある– でも不便な事もある
• スクリプトを渡してユーザーに実行してもらう
• Jenkins ユーザーは開発者なので自分でクリエイティブに活用する人も
©2010 CloudBees, Inc. All Rights Reserved
5©2010 CloudBees, Inc. All Rights Reserved
実装• GroovyShell を使えば簡単
GroovyShell shell = new GroovyShell(cl);
StringWriter out = new StringWriter();PrintWriter pw = new PrintWriter(out);shell.setVariable("out", pw);
Object output = shell.evaluate(script);pw.println("Result: "+output);
©2010 CloudBees, Inc. All Rights Reserved
6©2010 CloudBees, Inc. All Rights Reserved
リクエスト・レスポンス方式の限界• ウェブページという仕組み上の制約
• 長く実行するプログラムを書けない• スクリプトをコピペしないといけない• スクリプトに他の入力を渡すのが大変• スクリプトを scripting できない
©2010 CloudBees, Inc. All Rights Reserved
7©2010 CloudBees, Inc. All Rights Reserved
やっぱり CLI !• コマンドラインから実行すれば問題解決
Client JVM Jenkins JVMJenkins remoting
GroovyShell
©2010 CloudBees, Inc. All Rights Reserved
デモ
©2010 CloudBees, Inc. All Rights Reserved
9©2010 CloudBees, Inc. All Rights Reserved
2つの JVM を跨ぐ• Jenkins remoting layer を利用• よく考えると色々面白い問題が– System.out はどこへ?– new File(“data.txt”) はどこへ?
• 両方アクセスできるようにする工夫が色々– new FilePath(channel, “data.txt”)
©2010 CloudBees, Inc. All Rights Reserved
©2010 CloudBees, Inc. All Rights Reserved
10©2010 CloudBees, Inc. All Rights Reserved
2 つの JVM を跨ぐ• データアクセスを transparent に
Client JVM Jenkins JVMJenkins remoting
GroovyShellFilePath
©2010 CloudBees, Inc. All Rights Reserved
11©2010 CloudBees, Inc. All Rights Reserved
更に次のステップへ• 計算を他の VM へ送りたい
©2010 CloudBees, Inc. All Rights Reserved
12©2010 CloudBees, Inc. All Rights Reserved
更に次のステップへ• 計算を他の VM へ送りたい
©2010 CloudBees, Inc. All Rights Reserved
13©2010 CloudBees, Inc. All Rights Reserved
実験プロジェクト Droovy
• 分散計算プラットフォーム– Hadoop と勝負!ではなく、ちょっとした
やつ– 複数の計算機を気軽に跨ぎたい
• Jenkins の分散ビルド環境を利用
©2010 CloudBees, Inc. All Rights Reserved
14©2010 CloudBees, Inc. All Rights Reserved
ここで Jenkins 分散ビルドについて一言• マスター– HTTP リクエストを処理– 重要なデータを保管
• スレーブ– 170KB jar– 信頼性は低くてよい– 200 位は普通に繋げる
• リンク– 双方向バイトストリームで接続
Master
slave1
slave2
slave3
slave4
slave5
slave6
slave7
slave8
©2010 CloudBees, Inc. All Rights Reserved
15©2010 CloudBees, Inc. All Rights Reserved
Back to Droovy
• 複数の計算機を跨いでスクリプトを書きたい– 負荷テスト– 処理の並列化
• 暗黙にコードを共有したい• 通信路を意識しないプログラムを書きた
い
©2010 CloudBees, Inc. All Rights Reserved
16©2010 CloudBees, Inc. All Rights Reserved
クロージャをリモート実行
slave = connect();slave { println “This code executes on a slave”;}println “This code executes on master”;
©2010 CloudBees, Inc. All Rights Reserved
17©2010 CloudBees, Inc. All Rights Reserved
クロージャには束縛された変数を含む
slave = connect();
name = “Duke”;
slave { println “Hello from ${name}”;}
©2010 CloudBees, Inc. All Rights Reserved
18©2010 CloudBees, Inc. All Rights Reserved
コードは暗黙に共有される
def sayHello(name) { println “Hello from ${name}”; }
slave = connect();
name = “Duke”;
slave { sayHello(name);}
©2010 CloudBees, Inc. All Rights Reserved
デモ
©2010 CloudBees, Inc. All Rights Reserved
20©2010 CloudBees, Inc. All Rights Reserved
Droovy まとめ• コード共有とかの問題は Jenkins
remoting で解決済み• なぜ Groovy ?– クロージャ概念がリモート実行とうまくマッ
チ
• 誰か使ってみませんか?
©2010 CloudBees, Inc. All Rights Reserved
21©2010 CloudBees, Inc. All Rights Reserved
Jenkins Groovy Shell
• Groovysh もアプリケーションに埋め込める– だからやってみた
• モデルは前と同じ– 実行は Jenkins の中– I/O はクライアント JVM で
• 対話性は便利– 障害対応とか– スクリプトを書くための練習環境として
©2010 CloudBees, Inc. All Rights Reserved
22©2010 CloudBees, Inc. All Rights Reserved
Jenkins & Groovy
• Maven を使っていると時々…– 些細なプラグインを書かされる羽目になっ
たり– 既存のプラグインの設定項目が足りなかっ
たり– Ant を使ったほうが早い時があったり
©2010 CloudBees, Inc. All Rights Reserved
23©2010 CloudBees, Inc. All Rights Reserved
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>some directory</source> </sources> </configuration> </execution> </executions></plugin>
©2010 CloudBees, Inc. All Rights Reserved
24©2010 CloudBees, Inc. All Rights Reserved
Maven なんてやってられるか!
©2010 CloudBees, Inc. All Rights Reserved
25©2010 CloudBees, Inc. All Rights Reserved
GMaven
• そこで GMaven– ビルドの一部を Groovy で書ける–Maven の内部コンポーネントにアクセスで
きる– AntBuilder も当然使える
• 利用例– コードカバレッジの取得と集計
©2010 CloudBees, Inc. All Rights Reserved
26©2010 CloudBees, Inc. All Rights Reserved
GMaven の欠点• しかし…– Groovy を呼び出すだけで 15 行位書かないといけ
ない– 呼べるコンポーネントの量が少ない– MavenProject への変更が反映されない
• 利用できる局面は限定的に• パッチを当てたバージョンを作った– http://github.com/kohsuke/gmaven
• 今後は Gradle とかも試してみたい• Polyglot Maven がメンテナンスされれば…
©2010 CloudBees, Inc. All Rights Reserved
27©2010 CloudBees, Inc. All Rights Reserved
Groovy でテストを記述• Jenkins のテストの一部は Groovy で書
かれている–型宣言の手間が省ける• それでいて auto-completion は使える
– リテラルの構築が楽– さようなら throws節– プロパティアクセスなどが楽– 実行速度 ?
©2010 CloudBees, Inc. All Rights Reserved
28©2010 CloudBees, Inc. All Rights Reserved
void testVariablesResolved() {def project = createFreeStyleProject();hudson.nodeProperties.replaceBy([
new EnvironmentVariablesNodeProperty(new Entry("KEY1", "value"),new Entry("KEY2",'$KEY1'))]);
def builder = new CaptureEnvironmentBuilder();project.buildersList.add(builder);
buildAndAssertSuccess(project);
def envVars = builder.envVars;assertEquals("value", envVars["KEY1"]);assertEquals("value", envVars["KEY2"]);
}
©2010 CloudBees, Inc. All Rights Reserved
29©2010 CloudBees, Inc. All Rights Reserved
とはいえ…• 思ったより Groovy テストが増えてない
• Maven+Groovy+IntelliJ が壊れやすい– ソースディレクトリが認識されなかったり
• 型安全でなくてもいいが、補完は必須– Closure周りで型情報が失われる
©2010 CloudBees, Inc. All Rights Reserved
30©2010 CloudBees, Inc. All Rights Reserved
Jelly → Groovy
• Jenkins では HTML のレンダリングにJelly を使っている (JSTL のようなもの )
<html> <head> <meta name="ROBOTS" content="INDEX,NOFOLLOW" />
<j:set var="mode" value="header" /> <j:forEach var="pd" items="${h.pageDecorators}"> <st:include it="${pd}" page="header.jelly" /> </j:forEach> ...
©2010 CloudBees, Inc. All Rights Reserved
31©2010 CloudBees, Inc. All Rights Reserved
Jelly → Groovy
• Builder を書いて Groovy で置き換えてみた
html { head { meta(name:"ROBOTS",content:"INDEX,NOFOLLOW")
mode = "header"; h.pageDecorators.each { pd -> st.include(it:pd, page:"header.jelly") }...
©2010 CloudBees, Inc. All Rights Reserved
32©2010 CloudBees, Inc. All Rights Reserved
Jelly の欠点を解消• ツールサポートがない– Expression Language が補完できない– デバッガでステップ実行できない– タグ・属性が補完されない
©2010 CloudBees, Inc. All Rights Reserved
33©2010 CloudBees, Inc. All Rights Reserved
欠点を解消したつもり…• EL の補完は今ひとつ– 暗黙の変数など
• タグ・属性の補完は今ひとつ• 実行速度は大丈夫?
• IntelliJ には DSL の補完を助ける機構があるらしいので試してみたい
©2010 CloudBees, Inc. All Rights Reserved
34©2010 CloudBees, Inc. All Rights Reserved
YouDebug
• 障害対応の選択肢を広げる– ログやスタックトレースでは予期できない
時が• 開発中でも…– ブレークする条件が複雑な場合– ソースがない場合– リモートで実行する場合
• デバッガプロトコルを制御する DSL を作った
©2010 CloudBees, Inc. All Rights Reserved
35©2010 CloudBees, Inc. All Rights Reserved
YouDebug
• ブレークポイントでクロージャを実行• クロージャ内からローカル変数等にアク
セス– “@1” などで序数からアクセスもできる
breakpoint("org.acme.Main",15) { println "x=${x},y=${y}"}
©2010 CloudBees, Inc. All Rights Reserved
36©2010 CloudBees, Inc. All Rights Reserved
YouDebug
• JDI がサポートする全てのブレークポイントの種類が使える
• デバッガの用にターゲット VM で任意のメソッド・フィールドにアクセスできる
©2010 CloudBees, Inc. All Rights Reserved©2010 CloudBees, Inc. All Ri
ghts Reserved
デモ
©2010 CloudBees, Inc. All Rights Reserved
38©2010 CloudBees, Inc. All Rights Reserved
まとめ• Jenkins では色々 Groovy を使ってみた• 実行時にユーザーに使わせる機能として–文句なしに便利
• 開発時の生産性を上げるために–結局はツールサポートの問題に– Java には優れたツールサポートがあるので
Groovy で置き換えてもメリットが薄い場合も