Revision 36906272

View differences:

install.sh
1169 1169
            src/onedb/sqlite2mysql.rb \
1170 1170
            src/onedb/database_schema.rb \
1171 1171
            src/onedb/fsck \
1172
            src/onedb/actions"
1172
            src/onedb/onedb_live.rb"
1173 1173

  
1174 1174
ONEDB_SHARED_MIGRATOR_FILES="src/onedb/shared/2.0_to_2.9.80.rb \
1175 1175
                             src/onedb/shared/2.9.80_to_2.9.85.rb \
src/onedb/actions/purge_history.rb
1

  
2
require 'opennebula'
3

  
4
class OneDBAction
5
    class Base
6
        def initialize
7
            @client = nil
8
            @system = nil
9
        end
10

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

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

  
19
        def db_escape(string)
20
            string.gsub("'", "''")
21
        end
22
    end
23
end
24

  
25
class OneDBAction::PurgeHistory < OneDBAction::Base
26
    def run
27
        vmpool = OpenNebula::VirtualMachinePool.new(client)
28
        vmpool.info_all
29

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

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

  
48
            hash = vm.to_hash
49
            val_history = hash['VM']['HISTORY_RECORDS']['HISTORY']
50

  
51
            if Array === val_history && val_history.size > 1
52
                last_history = val_history.last
53
                vm.delete_element('HISTORY_RECORDS/HISTORY')
54
                vm.add_element('HISTORY_RECORDS', 'HISTORY' => last_history)
55

  
56
                body = db_escape(vm.to_xml)
57
                sql = "UPDATE vm_pool SET body = '#{body}' WHERE oid = #{vm.id}"
58

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

  
66
        0
67
    end
68
end
69

  
70
class OneDBAction::PurgeDoneVM < OneDBAction::Base
71
    def run
72
        vmpool = OpenNebula::VirtualMachinePool.new(client)
73
        vmpool.info(OpenNebula::Pool::INFO_ALL,
74
                    -1,
75
                    -1,
76
                    OpenNebula::VirtualMachine::VM_STATE.index('DONE'))
77

  
78
        vmpool.each do |vm|
79
            sql = "DELETE FROM vm_pool WHERE oid = #{vm.id}"
80

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

  
86
            sql = "DELETE FROM history WHERE vid = #{vm.id}"
87

  
88
            rc = system.sql_command(sql, false)
89
            if OpenNebula.is_error?(rc)
90
                raise "Error deleting record: #{rc.message}"
91
            end
92
        end
93

  
94
        0
95
    end
96
end
src/onedb/onedb
47 47

  
48 48
require 'cli/command_parser'
49 49
require 'onedb'
50
require 'onedb_live'
50 51
require 'opennebula'
51
require 'actions/purge_history'
52 52

  
53 53
FORCE={
54 54
    :name => "force",
......
425 425

  
426 426
    command :'purge-history' , purge_history_desc, :options=>[BACKUP] do
427 427
        begin
428
            action = OneDBAction::PurgeHistory.new
429
            action.run
428
            action = OneDBLive.new
429
            action.purge_history
430 430
        rescue Exception => e
431 431
            pp e.backtrace
432 432
            [-1, e.message]
433 433
        end
434

  
435
        0
434 436
    end
435 437

  
436 438
    ###########################################################################
......
442 444

  
443 445
    command :'purge-done' , purge_done_desc, :options=>[BACKUP] do
444 446
        begin
445
            action = OneDBAction::PurgeDoneVM.new
446
            action.run
447
            action = OneDBLive.new
448
            action.purge_done_vm
447 449
        rescue Exception => e
448 450
            pp e.backtrace
449 451
            [-1, e.message]
450 452
        end
453

  
454
        0 # exit code
451 455
    end
452 456
end
src/onedb/onedb_live.rb
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

Also available in: Unified diff