Class: VagrantPlugins::Abiquo::Provider

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Provider

Returns a new instance of Provider.



62
63
64
# File 'lib/vagrant-abiquo/provider.rb', line 62

def initialize(machine)
  @machine = machine
end

Class Method Details

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-abiquo/provider.rb', line 31

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

.virtualmachine(machine) ⇒ 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
# File 'lib/vagrant-abiquo/provider.rb', line 10

def self.virtualmachine(machine)
  @client = Helpers::ApiClient.new(machine)
  if !@virtualmachines
    vms_accept = {:accept => "application/vnd.abiquo.virtualmachines+json" }
    @virtualmachines = JSON.parse(@client.http_request(machine.provider_config.abiquo_api_uri+"/cloud/virtualmachines?limit=0","GET", vms_accept))
  end

  @virtualmachines['collection'].each do |vm|
    
    puts "VM id --> "+machine.id if not machine.id.nil?
    puts "Lista de VMs -->"+vm['id'].to_s
    if vm['id'].to_s == machine.id
      virtualmachine ||= {'status' => 'active'}
      @found = true
    end
  end
  if not @found
    virtualmachine ||= {'status' => 'not_created'}
  end
end

Instance Method Details

#action(name) ⇒ Object



66
67
68
69
# File 'lib/vagrant-abiquo/provider.rb', line 66

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.



77
78
# File 'lib/vagrant-abiquo/provider.rb', line 77

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.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vagrant-abiquo/provider.rb', line 98

def ssh_info
  droplet = Provider.droplet(@machine)

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

  return {
    :host => droplet['ip_address'],
    :port => '22',
    :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.



114
115
116
117
118
# File 'lib/vagrant-abiquo/provider.rb', line 114

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