-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c300528
Showing
9 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
require 'chef/knife' | ||
|
||
module KnifePlugins | ||
class SourceIngredientVirtualbox < Chef::Knife | ||
include Chef::Mixin::Checksum | ||
|
||
deps do | ||
require 'chef/version_constraint' | ||
require 'chef/exceptions' | ||
require 'chef/search/query' | ||
require 'chef/shef/ext' | ||
require 'chef/mixin/checksum' | ||
require 'json' | ||
require 'open-uri' | ||
require 'net/https' | ||
require 'uri' | ||
require 'nokogiri' | ||
require 'fileutils' | ||
end | ||
|
||
banner "knife source ingredient virtualbox" | ||
|
||
def download_file(source,destination) | ||
puts "Downloading #{source} to #{destination}" | ||
uri = URI.parse source | ||
Net::HTTP.start(uri.host, uri.port) do |http| | ||
request = Net::HTTP::Get.new uri.request_uri | ||
http.request request do |response| | ||
if response.response['Location']!=nil | ||
redirectURL=response.response['Location'] | ||
puts "#{redirectURL}***" | ||
# need to figure out how to handle redirects | ||
raise Error | ||
end | ||
|
||
open destination, 'wb' do |io| | ||
response.read_body do |chunk| | ||
io.write chunk | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def run | ||
begin | ||
FileUtils.touch(File.join(Chef::Config[:data_bag_path],'.writeable')) | ||
FileUtils.touch(File.join(Chef::Config[:file_cache_path],'.writeable')) | ||
rescue | ||
puts "Set role_path and file_cache_path in knife.rb to somewhere writable" | ||
exit 1 | ||
end | ||
data_bag_name = 'virtualbox' # maybe set this via cmdline later | ||
artifacts = Hash.new | ||
# http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/SHA256SUMS | ||
base_url = 'http://dlc.sun.com.edgesuite.net/virtualbox/' | ||
# this one directs us, and I can't handle 302s yet | ||
# base_url = 'http://download.virtualbox.org/virtualbox/' | ||
download_page=Nokogiri::HTML(open(base_url)) | ||
download_page.search( | ||
'//a/img[contains(@alt,"DIR")]/..').select{|l| | ||
l['href'] =~ /\d+\.\d+\.\d+\/$/}.map{|v| v['href']}.each do |ver_url| | ||
virtualbox_ver = ::File.basename(ver_url.sub('/','')) | ||
semantic_ver = virtualbox_ver | ||
# I only want 4.2.8 or higher | ||
next if not Chef::VersionConstraint.new(">= 4.2.8").include? semantic_ver | ||
|
||
# we wants rhel 6, osx, windows, and recent ubuntus, the guest additions and extpack | ||
wants = %w{ el6 OSX Win precise quantal VBoxGuestAdditions Pack } | ||
# The formats we want... note lack of i386, i686, openSUSE, sles, fedora, SunOS | ||
# generic Linux, src SDK | ||
formats = %w{ amd64 x86_64 exe iso dmg} | ||
formats << "#{virtualbox_ver}\\.vbox" | ||
# get the urls for all links that have a class starting with file | ||
package_urls = Nokogiri::HTML(open(base_url + ver_url)).search('//a' | ||
).select do |l| | ||
l['href'] =~ /(#{wants.join('|')}).*(#{formats.join('|')})/ | ||
end.map{ |l| l['href']} | ||
# populate our artifacts hash with info about this artifact source | ||
package_urls.each do |package_url| | ||
dbi_safe_ver = virtualbox_ver.gsub('.','_') | ||
package_filename = package_url | ||
package_url = base_url + ver_url + package_filename | ||
arch = case ::File.basename(package_filename) | ||
when /(x86_64|amd64)/ | ||
'x86_64' | ||
when /(i686|i586|i386)/ | ||
'i686' | ||
else | ||
['i686','x86_64'] | ||
end | ||
case package_filename | ||
when %r{ | ||
(?<package_ver> \d+\.\d+\.\d+ ){0} | ||
(?<build_num> \d+ ){0} | ||
VirtualBox-\g<package_ver>-\g<build_num>-OSX\.dmg | ||
}x | ||
package_ver = $1 | ||
build_num = $2 | ||
os = { | ||
'mac_os_x' => [ | ||
'10.7', | ||
'10.8' | ||
] | ||
} | ||
dbi_name = "osx_#{dbi_safe_ver}" | ||
when %r{ | ||
(?<package_ver> \d+\.\d+\.\d+ ){0} | ||
(?<build_num> \d+ ){0} | ||
VirtualBox-\g<package_ver>-\g<build_num>-Win\.exe | ||
}x | ||
package_ver = $1 | ||
build_num = $2 | ||
os = { | ||
'windows' => [ | ||
'2008r2', | ||
'2012', | ||
'7', | ||
'8' | ||
] | ||
} | ||
dbi_name = "windows_#{dbi_safe_ver}" | ||
when %r{ | ||
(?<package_ver> \d+\.\d+\.\d+ ){0} | ||
VBoxGuestAdditions_\g<package_ver>\.iso | ||
}x | ||
os = {} | ||
dbi_name = "guestadditions_#{dbi_safe_ver}_iso" | ||
when %r{ | ||
(?<package_ver> \d+\.\d+\.\d+ ){0} | ||
Oracle_VM_VirtualBox_Extension_Pack-\g<package_ver>\.vbox-extpack | ||
}x | ||
os = {} | ||
dbi_name = "extensionpack_#{dbi_safe_ver}" | ||
when %r{ | ||
(?<parch> (x86_64|i386) ){0} | ||
(?<pver> \d+\.\d+\.\d+ ){0} | ||
(?<distname> \w+ ){0} | ||
(?<distrelease> \d+ ){0} | ||
(?<build_num> \d+ ){0} | ||
(?<osrev> \d+ ){0} | ||
VirtualBox-\d+\.\d+-\g<pver>_\g<build_num>_\g<distname>\g<distrelease>-\g<osrev>\.\g<parch>\.rpm | ||
}x | ||
distname = $3 | ||
distrelease = $4 | ||
osrev = $6 | ||
os = {} | ||
os = { | ||
distname => | ||
[ | ||
distrelease | ||
] | ||
} | ||
dbi_name = "#{distname}#{distrelease}_#{osrev}_#{arch}_#{dbi_safe_ver}" | ||
when %r{ | ||
(?<parch> (amd64|i386) ){0} | ||
(?<pver> \d+\.\d+\.\d+ ){0} | ||
(?<distname> \w+ ){0} | ||
(?<build_num> \d+ ){0} | ||
virtualbox-\d+\.\d+_\g<pver>-\g<build_num>~Ubuntu~\g<distname>_\g<parch>\.deb | ||
}x | ||
pver = $1 | ||
build_num = $2 | ||
distname = $3 | ||
os = { | ||
'ubuntu' => | ||
[ | ||
case distname | ||
when /precise/ | ||
'12.04' | ||
when /quantal/ | ||
'12.10' | ||
end | ||
] | ||
} | ||
dbi_name = "ubuntu_#{distname}_#{arch}_#{dbi_safe_ver}_iso" | ||
else | ||
puts "XXX #{package_filename} not process" | ||
end | ||
|
||
# Let's put the files in our cache... might be useful later 8) | ||
cached_packagefile = ::File.join( | ||
Chef::Config[:file_cache_path],package_filename) | ||
if not ::File.exists? cached_packagefile | ||
# we should probably try to compare to SHA256 or something | ||
download_file(package_url) | ||
end | ||
|
||
# New we have all the artifact details | ||
artifacts[dbi_name] ||= Hash.new | ||
artifacts[dbi_name][:source] ||= package_url | ||
artifacts[dbi_name][:filename] ||= package_filename | ||
artifacts[dbi_name][:arch] ||= arch | ||
artifacts[dbi_name][:checksum] ||= checksum(cached_packagefile) | ||
artifacts[dbi_name][:version] ||= virtualbox_ver | ||
artifacts[dbi_name][:semantic_version] ||= semantic_ver | ||
artifacts[dbi_name][:os] ||= os | ||
end | ||
end | ||
|
||
#puts JSON.pretty_generate(artifacts) | ||
# Write out all data bag json | ||
data_bag_item_dir = ::File.join(Chef::Config[:data_bag_path],data_bag_name) | ||
Dir.mkdir data_bag_item_dir unless File.exists? data_bag_item_dir | ||
artifacts.each do |dbi,data| | ||
open(::File.join(data_bag_item_dir,dbi+'.json'),'w') do |f| | ||
f.write JSON.pretty_generate({id: dbi}.merge(data)) | ||
f.close | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Drop .chef/plugins/knife/source_ingredient_virtualbox.rb | ||
into your .chef/plugins/knife directory and run: | ||
|
||
``` | ||
knife source ingredient virtualbox | ||
``` | ||
|
||
:file_cache_path and :data_bag_path must writable and might need to be set in your knife.rb | ||
|
||
The latest available versions of virtualbox will be downloaded into | ||
your file_cache_path and your data bag path will get a virtualbox directory | ||
created which will be populated with data bag items for each version | ||
of vagrant that is available. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "el6_1_x86_64_4_2_8", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/VirtualBox-4.2-4.2.8_83876_el6-1.x86_64.rpm", | ||
"filename": "VirtualBox-4.2-4.2.8_83876_el6-1.x86_64.rpm", | ||
"arch": "x86_64", | ||
"checksum": "7a9c783a9aa724156124c635255143a37d61d61544f1535b05cf33906e073cd7", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
"el": [ | ||
"6" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "extensionpack_4_2_8", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/Oracle_VM_VirtualBox_Extension_Pack-4.2.8.vbox-extpack", | ||
"filename": "Oracle_VM_VirtualBox_Extension_Pack-4.2.8.vbox-extpack", | ||
"arch": [ | ||
"i686", | ||
"x86_64" | ||
], | ||
"checksum": "fa579416f382b58c4e93d3740d076ceba728e28d987e51aced5865a46cb9111c", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "guestadditions_4_2_8_iso", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/VBoxGuestAdditions_4.2.8.iso", | ||
"filename": "VBoxGuestAdditions_4.2.8.iso", | ||
"arch": [ | ||
"i686", | ||
"x86_64" | ||
], | ||
"checksum": "04a5402d8dcdefc83ffb2785351ddc57758781a3759137974469189392ae4ad5", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"id": "osx_4_2_8", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/VirtualBox-4.2.8-83876-OSX.dmg", | ||
"filename": "VirtualBox-4.2.8-83876-OSX.dmg", | ||
"arch": [ | ||
"i686", | ||
"x86_64" | ||
], | ||
"checksum": "e090da9766a1780a4f1fc6b3b85fe8b7ce71c8030d1fda9e7178716009d69a33", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
"mac_os_x": [ | ||
"10.7", | ||
"10.8" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "ubuntu_precise_x86_64_4_2_8_iso", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/virtualbox-4.2_4.2.8-83876~Ubuntu~precise_amd64.deb", | ||
"filename": "virtualbox-4.2_4.2.8-83876~Ubuntu~precise_amd64.deb", | ||
"arch": "x86_64", | ||
"checksum": "bf49e6b61dffd2130041550a79eb9347b79702874d1dccbe51bcacc8ecbfcfd0", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
"ubuntu": [ | ||
"12.04" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "ubuntu_quantal_x86_64_4_2_8_iso", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/virtualbox-4.2_4.2.8-83876~Ubuntu~quantal_amd64.deb", | ||
"filename": "virtualbox-4.2_4.2.8-83876~Ubuntu~quantal_amd64.deb", | ||
"arch": "x86_64", | ||
"checksum": "9e875b91f0ddaa9e5d08331e5b64d950ca6572b80caa8ff635d4cc0eed9476fc", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
"ubuntu": [ | ||
"12.10" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"id": "windows_4_2_8", | ||
"source": "http://dlc.sun.com.edgesuite.net/virtualbox/4.2.8/VirtualBox-4.2.8-83876-Win.exe", | ||
"filename": "VirtualBox-4.2.8-83876-Win.exe", | ||
"arch": [ | ||
"i686", | ||
"x86_64" | ||
], | ||
"checksum": "7864b538152b6ba672f94ac0e2be7e6a892d686bcf8035cd155e1fb1578ac77a", | ||
"version": "4.2.8", | ||
"semantic_version": "4.2.8", | ||
"os": { | ||
"windows": [ | ||
"2008r2", | ||
"2012", | ||
"7", | ||
"8" | ||
] | ||
} | ||
} |