vagrant-digitalocean provider の簡単な使い方

次のような Vagrantfile を用意します。

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure('2') do |config|
config.vm.provider :digital_ocean do |provider, override|
override.ssh.private_key_path = '~/.ssh/digitalocean-vagrant'
override.vm.box = 'digital_ocean'
override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box"
provider.token = ''
provider.image = 'CentOS 6.5 x64'
# San Francisco 1
provider.region = 'sfo1'
provider.size = '512mb'
end
end

前提条件としては

  • Apps & API メニューで Personal Access Tokens を生成すること。Read, Write アクセス付与。
  • Vagrant という名前の秘密鍵が登録されていること。
  • ~/.ssh/digitalocean-vagrant と対になる公開鍵 ~/.ssh/digitalocean-vagrant.pub が存在すること。
  • 選択されているリージョンは San Francisco 1 (slug名になっている)。

ちなみに現在のリージョンとslug名の対応は以下のようになっている。

slug Region Name
nyc1 New York 1
ams1 Amsterdam 1
sfo1 San Francisco 1
nyc2 New York 2
ams2 Amsterdam 2
sgp1 Singapore 1
lon1 London 1
nyc3 New York 3

調べ方はさっきのパーソナルトークンを指定して以下の様な curl コマンドを発行すればいいです。
jq でフィルタリングしてます。

curl -X GET "https://api.digitalocean.com/v2/regions" \
-H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
2>/dev/null | jq '.regions [] | .slug,.name'

EC2 と違って Droplet が存在しているだけで課金対象になるので用が済んだらすぐに

vagrant destroy -f

References

  1. VagrantとSSDなVPS(Digital Ocean)で1時間1円の使い捨て高速サーバ環境を構築する – Glide Note – グライドノート
  2. jq コマンドが強力すぎてヤバい件 | CUBE SUGAR STORAGE
  3. DigitalOcean API

vagrant-digitalocean で vagrant up すると NoMethodError となる件

以下の様なエラーとなります。

% vagrant up --provider=digital_ocean
Bringing machine 'default' up with 'digital_ocean' provider...
==> default: Using existing SSH key: Vagrant
==> default: Creating a new droplet...
/Users/wnoguchi/.vagrant.d/gems/gems/vagrant-digitalocean-0.6.2/lib/vagrant-digitalocean/actions/create.rb:37:in `call': undefined method `first' for nil:NilClass (NoMethodError)
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Users/wnoguchi/.vagrant.d/gems/gems/vagrant-digitalocean-0.6.2/lib/vagrant-digitalocean/actions/setup_key.rb:33:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:95:in `block in finalize_action'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builder.rb:116:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `block in run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/util/busy.rb:19:in `busy'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builtin/call.rb:53:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builtin/config_validate.rb:25:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/warden.rb:34:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/builder.rb:116:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `block in run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/util/busy.rb:19:in `busy'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/action/runner.rb:66:in `run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:196:in `action_raw'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:173:in `block in action'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/environment.rb:434:in `lock'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:161:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/machine.rb:161:in `action'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.6.3/lib/vagrant/batch_action.rb:82:in `block (2 levels) in run'

DigitalOcean API V2 は Public Beta なので仕様が変わったためのようです。

Public Beta
Due to the large number of changes to API v2 we have decided to release it as a beta to collect user feedback. Please note that >during this period based on user feedback we may make some large changes which may break compatibility.

DigitalOcean API

このあたりは以下の Issue とプルリクで修正方法が確立されているので早く本家にマージされてほしいです。

とりあえずなんとかしたい方は対処療法的には

~/.vagrant.d/gems/gems/vagrant-digitalocean-0.6.2/lib/vagrant-digitalocean/actions/create.rb

の 37 行目を

          @client.wait_for_event(env, result['droplet']['action_ids'].first)
# ↓
@client.wait_for_event(env, result['links']['actions'].first['id'])

とすればいいです。