sohatach's blog

http://github.com/soha

Scalaのcaseクラスとか、Javaから入って躓いたところまとめ

caseクラスやらシングルトンオブジェクトやらコンパニオンオブジェクトやら
Scalaの当初Javaの知識だけでは個人的に理解が難しかった点を自分用にまとめる。

参考
Scalaのクラスとオブジェクト、パターンマッチ
http://www.atmarkit.co.jp/ait/articles/1204/20/news139.html

・シングルトンオブジェクトとは

Scalaでは、staticがないため、Javaでいうstaticメソッドを作るには、
シングルトンオブジェクトというものを使います。
インスタンスのないstaticなメソッドが作れない代わりに
シングルトンのオブジェクトを作成するようです。
作り方は、クラス定義時の「class」の代わりに「object」というキーワードを使用します。

atmarkitのサイトによれば、以下のような例が出ています。

object SampleUtil{
    def hello() = println("hello")
}
 
scala> SampleUtil.hello()
hello

hello()メソッドをインスタンスをnewすることなしに使えているのでポイントですね。

・コンパニオンオブジェクトとは

atmarkitのサイトによれば、
> 「コンパニオンオブジェクト」とは、あるクラスに対して同じスコープ、同じ名前で定義されたシングルトンオブジェクトです。
> classとobjectが同じファイル、同じパッケージの中に、同じ名前で宣言されていれば、それはコンパニオンオブジェクトとなります。

特徴
> コンパニオンクラスのprivateなフィールドやメソッドに対してアクセスができる

class SampleCompanion private (num:Int)
  
object SampleCompanion {
    def apply(num:Int) = {
      new SampleCompanion(num)
    }
}
  
object Main {
    def main(args: Array[String]) = {
        val ins = SampleCompanion(10)
        println(ins)
    }
}

サンプルコードでは、
>val ins = SampleCompanion(10)
にてシングルトンオブジェクトのapplyメソッドを呼び出し、
SampleCompanionクラスのインスタンスを生成しています。(実体は、applyメソッドのnew SampleCompanion(num)の実行)

・applyメソッドとは

>「apply」という名前は特別な意味を持っており、オブジェクトのメソッドを関数を扱うように呼び出せます。
とのこと。
具体的には、上記コンパニオンオブジェクトのサンプルコードの
>SampleCompanion(10)
にて、実際には、
>SampleCompanion.apply(10)
を呼ぶのと同じ意味になるとのことです。結果的に、「SampleCompanion(10)」だけで新しいインスタンスが生成される。

これがScalaでの一般的なインスタンスの生成方法らしいですが、
正直私にはこのやり方の何が嬉しいのかよくわかっていません。
かつてJavaインスタンスを直接newするのではなく、DIコンテナからインスタンスを取得するのが
一般的になったことと似ていたりするのだろうか?

・unapplyメソッドとは

> 「unapply」はインスタンス構成要素を抽出するためのメソッドで、「抽出子」とも呼びます。
> このメソッド定義することで、オブジェクトをcase部分に指定できます。
とのこと。

def matchTest(value:Any) = {
  value match {
    case Apple => println("Apple")
    case Orange("DEKOPON") => println("Orange.name=DEKOPON")
    case _ => println("other")
  }
}

> オブジェクトに「unapply」(もしくは「unapplySeq」)というメソッドを定義した状態で、
> caseにそのオブジェクトを書くと、unapply(もしくは「unapplySeq」)が呼ばれます。
Apple,Orangeそれぞれのunapplyメソッドが呼ばれます。

・caseクラスとは

上記atmarkitのサイトによれば、
> あらかじめ想定されるデフォルト処理を定義した状態でクラスを作成するための仕組み

特徴
> applyが定義されるので、newを使用せずにインスタンス化可能
> caseクラスのコンストラクタ引数は、すべてvalとして扱われる
> toStringやequals、copyなどのメソッドが提供される
> unapplyが定義されるので、パターンマッチで使用することが可能
unapplyメソッドも自動で定義されるとのことです。

Skinny Frameworkで作ったアプリをEclipseでインポートする。

やること

Skinny Frameworkを使用して作成したサンプルアプリをEclipseへインポート。

環境

Windows8.1(64bit),Java7(64bit)
環境変数JAVA_HOMEの設定やjavaコマンドへのPATHは通しておいた方がいいと思います。)

Skinny Frameworkとは?

