<commit_before>require 'ostruct'
require 'metadata/VmConfig/VmConfig'
require 'disk/MiqDisk'
require 'VolumeManager/MiqVolumeManager'
require 'fs/MiqMountManager'
require 'metadata/MIQExtract/MIQExtract'

class MiqVm
    
    attr_reader :vmConfig, :vmConfigFile, :vim, :vimVm, :rhevm, :rhevmVm, :diskInitErrors, :wholeDisks
    
    def initialize(vmCfg, ost=nil)
        @ost = ost || OpenStruct.new
        $log.debug "MiqVm::initialize: @ost = nil" if $log && !@ost
        @vmDisks = nil
        @wholeDisks = []
        @vmRootTrees = nil
        @volumeManager = nil
        @applianceVolumeManager = nil
        @vmConfigFile = ""
        @diskInitErrors = {}
        unless vmCfg.kind_of?(Hash)
            @vmConfigFile = vmCfg
            @vmDir = File.dirname(vmCfg)
        end

        $log.debug "MiqVm::initialize: @ost.openParent = #{@ost.openParent}" if $log
        
        #
        # If we're passed an MiqVim object, then use VIM to obtain the Vm's
        # configuration through the instantiated server.
        # If we're passed a snapshot ID, then obtain the configration of the
        # VM when the snapshot was taken.
        #
        # TODO: move to MiqVmwareVm
        if (@vim = @ost.miqVim)
            $log.debug "MiqVm::initialize: accessing VM through server: #{@vim.server}" if $log.debug?
            @vimVm = @vim.getVimVm(vmCfg)
            $log.debug "MiqVm::initialize: setting @ost.miqVimVm = #{@vimVm.class}" if $log.debug?
            @ost.miqVimVm = @vimVm
            @vmConfig = VmConfig.new(@vimVm.getCfg(@ost.snapId))
        # TODO: move this to MiqRhevmVm.
        elsif (@rhevm = @ost.miqRhevm)
            $log.debug "MiqVm::initialize: accessing VM through RHEVM server" if $log.debug?
            $log.debug "MiqVm::initialize: vmCfg = #{vmCfg}"
            @rhevmVm = @rhevm.get_vm(vmCfg)
            $log.debug "MiqVm::initialize: setting @ost.miqRhevmVm = #{@rhevmVm.class}" if $log.debug?
            @ost.miqRhevmVm = @rhevmVm
            @vmConfig = VmConfig.new(getCfg(@ost.snapId))
            $log.debug "MiqVm::initialize: @vmConfig.getHash = #{@vmConfig.getHash.inspect}"
            $log.debug "MiqVm::initialize: @vmConfig.getDiskFileHash = #{@vmConfig.getDiskFileHash.inspect}"
        else
            @vimVm = nil
            @vmConfig = VmConfig.new(vmCfg)
        end
    end # def initialize
    
    def vmDisks
        @vmDisks ||= begin
            @volMgrPS = VolMgrPlatformSupport.new(@vmConfig.configFile, @ost)
            @volMgrPS.preMount

            openDisks(@vmConfig.getDiskFileHash)
        end
    end

    def openDisks(diskFiles)        
        pVolumes = Array.new
        
        $log.debug "openDisks: no disk files supplied." if !diskFiles
        
        #
        # Build a list of the VM's physical volumes.
        #
        diskFiles.each do |dtag, df|
            $log.debug "openDisks: processing disk file (#{dtag}): #{df}"
            dInfo = OpenStruct.new
            
            if @ost.miqVim
                dInfo.vixDiskInfo = Hash.new
                dInfo.vixDiskInfo[:fileName]    = @ost.miqVim.datastorePath(df)
                if @ost.miqVimVm && @ost.miqVim.isVirtualCenter?
                    @vdlConnection = @ost.miqVimVm.vdlVcConnection if !@vdlConnection
                    $log.debug "openDisks (VC): using disk file path: #{dInfo.vixDiskInfo[:fileName]}"
                else
                    @vdlConnection = @ost.miqVim.vdlConnection if !@vdlConnection
                    $log.debug "openDisks (ESX): using disk file path: #{dInfo.vixDiskInfo[:fileName]}"
                end
                dInfo.vixDiskInfo[:connection]  = @vdlConnection
            else
                dInfo.fileName = df
                disk_format = @vmConfig.getHash["#{dtag}.format"]  # Set by rhevm for iscsi and fcp disks
                dInfo.format = disk_format unless disk_format.blank?
            end

            mode = @vmConfig.getHash["#{dtag}.mode"]
            
            dInfo.hardwareId = dtag
            dInfo.baseOnly = @ost.openParent unless mode && mode["independent"]
            dInfo.rawDisk = @ost.rawDisk # force raw disk for testing
            $log.debug "MiqVolumeManager::openDisks: dInfo.baseOnly = #{dInfo.baseOnly}"
            
            begin
                d = applianceVolumeManager && applianceVolumeManager.lvHash[dInfo.fileName] if @rhevm
                if d
                    $log.debug "MiqVolumeManager::openDisks: using applianceVolumeManager for #{dInfo.fileName}" if $log.debug?
                    d.dInfo.fileName = dInfo.fileName
                    d.dInfo.hardwareId = dInfo.hardwareId<commit_msg>Modified some log messages, based on review comments.<commit_after>require 'ostruct'
