Module: Vmit::Utils

Defined in:
lib/vmit/utils.rb

Class Method Summary collapse

Class Method Details

.download_file(uri, target) ⇒ Object

Parameters:

  • uri (String)

    uri to download

  • target (String)

    where to donwload the file (directory)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vmit/utils.rb', line 80

def self.download_file(uri, target)
  progress = ProgressBar.new(File.basename(uri.path), 100)
  Net::HTTP.start(uri.host) do |http|
    begin
      file = open(target, 'wb')
      http.request_get(uri.path) do |response|
        dl_size = response.content_length
        already_dl = 0
        response.read_body do |segment|
        already_dl += segment.length
        if(already_dl != 0)
          progress.set((already_dl * 100) / dl_size)
        end
        file.write(segment)
        end
      end
    ensure
      file.close
    end
  end
end

.kernel_version(bzimage) ⇒ Object



61
62
63
# File 'lib/vmit/utils.rb', line 61

def self.kernel_version(bzimage)
  uname(bzimage).split(' ')[0]
end

.random_mac_addressString

Returns random MAC address.

Returns:

  • (String)

    random MAC address



45
46
47
# File 'lib/vmit/utils.rb', line 45

def self.random_mac_address
  ("%02x"%((rand 64).to_i*4|2))+(0..4).inject(""){|s,x|s+":%02x"%(rand 256).to_i}
end

.sha1_file(filename) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vmit/utils.rb', line 65

def self.sha1_file(filename)
  sha1 = Digest::SHA1.new
  File.open(filename) do |file|
    buffer = ''
    # Read the file 512 bytes at a time
    while not file.eof
      file.read(512, buffer)
      sha1.update(buffer)
    end
  end
  sha1.to_s
end

.uname(bzimage) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vmit/utils.rb', line 49

def self.uname(bzimage)
  offset = 0
  File.open(bzimage) do |f|
    f.seek(0x20E)
    offset = f.read(2).unpack('s')[0]
    f.seek(offset + 0x200)
    ver = f.read(128).unpack('Z*')[0]
    return ver
  end
  nil
end