Scalaフルスタックな Web アプリケーション開発フレームワークです。
2014/03/28 に1.0.0がリリースされたばかりとのこと。
キャッチコピーに「Scala on Rails」を掲げているとのことであり、
タイプセーフなRailsが実現できるのであれば、最も理想的なフレームワークなのではないかと個人的には思っています。
詳しくはこちらなど。
https://gist.github.com/seratch/7382298


やったこと

1. プロジェクトの作成

https://github.com/skinny-framework/skinny-framework/releases
より、skinny-blank-app.zipというファイルをダウンロードします。
今回最新バージョンは、1.0.9でした。こちらがプロジェクトの雛形となります。
zipファイルを任意のフォルダに展開します。
ここでは、C:\Scala\skinny\skinny-blank-appに展開しました。

2. サンプルの起動確認

プロジェクトを作成したフォルダをコマンドプロンプトで開きます。
(ここでは、C:\Scala\skinny\skinny-blank-app)
以下のコマンドを実行するとアプリが起動します。

skinny run

依存ライブラリのダウンロードなどを自動で行うようであり、
初回起動時は少し時間が掛かります。

>1. Waiting for source changes... (press enter to interrupt)
という表示が出ればアプリが起動しているようです。
デフォルトでローカルの8080ポートで起動しますので、
ブラウザで以下のURLにアクセスします。

http://localhost:8080

以下のような画面が出れば成功です。

f:id:sohatach:20140501004808p:plain


3. scaffoldによるCRUD機能生成
ここまでだと特に機能もなく、DBを使用することもないため、
scaffoldによるCRUD機能を生成します。

skinny g scaffold Books Book title:String author:String amount:Option[Int]

*** Skinny Generator Task ***

"src\main\scala\controller\ApplicationController.scala" skipped.
"src\main\scala\controller\BooksController.scala" created.
"src\main\scala\controller\Controllers.scala" modified.
"src\test\scala\controller\BooksControllerSpec.scala" created.
"src\test\scala\integrationtest\BooksController_IntegrationTestSpec.scala" created.
"src\test\resources\factories.conf" modified.
"src\main\scala\model\Book.scala" created.
"src\test\scala\model\BookSpec.scala" created.
"src\main\webapp\WEB-INF\views\books\_form.html.ssp" created.
"src\main\webapp\WEB-INF\views\books\new.html.ssp" created.
"src\main\webapp\WEB-INF\views\books\edit.html.ssp" created.
"src\main\webapp\WEB-INF\views\books\index.html.ssp" created.
"src\main\webapp\WEB-INF\views\books\show.html.ssp" created.
"src\main\resources\messages.conf" modified.
"src\main\resources\db\migration\V20140429222552__Create_books_table.sql" created.

[success] Total time: 1 s, completed 2014/04/29 22:25:52

最終的に上記のようなファイルが生成されます。

続いてテーブル作成のため、以下のコマンドを実行します。

skinny db:migrate

db\development.mv.dbというデータベースの実体ファイルが生成されました。

ここまでのscaffoldの流れは、Railsと同じですね。

改めてskinny runを実行し動作確認します。

http://localhost:8080/books
にアクセスすると以下のような画面が表示されると思います。
ここでは省略しますが、Newボタンを押下することで登録画面が表示され
Bookの登録・更新・削除ひととおりの機能が実装されています。

f:id:sohatach:20140501004819p:plain

4. Eclipseへのインポート

本題となります。
Skinny Frameworkのプロジェクトは、sbtでプロジェクトを管理しているようですので、
sbteclipseが使えます。
https://github.com/typesafehub/sbteclipse

まずは、project/plugins.sbtに以下の行を追加します。(末尾に追記すればよいです)

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")

その後以下のコマンドを実行し、Eclipseのプロジェクトファイルを生成します。

sbt

eclipse

これにてEclipseでインポートできるようになります。

2014/05/02 追記

コメントいただいています通り、

skinny eclipse

とすることで、自分でprojet/plugins.sbtを書き換えなくても
Eclipseのプロジェクトファイルが生成されました。
今後はこちらを使用するようにします。

@seratchさん、ありがとうございました。

Windows7 64bitへのRmagickのインストール

Rmagickとは、
RedmineやらなにやらRailsを動かすために必要な画像処理系ライブラリ。

今回Rubyについては、RailsInstallerを使って1.9.3(32bit)のものを入れました。
http://railsinstaller.org/en

