Statistics
| Branch: | Tag: | Revision:

one / src / onedb / onedb_live.rb @ 36906272

History | View | Annotate | Download (2.68 KB)

1

    
2
require 'opennebula'
3

    
4
class OneDBLive
5
    def initialize
6
        @client = nil
7
        @system = nil
8
    end
9

    
10
    def client
11
        @client ||= OpenNebula::Client.new
12
    end
13

    
14
    def system
15
        @system ||= OpenNebula::System.new(client)
16
    end
17

    
18
    def db_escape(string)
19
        string.gsub("'", "''")
20
    end
21

    
22
    def purge_history
23
        vmpool = OpenNebula::VirtualMachinePool.new(client)
24
        vmpool.info_all
25

    
26
        vmpool.each do |vm|
27
            # vmpool info only returns the last history record. We can check
28
            # if this VM can have more than one record using the sequence
29
            # number. If it's 0 or it does not exist we can skip the VM.
30
            # Also take tone that xpaths on VM info that comes from VMPool
31
            # or VM is different. We can not use absolute searches with
32
            # objects coming from pool.
33
            seq = vm['HISTORY_RECORDS/HISTORY/SEQ']
34
            next if !seq || seq == '0'
35

    
36
            # If the history can contain more than one record we get
37
            # all the info for two reasons:
38
            #
39
            #   * Make sure that all the info is written back
40
            #   * Refresh the information so it's less probable that the info
41
            #     was modified during this process
42
            vm.info
43

    
44
            hash = vm.to_hash
45
            val_history = hash['VM']['HISTORY_RECORDS']['HISTORY']
46

    
47
            if Array === val_history && val_history.size > 1
48
                last_history = val_history.last
49
                vm.delete_element('HISTORY_RECORDS/HISTORY')
50
                vm.add_element('HISTORY_RECORDS', 'HISTORY' => last_history)
51

    
52
                body = db_escape(vm.to_xml)
53
                sql = "UPDATE vm_pool SET body = '#{body}' WHERE oid = #{vm.id}"
54

    
55
                rc = system.sql_command(sql, false)
56
                if OpenNebula.is_error?(rc)
57
                    raise "Error updating record: #{rc.message}"
58
                end
59
            end
60
        end
61
    end
62

    
63
    def purge_done_vm
64
        vmpool = OpenNebula::VirtualMachinePool.new(client)
65
        vmpool.info(OpenNebula::Pool::INFO_ALL,
66
                    -1,
67
                    -1,
68
                    OpenNebula::VirtualMachine::VM_STATE.index('DONE'))
69

    
70
        vmpool.each do |vm|
71
            sql = "DELETE FROM vm_pool WHERE oid = #{vm.id}"
72

    
73
            rc = system.sql_command(sql, false)
74
            if OpenNebula.is_error?(rc)
75
                raise "Error deleting record: #{rc.message}"
76
            end
77

    
78
            sql = "DELETE FROM history WHERE vid = #{vm.id}"
79

    
80
            rc = system.sql_command(sql, false)
81
            if OpenNebula.is_error?(rc)
82
                raise "Error deleting record: #{rc.message}"
83
            end
84
        end
85
    end
86
end