require 'metadata/VmConfig/VmConfig'
require 'disk/MiqDisk'
require 'VolumeManager/MiqVolumeManager'
require 'fs/MiqMountManager'
require 'metadata/MIQExtract/MIQExtract'

class MiqVm
    
    attr_reader :vmConfig, :vmConfigFile, :vim, :vimVm, :rhevm, :rhevmVm, :diskInitErrors, :wholeDisks
    
    def initialize(vmCfg, ost=nil)
        @ost = ost || OpenStruct.new
        $log.debug "MiqVm::initialize: @ost = nil" if $log && !@ost
        @vmDisks = nil
        @wholeDisks = []
        @vmRootTrees = nil
        @volumeManager = nil
        @applianceVolumeManager = nil
        @vmConfigFile = ""
        @diskInitErrors = {}
        unless vmCfg.kind_of?(Hash)
            @vmConfigFile = vmCfg
            @vmDir = File.dirname(vmCfg)
        end

        $log.debug "MiqVm::initialize: @ost.openParent = #{@ost.openParent}" if $log
        
        #
        # If we're passed an MiqVim object, then use VIM to obtain the Vm's
        # configuration through the instantiated server.
        # If we're passed a snapshot ID, then obtain the configration of the
        # VM when the snapshot was taken.
        #
        # TODO: move to MiqVmwareVm
        if (@vim = @ost.miqVim)
            $log.debug "MiqVm::initialize: accessing VM through server: #{@vim.server}" if $log.debug?
            @vimVm = @vim.getVimVm(vmCfg)
            $log.debug "MiqVm::initialize: setting @ost.miqVimVm = #{@vimVm.class}" if $log.debug?
            @ost.miqVimVm = @vimVm
            @vmConfig = VmConfig.new(@vimVm.getCfg(@ost.snapId))
        # TODO: move this to MiqRhevmVm.
        elsif (@rhevm = @ost.miqRhevm)
            $log.debug "MiqVm::initialize: accessing VM through RHEVM server" if $log.debug?
            $log.debug "MiqVm::initialize: vmCfg = #{vmCfg}"
            @rhevmVm = @rhevm.get_vm(vmCfg)
            $log.debug "MiqVm::initialize: setting @ost.miqRhevmVm = #{@rhevmVm.class}" if $log.debug?
            @ost.miqRhevmVm = @rhevmVm
            @vmConfig = VmConfig.new(getCfg(@ost.snapId))
            $log.debug "MiqVm::initialize: @vmConfig.getHash = #{@vmConfig.getHash.inspect}"
            $log.debug "MiqVm::initialize: @vmConfig.getDiskFileHash = #{@vmConfig.getDiskFileHash.inspect}"
        else
            @vimVm = nil
            @vmConfig = VmConfig.new(vmCfg)
        end
    end # def initialize
    
    def vmDisks
        @vmDisks ||= begin
            @volMgrPS = VolMgrPlatformSupport.new(@vmConfig.configFile, @ost)
            @volMgrPS.preMount

            openDisks(@vmConfig.getDiskFileHash)
        end
    end

    def openDisks(diskFiles)        
        pVolumes = Array.new
        
        $log.debug "openDisks: no disk files supplied." if !diskFiles
        
        #
        # Build a list of the VM's physical volumes.
        #
        diskFiles.each do |dtag, df|
            $log.debug "openDisks: processing disk file (#{dtag}): #{df}"
            dInfo = OpenStruct.new
            
            if @ost.miqVim
                dInfo.vixDiskInfo = Hash.new
                dInfo.vixDiskInfo[:fileName]    = @ost.miqVim.datastorePath(df)
                if @ost.miqVimVm && @ost.miqVim.isVirtualCenter?
                    @vdlConnection = @ost.miqVimVm.vdlVcConnection if !@vdlConnection
                    $log.debug "openDisks (VC): using disk file path: #{dInfo.vixDiskInfo[:fileName]}"
                else
                    @vdlConnection = @ost.miqVim.vdlConnection if !@vdlConnection
                    $log.debug "openDisks (ESX): using disk file path: #{dInfo.vixDiskInfo[:fileName]}"
                end
                dInfo.vixDiskInfo[:connection]  = @vdlConnection
            else
                dInfo.fileName = df
                disk_format = @vmConfig.getHash["#{dtag}.format"]  # Set by rhevm for iscsi and fcp disks
                dInfo.format = disk_format unless disk_format.blank?
            end

            mode = @vmConfig.getHash["#{dtag}.mode"]
            
            dInfo.hardwareId = dtag
            dInfo.baseOnly = @ost.openParent unless mode && mode["independent"]
            dInfo.rawDisk = @ost.rawDisk # force raw disk for testing
            $log.debug "MiqVm::openDisks: dInfo.baseOnly = #{dInfo.baseOnly}"
            
            begin
                d = applianceVolumeManager && applianceVolumeManager.lvHash[dInfo.fileName] if @rhevm
                if d
                    $log.debug "MiqVm::openDisks: using applianceVolumeManager for #{dInfo.fileName}" if $log.debug?
                    d.dInfo.fileName = dInfo.fileName
                    d.dInfo.hardwareId = dInfo.hardwareId
{'fraction_non_alphanumeric': 0.10852381969664601, 'fraction_numerical': 0.0, 'mean_word_length': 2.0, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1340488', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2966, 'n_words': 636}