Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.74 KB)

1

    
2
module OneDBFsck
3
    # Needs:
4
    #
5
    #   @data_marketplaceapp: set by do_check_marketplaceapp
6
    #
7
    # Sets:
8
    #
9
    #   @fixes_marketplace: used by do_check_marketplace
10

    
11
    def check_marketplace
12
        marketplace = @data_marketplaceapp[:marketplace]
13

    
14
        @fixes_marketplace = {}
15
        markets_fix = {}
16

    
17
        # DATA: check marketplace pool
18
        @db.fetch("SELECT oid,body FROM marketplace_pool") do |row|
19
            market_id = row[:oid]
20
            doc = Nokogiri::XML(row[:body],nil,NOKOGIRI_ENCODING){|c| c.default_xml.noblanks}
21

    
22
            apps_elem = doc.root.at_xpath("MARKETPLACEAPPS")
23
            apps_elem.remove if !apps_elem.nil?
24

    
25
            apps_new_elem = doc.create_element("MARKETPLACEAPPS")
26
            doc.root.add_child(apps_new_elem)
27

    
28
            error = false
29

    
30
            # DATA: CHECK: are all apps in the marketplace?
31
            marketplace[market_id][:apps].each do |id|
32
                id_elem = apps_elem.at_xpath("ID[.=#{id}]")
33

    
34
                if id_elem.nil?
35
                    error = true
36

    
37
                    log_error(
38
                        "Marketplace App #{id} is missing from Marketplace #{market_id} "<<
39
                        "app id list")
40
                else
41
                    id_elem.remove
42
                end
43

    
44
                apps_new_elem.add_child(doc.create_element("ID")).content = id.to_s
45
            end
46

    
47
            # DATA: CHECK: listed apps that don't belong to the marketplace
48
            apps_elem.xpath("ID").each do |id_elem|
49
                error = true
50

    
51
                log_error(
52
                    "Marketplace App #{id_elem.text} is in Marketplace #{market_id} "<<
53
                    "app id list, but it should not")
54
            end
55

    
56
            zone_elem = doc.root.at_xpath("ZONE_ID")
57

    
58
            # DATA: CHECK: zone id
59
            if (zone_elem.nil? || zone_elem.text == "-1")
60
                error = true
61

    
62
                log_error("Marketplace #{market_id} has an invalid ZONE_ID. Will be set to 0")
63

    
64
                if (zone_elem.nil?)
65
                    zone_elem = doc.root.add_child(doc.create_element("ZONE_ID"))
66
                end
67

    
68
                zone_elem.content = "0"
69
            end
70

    
71
            if (error)
72
                @fixes_marketplace[row[:oid]] = doc.root.to_s
73
            end
74
        end
75
    end
76

    
77
    def fix_marketplace
78
        # DATA: FIX: update each marketplace that needs fixing
79
        if !db_version[:is_slave]
80
            @db.transaction do
81
                @fixes_marketplace.each do |id, body|
82
                    @db[:marketplace_pool].where(:oid => id).update(:body => body)
83
                end
84
            end
85
        elsif !@fixes_marketplace.empty?
86
            log_msg("^ Marketplace errors need to be fixed in the master OpenNebula")
87
        end
88
    end
89
end
90