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.



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

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.



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

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.



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

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



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

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.



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

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.



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

def save(action_handler)
  # 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