Class: BoxGrinder::OVFCatalog

Inherits:
BasePlugin
  • Object
show all
Defined in:
lib/ovfcatalog-plugin.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ovfcatalog-plugin.rb', line 21

def execute
  @log.info "Uploading #{@appliance_config.name} to OVFCatalog..."
  begin
    #TODO move to a block
    upload_files(@previous_deliverables[:disk])
    @log.info "Appliance #{@appliance_config.name} uploaded."
  rescue => e
    @log.error e
    @log.error "An error occurred while uploading files."
  end
end

#guess_format(file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ovfcatalog-plugin.rb', line 77

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

#upload_files(disk_file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ovfcatalog-plugin.rb', line 33

def upload_files(disk_file)
  server_host = @plugin_config['host']
  server_port = @plugin_config['port']
  name = @plugin_config['name']
  ram = @plugin_config['ram']
  cpu = @plugin_config['cpu']
  category = @plugin_config['category']
  icon_path = @plugin_config['icon_path']
  description = "#{@appliance_config.name}-#{@appliance_config.version}.#{@appliance_config.release}-#{@appliance_config.os.name}-#{@appliance_config.os.version}-#{@appliance_config.hardware.arch}"
  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 "Name:".ljust(40) + name
  puts "Description:".ljust(40) + description
  puts "Category:".ljust(40) + category
  puts "RAM:".ljust(40) + "#{ram} MB"
  puts "CPU:".ljust(40) + cpu
  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', cpu),
              Curl::PostField.content('object.ram', ram),
              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

#validateObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/ovfcatalog-plugin.rb', line 10

def validate
  set_default_config_value('category', 'Misc')
  set_default_config_value('name', @appliance_config.name)
  set_default_config_value('description', "#{@appliance_config.name}-#{@appliance_config.version}.#{@appliance_config.release}-#{@appliance_config.os.name}-#{@appliance_config.os.version}-#{@appliance_config.hardware.arch}")
  set_default_config_value('port', '9000')
  set_default_config_value('ram', '512')
  set_default_config_value('cpu', '1')
  set_default_config_value('host', 'rs.bcn.abiquo.com')
  set_default_config_value('icon_path', 'http://rs.bcn.abiquo.com:9000/public/icons/q.png')
end