Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nota-ja/db46a78f06e479710baa to your computer and use it in GitHub Desktop.
Save nota-ja/db46a78f06e479710baa to your computer and use it in GitHub Desktop.
cf blue-green-deploy プラグインの --smoke-test オプションを試してみる

cf blue-green-deploy プラグインの --smoke-test オプションを試してみる

この投稿は Open PaaS Advent Calendar 2015 の12日目の記事です。

昨日, cf blue-green-deploy プラグインを試してみる という記事を書いたのですが,

本当は --smoke-test オプションも試したかったのですが,記事も長くなり,時間も無くなってきたので,今日はここまでにしたいと思います。

という心残りがあったので,記事化する事にしました。時間が足りなかっただけで,決して「一粒で2度美味しい」を最初から狙ってたわけではありません。信じてください。

cf blue-green-deploy プラグイン については 昨日の記事 を読了済みの前提で省略して,いきなり --smoke-test オプションから始めます。

blue-green-deploy プラグインの --smoke-test オプション

README によると,

  • Optionally runs smoke tests against the newly pushed app to verify the deployment
  • If smoke tests fail, newly pushed app gets marked as failed and left around for investigation
  • If smoke tests pass, remaps routes from the currently live app to the newly deployed app

とあるので,smoke test が成功した時のみ更新後のアプリに置き換えられる (即ち失敗した時は更新前のアプリが残る) という振る舞いをさせるためのオプションです。

テスト・スクリプトの作成

今回は,アプリの出力を

  • 変更前: Ruby 2.2.2-p95
  • 変更後: TEST Ruby 2.2.2-p95

とすることを想定して,以下のテストスクリプトを用意しました。

$ cat test.sh
#!/bin/bash

app_fqdn=$1

result=$(curl $app_fqdn)
if [ "$result" = "TEST Ruby 2.2.2-p95" ]; then
    echo OK
    exit 0
fi
echo NG: $result
exit 1

テスト・スクリプトには引数としてアプリの FQDN が渡される

The only argument passed to the smoke test script is the FQDN of the newly pushed app.

ので,それを使ってアプリにアクセスし,結果の文字列を評価するだけの簡単なスクリプトです。

アプリを更新して blue-green-deploy (わざと間違えて更新)

アプリを更新します。しかし最初はわざとテストに通らないような更新を行います。

$ git diff
diff --git a/ruby_ver.rb b/ruby_ver.rb
index cab0a9d..14f458d 100644
--- a/ruby_ver.rb
+++ b/ruby_ver.rb
@@ -3,7 +3,7 @@ require "cf-autoconfig"

 class RubyVer < Sinatra::Base
   get "/" do
-    "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}\n"
+    "TES Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}\n"
   end

   get "/env" do

TEST とすべきところが TES になっています。

この状態で,前回同様アプリにアクセスし続けるスクリプトを回しながら blue-green-deploy を行ってみます。

cf CLI の出力

(更新開始時刻: 2015-12-12T19:17:02)

$ cf blue-green-deploy rubyver --smoke-test ./test.sh
Deleting app rubyver-failed in org nota / space hundred as nota...
OK
Creating app rubyver-new in org nota / space hundred as nota...
OK

Using route rubyver-new.10.244.0.34.xip.io
Binding rubyver-new.10.244.0.34.xip.io to rubyver-new...
OK

Uploading rubyver-new...
Uploading app files from: /Users/nota/repos/My/cf-example-ruby-ver
Uploading 1.7K, 6 files
Done uploading
OK

Starting app rubyver-new in org nota / space hundred as nota...
-----> Downloaded app package (4.0K)
-------> Buildpack version 1.6.2
-----> Compiling Ruby/Rack
-----> Using Ruby version: ruby-2.2.2
-----> Installing dependencies using 1.9.7
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       Fetching gem metadata from https://rubygems.org/............
       Fetching version metadata from https://rubygems.org/..
       Installing cf-runtime 0.2.0
       Using bundler 1.9.7
       Installing tilt 1.4.1
       Installing rack 1.5.2
       Installing cf-autoconfig 0.2.1
       Installing rack-protection 1.5.0
       Installing sinatra 1.4.2
       Bundle complete! 2 Gemfile dependencies, 7 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into ./vendor/bundle.
       Bundle completed (7.11s)
       Cleaning up the bundler cache.
###### WARNING:
       You have not declared a Ruby version in your Gemfile.
       To set your Ruby version add this line to your Gemfile:
       ruby '2.2.2'
       # See https://devcenter.heroku.com/articles/ruby-versions for more information.
###### WARNING:
       No Procfile detected, using the default web server (webrick)
       https://devcenter.heroku.com/articles/ruby-default-web-server

-----> Uploading droplet (17M)

1 of 1 instances running

App started


OK

App rubyver-new was started using this command `bundle exec rackup config.ru -p $PORT`

Showing health and status for app rubyver-new in org nota / space hundred as nota...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: rubyver-new.10.244.0.34.xip.io
package uploaded: Sat Dec 12 10:17:08 UTC 2015
stack: cflinuxfs2

     state     since                    cpu    memory          disk      details