参考サイト
http://www.ownway.info/Ruby/index.php?rmagick%2Fhowtoinstall%2Fwindows
http://blog.livedoor.jp/zwassyoiz/archives/29226159.html
http://chiku2gonzalez.hatenablog.com/entry/2013/05/28/005548


Ruby DEVELOPMENT KITは、RailsInstallerから入れた場合、
一緒に以下にインストールされています。
C:\RailsInstaller\DevKit

古いImageMagcikのWindows用バイナリは、以下からダウンロードできます。
http://ftp.sunet.se/pub/multimedia/graphics/ImageMagick/binaries/


Windows環境で実績があるらしいImageMagick-6.8.7-8-Q16-x86-dll.exeをダウンロードし、
C:\Program Files (x86)\ImageMagick-6.8.7-Q16
にインストールした。
インストールの際に、
「Install development headers and libraries for C and C++
にチェックを入れた。

cd C:\RailsInstaller\DevKit

ruby dk.rb init

devkit下に生成されたconfig.ymlの1番下の行にでもに以下を追加。(冒頭のハイフンも必要なので注意)

- C:\RailsInstaller\Ruby1.9.3

ruby dk.rb install

コンピュータ->プロパティ->システムの詳細設定->環境変数からPathの冒頭に以下を追加する。
"C:\Program Files (x86)\ImageMagick-6.8.7-Q16"
念のためOSを再起動しておく。

gem install rmagick -- '--platform=ruby --with-opt-dir="c:\Program Files (x86)\ImageMagick-6.8.7-Q16"'

私の場合、環境変数CPATH(またはC_INCLUDE_PATH、CPP_INCLUDE_PATH)、LIBRARY_PATHの指定は特に必要なかったようです。

>gem install rmagick -- '--platform=ruby --with-opt-dir="c:\ProgramFiles (x86)\ImageMagick-6.8.7-Q16"'
Fetching: rmagick-2.13.2.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed rmagick-2.13.2
1 gem installed

WEB+DB PRESS No.75を参考にChefを動かしてみた。

Windows8へのChef+Vagrant環境構築

インストールする必要のあるもの

