Class: Berkshelf::ChefAPILocation

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

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

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.



94
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
# File 'lib/berkshelf/locations/chef_api_location.rb', line 94

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::ClientKeyFileNotFoundOrInvalid => ex
  raise ClientKeyFileNotFound, ex
end

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key.



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

def client_key
  @client_key
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



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

def node_name
  @node_name
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Class Method Details

.finalizerProc

Returns:

  • (Proc)


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

def finalizer
  proc { conn.terminate if defined?(conn) && conn.alive? }
end

.validate_client_key(client_key) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

.validate_client_key!(client_key) ⇒ Object



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

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)


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

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

.validate_node_name!(node_name) ⇒ Object



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

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

  true
end

.validate_uri(uri) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

.validate_uri!(uri) ⇒ Object

Raises:

See Also:



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

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:



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

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)

  cached
end

#target_cookbookRidley::CookbookResource

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

Returns:

  • (Ridley::CookbookResource)


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

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,
         Ridley::Errors::ResourceNotFound
    @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



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

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

#to_sObject



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

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