Statistics
| Branch: | Tag: | Revision:

one / src / onedb / fsck / marketplaceapp.rb @ 8359d6ba

History | View | Annotate | Download (3.45 KB)

1

    
2
module OneDBFsck
3
    # Sets:
4
    #
5
    #   @data_marketplaceapp: used also by marketplace check
6
    #   @fixes_marketplaceapp: used by do_check_marketplaceapp
7

    
8
    def check_marketplaceapp
9
        @data_marketplaceapp = {
10
            marketplace: {}
11
        }
12

    
13
        marketplace = @data_marketplaceapp[:marketplace]
14

    
15
        # DATA: create marketplace hash with its name and empty apps array
16
        @db.fetch("SELECT oid, name FROM marketplace_pool") do |row|
17
            marketplace[row[:oid]] = {:name => row[:name], :apps => []}
18
        end
19

    
20
        @fixes_marketplaceapp = {}
21
        apps_fix = @fixes_marketplaceapp
22

    
23
        # DATA: go through all apps
24
        @db.fetch("SELECT oid,body FROM marketplaceapp_pool") do |row|
25
            doc = Document.new(row[:body])
26

    
27
            market_id   = doc.root.get_text('MARKETPLACE_ID').to_s.to_i
28
            market_name = doc.root.get_text('MARKETPLACE')
29

    
30
            ####################################################################
31
            # DATA: TODO, BUG: this code will only work for a standalone oned.
32
            # In a federation, the image ID will refer to a different image
33
            # in each zone
34
            ####################################################################
35

    
36
            # DATA: get image origin id. Does it work?
37
            origin_id = doc.root.get_text('ORIGIN_ID').to_s.to_i
38
            if origin_id >= 0 && doc.root.get_text('STATE').to_s.to_i == 2 # LOCKED
39
                counters[:image][origin_id][:app_clones].add(row[:oid])
40
            end
41

    
42
            ####################################################################
43
            #####################################################################
44

    
45
            if market_id != -1
46
                market_entry = marketplace[market_id]
47

    
48
                # DATA: CHECK: does marketplace for this app exist?
49
                if market_entry.nil?
50
                    log_error("Marketplace App #{row[:oid]} has marketplace #{market_id}, but it does not exist. The app is probably unusable, and needs to be deleted manually:\n"<<
51
                        "  * The DB entry can be deleted with the command:\n"<<
52
                        "    DELETE FROM marketplaceapp_pool WHERE oid=#{row[:oid]};\n"<<
53
                        "  * Run fsck again.\n", false)
54
                else
55
                    # DATA: CHECK: marketplace name is correct
56
                    if market_name != market_entry[:name]
57
                        log_error("Marketplace App #{row[:oid]} has a wrong name for marketplace #{market_id}, #{market_name}. It will be changed to #{market_entry[:name]}")
58

    
59
                        doc.root.each_element('MARKETPLACE') do |e|
60
                            e.text = market_entry[:name]
61
                        end
62

    
63
                        apps_fix[row[:oid]] = doc.root.to_s
64
                    end
65

    
66
                    # DATA: Add app to marketplace list. Used in marketplace check
67
                    market_entry[:apps] << row[:oid]
68
                end
69
            end
70
        end
71
    end
72

    
73
    def fix_marketplaceapp
74
        # DATA: FIX: fix marketplace app data
75
        if !db_version[:is_slave]
76
            @db.transaction do
77
                @fixes_marketplaceapp.each do |id, body|
78
                    @db[:marketplaceapp_pool].where(:oid => id).update(:body => body)
79
                end
80
            end
81
        elsif !@fixes_marketplaceapp.empty?
82
            log_msg("^ Marketplace App errors need to be fixed in the master OpenNebula")
83
        end
84
    end
85
end
86