Created
November 17, 2011 18:24
-
-
Save rubiojr/1373991 to your computer and use it in GitHub Desktop.
Abiquo playrepo-uploader
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
#!/usr/bin/env ruby | |
# | |
# Needs ruby and rubygems installed | |
# | |
# Install deps first: | |
# | |
# apt-get install ruby rubygems | |
# | |
# gem install --no-ri --no-rdoc clamp curb diskid | |
# | |
# Usage: | |
# | |
# playrepo-uploader --name "Ubuntu Oneiric" \ | |
# --category "Operating Systems" \ | |
# --description "This is Ubuntu" \ | |
# ubuntu-oneiric.vmdk | |
# | |
require 'rubygems' | |
require 'curb' | |
require 'diskid' | |
require 'clamp' | |
class UploaderCommand < Clamp::Command | |
parameter 'FILE', "Disk file", :attribute_name => 'disk_file' | |
option '--server-port', "PORT", "PlayRepo service port", :default => '9000', :attribute_name => 'server_port' | |
option '--server-host', "HOST", "PlayRepo service host", :default => 'rs.bcn.abiquo.com', :attribute_name => 'server_host' | |
option '--name', "NAME", "Virtual template name" | |
option '--category', "CATEGORY", "Virtual template category", :default => "Misc" | |
option '--description', "DESCRIPTION", "Virtual template description", :default => '' | |
option '--icon-path', "URL", "Virtual template icon URL", | |
:default => "http://rs.bcn.abiquo.com:9000/public/icons/q.png", | |
:attribute_name => 'icon_path' | |
def guess_format(file) | |
format_map = { | |
"vmdk_streamOptimized" => "VMDK_STREAM_OPTIMIZED", | |
"qcow2" => "QCOW2_SPARSE", | |
"vmdk_monolithicSparse" => "VMDK_SPARSE" | |
} | |
f = DiskID::Client.identify(file) | |
fstring = '' | |
if f['format'] == 'vmdk' | |
fstring = format_map["vmdk_#{f['variant']}"] | |
elsif f['format'] == 'qcow2' | |
fstring = "QCOW2_SPARSE" | |
else | |
end | |
fstring | |
end | |
def execute | |
if disk_file.nil? or not File.exist?(disk_file) | |
puts "Invalid disk file" | |
exit 1 | |
end | |
disk_info = DiskID::Client.identify(disk_file) | |
units = disk_info['virtual_size'].gsub(/[0-9]|\./,'') | |
virtual_size = disk_info['virtual_size'].gsub(/G|M/,'') | |
if units == 'G' | |
vsbytes = virtual_size.to_f * 1024 * 1024 * 1024 | |
else | |
vsbytes = virtual_size.to_f * 1024 * 1024 | |
end | |
puts "Creating virtual image" | |
puts "Name:".ljust(40) + name | |
puts "Description:".ljust(40) + description | |
puts "Category:".ljust(40) + category | |
puts "RAM:".ljust(40) + "512 MB" | |
puts "CPU:".ljust(40) + "1" | |
puts "HD:".ljust(40) + "#{virtual_size} #{units} (#{vsbytes} bytes)" | |
puts "Filesize:".ljust(40) + "#{FileTest.size(disk_file)} bytes" | |
c = Curl::Easy.new "http://#{server_host}:#{server_port}/createOvf" | |
c.multipart_form_post = true | |
c.on_progress { |dt, dn, ut, un| print "\r\e[0KUploading disk: %.0f%" % ((un*100)/ut); true } | |
c.http_post Curl::PostField.content('object.diskFilePath', disk_file), | |
Curl::PostField.content('object.diskFileSize', FileTest.size(disk_file).to_s), | |
Curl::PostField.content('object.diskFileFormat', guess_format(disk_file)), | |
Curl::PostField.content('object.name', name), | |
Curl::PostField.content('object.description', description), | |
Curl::PostField.content('object.categoryName', category), | |
Curl::PostField.content('object.iconPath', icon_path), | |
Curl::PostField.content('object.cpu', "1"), | |
Curl::PostField.content('object.ram', "512"), | |
Curl::PostField.content('object.ramSizeUnit', "MB"), | |
Curl::PostField.content('object.hd', virtual_size), | |
Curl::PostField.content('object.hdSizeUnit', "#{units}B"), | |
Curl::PostField.content('object.hdInBytes', vsbytes.to_s ), | |
Curl::PostField.file('diskFile', disk_file) | |
puts | |
end | |
end | |
UploaderCommand.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment