2016年2月26日金曜日

itamaeでrbenv入れるのにはまった恥ずかしい話

ec2でrbenv入れるのにitamaeで入れようとしてたのですが、
rbenvのインストール手順を普通になぞっていったら地味にはまってしまいました。

最初に書いたレシピはこんな感じ
rbenv/default.rb
%w(
  git
  gcc-c++
  glibc-headers
  openssl-devel
  readline
  libyaml-devel
  readline-devel
  zlib
  zlib-devel
  libffi-devel
  libxml2
  libxslt
  libxml2-devel
  libxslt-devel
  sqlite-devel
  mysql-devel
  patch
).each do |pkg|
  package "#{pkg}" do
    action :install
  end
end

git '/usr/local/rbenv' do
  repository 'https://github.com/sstephenson/rbenv.git'
end

directory "/urs/local/rbenv/plugins" do
  action :create
end

git '/usr/local/rbenv/plugins/ruby-build' do
  repository 'https://github.com/sstephenson/ruby-build.git'
end

remote_file "/etc/profile.d/rbenv.sh" do
  action :create
  source "files/etc/profile.d/rbenv.sh"
  owner 'root'
  group 'root'
  mode '644'
end

execute 'install ruby' do
  command("source /etc/profile.d/rbenv.sh; rbenv install 2.3.0")
  command("source /etc/profile.d/rbenv.sh; rbenv global 2.3.0; rbenv rehash")
end

実行してみるとこんな感じ
$ bundle exec itamae ssh -i pem/test.pem -h 172.31.21.186 roles/test.rb
 INFO : Starting Itamae...
 INFO : Recipe: /home/ec2-user/deploy/xxx/roles/test.rb
 INFO :   Recipe: /home/ec2-user/deploy/xxx/cookbooks/rbenv/default.rb
 INFO :     package[git] installed will change from 'false' to 'true'
 INFO :     package[gcc-c++] installed will change from 'false' to 'true'
 INFO :     package[openssl-devel] installed will change from 'false' to 'true'
 INFO :     package[libyaml-devel] installed will change from 'false' to 'true'
 INFO :     package[readline-devel] installed will change from 'false' to 'true'
 INFO :     package[libffi-devel] installed will change from 'false' to 'true'
 INFO :     package[libxml2-devel] installed will change from 'false' to 'true'
 INFO :     package[libxslt-devel] installed will change from 'false' to 'true'
 INFO :     package[sqlite-devel] installed will change from 'false' to 'true'
 INFO :     package[mysql-devel] installed will change from 'false' to 'true'
 INFO :     package[patch] installed will change from 'false' to 'true'
 INFO :     git[/usr/local/rbenv] exist will change from 'false' to 'true'
 INFO :     directory[/urs/local/rbenv/plugins] exist will change from 'false' to 'true'
 INFO :     git[/usr/local/rbenv/plugins/ruby-build] exist will change from 'false' to 'true'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] exist will change from 'false' to 'true'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] mode will be '0644'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] owner will be 'root'
 INFO :     remote_file[/etc/profile.d/rbenv.sh] group will be 'root'
 INFO :     diff:
 INFO :     --- /dev/null 2016-02-25 02:37:28.211836042 +0000
 INFO :     +++ /tmp/itamae_tmp/1456368153.1309056 2016-02-25 02:42:33.144609603 +0000
 INFO :     @@ -0,0 +1,3 @@
 INFO :     +export RBENV_ROOT=/usr/local/rbenv
 INFO :     +export PATH="$RBENV_ROOT/bin:$PATH"
 INFO :     +eval "$(rbenv init -)"
 INFO :     execute[install ruby] executed will change from 'false' to 'true'

正常に終わったように見えてるが、rubyのバージョンが変わってない。。。
$ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

レシピのrubyインストール部分を以下のようにコマンドを分割する形に修正
rbenv/default.rb
execute 'install ruby' do
  command("source /etc/profile.d/rbenv.sh; rbenv install 2.3.0")
end

execute 'set ruby global' do
  command("source /etc/profile.d/rbenv.sh; rbenv global 2.3.0; rbenv rehash")
end

再度実行してみる
$ bundle exec itamae ssh -i pem/test.pem -h 172.31.21.186 roles/test.rb
 INFO : Starting Itamae...
 INFO : Recipe: /home/ec2-user/deploy/xxx/roles/test.rb
 INFO :   Recipe: /home/ec2-user/deploy/xxx/cookbooks/rbenv/default.rb
 INFO :     execute[install ruby] executed will change from 'false' to 'true'
 INFO :     execute[set ruby global] executed will change from 'false' to 'true'

ちゃんとrubyのバージョンがrbenvで入れたものに変わりました
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]


参考URL
http://qiita.com/fukuiretu/items/337e6ae15c1f01e93ec3

2016年2月16日火曜日

simplecovでrspecのテスト書いているのにカバレッジ0%のクラスがある

simplecovを使ってrailsのカバレッジを計測しているのですが、
異常に数値が低いなと思い、詳細を見ていったところ、
rspec書いているのに0%になっているクラスがいたので調べてみました。

修正前のrails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
require 'simplecov'
require 'simplecov-rcov'
〜
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails'

修正後のrails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails'

require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
〜

最初はsimplecovのrequireをrequireの中で一番下に書き、
rails_helper.rbの末尾のところでSimpleCov.startさせていました。
調べたところどうやら他のモジュールが読み込まれるよりも先に
simplecovを読み込ませるようにしないといけないようです。
カバレッジ0%だったものが修正後のものではちゃんと計測できていました。


参考URL
https://github.com/colszowka/simplecov/issues/77

2016年2月9日火曜日

railsで指定したid順にDBから取得する

railsで指定したidの順番でDBから取得したかったので調べました。
このやりかたはDBがMySQLのときしか使えないようです。

idlist = [3, 8 ,2 ,4 ,6]

users = users.order("field(id, #{idlist.join(',')})")

参考URL
http://qiita.com/corin8823/items/318220f6f1b14a1bb7ba
http://qiita.com/takuya-i/items/a7ef5105952e32f1affd

2016年2月6日土曜日

FactoryGirlでシリアライズしたデータを登録する

DBにHashをシリアライズしたものを文字列として保存していて、
そのテストデータをFactoryGirlで作りたくて調べました。

migration
class User < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.text :info
    end
  end
end

model
class User < ActiveRecord::Base
  serialize :info
end

最初に以下のようなStringで入れてみたりしましたがダメでした。
"{ memo1: 'memo', memo2: 'memo2', count: 2 }"

調べたところ、()や{}で囲えばよいようです
factory
require 'faker'
FactoryGirl.define do
  factory :user do
    id 1
    name 'Taro'
    info ({ memo1: 'memo', memo2: 'memo2', count: 2 })
  end
end

参考URL
http://www.rubycoloredglasses.com/2012/06/add-a-serialized-hash-attribute-to-a-factory_girl-definition/