Module: VmxConfig

Included in:
VmtxConfig
Defined in:
lib/metadata/VmConfig/vmxConfig.rb

Instance Method Summary collapse

Instance Method Details

#convert(filename) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 4

def convert(filename)
  # $log.debug "Processing VMware Configuration file [#{filename}]"
  fileData = File.read(filename)

  # Append the .vmsd file data.  Centralized file for storing information and metadata about snapshots.
  begin
    f = nil
    vmsd_filename = File.join(File.dirname(filename), File.basename(filename, ".*") + ".vmsd")
    f = File.open(vmsd_filename)
    fileData << convert_vmsd(f)
  rescue
  ensure
    f.close if f
  end
  fileData
end

#convert_vmsd(f) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 21

def convert_vmsd(f)
  # Only read the enabled snapshots from the file and ignore the rest
  numSnapshots = timeHigh = timeLow = nil
  knownSnapshots = []
  fileData = ""

  # First we need to find how many snapshots
  f.each_line do |line|
    # After that keep reading, but only store up to that many snapshots from the file
    if line =~ /numSnapshots = "(\d+)"/
      numSnapshots = $1.to_i
      break
    end
  end

  f.each_line do |line|
    if line =~ /^snapshot(\d+)/
      lastReadSnapshot = $1.to_i
      unless knownSnapshots.include?(lastReadSnapshot)
        next if knownSnapshots.length == numSnapshots
        knownSnapshots << lastReadSnapshot
      end

      # Check for createTimeHigh and createTimeLow and convert them
      #   The pack/unpack is needed so that the negative numbers get converted correctly
      skipLine = false

      if line =~ /createTimeHigh = \"(-?\d+)\"/
        timeHigh = [$1.to_i].pack('L').unpack('L')[0] << 32
        skipLine = true
      end

      if line =~ /createTimeLow = \"(-?\d+)\"/
        timeLow = [$1.to_i].pack('L').unpack('L')[0]
        skipLine = true
      end

      if timeHigh && timeLow
        begin
          create_time = Time.at(((timeHigh + timeLow) / 1000000.0)).getutc.iso8601(6)
          fileData << "snapshot#{lastReadSnapshot}.create_time = \"#{create_time}\"\n"
        rescue
        end
        timeHigh = timeLow = nil
      end

      next if skipLine
    end

    fileData << line
  end
  fileData
end

#diskAttribute(filename, attr) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 83

def diskAttribute(filename, attr)
  f = File.join(@configPath, filename)
  retVal = getDiskAttribute(f, attr)

  # If we are uable to get the data from the disk, check if this is
  # a snapshot disk and try to look at the base disk.
  if retVal.nil? && isSnapshotDisk(f)
    bf = getBaseDiskName(f)
    retVal = getDiskAttribute(bf, attr)
  end

  retVal
end

#diskControllerType(filename) ⇒ Object



79
80
81
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 79

def diskControllerType(filename)
  diskAttribute(filename, "adaptertype")
end

#diskCreateType(filename) ⇒ Object



75
76
77
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 75

def diskCreateType(filename)
  diskAttribute(filename, "createtype")
end

#getBaseDiskName(filename) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 117

def getBaseDiskName(filename)
  fn = File.basename(filename, File.extname(filename))
  if fn[-7, 1] == "-" && !fn[-6..-1].to_i.zero?
    return File.join(File.dirname(filename), fn[0...-7] + File.extname(filename))
  else
    return filename
  end
end

#getDiskAttribute(filename, attr) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 97

def getDiskAttribute(filename, attr)
  retVal = nil
  begin
    if File.exist?(filename)
      File.read(filename, 2048).each_line do |line|
        if line.downcase.include?(attr)
          retVal = line.split("=")[1].strip.tr("\"", "")
          break
        end
      end
    end
  rescue
  end
  retVal
end

#getScsiTypeObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 135

def getScsiType
  # scsiType = "lsilogic"
  scsiType = nil
  # Check scsi 0 to 3 for the Virtual Dev scsi value
  0.upto(3) do |i|
    if @cfgHash["scsi#{i}.virtualdev"]
      stype = @cfgHash["scsi#{i}.virtualdev"]
      scsiType = stype if ["lsilogic", "buslogic"].include?(stype.downcase)
      break
    end
  end

  # If we did not find the type on the adapter check the disks
  if scsiType.nil?
    type = @cfgHash.each_pair do |k, v|
      # Look for any disk with the adapterType set
      break(v) if k.downcase.include?("adaptertype") && ["lsilogic", "buslogic"].include?(v.downcase)
    end
    scsiType = type if type.kind_of?(String)
  end
  return "lsilogic" if scsiType.nil?
  scsiType
end

#getVmTypeObject



126
127
128
129
130
131
132
133
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 126

def getVmType
  type = @cfgHash.each_pair do |k, v|
    # Look for any disk with the create type of vmfs
    break(v) if k.downcase.include?("createtype") && v[0..3].downcase === "vmfs"
  end
  return "ESX" if type.kind_of?(String) && type[0..3].downcase == "vmfs"
  "Server"
end

#isSnapshotDisk(filename) ⇒ Object



113
114
115
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 113

def isSnapshotDisk(filename)
  getBaseDiskName(filename) != filename
end

#vendorObject



159
160
161
# File 'lib/metadata/VmConfig/vmxConfig.rb', line 159

def vendor
  "vmware"
end