Revision effd1ea5

View differences:

install.sh
1164 1164
            src/onedb/vcenter_one54.rb \
1165 1165
            src/onedb/sqlite2mysql.rb \
1166 1166
            src/onedb/database_schema.rb \
1167
            src/onedb/fsck"
1167
            src/onedb/fsck \
1168
            src/onedb/actions"
1168 1169

  
1169 1170
ONEDB_SHARED_MIGRATOR_FILES="src/onedb/shared/2.0_to_2.9.80.rb \
1170 1171
                             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
src/onedb/onedb
48 48
require 'cli/command_parser'
49 49
require 'onedb'
50 50
require 'opennebula'
51
require 'actions/purge_history'
51 52

  
52 53
FORCE={
53 54
    :name => "force",
......
414 415
            [-1, e.message]
415 416
        end
416 417
    end
418

  
419
    ###########################################################################
420
    # Purge history
421
    ###########################################################################
422
    purge_history_desc = <<-EOT.unindent
423
        Deletes all but the last history records from non DONE VMs
424
    EOT
425

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

  
417 436
end

Also available in: Unified diff