Class: Chef::Provisioning::ChefImageSpec

Inherits:
ImageSpec
  • Object
show all
Defined in:
lib/chef/provisioning/chef_image_spec.rb

Overview

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

Instance Attribute Summary

Attributes inherited from ImageSpec

#image_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ImageSpec

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

Constructor Details

#initialize(node, chef_server) ⇒ ChefImageSpec


12
13
14
15
# File 'lib/chef/provisioning/chef_image_spec.rb', line 12

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

Class Method Details

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

Creates a new empty ImageSpec with the given name.


37
38
39
# File 'lib/chef/provisioning/chef_image_spec.rb', line 37

def self.empty(id, chef_server = Cheffish.default_chef_server)
  ChefImageSpec.new({ 'id' => id }, chef_server)
end

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

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


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chef/provisioning/chef_image_spec.rb', line 21

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

.id_from(chef_server, name) ⇒ Object


49
50
51
# File 'lib/chef/provisioning/chef_image_spec.rb', line 49

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

Instance Method Details

#delete(action_handler) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/provisioning/chef_image_spec.rb', line 74

def delete(action_handler)
  # Delete the image from the server.
  _self = self
  _chef_server = _self.chef_server
  Chef::Provisioning.inline_resource(action_handler) do
    chef_data_bag_item _self.name do
      data_bag 'images'
      chef_server _chef_server
      action :delete
    end
  end
end

#idObject

Globally unique identifier for this image. Does not depend on the image's location or existence.


45
46
47
# File 'lib/chef/provisioning/chef_image_spec.rb', line 45

def id
  ChefImageSpec.id_from(chef_server, name)
end

#save(action_handler) ⇒ Object

Save this image 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_image and ready_image.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/provisioning/chef_image_spec.rb', line 58

def save(action_handler)
  # Save the image to the server.
  _self = self
  _chef_server = _self.chef_server
  Chef::Provisioning.inline_resource(action_handler) do
    # Create the data bag if needed
    chef_data_bag 'images'
    # Save the image
    chef_data_bag_item _self.name do
      data_bag 'images'
      chef_server _chef_server
      raw_data _self.image_data
    end
  end
end