Ruby(1.9以上の最新のもの)
VirtualBox
・Vagrant
・Box(VirtualBox上で動かすOSイメージ、既にChefの入ったものを使う)
・knife-solo(ローカルPCに入れ、リモートでChefを操作する)
・(Cygwin

1-1. Rubyのインストール

Rubyインストーラをダウンロードします。ここでは無難に1.9(32bit)を入れます。
rubyinstaller-1.9.3-p448.exeをダウンロードしました。
http://rubyinstaller.org/downloads/
Rubyの実行ファイルへ環境変数PATHを設定する」
にチェックを入れ、環境変数PATHに入れます。

加えてRubyの追加パッケージを入れる際にコンパイル環境が必要になることがあることに備え
DEVELOPMENT KITもインストールします。
同じく
http://rubyinstaller.org/downloads/
より、DevKit-tdm-32-4.5.2-20111229-1559-sfx.exeをダウンロードします。
ダブルクリックし、展開するとインストール先を求められるので、
ここでは、C:\DevKitにインストールします。

インストールには、C:\DevKitにて以下を実行します。

ruby dk.rb init
ruby dk.rb install

参考
DevKitのインストール
http://www.rubylife.jp/railsinstall/rails/index4.html

Ruby1.9には、DevKit-tdm-32-4.5.2-20111229-1559-sfx.exeが対応するそうです。
https://github.com/rubygems/rubygems/issues/594


あとでbundleを使うためこれも入れます。

gem install bundler

bundlerとは、Rubyのパッケージ管理ツールで、必要なライブラリ(Gemファイル)の種類やバージョンを指定するものです。
Gemfileというファイルに記載します。(あとで出てきます)

参考
Bundlerを使ったGemパッケージの管理
http://www.rubylife.jp/rails/ini/index2.html

ツールを使いたいだけの人のための bundler 入門 (例: vagrant + veewee)
http://qiita.com/znz@github/items/5471e5826fde29fa9a80

1-2. VirtualBoxのインストール

https://www.virtualbox.org/wiki/Downloads
Windows用のインストーラをダウンロードし、インストールします。

1-3. Vagrantのインストール

http://downloads.vagrantup.com/
こちらもWindows用の最新インストーラをダウンロードし、インストールします。

1-4. Box(OSイメージ)のインストール

http://www.vagrantbox.es/
にいろいろなイメージファイルがありますが、
ここでは、Chef入りの以下のものを使用します。
https://s3.amazonaws.com/itmat-public/centos-6.3-chef-10.14.2.box

vagrant box add centos https://s3.amazonaws.com/itmat-public/centos-6.3-chef-10.14.2.box

でインストールします。


インストール関連は以上で終わり。
次からは、その後の準備作業になります。


2-1. Vagrant環境の構築

vagrant init centos
を実行することで先ほどインストールしたBoxのcentosの環境が構築されます。
実行時のディレクトリ下に設定ファイルが展開されるため、適当にディレクトリを掘って実行します。

2-2. Vagrant環境の起動

vagrant up
2-1で環境を構築したディレクトリで上記コマンドを実行することで、OSが起動します。

OSが起動せず途中で止まってしまう場合
BIOSで仮想化支援機能が無効になっている可能性が考えられます。
BIOSの設定を見直してください。

OSが起動するとSSHでログインできるようになっているはずです。
(デフォルトでlocalhostの2222ポート?)

vagrant ssh

でログインできることを確認する。
TeraTermなどのツールでもSSHログインできます。(接続情報は画面上のメッセージ参照)

.ssh/configの設定(この設定によりホスト名を指定して簡単にSSH出来るようになります)

cd %USERPROFILE%
mkdir .ssh
vagrant ssh-config --host centos > %USERPROFILE%\.ssh\config

set HOME=%USERPROFILE%

環境変数HOMEを設定しておかないとsshコマンドがconfigが参照できない可能性があるため設定しておく。

2-3. chef-soloを試す。(以下本章はcentos上で実行する)

試しにnginxをインストールしてみます。まずは

sudo rpm -qa | grep nginx

でインストールされていないことを確認します。

centossshログインし、

sudo knife cookbook create nginx

でレシピファイルを生成します。

sudo vi /var/chef/cookbooks/nginx/recipes/default.rb

で中身を以下のように書き換えます。

package "nginx" do
action :install
end

sudo chef-solo -o nginx

これでnginxがインストールされます。

sudo rpm -qa | grep nginx

でインストールされたことがわかります。


3-1. knife-soloのインストール(ここからWindows上の作業に戻る)

vagrant upを実行したディレクトリに以下のGemfileを追加する。

source 'https://rubygems.org'
gem 'knife-solo', '~> 0.3.0.pre5'

bundle install

でインストール。

bundle exec knife configure

で初期化する。

以下のようなエラーが出ました。

C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api.rb:20:in `require': cannot load such file -- ffi (LoadError)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api.rb:20:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api/security.rb:19:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/api/security.rb:19:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/security.rb:19:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/win32/security.rb:19:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control/windows.rb:20:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control/windows.rb:20:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:30:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:30:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:27:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/file_access_control.rb:22:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/mixin/enforce_ownership_and_permissions.rb:19:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/mixin/enforce_ownership_and_permissions.rb:19:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/provider.rb:23:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/provider.rb:23:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/platform.rb:25:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/platform.rb:25:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife/core/ui.rb:22:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife/core/ui.rb:22:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife.rb:26:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/knife.rb:26:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/application/knife.rb:18:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/lib/chef/application/knife.rb:18:in `'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/bin/knife:23:in `require'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/chef-11.4.4/bin/knife:23:in `'
from C:/Ruby193/bin/knife:23:in `load'
from C:/Ruby193/bin/knife:23:in `

'

ffiパッケージがロードできないとのエラーです。
その場合は、Gemfileに以下を追加します。

gem 'ffi'

そして再度

bundle install
bundle exec knife configure

以下のようにいろいろ聞かれますが、すべてデフォルトで問題なしです。

c:\Users\you\centos>bundle exec knife configure
WARNING: No knife configuration file found
Where should I put the config file? [C:/Users/you/.chef/knife.rb]
Please enter the chef server URL: http://localhost:4000
Please enter an existing username or clientname for the API: [you]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem]
Please enter the path to a chef repository (or leave blank):

****

You must place your client key in:
C:/Users/you/.chef/you.pem
Before running commands with Knife!

****

You must place your validation key in:
c:/etc/chef/validation.pem
Before generating instance data with Knife!

****

Configuration file written to C:/Users/you/.chef/knife.rb

knife-soloでChef用レポジトリを作る

bundle exec knife solo init chef-repo

treeコマンドで確認すると以下のようなディレクトリが作成されてます。

c:\Users\you\centos>tree chef-repo
フォルダー パスの一覧
ボリューム シリアル番号は 00000092 A027:637A です
C:\USERS\YOU\CENTOS\CHEF-REPO
├─.chef
├─cookbooks
├─data_bags
├─nodes
├─roles
└─site-cookbooks

knife solo prepareでノードにChef Soloを入れる。

C:\Users\you\centos>bundle exec knife solo prepare centos
Bootstrapping Chef...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6510 100 6510 0 0 973 0 0:00:06 0:00:06 --:--:-- 20666
Downloading Chef 11.4.4 for el...
Installing Chef 11.4.4
warning: /tmp/tmp.8yNJ4RIq/chef-11.4.4.x86_64.rpm: Header V4 DSA/SHA1 Signature,
key ID 83ef826a: NOKEY
Preparing... ########################################### [100%]
1:chef ########################################### [100%]
Thank you for installing Chef!
Generating node config 'nodes/centos.json'...

もともと既にChefが入っていましたが、バージョンアップしているのがわかります。



以上で準備作業が終わりとなります。
次はクックブックの作成。

3-2. site-cookbooksにクックブックを作る。

C:\Users\you\centos\chef-repo>bundle exec knife cookbook create nginx -o site-cookbooks

Creating cookbook nginx

Creating README for cookbook: nginx

Creating CHANGELOG for cookbook: nginx

Creating metadata for cookbook: nginx

レシピの編集。

C:\Users\you\centos\chef-repo>notepad site-cookbooks\nginx\recipes\default.rb

デフォルトは空なので、以下を追記する。

package "nginx" do
action :install
end


centosへのnginxレシピの適用。

bundle exec knife solo cook centos -o nginx

本来は上記で適用されるはずですが、Windows環境のためかrsyncまわりでエラーが出ました。

C:\Users\you\centos\chef-repo>bundle exec knife solo cook centos -o nginx
Running Chef on centos...
Checking Chef version...
Generating node config 'nodes/centos.json'...
Uploading the kitchen...
ERROR: RuntimeError: Failed to launch command rsync -rl --chmod=ugo=rwX --rsh="ssh vagrant@centos" --delete --exclude 'revision-deploys' --exclude 'tmp' --exclude '.git' --exclude '.hg' --exclude '.svn' --exclude '.bzr' /cygdrive/C/Users/you/centos/chef-repo/cookbooks/ :~/chef-solo/cookbooks-1

https://www.itefix.no/i2/content/cwrsync-free-edition
からWindows用Rsyncをダウンロードします。(cwRsync_4.0.5_Installer.zipを落としました)
C:\Program Files (x86)\cwRsync\binをPATHに通します。
sshコマンドが被るのでGit for Windowsなどが入っている場合は、それより前にPATHを通してください。(Gitのsshコマンドだと動かないようです)

>Bad owner or permissions on /cygdrive/c/Users/you/.ssh/config
といったエラーが出る場合、configファイルのパーミッションを600に設定する必要があります。
cwRsyncの中にchmodコマンドは入っていますが、Windows8環境では正常に動かない(設定しても反映されない)ことがあるようです。
その際は、Cygwinを入れて以下のコマンドを実行します。
chgrp -R Users ~/.ssh
ファイルの所有グループを設定することでchmodが効くようになります。
(chgrpコマンドはcwRsyncに入ってないのでわざわざCygwinを入れる必要があります)
http://superuser.com/questions/397288/using-cygwin-in-windows-8-chmod-600-does-not-work-as-expected

C:\Users\you\centos\chef-repo>bundle exec knife solo cook centos -o nginx
Running Chef on centos...
Checking Chef version...
Uploading the kitchen...
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Generating solo config...
cygwin warning:
MS-DOS style path detected: C:/Users/you/.vagrant.d/insecure_private_key
Preferred POSIX equivalent is: /cygdrive/c/Users/you/.vagrant.d/insecure_private_key
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Running Chef...
Starting Chef Client, version 11.4.4[0m
[2013-07-21T10:33:43+02:00] WARN: Run List override has been provided.
[2013-07-21T10:33:43+02:00] WARN: Original Run List: []
[2013-07-21T10:33:43+02:00] WARN: Overridden Run List: [recipe[nginx]]
Compiling Cookbooks...[0m
Converging 1 resources[0m
Recipe: nginx::default[0m
* package[nginx] action install[0m (up to date)[0m
Chef Client finished, 0 resources updated[0m

警告がたくさん出ています。この警告を消すには、
CYGWINという環境変数に以下を設定すると良いようです。

nodosfilewarning

3-3. 新たなクックブックの追加。

クックブックの名前は任意だが、ここではbase_packegesという名前で作成。

bundle exec knife cookbook create base_packages -o site-cookbooks

C:\Users\you\centos\chef-repo>notepad site-cookbooks\base_packages\recipes\default.rb

%w{gcc make git readline readline-devel}.each do |pkg|
package pkg do
action :install
end
end

bundle exec knife solo cook centos -o base_packages

としても良いが、Node単位で適用するクックブックを定義できるため、そちらを使用する。

chef-repo\nodes\centos.jsonに以下を記載する。

{
"run_list":[
"recipe[base_packages]",
"recipe[nginx]"
]
}

3-4. クックブックの適用

bundle exec knife solo cook centos

CloudBeesでDBへの接続情報を環境変数で渡すやり方

GitHub等でソースコードを公開する際に、git管理上のファイルとしてDB接続情報(uri,ユーザ名,パスワード)を記載したままコミットしてしまうと
DBへのアクセス情報が漏れてしまい、誰でもDBにアクセスできるようになってしまいます。
このような場合、git管理上のファイルに直接接続情報を記載するのではなく、デプロイ時の環境変数を接続情報として使用できるようにします。

以下、Play2.1の例です。(他の言語やフレームワークでもやり方は同じです)

conf/application.confに以下の記載をします。

db.url=${db.url}
db.driver=org.mysql.Driver
db.user=${db.user}
db.pass=${db.pass}

この状態で以下のコマンドによりデプロイすると、${db.url}などが変数展開され、動作時に指定した値に書き変わります。

bees app:deploy -a sampleapp -t play2 -P db.url="jdbc:mysql://dbserver/dbname" -P db.user="dbuser" -P db.pass="dbpass" dist/sampleapp-1.0.zip

ここで

sampleappは、アプリ名
dbserverは、DBサーバ名またはIPアドレス
dbnameは、データベース名
dbuserは、データベース接続ユーザ名
dbpassは、データベース接続パスワード

となっています。
(-t play2 は、play2アプリであるとの指定)

上記は、CloudBeesでの例ですが、Herokuなどでも同じように環境変数で値を設定することができます。


参考
http://developer.cloudbees.com/bin/view/RUN/Configuration+Parameters
http://wiki.cloudbees.com/bin/view/RUN/CloudBeesWebXml

Play2サンプルプロジェクトのEclipseへのインポート

1.環境の準備

http://download.playframework.org/releases/play-2.0.4.zip
ここからplay-2.0.4.zipをダウンロードし、
C:\play-2.0.4に展開します。

play_env.batを以下の内容で作成し、ユーザーのホームディレクトリ(C:\Users\ユーザー名)に置きました。

set GIT_PATH="C:\Program Files (x86)\Git"
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11

set PLAY_HOME=C:\play-2.0.4
set PATH=%JAVA_HOME%\bin;%PATH%
set PATH=%PLAY_HOME%;%PATH%
set PATH=%GIT_PATH%\bin;%PATH%

cd %PLAY_HOME%

コマンドプロンプトを起動し、ホームディレクトリで

play_env

と実行するとPlayの実行環境が設定されます。


2.サンプルプロジェクトのEclipse

Scala用の各サンプルプロジェクトのフォルダに移動し、

play eclipsify

を実行することで、Eclipseのプロジェクトとしてインポートできるようになります。

bitbucketはじめました

今からMercurial覚えるのはちょっとと思ってましたが、実際にはGitにも対応しており、
無料でプライベートレポジトリが持てるなんて、GitHubより優れている。
しかもレポジトリ数無制限だとか。
http://www.atlassian.com/ja/software/bitbucket/overview

今まで書籍のサンプルコードなど勝手にGitHubにあげちゃマズイよなと思っていましたが、
これで心置きなくサンプルコードのGit管理(リモートバックアップ)ができます。
しかも無料で使えるチーム管理の概念があるらしく、まだ公開したくないちょっとした検証プロジェクトなどを複数名で管理する場合に有効に使えそう。
bitbucket万歳w