Class: VagrantPlugins::DigitalOcean::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-digitalocean/provider.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Provider

Returns a new instance of Provider.



41
42
43
# File 'lib/vagrant-digitalocean/provider.rb', line 41

def initialize(machine)
  @machine = machine
end

Class Method Details

.droplet(machine, opts = {}) ⇒ Object

This class method caches status for all droplets within the Digital Ocean account. A specific droplet’s status may be refreshed by passing :refresh => true as an option.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-digitalocean/provider.rb', line 10

def self.droplet(machine, opts = {})
  client = Helpers::ApiClient.new(machine)

  # load status of droplets if it has not been done before
  if !@droplets
    result = client.request('/droplets')
    @droplets = result['droplets']
  end

  if opts[:refresh] && machine.id
    # refresh the droplet status for the given machine
    @droplets.delete_if { |d| d['id'].to_s == machine.id }
    result = client.request("/droplets/#{machine.id}")
    @droplets << droplet = result['droplet']
  else
    # lookup droplet status for the given machine
    droplet = @droplets.find { |d| d['id'].to_s == machine.id }
  end

  # if lookup by id failed, check for a droplet with a matching name
  # and set the id to ensure vagrant stores locally
  # TODO allow the user to configure this behavior
  if !droplet
    name = machine.config.vm.hostname || machine.name
    droplet = @droplets.find { |d| d['name'] == name.to_s }
    machine.id = droplet['id'].to_s if droplet
  end

  droplet ||= {'status' => 'not_created'}
end

Instance Method Details

#action(name) ⇒ Object



45
46
47
48
# File 'lib/vagrant-digitalocean/provider.rb', line 45

def action(name)
  return Actions.send(name) if Actions.respond_to?(name)
  nil
end

#machine_id_changedObject

This method is called if the underying machine ID changes. Providers can use this method to load in new data for the actual backing machine or to realize that the machine is now gone (the ID can become ‘nil`). No parameters are given, since the underlying machine is simply the machine instance given to this object. And no return value is necessary.



56
57
# File 'lib/vagrant-digitalocean/provider.rb', line 56

def machine_id_changed
end

#ssh_infoObject

This should return a hash of information that explains how to SSH into the machine. If the machine is not at a point where SSH is even possible, then ‘nil` should be returned.

The general structure of this returned hash should be the following:

{
  :host => "1.2.3.4",
  :port => "22",
  :username => "mitchellh",
  :private_key_path => "/path/to/my/key"
}

Note: Vagrant only supports private key based authenticatonion, mainly for the reason that there is no easy way to exec into an ‘ssh` prompt with a password, whereas we can pass a private key via commandline.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vagrant-digitalocean/provider.rb', line 77

def ssh_info
  droplet = Provider.droplet(@machine)

  return nil if droplet['status'].to_sym != :active

  # TODO remove config.ssh.username reference when Vagrant 1.2 is released
  return {
    :host => droplet['ip_address'],
    :port => '22',
    :username => @machine.config.ssh.username || 'root',
    :private_key_path => nil
  }
end

#stateObject

This should return the state of the machine within this provider. The state must be an instance of MachineState. Please read the documentation of that class for more information.



94
95
96
97
98
# File 'lib/vagrant-digitalocean/provider.rb', line 94

def state
  state = Provider.droplet(@machine)['status'].to_sym
  long = short = state.to_s
  Vagrant::MachineState.new(state, short, long)
end