Class: ChefMetal::ChefMachineSpec

Inherits:
MachineSpec show all
Defined in:
lib/chef_metal/chef_machine_spec.rb

Overview

Specification for a machine. Sufficient information to find and contact it after it has been set up.

Instance Attribute Summary

Attributes inherited from MachineSpec

#node

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MachineSpec

#driver_url, #location, #location=, #name

Constructor Details

#initialize(node, chef_server) ⇒ ChefMachineSpec

Returns a new instance of ChefMachineSpec.



12
13
14
15
# File 'lib/chef_metal/chef_machine_spec.rb', line 12

def initialize(node, chef_server)
  super(node)
  @chef_server = chef_server
end

Class Method Details

.empty(name, chef_server = Cheffish.default_chef_server) ⇒ Object

Creates a new empty MachineSpec with the given name.



35
36
37
# File 'lib/chef_metal/chef_machine_spec.rb', line 35

def self.empty(name, chef_server = Cheffish.default_chef_server)
  ChefMachineSpec.new({ 'name' => name, 'normal' => {} }, chef_server)
end

.get(name, chef_server = Cheffish.default_chef_server) ⇒ Object

Get a MachineSpec from the chef server. If the node does not exist on the server, it returns nil.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chef_metal/chef_machine_spec.rb', line 21

def self.get(name, chef_server = Cheffish.default_chef_server)
  chef_api = Cheffish.chef_server_api(chef_server)
  begin
    ChefMachineSpec.new(chef_api.get("/nodes/#{name}"), chef_server)
  rescue Net::HTTPServerException => e
    if e.response.code == '404'
      nil
    else
      raise
    end
  end
end

.id_from(chef_server, name) ⇒ Object



47
48
49
# File 'lib/chef_metal/chef_machine_spec.rb', line 47

def self.id_from(chef_server, name)
  "#{chef_server[:chef_server_url]}/nodes/#{name}"
end

Instance Method Details

#idObject

Globally unique identifier for this machine. Does not depend on the machine’s location or existence.



43
44
45
# File 'lib/chef_metal/chef_machine_spec.rb', line 43

def id
  ChefMachineSpec.id_from(chef_server, name)
end

#save(action_handler) ⇒ Object

Save this node to the server. If you have significant information that could be lost, you should do this as quickly as possible. Data will be saved automatically for you after allocate_machine and ready_machine.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chef_metal/chef_machine_spec.rb', line 56

def save(action_handler)
  if location && (!location.is_a?(Hash) || !location['driver_url'])
    raise "Drivers must specify a canonical driver_url in machine_spec.location.  Contact your driver's author."
  end
  # Save the node to the server.
  _self = self
  _chef_server = _self.chef_server
  ChefMetal.inline_resource(action_handler) do
    chef_node _self.name do
      chef_server _chef_server
      raw_json _self.node
    end
  end
end