Class: VagrantPlugins::Linode::Provider

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Provider

Returns a new instance of Provider.



7
8
9
# File 'lib/vagrant-linode/provider.rb', line 7

def initialize(machine)
  @machine = machine
end

Class Method Details

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

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



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
40
41
42
43
44
45
46
47
# File 'lib/vagrant-linode/provider.rb', line 14

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

  # @todo how do I reuse VagrantPlugins::Linode::Actions::ConnectLinode ?
  # ..and nuke the helper
  # client = env[:linode_api]

  # load status of linodes if it has not been done before
  unless @linodes
    @linodes = client.linode.list.each { |l| l.network = client.linode.ip.list linodeid: l.linodeid }
  end

  if opts[:refresh] && machine.id
    # refresh the linode status for the given machine
    @linodes.delete_if { |d| d['linodeid'].to_s == machine.id }
    linode = client.linode.list(linodeid: machine.id).first
    linode.network = client.linode.ip.list linodeid: linode['linodeid']
    @linodes << linode
  elsif machine.id
    # lookup linode status for the given machine
    linode = @linodes.find { |d| d['linodeid'].to_s == machine.id }
  end

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

  linode ||= { status: :not_created }
end

Instance Method Details

#action(name) ⇒ Object

Attempt to get the action method from the Action class if it exists, otherwise return nil to show that we don’t support the given action.



52
53
54
55
56
# File 'lib/vagrant-linode/provider.rb', line 52

def action(name)
  action_method = "action_#{name}"
  return Actions.send(action_method) if Actions.respond_to?(action_method)
  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.



64
65
66
67
68
# File 'lib/vagrant-linode/provider.rb', line 64

def machine_id_changed
  if @machine.id
    Provider.linode(@machine, refresh: true)
  end
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.



88
89
90
91
# File 'lib/vagrant-linode/provider.rb', line 88

def ssh_info
  env = @machine.action('read_ssh_info')
  env[:machine_ssh_info]
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.



96
97
98
99
100
101
102
103
104
# File 'lib/vagrant-linode/provider.rb', line 96

def state
  env = @machine.action('read_state')
  state_id = env[:machine_state]

  short = I18n.t("vagrant_linode.states.short_#{state_id}")
  long = I18n.t("vagrant_linode.states.long_#{state_id}")

  Vagrant::MachineState.new(state_id, short, long)
end

#to_sObject



106
107
108
109
# File 'lib/vagrant-linode/provider.rb', line 106

def to_s
  id = @machine.id.nil? ? 'new' : @machine.id
  "Linode (#{id})"
end