Skip to content

Instantly share code, notes, and snippets.

@lukewpatterson
Created April 18, 2012 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukewpatterson/2415533 to your computer and use it in GitHub Desktop.
Save lukewpatterson/2415533 to your computer and use it in GitHub Desktop.
Vagrant+Chef_Solo, download cookbooks from Vagrantfile, don't use this pattern though
# http://wiki.opscode.com/display/chef/Cookbook+Site+API
# think http://wiki.opscode.com/display/chef/Managing+Cookbooks+With+Knife#ManagingCookbooksWithKnife-install
def download_and_uncompress(name, version)
def download(file_url)
uri = URI(file_url)
tempfile = Tempfile.new(Random.rand(100).to_s())
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri.request_uri
http.request request do |response|
open tempfile.path, 'w' do |io|
response.read_body do |chunk|
io.write chunk
end
end
end
end
return tempfile
end
def ungzip(file)
zipped = Zlib::GzipReader.new(file)
unzipped = StringIO.new(zipped.read)
zipped.close
unzipped
end
def untar(io, destination)
Gem::Package::TarReader.new io do |tar|
tar.each do |tarfile|
destination_file = File.join destination, tarfile.full_name
if tarfile.directory?
FileUtils.mkdir_p destination_file
else
destination_directory = File.dirname(destination_file)
FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
File.open destination_file, "wb" do |f|
f.print tarfile.read
end
end
end
end
end
# http://wiki.opscode.com/display/chef/Cookbook+Site+API#CookbookSiteAPI-%2Fcookbooks%2F%5C%2Fversions%2F%5C
url = "http://cookbooks.opscode.com/api/v1/cookbooks/#{URI.encode(name)}/versions/#{URI.encode(version)}"
response = Net::HTTP.get_response(URI.parse(url))
data = response.body
json = JSON.parse(data)
if json.has_key? 'error_messages'
raise 'Error retrieving ' + name + ': ' + json['error_messages'].to_s()
end
downloadedfile = download(json["file"])
#https://gist.github.com/1335041
untar(ungzip(downloadedfile), 'cookbooks')
end
download_and_uncompress('build-essential', '1_0_0')
download_and_uncompress('git', '0_10_0')
download_and_uncompress('python', '1_0_6')
download_and_uncompress('nodejs', '0_6_11')
download_and_uncompress('ruby', '0_9_0')
download_and_uncompress('npm', '0_1_0')
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = "cookbooks"
chef.add_recipe "git"
chef.add_recipe "python"
chef.add_recipe "nodejs"
chef.add_recipe "ruby"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment