9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrant の provisioning を使って、ruby をコードからインストールしてみる

Last updated at Posted at 2014-03-23

Vagrant 環境で serverspec のサンプルテストが実行されるまでの流れをメモ のつづき

serverspec で、ruby 2.1 がインストールされていることのテストを書く

spec/default/ruby_spec.rb
require 'spec_helper'

describe command('/usr/local/bin/ruby -v') do
  it { should return_stdout 'ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]' }
end

テスト実行

$ bundle exec rake spec 

ruby -S rspec spec/default/ruby_spec.rb
F

Failures:

  1) Command "ruby -v" should return stdout /\Aruby 2\.0\.0p451/
     Failure/Error: it { should return_stdout /\Aruby 2\.0\.0p451/ }
       sudo ruby -v
       expected Command "ruby -v" to return stdout /\Aruby 2\.0\.0p451/
     # ./spec/default/ruby_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 4.46 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/default/ruby_spec.rb:4 # Command "ruby -v" should return stdout /\Aruby 2\.0\.0p451/
ruby -S rspec spec/default/ruby_spec.rb failed

想定通りテスト失敗

まずは手動で、ruby 2.1 を仮想マシンにインストール

以下、vagrant ssh した後の入力

ログイン元のロケールを引き継いで警告が出たので、設定

$ echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc

wget

$ sudo yum install wget

openssl

$ wget http://www.openssl.org/source/openssl-1.0.1f.tar.gz
$ tar xzf ./openssl-1.0.1f.tar.gz
$ cd openssl-1.0.1f
$ ./config --prefix=/usr/local/openssl shared
$ make
$ sudo make install

zlib

$ wget http://zlib.net/zlib-1.2.8.tar.gz
$ tar xzf ./zlib-1.2.8.tar.gz
$ cd zlib-1.2.8
$ ./configure
$ make
$ sudo make install

ruby

$ wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz
$ tar xzf ruby-2.1.1.tar.gz
$ cd ruby-2.1.1
$ ./configure --with-opt-dir=/usr/local/openssl --enable-shared 
$ make 
$ sudo make install

serverspec が通ることを確認

$ bundle exec rake spec
ruby -S rspec spec/default/ruby_spec.rb
.

Finished in 3.93 seconds
1 example, 0 failures

Vagrant の provisioning を使って shell で、仮想サーバに ruby 2.1 をインストール

provisioning って?

  • vagrant up 時に実行させる処理を指定できる設定

chef 等のプロビジョニングツールを指定することもできるがまずは、シェルスクリプトを直書きしてみる

$ git diff
diff --git a/Vagrantfile b/Vagrantfile
index 4fffb32..030cfc3 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -78,6 +78,42 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   #   puppet.manifest_file  = "site.pp"
   # end

+  config.vm.provision :shell, :inline => <<-EOT
+    echo "encode setting"
+    # refs : http://d.hatena.ne.jp/tkrd/20120828/1346123699
+    echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc
+
+    echo "install wget"
+    sudo yum -y install wget
+
+    echo "install openssl"
+    wget http://www.openssl.org/source/openssl-1.0.1f.tar.gz
+    tar xzf ./openssl-1.0.1f.tar.gz
+    cd openssl-1.0.1f
+    ./config --prefix=/usr/local/openssl shared
+    make
+    sudo make install
+    cd
+
+    echo "install zlib"
+    wget http://zlib.net/zlib-1.2.8.tar.gz
+    tar xzf ./zlib-1.2.8.tar.gz
+    cd zlib-1.2.8
+    ./configure
+    make
+    sudo make install
+    cd
+
+    echo "install ruby"
+    wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz
+    tar xzf ruby-2.1.1.tar.gz
+    cd ruby-2.1.1
+    ./configure --with-opt-dir=/usr/local/openssl --enable-shared
+    make
+    sudo make install
+    cd
+  EOT
+
   # Enable provisioning with chef solo, specifying a cookbooks path, roles
   # path, and data_bags path (all relative to this Vagrantfile), and adding
   # some recipes and/or roles.

一度 vagrant を削除して、再生成

$ vagrant destroy
$ vagrant up

15分位掛かってインストール成功

再度serverspec が通ることを確認

$ bundle exec rake spec
ruby -S rspec spec/default/ruby_spec.rb
.

Finished in 4.36 seconds
1 example, 0 failures
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?