Class: Berkshelf::ChefAPILocation

Inherits:
Object
  • Object
show all
Includes:
Location
Defined in:
lib/berkshelf/locations/chef_api_location.rb

Overview

Author:

Constant Summary

Constants included from Location

Location::OPSCODE_COMMUNITY_API

Instance Attribute Summary collapse

Attributes included from Location

#name, #version_constraint

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Location

#downloaded?, included, init, #to_json, #validate_cached

Constructor Details

#initialize(name, version_constraint, options = {}) ⇒ ChefAPILocation

Returns a new instance of ChefAPILocation.

Parameters:

  • name (#to_s)
  • version_constraint (Solve::Constraint)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :chef_api (String, Symbol)

    a URL to a Chef API. Alternatively the symbol :config can be provided which will instantiate this location with the values found in your Berkshelf configuration.

  • :node_name (String) — default: Berkshelf::Config.instance.chef.node_name

    the name of the client to use to communicate with the Chef API.

  • :client_key (String) — default: Berkshelf::Config.instance.chef.client_key

    the filepath to the authentication key for the client

  • :verify_ssl (Boolean) — default: Berkshelf::Config.instance.chef.ssl.verify

Raises:

  • (ClientKeyFileNotFound)

    if the value for :client_key does not contain a filepath pointing to a readable file containing a Chef client key.

    If the :chef_api option is given the symbol :config and your Berkshelf config does not have a value for chef.client_key which points to a readable file containing a Chef client key.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/berkshelf/locations/chef_api_location.rb', line 95

def initialize(name, version_constraint, options = {})
  options = options.reverse_merge(
    client_key: Berkshelf::Config.instance.chef.client_key,
    node_name: Berkshelf::Config.instance.chef.node_name,
    verify_ssl: Berkshelf::Config.instance.ssl.verify
  )

  @name               = name
  @version_constraint = version_constraint
  @downloaded_status  = false

  if options[:chef_api] == :knife
    Berkshelf.formatter.deprecation "specifying 'chef_api :knife' is deprecated. Please use 'chef_api :config'."
    options[:chef_api] = :config
  end

  validate_options!(options)

  if options[:chef_api] == :config
    unless Berkshelf::Config.instance.chef.node_name.present? &&
      Berkshelf::Config.instance.chef.client_key.present? &&
      Berkshelf::Config.instance.chef.chef_server_url.present?

      msg = "A Berkshelf configuration is required with a 'chef.client_key', 'chef.chef_server_Url',"
      msg << " and 'chef.node_name' setting to install or upload cookbooks using 'chef_api :config'."

      raise Berkshelf::ConfigurationError, msg
    end
    @node_name  = Berkshelf::Config.instance.chef.node_name
    @client_key = Berkshelf::Config.instance.chef.client_key
    @uri        = Berkshelf::Config.instance.chef.chef_server_url
  else
    @node_name  = options[:node_name]
    @client_key = options[:client_key]
    @uri        = options[:chef_api]
  end

  @conn = Ridley.new(
    server_url: uri,
    client_name: node_name,
    client_key: client_key,
    ssl: {
      verify: options[:verify_ssl]
    }
  )

  # Why do we use a class function for defining our finalizer?
  # http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
  ObjectSpace.define_finalizer(self, self.class.finalizer)
rescue Ridley::Errors::ClientKeyFileNotFound => ex
  raise ClientKeyFileNotFound, ex
end

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key.



73
74
75
# File 'lib/berkshelf/locations/chef_api_location.rb', line 73

def client_key
  @client_key
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



72
73
74
# File 'lib/berkshelf/locations/chef_api_location.rb', line 72

def node_name
  @node_name
end

#uriObject (readonly)

Returns the value of attribute uri.



71
72
73
# File 'lib/berkshelf/locations/chef_api_location.rb', line 71

def uri
  @uri
end

Class Method Details

.finalizerProc

Returns:

  • (Proc)


6
7
8
# File 'lib/berkshelf/locations/chef_api_location.rb', line 6

def finalizer
  proc { conn.terminate if conn.alive? }
end

.validate_client_key(client_key) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


31
32
33
# File 'lib/berkshelf/locations/chef_api_location.rb', line 31

def validate_client_key(client_key)
  File.exists?(client_key)
end

.validate_client_key!(client_key) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/berkshelf/locations/chef_api_location.rb', line 38

def validate_client_key!(client_key)
  unless validate_client_key(client_key)
    raise InvalidChefAPILocation
  end

  true
end

.validate_node_name(node_name) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


13
14
15
# File 'lib/berkshelf/locations/chef_api_location.rb', line 13

def validate_node_name(node_name)
  node_name.is_a?(String) && !node_name.empty?
end

.validate_node_name!(node_name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/berkshelf/locations/chef_api_location.rb', line 20

def validate_node_name!(node_name)
  unless validate_node_name(node_name)
    raise InvalidChefAPILocation
  end

  true
end

.validate_uri(uri) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


49
50
51
# File 'lib/berkshelf/locations/chef_api_location.rb', line 49

def validate_uri(uri)
  uri =~ URI.regexp(['http', 'https'])
end

.validate_uri!(uri) ⇒ Object

Raises:

See Also:



57
58
59
60
61
62
63
# File 'lib/berkshelf/locations/chef_api_location.rb', line 57

def validate_uri!(uri)
  unless validate_uri(uri)
    raise InvalidChefAPILocation, "'#{uri}' is not a valid Chef API URI."
  end

  true
end

Instance Method Details

#download(destination) ⇒ Berkshelf::CachedCookbook

Parameters:

  • destination (#to_s)

Returns:



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/berkshelf/locations/chef_api_location.rb', line 151

def download(destination)
  berks_path = File.join(destination, "#{name}-#{target_cookbook.version}")
  
  temp_path = target_cookbook.download
  FileUtils.mv(temp_path, berks_path)

  cached = CachedCookbook.from_store_path(berks_path)
  validate_cached(cached)

  set_downloaded_status(true)
  cached
end

#target_cookbookRidley::CookbookResource

Returns a Ridley::CookbookResource representing the cookbook that should be downloaded for this location

Returns:

  • (Ridley::CookbookResource)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/berkshelf/locations/chef_api_location.rb', line 168

def target_cookbook
  return @target_cookbook unless @target_cookbook.nil?

  begin
    @target_cookbook = if version_constraint
      conn.cookbook.satisfy(name, version_constraint)
    else
      conn.cookbook.latest_version(name)
    end
  rescue Ridley::Errors::HTTPNotFound
    @target_cookbook = nil
  end

  if @target_cookbook.nil?
    msg = "Cookbook '#{name}' found at #{self}"
    msg << " that would satisfy constraint (#{version_constraint}" if version_constraint
    raise CookbookNotFound, msg
  end

  @target_cookbook
end

#to_hashObject



190
191
192
# File 'lib/berkshelf/locations/chef_api_location.rb', line 190

def to_hash
  super.merge(value: self.uri)
end

#to_sObject



194
195
196
# File 'lib/berkshelf/locations/chef_api_location.rb', line 194

def to_s
  "#{self.class.location_key}: '#{uri}'"
end