Class: Berkshelf::Source

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/berkshelf/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Source

Returns a new instance of Source.

Parameters:



10
11
12
13
# File 'lib/berkshelf/source.rb', line 10

def initialize(source)
  @source = source
  @universe   = nil
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/berkshelf/source.rb', line 7

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object



121
122
123
124
# File 'lib/berkshelf/source.rb', line 121

def ==(other)
  return false unless other.is_a?(self.class)
  uri == other.uri
end

#api_clientObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/berkshelf/source.rb', line 15

def api_client
  @api_client ||= begin
                    if source == :chef_server
                      APIClient.chef_server(
                        ssl: Berkshelf::Config.instance.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_universeArray<APIClient::RemoteCookbook>

Forcefully obtain the universe from the API endpoint and assign it to #universe. This will reload the value of #universe even if it has been loaded before.

Returns:

  • (Array<APIClient::RemoteCookbook>)


48
49
50
51
52
53
# File 'lib/berkshelf/source.rb', line 48

def build_universe
  @universe = api_client.universe
rescue => ex
  @universe = Array.new
  raise ex
end

#cookbook(name, version) ⇒ APIClient::RemoteCookbook

Parameters:

Returns:

  • (APIClient::RemoteCookbook)


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

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.

Returns:

  • (true, false)

    true if this a default source, false otherwise



91
92
93
# File 'lib/berkshelf/source.rb', line 91

def default?
  @default_ ||= uri.host == URI.parse(Berksfile::DEFAULT_API_URL).host
end

#hashObject



117
118
119
# File 'lib/berkshelf/source.rb', line 117

def hash
  @uri.host.hash
end

#inspectObject



113
114
115
# File 'lib/berkshelf/source.rb', line 113

def inspect
  "#<#{self.class.name} uri: #{@uri.to_s.inspect}>"
end

#latest(name) ⇒ APIClient::RemoteCookbook

Parameters:

Returns:

  • (APIClient::RemoteCookbook)


98
99
100
# File 'lib/berkshelf/source.rb', line 98

def latest(name)
  versions(name).sort.last
end

#search(name) ⇒ Array<APIClient::RemoteCookbook]

The list of remote cookbooks that match the given query.

Parameters:

Returns:

  • (Array<APIClient::RemoteCookbook])

    Array<APIClient::RemoteCookbook]



79
80
81
82
83
84
# File 'lib/berkshelf/source.rb', line 79

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

#to_sObject



109
110
111
# File 'lib/berkshelf/source.rb', line 109

def to_s
  "#{uri}"
end

#universeArray<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.

Returns:

  • (Array<APIClient::RemoteCookbook>)


62
63
64
# File 'lib/berkshelf/source.rb', line 62

def universe
  @universe || build_universe
end

#uriObject



36
37
38
39
40
41
42
# File 'lib/berkshelf/source.rb', line 36

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>

Parameters:

Returns:

  • (Array<APIClient::RemoteCookbook>)


105
106
107
# File 'lib/berkshelf/source.rb', line 105

def versions(name)
  universe.select { |cookbook| cookbook.name == name }
end