読者です 読者をやめる 読者になる 読者になる

VagrantをバージョンアップしたらCentOSがはぶられてた

Vagrant Linux

CoreOSを試してみたくて CentOS 5.9 のサーバで Vagrant を 1.2.2 から 1.2.7 へ上げたら動かなくなった。

# rpm -Uvh http://files.vagrantup.com/packages/7ec0ee1d00a916f80b109a298bab08e391945243/vagrant_1.2.7_x86_64.rpm
Retrieving http://files.vagrantup.com/packages/7ec0ee1d00a916f80b109a298bab08e391945243/vagrant_1.2.7_x86_64.rpm
Preparing...                ########################################### [100%]
   1:vagrant                ########################################### [100%]

アップデート自体は問題ないけど、vagrantコマンドを実行してみると以下の通りエラーになる。

$ vagrant up
/opt/vagrant/embedded/gems/gems/vagrant-1.2.7/plugins/hosts/fedora/host.rb:39:in `block in initialize': undefined method `[]' for nil:NilClass (NoMethodError)
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/plugins/hosts/fedora/host.rb:38:in `open'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/plugins/hosts/fedora/host.rb:38:in `open'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/plugins/hosts/fedora/host.rb:38:in `initialize'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/environment.rb:502:in `new'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/environment.rb:502:in `host'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/environment.rb:514:in `block in action_runner'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/action/runner.rb:28:in `call'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/action/runner.rb:28:in `run'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/environment.rb:275:in `hook'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/lib/vagrant/environment.rb:136:in `initialize'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/bin/vagrant:62:in `new'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.2.7/bin/vagrant:62:in `<top (required)>'
        from /opt/vagrant/bin/../embedded/gems/bin/vagrant:23:in `load'
        from /opt/vagrant/bin/../embedded/gems/bin/vagrant:23:in `<main>'

エラーになった/opt/vagrant/embedded/gems/gems/vagrant-1.2.7/plugins/hosts/fedora/host.rbのinitializeを見てみる。

      def initialize(*args)
        super

        @nfs_server_binary = "/etc/init.d/nfs"

        # On Fedora 16+, systemd replaced init.d, so we have to use the
        # proper NFS binary. This checks to see if we need to do that.
        release_file = Pathname.new("/etc/redhat-release")
        begin
          release_file.open("r") do |f|
            version_number = /Fedora.*release ([0-9]+)/.match(f.gets)[1].to_i
            if version_number >= 16
              # "service nfs-server" will redirect properly to systemctl
              # when "service nfs-server restart" is called.
              @nfs_server_binary = "/usr/sbin/service nfs-server"
            end
          end

version_numberを取るときに/etc/redhat-releaseFedoraで始まらないとnilになるのでここが悪そう。 Fedora以外はスキップするように修正してみる。

@@ -36,7 +36,9 @@
         release_file = Pathname.new("/etc/redhat-release")
         begin
           release_file.open("r") do |f|
-            version_number = /Fedora.*release ([0-9]+)/.match(f.gets)[1].to_i
+            contents = f.gets
+            next unless contents =~ /^Fedora/
+            version_number = /Fedora.*release ([0-9]+)/.match(contents)[1].to_i
             if version_number >= 16
               # "service nfs-server" will redirect properly to systemctl
               # when "service nfs-server restart" is called.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 80 => 8080 (adapter 1)
[default] -- 8080 => 18080 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

無事動いた!

後でググってみたら1.2.5から出ている不具合らしい。

https://github.com/mitchellh/vagrant/pull/2008

そしてソース見て気づいたけど、本当にはぶられているのはRedHatやSLだった・・・