#0   running   2015-12-12 07:17:32 PM   0.0%   40.5M of 256M   0 of 1G
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    19  100    19    0     0     65      0 --:--:-- --:--:-- --:--:--    65
NG: TES Ruby 2.2.2-p95

Removing route rubyver-new.10.244.0.34.xip.io from app rubyver-new in org nota / space hundred as nota...
OK
Renaming app rubyver-new to rubyver-failed in org nota / space hundred as nota...
OK
Smoke tests failed

(更新終了時刻: 2015-12-12T19:17:35)

テスト・スクリプトが異常終了し,

NG: TES Ruby 2.2.2-p95

smoke test が失敗と判定されていることがわかります。

Smoke tests failed

loop.sh の出力

$ ./loop.sh
2015-12-12T19:16:59JST
Ruby 2.2.2-p95
2015-12-12T19:17:00JST
Ruby 2.2.2-p95
2015-12-12T19:17:01JST
Ruby 2.2.2-p95
2015-12-12T19:17:02JST
Ruby 2.2.2-p95
2015-12-12T19:17:04JST
Ruby 2.2.2-p95
2015-12-12T19:17:05JST
Ruby 2.2.2-p95
2015-12-12T19:17:06JST
Ruby 2.2.2-p95
2015-12-12T19:17:07JST
Ruby 2.2.2-p95
2015-12-12T19:17:08JST
Ruby 2.2.2-p95
2015-12-12T19:17:09JST
Ruby 2.2.2-p95
2015-12-12T19:17:10JST
Ruby 2.2.2-p95
2015-12-12T19:17:11JST
Ruby 2.2.2-p95
2015-12-12T19:17:12JST
Ruby 2.2.2-p95
2015-12-12T19:17:13JST
Ruby 2.2.2-p95
2015-12-12T19:17:14JST
Ruby 2.2.2-p95
2015-12-12T19:17:15JST
Ruby 2.2.2-p95
2015-12-12T19:17:16JST
Ruby 2.2.2-p95
2015-12-12T19:17:17JST
Ruby 2.2.2-p95
2015-12-12T19:17:18JST
Ruby 2.2.2-p95
2015-12-12T19:17:19JST
Ruby 2.2.2-p95
2015-12-12T19:17:20JST
Ruby 2.2.2-p95
2015-12-12T19:17:21JST
Ruby 2.2.2-p95
2015-12-12T19:17:22JST
Ruby 2.2.2-p95
2015-12-12T19:17:23JST
Ruby 2.2.2-p95
2015-12-12T19:17:25JST
Ruby 2.2.2-p95
2015-12-12T19:17:26JST
Ruby 2.2.2-p95
2015-12-12T19:17:27JST
Ruby 2.2.2-p95
2015-12-12T19:17:28JST
Ruby 2.2.2-p95
2015-12-12T19:17:29JST
Ruby 2.2.2-p95
2015-12-12T19:17:30JST
Ruby 2.2.2-p95
2015-12-12T19:17:31JST
Ruby 2.2.2-p95
2015-12-12T19:17:32JST
Ruby 2.2.2-p95
2015-12-12T19:17:33JST
Ruby 2.2.2-p95
2015-12-12T19:17:34JST
Ruby 2.2.2-p95
2015-12-12T19:17:35JST
Ruby 2.2.2-p95
2015-12-12T19:17:36JST
Ruby 2.2.2-p95
^C

loop.sh の出力はずっと旧バージョンのままで,エラーも出ていません。

アプリを更新して blue-green-deploy (正しく更新)

今度はアプリを正しく更新します。

$ git diff
diff --git a/ruby_ver.rb b/ruby_ver.rb
index cab0a9d..27f490c 100644
--- a/ruby_ver.rb
+++ b/ruby_ver.rb
@@ -3,7 +3,7 @@ require "cf-autoconfig"

 class RubyVer < Sinatra::Base
   get "/" do
-    "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}\n"
+    "TEST Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}\n"
   end

   get "/env" do

この状態でアプリを再度更新します。

cf CLI の出力

(更新開始時刻: 2015-12-12T19:28:30)

$ cf blue-green-deploy rubyver --smoke-test ./test.sh
Deleting app rubyver-failed in org nota / space hundred as nota...
OK
Creating app rubyver-new in org nota / space hundred as nota...
OK

Using route rubyver-new.10.244.0.34.xip.io
Binding rubyver-new.10.244.0.34.xip.io to rubyver-new...
OK

Uploading rubyver-new...
Uploading app files from: /Users/nota/repos/My/cf-example-ruby-ver
Uploading 1.7K, 6 files
Done uploading
OK

Starting app rubyver-new in org nota / space hundred as nota...
-----> Downloaded app package (4.0K)
-------> Buildpack version 1.6.2
-----> Compiling Ruby/Rack
-----> Using Ruby version: ruby-2.2.2
-----> Installing dependencies using 1.9.7
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       Fetching gem metadata from https://rubygems.org/............
       Fetching version metadata from https://rubygems.org/..
       Installing tilt 1.4.1
       Using bundler 1.9.7
       Installing rack 1.5.2
       Installing cf-runtime 0.2.0
       Installing rack-protection 1.5.0
       Installing cf-autoconfig 0.2.1
       Installing sinatra 1.4.2
       Bundle complete! 2 Gemfile dependencies, 7 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into ./vendor/bundle.
       Bundle completed (8.96s)
       Cleaning up the bundler cache.
