Class: VmConfig
- Inherits:
-
Object
- Object
- VmConfig
- Defined in:
- lib/metadata/VmConfig/VmConfig.rb
Instance Attribute Summary collapse
-
#configFile ⇒ Object
readonly
Returns the value of attribute configFile.
Instance Method Summary collapse
- #dump_config_to_log(log_level = :warn) ⇒ Object
- #find_file(filename, file_list = files) ⇒ Object
-
#getAllDiskKeys ⇒ Object
getDiskFileHash.
- #getDiskFileHash ⇒ Object
- #getDriveImageList ⇒ Object
- #getHash ⇒ Object
- #getSnapshotDiskFileHash ⇒ Object
-
#getSnapshotDiskKeys ⇒ Object
getSnapshotDiskFileHash.
-
#initialize(filename) ⇒ VmConfig
constructor
A new instance of VmConfig.
- #postProcessDisks ⇒ Object
- #process_file(data_lines) ⇒ Object
- #set_vmconfig_path(config_path) ⇒ Object
- #to_xml(diskStats = true, miqvm = nil) ⇒ Object (also: #toXML)
- #vendor ⇒ Object
Constructor Details
#initialize(filename) ⇒ VmConfig
Returns a new instance of VmConfig.
15 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 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 15 def initialize(filename) @configFile = nil @configPath = nil @direct_file_access = true if filename.kind_of? Hash @direct_file_access = false @cfgHash = filename else @cfgHash = {} # If filename contains embedded new-line chars treat it as data. Otherwise, its a filename. if filename.index("\n") f = filename else set_vmconfig_path(filename) begin configType = File.extname(filename).delete(".").downcase require "metadata/VmConfig/#{configType}Config" rescue LoadError => e raise e, "Filetype unrecognized for file #{filename}" end extend Kernel.const_get(configType.capitalize + "Config") f = convert(filename) end process_file(f) f.close if f.class == File postProcessDisks end # Handle oddities configuration_fixup end |
Instance Attribute Details
#configFile ⇒ Object (readonly)
Returns the value of attribute configFile.
13 14 15 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 13 def configFile @configFile end |
Instance Method Details
#dump_config_to_log(log_level = :warn) ⇒ Object
241 242 243 244 245 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 241 def dump_config_to_log(log_level = :warn) $log.send(log_level, "VmConfig: Start of configuration info for: #{@configFile}") @cfgHash.each { |k, v| $log.send(log_level, "#{k} = #{v}") } $log.send(log_level, "VmConfig: End of configuration info for: #{@configFile}") end |
#find_file(filename, file_list = files) ⇒ Object
251 252 253 254 255 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 251 def find_file(filename, file_list = files) # basename = File.basename(filename) # return file_list.detect {|f| f[:name] == basename} file_list.detect { |f| f[:path] == filename } end |
#getAllDiskKeys ⇒ Object
getDiskFileHash
120 121 122 123 124 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 120 def getAllDiskKeys dskKeys = @cfgHash.keys.delete_if { |e| !diskKey?(e) }.collect! { |e| e.gsub(".filename", "") } dskKeys.delete_if { |dk| @cfgHash[dk + ".present"].to_s.downcase == "false" } dskKeys.sort end |
#getDiskFileHash ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 101 def getDiskFileHash return @diskFileHash if @diskFileHash @diskFileHash = {} getAllDiskKeys.each do |dk| next if @cfgHash[dk + ".devicetype"] && @cfgHash[dk + ".devicetype"].include?("cdrom") next if @cfgHash[dk + ".filename"] && @cfgHash[dk + ".filename"].downcase == "auto detect" filename = @cfgHash[dk + ".filename"] @diskFileHash[dk] = filename if @direct_file_access ds, _dir, _name = split_filename(filename) if ds.nil? && !Pathname.new(filename).absolute? @diskFileHash[dk] = File.(File.join(@configPath, filename)) end end end @diskFileHash end |
#getDriveImageList ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 215 def getDriveImageList vmDisks = [] # Convert config file to XML xml = toXML(false) # Find the disk section of the XML xml.root.each_recursive do |e| # Only process drives that are present and have a filename if (!e.attributes['present'].nil? && e.attributes['present'].downcase == 'true') && !e.attributes['filename'].nil? && (!e.attributes['type'].nil? && e.attributes['type'].downcase == 'disk') && (!e.attributes['id'].nil? && e.attributes['id'].include?(":")) # Make sure the disk we are looking at is not a CD-ROM if e.attributes['devicetype'].nil? || (!e.attributes['devicetype'].nil? && e.attributes['devicetype'].downcase.index("cd").nil?) diskName = e.attributes['filename'].tr("\"", "").strip.tr("\\", "/") diskName = File.join(@configPath, diskName) unless diskName[0..0] == "/" $log.debug "Adding Disk name to list: [#{diskName}]" begin vmDisks.push(File.(diskName)) rescue vmDisks.push(diskName) end $log.debug "Adding Disk name to list: [#{diskName}]" # $log.info "vmDisk Array = #{vmDisks.to_s()}" end end end vmDisks end |
#getHash ⇒ Object
97 98 99 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 97 def getHash @cfgHash end |
#getSnapshotDiskFileHash ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 126 def getSnapshotDiskFileHash return @snapshotdiskFileHash if @snapshotdiskFileHash # For snapshots we need to keep the disks order by snapshot uid, then by device @snapshotdiskFileHash = Hash.new { |h, k| h[k] = {} } getSnapshotDiskKeys.each do |dk| next if @cfgHash[dk + ".devicetype"].to_s.include?("cdrom") next if @cfgHash[dk + ".filename"].to_s.downcase == "auto detect" dk =~ /^snapshot\d+/ sn_uid = @cfgHash["#{$&}.uid"] key = @cfgHash[dk + ".node"] # Independent disks do not par-take in snapshots. Check the mode of the parent disk. next if @cfgHash["#{key}.mode"].to_s.include?('independent') @snapshotdiskFileHash[sn_uid]['disks'] ||= {} filename = @cfgHash[dk + ".filename"] disk_path = filename if @direct_file_access ds, _dir, _name = split_filename(filename) if ds.nil? && !Pathname.new(@cfgHash[dk + ".filename"]).absolute? disk_path = File.join(@configPath, filename) end end @snapshotdiskFileHash[sn_uid]['disks'][key] = disk_path end @snapshotdiskFileHash end |
#getSnapshotDiskKeys ⇒ Object
getSnapshotDiskFileHash
156 157 158 159 160 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 156 def getSnapshotDiskKeys dskKeys = @cfgHash.keys.delete_if { |e| !snapshotdiskKey?(e) }.collect! { |e| e.gsub(".filename", "") } dskKeys.delete_if { |dk| @cfgHash[dk + ".present"].to_s.downcase == "false" } dskKeys.sort end |
#postProcessDisks ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 68 def postProcessDisks # Convert absolute paths to relative paths for the disks if @direct_file_access getAllDiskKeys.each do |dk| next if @cfgHash[dk + ".devicetype"] && @cfgHash[dk + ".devicetype"].include?("cdrom") next if @cfgHash[dk + ".filename"] && @cfgHash[dk + ".filename"].downcase == "auto detect" begin dskPath = Pathname.new(@cfgHash[dk + ".filename"]) begin @cfgHash[dk + ".filename"] = dskPath.relative_path_from(Pathname.new(@configPath)).to_s.tr("\\", "/") if dskPath.absolute? rescue @cfgHash[dk + ".filename"] = dskPath.to_s.tr("\\", "/") end if self.respond_to?(:diskCreateType) createType = diskCreateType(@cfgHash[dk + ".filename"]) @cfgHash[dk + ".createType"] = createType if createType end if self.respond_to?(:diskControllerType) adapterType = diskControllerType(@cfgHash[dk + ".filename"]) @cfgHash[dk + ".adapterType"] = adapterType if adapterType end rescue => err $log.warn "VmConfig: Failed to convert path: #{@cfgHash[dk + ".filename"]}, #{err}" end end end end |
#process_file(data_lines) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 52 def process_file(data_lines) data_lines.each_line do |line| line.AsciiToUtf8!.strip! next if line.length == 0 next if line =~ /^#.*$/ next unless line.include?("=") k, v = line.split(/\s*=\s*/) # Note: All key names are lower-cased for easy lookup k = k.downcase v = v.gsub(/^"/, "").gsub(/"$/, "") @cfgHash[k] = v end end |
#set_vmconfig_path(config_path) ⇒ Object
162 163 164 165 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 162 def set_vmconfig_path(config_path) @configFile = normalize_vmconfig_file(config_path) @configPath = File.dirname(@configFile) end |
#to_xml(diskStats = true, miqvm = nil) ⇒ Object Also known as: toXML
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 167 def to_xml(diskStats = true, miqvm = nil) # If loaded directly from a hash object, try to add snapshot metadata before processing load_vim_snapshots(miqvm) set_vmconfig_path(miqvm.vmConfigFile) unless miqvm.nil? normalize_file_paths xml = MiqXml.createDoc("<vm_configuration><hardware></hardware><vm/></vm_configuration>", "vendor" => vendor) # Sort the Hash into an array to group like keys and add each array to the XML doc @cfgHash.sort { |a, b| a <=> b }.each do |(k, v)| next if k[0..0] == '.' insert_XML(k, v, xml) end # Pre-load data unless miqvm.nil? # Make sure we have the volume manager and loaded begin miqvm.rootTrees[0] @vol_mgr_loaded = true rescue LoadError $log.warn "add_disk_stats [#{$!.class}]-[#{$!}]" end files(miqvm) end # Add virtual disk sizes add_disk_stats(xml, miqvm) if diskStats # Here we list all files in the directory and get their size on disk. add_file_sizes(xml, miqvm) # Add partition and volume information add_volumes(xml, miqvm) # Add date and size info for snapshots add_snapshot_size(xml, miqvm) # TODO: Re-enable # normalize_path_names(xml) xml end |
#vendor ⇒ Object
247 248 249 |
# File 'lib/metadata/VmConfig/VmConfig.rb', line 247 def vendor "unknown" end |