Skip to content

Commit

Permalink
enable downloading from private github repos
Browse files Browse the repository at this point in the history
  • Loading branch information
punkle committed Jan 12, 2014
1 parent 9d12f89 commit 45b910f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions berkshelf.gemspec
Expand Up @@ -41,6 +41,7 @@ Gem::Specification.new do |s|
s.add_dependency 'ridley', '~> 2.3' s.add_dependency 'ridley', '~> 2.3'
s.add_dependency 'solve', '>= 0.8.0' s.add_dependency 'solve', '>= 0.8.0'
s.add_dependency 'thor', '~> 0.18.0' s.add_dependency 'thor', '~> 0.18.0'
s.add_dependency 'octokit', '~> 2.6'


s.add_development_dependency 'aruba', '~> 0.5' s.add_development_dependency 'aruba', '~> 0.5'
s.add_development_dependency 'chef-zero', '~> 1.5.0' s.add_development_dependency 'chef-zero', '~> 1.5.0'
Expand Down
4 changes: 4 additions & 0 deletions lib/berkshelf/config.rb
Expand Up @@ -123,5 +123,9 @@ def initialize(path = self.class.path, options = {})
type: Boolean, type: Boolean,
default: true, default: true,
required: true required: true
attribute 'github.access_token',
type: String,
default: '',
required: false
end end
end end
25 changes: 20 additions & 5 deletions lib/berkshelf/downloader.rb
@@ -1,6 +1,7 @@
require 'net/http' require 'net/http'
require 'zlib' require 'zlib'
require 'archive/tar/minitar' require 'archive/tar/minitar'
require 'octokit'


module Berkshelf module Berkshelf
class Downloader class Downloader
Expand Down Expand Up @@ -72,18 +73,32 @@ def try_download(source, name, version)
when :github when :github
tmp_dir = Dir.mktmpdir tmp_dir = Dir.mktmpdir
archive_path = File.join(tmp_dir, "#{name}-#{version}.tar.gz") archive_path = File.join(tmp_dir, "#{name}-#{version}.tar.gz")
out_dir = File.join(tmp_dir, "#{name}-#{version}") unpack_dir = File.join(tmp_dir, "#{name}-#{version}")
url = URI("https://codeload.github.com/#{remote_cookbook.location_path}/tar.gz/v#{version}")
github_access_token = Berkshelf::Config.instance.github.access_token
github_config = {}
github_config[:access_token] = github_access_token unless github_access_token == ''
github_client = Octokit::Client.new github_config

begin
url = URI(github_client.archive_link(remote_cookbook.location_path, ref: "v#{version}"))
rescue Octokit::Unauthorized
raise CookbookNotFound
end


Net::HTTP.start(url.host, use_ssl: url.scheme == "https") do |http| Net::HTTP.start(url.host, use_ssl: url.scheme == "https") do |http|
resp = http.get(url.path) resp = http.get(url.request_uri)
raise CookbookNotFound unless resp.is_a?(Net::HTTPSuccess)
open(archive_path, "wb") { |file| file.write(resp.body) } open(archive_path, "wb") { |file| file.write(resp.body) }
end end


tgz = Zlib::GzipReader.new(File.open(archive_path, "rb")) tgz = Zlib::GzipReader.new(File.open(archive_path, "rb"))
Archive::Tar::Minitar.unpack(tgz, tmp_dir) Archive::Tar::Minitar.unpack(tgz, unpack_dir)


out_dir # we need to figure out where the cookbook is located in the archive. This is because the directory name
# pattern is not cosistant between private and public github repositories
cookbook_directory = Dir.entries(unpack_dir).select{ |f| f.start_with? "#{remote_cookbook.location_path.sub('/', '-')}"}[0]
File.join(unpack_dir, cookbook_directory)
else else
raise RuntimeError, "unknown location type #{remote_cookbook.location_type}" raise RuntimeError, "unknown location type #{remote_cookbook.location_type}"
end end
Expand Down

0 comments on commit 45b910f

Please sign in to comment.