Class: DiskIDCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
bin/diskid

Instance Method Summary collapse

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'bin/diskid', line 16

def execute

  if file.nil? or not File.exist?(file)
    puts
    puts "Usage: diskid path-to-disk-file"
    puts
    puts "Supported formats: vvfat vpc vmdk vdi sheepdog raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd dmg tftp ftps ftp https http cow cloop bochs blkverify blkdebug"
    exit 1
  end
  
  # Use local qemu-img to identify the disk
  if local?
    di = DiskID::Inspector.new(file, 'qemu-img').identify
    begin 
      puts di.send "to_#{format}"
    rescue NoMethodError
      $stderr.puts "Invalid format."
    end
  else
    begin
      RestClient.get "http://#{server_host}:#{server_port}/service-check"
    rescue => e
      $stderr.puts "Couldn't reach http://#{server_host}."
      exit 1
    end

    fsize = File.size(file).bytes.to.megabytes.to_f.round
    unit = 'M'
    if fsize > 1024
      fsize = fsize.megabytes.to.gigabytes.to_f.round
      unit = 'G'
    end

    fsize = "#{fsize}#{unit}"

    #out = `head -n 20 #{file}|curl -s -X POST -F client_version=#{DiskID::VERSION} -F chunk=@- -F file_size=#{fsize} -F file_name=#{File.basename(file)} http://#{server_host}:#{server_port}/?format=#{format} 2>&1`
    out = RestClient.post "http://#{server_host}:#{server_port}/",
                    :chunk => File.read(file, 20000),
                    :client_version => DiskID::VERSION,
                    :file_size => fsize,
                    :file_name => File.basename(file),
                    :format => format
    puts out
  end
end