###### WARNING:
       You have not declared a Ruby version in your Gemfile.
       To set your Ruby version add this line to your Gemfile:
       ruby '2.2.2'
       # See https://devcenter.heroku.com/articles/ruby-versions for more information.
###### WARNING:
       No Procfile detected, using the default web server (webrick)
       https://devcenter.heroku.com/articles/ruby-default-web-server

-----> Uploading droplet (17M)

1 of 1 instances running

App started


OK

App rubyver-new was started using this command `bundle exec rackup config.ru -p $PORT`

Showing health and status for app rubyver-new in org nota / space hundred as nota...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: rubyver-new.10.244.0.34.xip.io
package uploaded: Sat Dec 12 10:28:34 UTC 2015
stack: cflinuxfs2

     state     since                    cpu    memory          disk      details
#0   running   2015-12-12 07:28:59 PM   0.0%   40.3M of 256M   0 of 1G
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    20  100    20    0     0     67      0 --:--:-- --:--:-- --:--:--    67
OK

Removing route rubyver-new.10.244.0.34.xip.io from app rubyver-new in org nota / space hundred as nota...
OK
Creating route rubyver.10.244.0.34.xip.io for org nota / space hundred as nota...
OK
Route rubyver.10.244.0.34.xip.io already exists
Adding route rubyver.10.244.0.34.xip.io to app rubyver-new in org nota / space hundred as nota...
OK
Renaming app rubyver to rubyver-old in org nota / space hundred as nota...
OK
Renaming app rubyver-new to rubyver in org nota / space hundred as nota...
OK
Removing route rubyver.10.244.0.34.xip.io from app rubyver-old in org nota / space hundred as nota...
OK

(更新終了時刻: 2015-12-12T19:29:02)

smoke test で失敗したログは見受けられません。

loop.sh の出力

$ ./loop.sh
2015-12-12T19:28:26JST
Ruby 2.2.2-p95
2015-12-12T19:28:28JST
Ruby 2.2.2-p95
2015-12-12T19:28:29JST
Ruby 2.2.2-p95
2015-12-12T19:28:30JST
Ruby 2.2.2-p95
2015-12-12T19:28:31JST
Ruby 2.2.2-p95
2015-12-12T19:28:32JST
Ruby 2.2.2-p95
2015-12-12T19:28:33JST
Ruby 2.2.2-p95
2015-12-12T19:28:34JST
Ruby 2.2.2-p95
2015-12-12T19:28:35JST
Ruby 2.2.2-p95
2015-12-12T19:28:36JST
Ruby 2.2.2-p95
2015-12-12T19:28:37JST
Ruby 2.2.2-p95
2015-12-12T19:28:38JST
Ruby 2.2.2-p95
2015-12-12T19:28:39JST
Ruby 2.2.2-p95
2015-12-12T19:28:41JST
Ruby 2.2.2-p95
2015-12-12T19:28:42JST
Ruby 2.2.2-p95
2015-12-12T19:28:43JST
Ruby 2.2.2-p95
2015-12-12T19:28:44JST
Ruby 2.2.2-p95
2015-12-12T19:28:45JST
Ruby 2.2.2-p95
2015-12-12T19:28:46JST
Ruby 2.2.2-p95
2015-12-12T19:28:47JST
Ruby 2.2.2-p95
2015-12-12T19:28:48JST
Ruby 2.2.2-p95
2015-12-12T19:28:49JST
Ruby 2.2.2-p95
2015-12-12T19:28:50JST
Ruby 2.2.2-p95
2015-12-12T19:28:51JST
Ruby 2.2.2-p95
2015-12-12T19:28:52JST
Ruby 2.2.2-p95
2015-12-12T19:28:53JST
Ruby 2.2.2-p95
2015-12-12T19:28:54JST
Ruby 2.2.2-p95
2015-12-12T19:28:55JST
Ruby 2.2.2-p95
2015-12-12T19:28:56JST
Ruby 2.2.2-p95
2015-12-12T19:28:57JST
Ruby 2.2.2-p95
2015-12-12T19:28:58JST
Ruby 2.2.2-p95
2015-12-12T19:29:00JST
Ruby 2.2.2-p95
2015-12-12T19:29:01JST
Ruby 2.2.2-p95
2015-12-12T19:29:02JST
Ruby 2.2.2-p95
2015-12-12T19:29:03JST
TEST Ruby 2.2.2-p95
2015-12-12T19:29:04JST
TEST Ruby 2.2.2-p95
2015-12-12T19:29:05JST
TEST Ruby 2.2.2-p95
^C

更新が成功して,2015-12-12T19:29:03 以降は更新後の出力が取れていることがわかります。

まとめ

ということで,無停止で,しかもちゃんとテストがOKの時だけアプリの更新が行える blue-green-deploy プラグイン。Cloud Foundry の利用者なら,使わない手はないと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment