Class: Berkshelf::API::SiteConnector::Opscode
- Inherits:
-
Object
- Object
- Berkshelf::API::SiteConnector::Opscode
- Includes:
- Logging, Celluloid
- Defined in:
- lib/berkshelf/api/site_connector/opscode.rb
Constant Summary collapse
- V1_API =
'http://cookbooks.opscode.com/api/v1'.freeze
Instance Attribute Summary collapse
- #api_uri ⇒ String readonly
-
#retries ⇒ Integer
readonly
How many retries to attempt on HTTP requests.
-
#retry_interval ⇒ Float
readonly
Time to wait between retries.
Class Method Summary collapse
Instance Method Summary collapse
-
#cookbooks ⇒ Array<String>
A list of cookbook names available on the server.
-
#download(name, version, destination = Dir.mktmpdir) ⇒ String?
Download the cookbook with the given name and version to the destination.
-
#download_uri(name, version) ⇒ String?
Return the location where a cookbook of the given name and version can be downloaded from.
- #find(name, version) ⇒ Hashie::Mash?
-
#initialize(options = {}) ⇒ Opscode
constructor
A new instance of Opscode.
-
#stream(target) ⇒ Tempfile
Stream the response body of a remote URL to a file on the local file system.
-
#versions(cookbook) ⇒ Array<String>
A list of versions of this cookbook available on the server.
Methods included from Logging
Constructor Details
#initialize(options = {}) ⇒ Opscode
Returns a new instance of Opscode.
45 46 47 48 49 50 51 52 53 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 45 def initialize( = {}) = { url: V1_API, retries: 5, retry_interval: 0.5 }.merge() @api_uri = [:url] @connection = Faraday.new(@api_uri) do |c| c.response :parse_json c.use Faraday::Adapter::NetHttp end end |
Instance Attribute Details
#api_uri ⇒ String (readonly)
31 32 33 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 31 def api_uri @api_uri end |
#retries ⇒ Integer (readonly)
Returns how many retries to attempt on HTTP requests.
34 35 36 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 34 def retries @retries end |
#retry_interval ⇒ Float (readonly)
Returns time to wait between retries.
37 38 39 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 37 def retry_interval @retry_interval end |
Class Method Details
.uri_escape_version(version) ⇒ String
13 14 15 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 13 def uri_escape_version(version) version.to_s.gsub('.', '_') end |
.version_from_uri(uri) ⇒ String
20 21 22 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 20 def version_from_uri(uri) File.basename(uri.to_s).gsub('_', '.') end |
Instance Method Details
#cookbooks ⇒ Array<String>
Returns A list of cookbook names available on the server.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 57 def cookbooks start = 0 count = connection.get("cookbooks").body["total"] cookbooks = Array.new while count > 0 cookbooks += connection.get("cookbooks?start=#{start}&items=#{count}").body["items"] start += 100 count -= 100 end cookbooks.map { |cb| cb["cookbook_name"] } end |
#download(name, version, destination = Dir.mktmpdir) ⇒ String?
Download the cookbook with the given name and version to the destination. The directory containing the extracted contents will be returned on success. On failure, nil will be returned.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 101 def download(name, version, destination = Dir.mktmpdir) log.debug "Downloading #{name}(#{version})" if uri = download_uri(name, version) begin archive = stream(uri) Archive.extract(archive.path, destination) destination rescue => ex log.warn "Error downloading/extracting #{name} (#{version}): #{ex}" nil ensure archive.unlink unless archive.nil? end end end |
#download_uri(name, version) ⇒ String?
Return the location where a cookbook of the given name and version can be downloaded from
125 126 127 128 129 130 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 125 def download_uri(name, version) unless cookbook = find(name, version) return nil end cookbook[:file] end |
#find(name, version) ⇒ Hashie::Mash?
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 138 def find(name, version) response = connection.get("cookbooks/#{name}/versions/#{self.class.uri_escape_version(version)}") case response.status when (200..299) response.body else nil end end |
#stream(target) ⇒ Tempfile
Stream the response body of a remote URL to a file on the local file system
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 155 def stream(target) local = Tempfile.new('opscode-site-stream') local.binmode retryable(tries: retries, on: OpenURI::HTTPError, sleep: retry_interval) do open(target, 'rb', connection.headers) do |remote| local.write(remote.read) end end local ensure local.close(false) unless local.nil? end |
#versions(cookbook) ⇒ Array<String>
Returns A list of versions of this cookbook available on the server.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/berkshelf/api/site_connector/opscode.rb', line 76 def versions(cookbook) response = connection.get("cookbooks/#{cookbook}") case response.status when (200..299) response.body['versions'].collect do |version_uri| self.class.version_from_uri(version_uri) end else Array.new end end |