Monthly Archives: December 2010

Installing Chef Server with RVM

Chef is a systems integration framework, built to bring the benefits of configuration management to your entire infrastructure.

The tutorials of install, and basic usage are written here.

I want to use it with RVM’s Ruby on Ubuntu Server 10.10.

RVM Installation

I use RVM with system wide install, whose instruction is here.
RVM: Ruby Version Manager – Installing RVM System Wide

chef-server# apt-get install curl git-core
chef-server# bash < <( curl -L http://bit.ly/rvm-install-system-wide )

Ruby 1.9.2 Installation

After installed RVM, you need to install Ruby 1.9.2 with RVM.

chef-server# apt-get install build-essential libssl-dev zlib1g-dev libreadline-dev
chef-server# source '/usr/local/lib/rvm'
chef-server# rvm install 1.9.2 -C --with-readline-dir=/usr --with-openssl-dir=/usr --with-zlib-dir=/usr

Bootstrap Chef-Server Installation

Now, you are prepared for installing Chef.
There is a nice tutorial for bootstrap installation of Chef-Server.
Bootstrap Chef RubyGems Installation – Chef – Opscode Open Source Wiki

Chef Solo Attributes Configuration

chef-server$ vi ~/chef.json
{
  "chef": {
    "server_url": "http://localhost:4000"
  },
  "run_list": [ "recipe[chef::bootstrap_server]" ]
}

Temporary PATH configuration

Before using Rubygems, you should set PATH configuration temporary, because I want to use chef to set all configurations.

chef-server# source '/usr/local/lib/rvm'
chef-server# rvm use 1.9.2
chef-server# gem install chef

Other Necessary Packages Installation

chef-server# apt-get install wget ssl-cert
chef-server# gem install chef

Chef Solo Installation

chef-server# mkdir -p /etc/chef
chef-server# vi /etc/chef/solo.rb

The content of “/etc/chef/solo.rb” is following.

file_cache_path "/tmp/chef-solo"
cookbook_path "/tmp/chef-solo/cookbooks"

You can use `chef-solo` command to install chef-server.

chef-server# chef-solo -c /etc/chef/solo.rb -j ~/chef.json -r http://s3.amazonaws.com/chef-solo/bootstrap-latest.tar.gz

chef-client, chef-server, chef-solr, chef-solr-indexer run script PATH configuration

Having finished installing, you will notice that chef-* fail to start on looking at logs “/etc/sv/chef-*/logs/main/current”.
The reason of this failure is that Ruby and Gems PATH are not set correctly, so you should set them like this.

chef-server# vi /etc/sv/{chef-client, chef-server, chef-solr, chef-solr-indexer}/run
- PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
---
+ export PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/rvm/gems/ruby-1.9.2-p0/bin
+ export GEM_HOME=/usr/local/rvm/gems/ruby-1.9.2-p0
+ export GEM_PATH=/usr/local/rvm/gems/ruby-1.9.2-p0

These chef processes are watched by “runit”, and after you rewrite them, all processes will start correctly.
If you want to start/stop/restart manually, use `/etc/init.d/chef-* {start/stop/restart}` command.

要は、PATH の設定だけちゃんとやっておけば RVM でインストールした Ruby でも Chef はインストールできますよということです。

References

Steak で RESTful API の受け入れテストを書く

Steak は Ruby 製の受け入れテストのためのフレームワークです。
cavalle/steak – GitHub

Github にある説明を読んでいただくとわかると思いますが Steak ではテストは RSpec のように書くことができます。
同様のツールに Cucumber もありますが、Cucumber は英語や日本語で書かなければならないので、自分の中でどうもしっくりしませんでした。
そのため、自分にとっては Steak のようにかけた方がずっと書きやすいです。
ということで、これからは Steak を使って受け入れテストを書いていこうと思っています。

そんな Steak ですが、RESTful API のテストもしたいです。
どうも Steak の裏で動いている Capybara というテストフレームワークは、 visit ‘/’ のように GET することはできるのですが、 POST PUT DELETE というような RESTful API をテストする上ではそれだけでは足りません。

以下のような方法で、POST 等のメソッドを呼び出すことが可能なようです。

$RAILS_ROOT/spec/acceptance/support/helper.rb

module HelperMethods
  def setup_driver
    @driver = Capybara.current_session.driver
  end

  def post(path, params = {})
    @driver.process :post, path, params
  end

  def put(path, params = {})
    @driver.process :put, path, params
  end

  def delete(path, params = {})
    @driver.process :delete, path, params
  end
end

$RAILS_ROOT/spec/acceptance/api/articles_spec.rb

require File.dirname(__FILE__) + '/../acceptance_helper'

feature "Articles" do
  background do
    setup_driver
  end

  scenario 'update article' do
    params = { :article => {} }
    params[:article][:title] = 'foo'
    put('/api/articles/1.json', params)
  end
end

@driver.status_code とかでレスポンスのステータスコードが取得できたりします。
ほかの情報はここなどを読むとよいでしょう。
Class: Capybara::Driver::Base

参考文献

JRuby で ActiveRecord 3 をちょこっと試す

JRuby で ActiveRecord を試してみたいと思いました。
JRuby は RVM で 1.5.6 がインストールされています。

$ echo "rvm jruby" >> .rvmrc

こんな感じで .rvmrc を使用して JRuby 環境になっているものとします。

まずは ActiveRecord のインストール。

$ gem install activerecord
Successfully installed activesupport-3.0.3
Successfully installed builder-2.1.2
Successfully installed i18n-0.5.0
Successfully installed activemodel-3.0.3
Successfully installed arel-2.0.6
Successfully installed tzinfo-0.3.23
Successfully installed activerecord-3.0.3
7 gems installed (...)

MySQL を使いたいので、普通の Ruby っぽく

$ gem install mysql

とやってみましたが、ダメでした。

$ gem install activerecord-jdbcmysql-adapter
Successfully installed activerecord-jdbc-adapter-1.0.3-java
Successfully installed jdbc-mysql-5.0.4
Successfully installed activerecord-jdbcmysql-adapter-1.0.3-java
3 gems installed (...)

こういうのを使う必要があるらしいです。
establish_connection のやり方も若干違います。

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => 'jdbcmysql',
  :host => 'localhost',
  :username => 'root',
  :password => '',
  :database => 'piyopiyo_development',
  :pool => 5
)

あとは同じみたいです。

参考文献