Class: Manageiq::BlackBox

Inherits:
Object
  • Object
show all
Defined in:
lib/blackbox/VmBlackBox.rb,
lib/blackbox/xmlStorage.rb

Constant Summary collapse

GLOBAL_CONFIG_FILE =
"/miq.yml"
METADATA_CONFIG_FILE =
"/metadata/xmldata.yml"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vmName, ost = nil) ⇒ BlackBox

Returns a new instance of BlackBox.



13
14
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
# File 'lib/blackbox/VmBlackBox.rb', line 13

def initialize(vmName, ost = nil)
  @config_name = vmName
  @write_data_externally = true    # For now we are always writing externally

  ost ||= OpenStruct.new

  if ost.miqVm
    @vmCfg = ost.miqVm.vm.vmConfig
  elsif ost.skipConfig == true
    @vmCfg = VmConfig.new({})
  else
    @vmCfg = VmConfig.new(@config_name)
  end

  # Get path to local data directory
  if ost.config && ost.config.dataDir
    @localDataDir = File.join(ost.config.dataDir, Digest::MD5.hexdigest(@config_name))
  elsif $miqHostCfg && $miqHostCfg.dataDir
    @localDataDir = File.join($miqHostCfg.dataDir, Digest::MD5.hexdigest(@config_name))
  else
    @localDataDir = "/tmp"
  end

  loadGlobalSettings

  @xmlData = loadXmlConfig
end

Class Method Details

.vmId(vmName) ⇒ Object



41
42
43
# File 'lib/blackbox/VmBlackBox.rb', line 41

def self.vmId(vmName)
  Manageiq::BlackBox.new(vmName).vmId
end

Instance Method Details

#closeObject



53
54
# File 'lib/blackbox/VmBlackBox.rb', line 53

def close
end

#get_dump_name(filename) ⇒ Object



33
34
35
36
37
# File 'lib/blackbox/xmlStorage.rb', line 33

def get_dump_name(filename)
  filepath = File.join(File.dirname(filename), "#{File.basename(filename, '.*')}.dmp")
  filepath.tr!('\\', '/')
  filepath
end

#loadXmlData(filename, ost = nil) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blackbox/xmlStorage.rb', line 39

def loadXmlData(filename, ost = nil)
  # Make sure we have a valid openstruct handle and "from_time" is in a valid format
  ost = OpenStruct.new if ost.nil?
  # Check 'from_time' value and possible remove from the open struct
  validate_from_time(ost)
  ret = OpenStruct.new(:items_selected => 0, :items_total => 0)

  ret.xml = MiqXml.createDoc("<vmmetadata/>")
  ret.xml.root.add_attributes("original_filename" => filename, "from_time" => ost.from_time.to_s, "taskid" => ost.taskid)
  if filename == "vmevents"
    # Change the name of the root element so the data does not go through state data processing.
    ret.xml.root.name = "vmevents"
    # ret.xml.root << @mk.view("events").find_range_by_hash(ost.from_time.nil? ? nil : {:timestamp=>ost.from_time}).to_xml.root
  else
    @xmlData.each do |x|
      if x[:docs].include?(filename.downcase)
        unless ret.last_scan
          ret.last_scan = x[:time]
        else
          ret.last_diff_scan = x[:time] unless ret.last_diff_scan
        end
        ret.items_total += 1

        # if we have a "from time" make sure we do not include anything older
        next if ost.from_time && ost.from_time.to_i > x[:time].to_i

        # load the xml and check what kind of xml we have (full or diff)
        xmlNode = getXmlFile(filename, x[:time])
        next if xmlNode.nil?
        xmlNode = xmlNode.root

        xml_type = getXmlType(xmlNode)

        # If we have a "from_time" we are not sending full scans
        next if ost.from_time && xml_type == "full"

        # Create a new "item" element for the xml and record the scan type
        e = ret.xml.root.add_element("item", {"scanType" => xml_type})
        e << xmlNode
        # Keep count of the number of items we add
        ret.items_selected += 1
      end
    end
  end
  ret.xml.root.add_attributes("items_selected" => ret.items_selected, "items_total" => ret.items_total,
              "last_scan" => ret.last_scan, "last_diff_scan" => ret.last_diff_scan)
  ret
end

#postSync(options = {}) ⇒ Object

This method is used to cleanup local storage data after it is sent to the server.



57
58
59
# File 'lib/blackbox/VmBlackBox.rb', line 57

def postSync(options = {})
  deleteLocalDataDir(options)
end

#saveXmlData(xml, filename, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blackbox/xmlStorage.rb', line 5

def saveXmlData(xml, filename, options = {})
  filename.downcase!
  options = {:saveDiff => true}.merge(options)

  xml.root.add_attribute("original_filename", filename)
  scanTime = xml.root.attributes['created_on']
  scanTime ||= xml.root.attributes[:created_on]
  scanTime = scanTime.to_s
  path = getXmlFileName(filename, scanTime)

  xml_prev = nil
  xml_prev = getLastXmlFile(filename) if options[:saveDiff]

  xml = xml.to_xml if xml.kind_of?(Hash)  # XmlHash
  if xml.kind_of?(Hash)  # XmlHash
    writeData(path, xml.to_xml.to_s)
    # TODO: Change to store xml ask Marshal dump file
    # path = get_dump_name(path)
    # data = Marshal.dump(xml)
    # File.open(path, "wb") {|f| f.write(data)}
  else
    writeData(path, xml.to_s)
  end
  addConfigFile(filename, scanTime)

  XmlDiffAndStore(filename, xml, xml_prev) if xml_prev
end

#vmIdObject



49
50
51
# File 'lib/blackbox/VmBlackBox.rb', line 49

def vmId
  @cfg[:vmId]
end

#vmId=(uuid) ⇒ Object



45
46
47
# File 'lib/blackbox/VmBlackBox.rb', line 45

def vmId=(uuid)
  saveGlobalValue(:vmId, uuid)
end