Module: Vmit::Utils

Defined in:
lib/vmit/utils.rb

Class Method Summary collapse

Class Method Details

.archObject



48
49
50
# File 'lib/vmit/utils.rb', line 48

def self.arch
  Cheetah.run('arch', :stdout => :capture).strip
end

.download_file(uri, target) ⇒ Object

Parameters:

  • uri (String)

    uri to download

  • target (String)

    where to donwload the file (directory)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/vmit/utils.rb', line 130

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



111
112
113
# File 'lib/vmit/utils.rb', line 111

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

.port_open?(host, port) ⇒ Boolean

Note:

uses nmap

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vmit/utils.rb', line 54

def self.port_open?(host, port)
  begin
    # use logger => nil until a sane way of handling
    # non zero return codes is implemented in cheetah
    # https://github.com/openSUSE/cheetah/pull/19
    Cheetah.run(['nmap', host, '-p',
                  port.to_s, '-sV', '--version-all', '-oG', '-'],
                ['grep', '-iq', "#{port}/open"], :logger => nil)
    true
  rescue Cheetah::ExecutionFailed
    false
  end
end

.random_mac_addressString

Returns random MAC address.

Returns:

  • (String)

    random MAC address



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

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



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vmit/utils.rb', line 115

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



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vmit/utils.rb', line 99

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

.wait_for_port(host, port, &block) ⇒ Object

Waits unntil that host port is open

Examples:

Vmit::Utils.wait_for_port('192.168.0.1', 22) do
  # do something
end


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

def self.wait_for_port(host, port, &block)
  chars = %w{ | / - \\ }
  if block
    thread = Thread.new(&block)
    thread.abort_on_exception = true
  end

  Vmit.logger.info "Waiting for machine port #{port}..."
  while true
    print chars[0]

    if port_open?(host, port)
      Thread.kill(thread) if thread
      break
    end
    break if thread && !thread.alive?
    sleep(1)
    print "\b"
    chars.push chars.shift
  end
  puts
end