Class: Berkshelf::Source
- Inherits:
-
Object
- Object
- Berkshelf::Source
- Includes:
- Comparable
- Defined in:
- lib/berkshelf/source.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #api_client ⇒ Object
-
#build_universe ⇒ Array<APIClient::RemoteCookbook>
Forcefully obtain the universe from the API endpoint and assign it to #universe.
- #cookbook(name, version) ⇒ APIClient::RemoteCookbook
-
#default? ⇒ true, false
Determine if this source is a “default” source, as defined in the Berksfile.
- #hash ⇒ Object
-
#initialize(source) ⇒ Source
constructor
A new instance of Source.
- #inspect ⇒ Object
- #latest(name) ⇒ APIClient::RemoteCookbook
-
#search(name) ⇒ Array<APIClient::RemoteCookbook]
The list of remote cookbooks that match the given query.
- #ssl_policy ⇒ Object
- #to_s ⇒ Object
-
#universe ⇒ Array<APIClient::RemoteCookbook>
Return the universe from the API endpoint.
- #uri ⇒ Object
- #versions(name) ⇒ Array<APIClient::RemoteCookbook>
Constructor Details
#initialize(source) ⇒ Source
Returns a new instance of Source.
12 13 14 15 |
# File 'lib/berkshelf/source.rb', line 12 def initialize(source) @source = source @universe = nil end |
Instance Attribute Details
#source ⇒ Object
Returns the value of attribute source.
9 10 11 |
# File 'lib/berkshelf/source.rb', line 9 def source @source end |
Instance Method Details
#==(other) ⇒ Object
130 131 132 133 |
# File 'lib/berkshelf/source.rb', line 130 def ==(other) return false unless other.is_a?(self.class) uri == other.uri end |
#api_client ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/berkshelf/source.rb', line 21 def api_client @api_client ||= begin = { verify: Berkshelf::Config.instance.ssl.verify } [:cert_store] = ssl_policy.store if ssl_policy.store if source == :chef_server APIClient.chef_server( ssl: , timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, client_name: Berkshelf::Config.instance.chef.node_name, server_url: Berkshelf::Config.instance.chef.chef_server_url, client_key: Berkshelf::Config.instance.chef.client_key ) else APIClient.new(uri, timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, ssl: Berkshelf::Config.instance.ssl ) end end end |
#build_universe ⇒ Array<APIClient::RemoteCookbook>
57 58 59 60 61 62 |
# File 'lib/berkshelf/source.rb', line 57 def build_universe @universe = api_client.universe rescue => ex @universe = Array.new raise ex end |
#cookbook(name, version) ⇒ APIClient::RemoteCookbook
79 80 81 |
# File 'lib/berkshelf/source.rb', line 79 def cookbook(name, version) universe.find { |cookbook| cookbook.name == name && cookbook.version == version } end |
#default? ⇒ true, false
Determine if this source is a “default” source, as defined in the Berksfile.
100 101 102 |
# File 'lib/berkshelf/source.rb', line 100 def default? @default_ ||= uri.host == URI.parse(Berksfile::DEFAULT_API_URL).host end |
#hash ⇒ Object
126 127 128 |
# File 'lib/berkshelf/source.rb', line 126 def hash @uri.host.hash end |
#inspect ⇒ Object
122 123 124 |
# File 'lib/berkshelf/source.rb', line 122 def inspect "#<#{self.class.name} uri: #{@uri.to_s.inspect}>" end |
#latest(name) ⇒ APIClient::RemoteCookbook
107 108 109 |
# File 'lib/berkshelf/source.rb', line 107 def latest(name) versions(name).sort.last end |
#search(name) ⇒ Array<APIClient::RemoteCookbook]
The list of remote cookbooks that match the given query.
88 89 90 91 92 93 |
# File 'lib/berkshelf/source.rb', line 88 def search(name) universe .select { |cookbook| cookbook.name =~ Regexp.new(name) } .group_by(&:name) .collect { |_, versions| versions.max_by { |v| Semverse::Version.new(v.version) } } end |
#ssl_policy ⇒ Object
17 18 19 |
# File 'lib/berkshelf/source.rb', line 17 def ssl_policy @ssl_policy ||= SSLPolicy.new end |
#to_s ⇒ Object
118 119 120 |
# File 'lib/berkshelf/source.rb', line 118 def to_s "#{uri}" end |
#universe ⇒ Array<APIClient::RemoteCookbook>
Return the universe from the API endpoint.
This is lazily loaded so the universe will be retrieved from the API endpoint on the first call and cached for future calls. Send the #build_universe message if you want to reload the cached universe.
71 72 73 |
# File 'lib/berkshelf/source.rb', line 71 def universe @universe || build_universe end |
#uri ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/berkshelf/source.rb', line 45 def uri @uri ||= if source == :chef_server SourceURI.parse(Berkshelf::Config.instance.chef.chef_server_url) else SourceURI.parse(source) end end |
#versions(name) ⇒ Array<APIClient::RemoteCookbook>
114 115 116 |
# File 'lib/berkshelf/source.rb', line 114 def versions(name) universe.select { |cookbook| cookbook.name == name } end |