diff --git a/src/onedb/fsck.rb b/src/onedb/fsck.rb index 53501eb9e3..aa5494f978 100644 --- a/src/onedb/fsck.rb +++ b/src/onedb/fsck.rb @@ -302,7 +302,7 @@ EOT end end - def fsck + def fsck(dry = false) # ---------------------------------------------------------------------- # Read existing UIDs and GIDs # ---------------------------------------------------------------------- @@ -328,7 +328,7 @@ EOT check_pool_control - fix_pool_control + fix_pool_control unless dry log_time() @@ -345,12 +345,12 @@ EOT ######################################################################## check_user - fix_user + fix_user unless dry log_time check_group - fix_group + fix_group unless dry log_time @@ -381,25 +381,25 @@ EOT init_cluster check_host_cluster - fix_host_cluster + fix_host_cluster unless dry log_time check_datastore_cluster - fix_datastore_cluster + fix_datastore_cluster unless dry log_time check_network_cluster - fix_network_cluster + fix_network_cluster unless dry log_time - check_fix_cluster + check_cluster + fix_cluster unless dry - log_time - - check_fix_cluster_relations + check_cluster_relations + fix_cluster_relations unless dry log_time @@ -419,11 +419,12 @@ EOT log_time check_datastore_image - fix_datastore_image + fix_datastore_image unless dry log_time - check_fix_datastore + check_datastore + fix_datastore unless dry log_time @@ -448,7 +449,7 @@ EOT log_time check_vm - fix_vm + fix_vm unless dry log_time @@ -458,14 +459,14 @@ EOT # DATA: VNC Bitmap check_cluster_vnc_bitmap - fix_cluster_vnc_bitmap + fix_cluster_vnc_bitmap unless dry log_time # history check_history - fix_history + fix_history unless dry log_time @@ -476,7 +477,7 @@ EOT ######################################################################## check_vrouter - fix_vrouter + fix_vrouter unless dry log_time @@ -490,7 +491,7 @@ EOT ######################################################################## check_host - fix_host + fix_host unless dry log_time @@ -508,13 +509,13 @@ EOT check_marketplaceapp - fix_marketplaceapp + fix_marketplaceapp unless dry log_time() check_marketplace - fix_marketplace + fix_marketplace unless dry log_time() @@ -535,7 +536,7 @@ EOT check_image - fix_image + fix_image unless dry log_time @@ -548,7 +549,7 @@ EOT init_network_lease_counters check_network - fix_network + fix_network unless dry log_time @@ -558,9 +559,11 @@ EOT # USER QUOTAS ######################################################################## - check_fix_quotas('user') + @fix_quotas = {} - check_fix_quotas('user', 'running') + check_quotas('user') + + fix_quotas('user') unless dry log_time ######################################################################## @@ -569,9 +572,9 @@ EOT # GROUP QUOTAS ######################################################################## - check_fix_quotas('group') + check_quotas('group') - check_fix_quotas('group', 'running') + fix_quotas('group') unless dry log_time @@ -583,7 +586,7 @@ EOT check_template - fix_template + fix_template unless dry log_time @@ -593,12 +596,12 @@ EOT check_scheduled_actions - fix_scheduled_actions + fix_scheduled_actions unless dry log_time # Log results - log_total_errors + log_total_errors(dry) return true end @@ -628,11 +631,17 @@ EOT @log_file.flush end - def log_total_errors() + def log_total_errors(dry) puts log_msg "Total errors found: #{@errors}" - log_msg "Total errors repaired: #{@repaired_errors}" - log_msg "Total errors unrepaired: #{@unrepaired_errors}" + if dry + log_msg "Total repairable errors: #{@repaired_errors}" + log_msg "Total unrepairable errors: #{@unrepaired_errors}" + log_msg "Dry run, no changes have been written to the DB" + else + log_msg "Total errors repaired: #{@repaired_errors}" + log_msg "Total errors unrepaired: #{@unrepaired_errors}" + end puts "A copy of this output was stored in #{LOG}" end diff --git a/src/onedb/fsck/cluster.rb b/src/onedb/fsck/cluster.rb index b043d05072..f2d89d3f98 100644 --- a/src/onedb/fsck/cluster.rb +++ b/src/onedb/fsck/cluster.rb @@ -4,6 +4,11 @@ module OneDBFsck # Gather clusters information def init_cluster cluster = @data_cluster = {} + @fix_cluster = {} + @add_ds_relation = {} + @add_vnet_relation = {} + @del_ds_relation = {} + @del_vnet_relation = {} @db.fetch('SELECT oid, name FROM cluster_pool') do |row| cluster[row[:oid]] = {} @@ -16,36 +21,55 @@ module OneDBFsck end # Check and fix clusters information - def check_fix_cluster + def check_cluster cluster = @data_cluster - create_table(:cluster_pool, :cluster_pool_new) + @db.fetch('SELECT oid,body from cluster_pool') do |row| + cluster_id = row[:oid] + doc = nokogiri_doc(row[:body], 'cluster_pool') + + @error = false + + # Hosts + process_hosts(doc, cluster_id, cluster[cluster_id][:hosts]) + + # Datastores + process_ds(doc, cluster_id, cluster[cluster_id][:datastores]) + + # VNets + process_vnets(doc, cluster_id, cluster[cluster_id][:vnets]) + + @fix_cluster[cluster_id] = doc.root.to_s if @error + end + end + + def fix_cluster @db.transaction do - @db.fetch('SELECT * from cluster_pool') do |row| - cluster_id = row[:oid] - - doc = nokogiri_doc(row[:body], 'cluster_pool') - - # Hosts - process_hosts(doc, cluster_id, cluster[cluster_id][:hosts]) - - # Datastores - process_ds(doc, cluster_id, cluster[cluster_id][:datastores]) - - # VNets - process_vnets(doc, cluster_id, cluster[cluster_id][:vnets]) - - row[:body] = doc.root.to_s - - # commit - @db[:cluster_pool_new].insert(row) + @fix_cluster.each do |id, body| + @db[:cluster_pool].where(:oid => id).update(:body => body) end end + end - # Rename table - @db.run('DROP TABLE cluster_pool') - @db.run('ALTER TABLE cluster_pool_new RENAME TO cluster_pool') + def fix_cluster_relations + @db.transaction do + @add_ds_relation.each do |cid, oid| + @db[:cluster_datastore_relation].insert(:cid => cid, :oid => oid) + end + + @add_vnet_relation.each do |cid, oid| + @db[:cluster_network_relation].insert(:cid => cid, :oid => oid) + end + + @del_ds_relation.each do |cid, oid| + @db[:cluster_datastore_relation].where(:cid => cid, :oid => oid).delete + end + + @del_vnet_relation.each do |cid, oid| + @db[:cluster_network_relation].where(:cid => cid, :oid => oid).delete + end + end end # Process cluster hosts @@ -65,6 +89,7 @@ module OneDBFsck if id_elem.empty? log_error("Host #{id} is missing from cluster " \ "#{cid} host id list") + @error = true end aux = doc.create_element('ID') @@ -77,6 +102,7 @@ module OneDBFsck log_error("Host #{id} is in cluster #{cid} " \ 'host id list, but it should not') + @error = true end end @@ -97,6 +123,7 @@ module OneDBFsck if id_elem.empty? log_error("Datastore #{id} is missing from cluster " \ "#{cid} datastore id list") + @error = true end aux = doc.create_element('ID') @@ -109,7 +136,7 @@ module OneDBFsck log_error('Table cluster_datastore_relation is missing relation ' \ "cluster #{cid}, datastore #{id}") - @db[:cluster_datastore_relation].insert(:cid => cid, :oid => id) + @add_ds_relation[cid] = id end ds_elem.children.each do |id| @@ -117,6 +144,8 @@ module OneDBFsck log_error("Datastore #{id} is in cluster #{cid} datastore id " \ 'list, but it should not') + + @error = true end end @@ -137,6 +166,7 @@ module OneDBFsck if id_elem.empty? log_error("VNet #{id} is missing from cluster #{cid} " \ 'vnet id list') + @error = true end aux = doc.create_element('ID') @@ -149,7 +179,7 @@ module OneDBFsck log_error('Table cluster_network_relation is missing relation ' \ "cluster #{cid}, vnet #{id}") - @db[:cluster_network_relation].insert(:cid => cid, :oid => id) + @add_vnet_relation[cid] = id end vnets_elem.children.each do |id| @@ -157,55 +187,34 @@ module OneDBFsck log_error("VNet #{id} is in cluster #{cid} vnet id list, " \ 'but it should not') + @error = true end end # Check and fix cluster relations with datastores and VNets - def check_fix_cluster_relations + def check_cluster_relations cluster = @data_cluster - @db.transaction do - create_table(:cluster_datastore_relation, - :cluster_datastore_relation_new) - - @db.fetch('SELECT * from cluster_datastore_relation') do |row| - if !cluster[row[:cid]] || cluster[row[:cid]][:datastores].count( - row[:oid] - ) != 1 - log_error('Table cluster_datastore_relation contains ' \ - "relation cluster #{row[:cid]}, datastore " \ - "#{row[:oid]}, but it should not") - else - @db[:cluster_datastore_relation_new].insert(row) - end + @db.fetch('SELECT * from cluster_datastore_relation') do |row| + if !cluster[row[:cid]] || cluster[row[:cid]][:datastores].count( + row[:oid] + ) != 1 + log_error('Table cluster_datastore_relation contains ' \ + "relation cluster #{row[:cid]}, datastore " \ + "#{row[:oid]}, but it should not") + @del_ds_relation[row[:cid]] = row[:oid] end - - @db.run('DROP TABLE cluster_datastore_relation') - @db.run('ALTER TABLE cluster_datastore_relation_new ' \ - 'RENAME TO cluster_datastore_relation') end - log_time - - @db.transaction do - create_table(:cluster_network_relation, - :cluster_network_relation_new) - - @db.fetch('SELECT * from cluster_network_relation') do |row| - if !cluster[row[:cid]] || cluster[row[:cid]][:vnets].count( - row[:oid] - ) != 1 - log_error('Table cluster_network_relation contains ' \ - "relation cluster #{row[:cid]}, " \ - "vnet #{row[:oid]}, but it should not") - else - @db[:cluster_network_relation_new].insert(row) - end + @db.fetch('SELECT * from cluster_network_relation') do |row| + if !cluster[row[:cid]] || cluster[row[:cid]][:vnets].count( + row[:oid] + ) != 1 + log_error('Table cluster_network_relation contains ' \ + "relation cluster #{row[:cid]}, " \ + "vnet #{row[:oid]}, but it should not") + @del_vnet_relation[row[:cid]] = row[:oid] end - - @db.run('DROP TABLE cluster_network_relation') - @db.run('ALTER TABLE cluster_network_relation_new ' \ - 'RENAME TO cluster_network_relation') end end diff --git a/src/onedb/fsck/datastore.rb b/src/onedb/fsck/datastore.rb index eca174b209..22b6784cc8 100644 --- a/src/onedb/fsck/datastore.rb +++ b/src/onedb/fsck/datastore.rb @@ -11,55 +11,48 @@ module OneDBFsck end # Check datastores images and fix it - def check_fix_datastore + def check_datastore datastore = @data_datastore + @fixes_ds = {} - create_table(:datastore_pool, :datastore_pool_new) + @db.fetch('SELECT oid, body FROM datastore_pool') do |row| + ds_id = row[:oid] + doc = nokogiri_doc(row[:body], 'datastore_pool') - @db.transaction do - @db.fetch('SELECT * from datastore_pool') do |row| - ds_id = row[:oid] - doc = nokogiri_doc(row[:body], 'datastore_pool') - images_elem = doc.root.xpath('IMAGES').remove - images_new_elem = doc.create_element('IMAGES') + update = fix_permissions('DATASTORE', row[:oid], doc) - check_ugid(doc) + images_elem = doc.root.xpath('IMAGES').remove + images_new_elem = doc.create_element('IMAGES') - doc.root.add_child(images_new_elem) + check_ugid(doc) - datastore[ds_id][:images].each do |id| - id_elem = images_elem.xpath("ID[.=#{id}]").remove + doc.root.add_child(images_new_elem) - if id_elem.nil? - log_error( - "Image #{id} is missing from Datastore #{ds_id} " \ - 'image id list' - ) - end + datastore[ds_id][:images].each do |id| + id_elem = images_elem.xpath("ID[.=#{id}]").remove - i_e = doc.create_element('ID') - images_new_elem.add_child(i_e).content = id.to_s - end - - images_elem.children.each do |id_elem| + if id_elem.nil? log_error( - "Image #{id_elem.text} is in Datastore #{ds_id} " \ - 'image id list, but it should not' + "Image #{id} is missing from Datastore #{ds_id} " \ + 'image id list' ) + update = true end - fix_permissions('DATASTORE', row[:oid], doc) - - row[:body] = doc.root.to_s - - # commit - @db[:datastore_pool_new].insert(row) + i_e = doc.create_element('ID') + images_new_elem.add_child(i_e).content = id.to_s end - end - # Rename table - @db.run('DROP TABLE datastore_pool') - @db.run('ALTER TABLE datastore_pool_new RENAME TO datastore_pool') + images_elem.children.each do |id_elem| + log_error( + "Image #{id_elem.text} is in Datastore #{ds_id} " \ + 'image id list, but it should not' + ) + update = true + end + + @fixes_ds[ds_id] = doc.root.to_s if update + end end # Check datastores clusters @@ -81,7 +74,7 @@ module OneDBFsck e.remove - @fixes_datastore_cluster[oid] = { :body => doc.root.to_s } + @fixes_datastore_cluster[oid] = doc.root.to_s else cluster_entry[:datastores] << oid end @@ -131,12 +124,19 @@ module OneDBFsck end end + # Fix datastores clusters + def fix_datastore + @db.transaction do + @fixes_ds.each do |id, body| + @db[:datastore_pool].where(:oid => id).update(:body => body) + end + end + end + # Fix datastores clusters def fix_datastore_cluster @db.transaction do - @fixes_datastore_cluster.each do |id, entry| - body = entry[:body] - + @fixes_datastore_cluster.each do |id, body| @db[:datastore_pool].where(:oid => id).update(:body => body) end end diff --git a/src/onedb/fsck/history.rb b/src/onedb/fsck/history.rb index b8f762f57d..cbe0cb7069 100644 --- a/src/onedb/fsck/history.rb +++ b/src/onedb/fsck/history.rb @@ -12,6 +12,10 @@ module OneDBFsck log_time check_history_retime + + log_error('Removing possibly corrupted records from VM showback '\ + "please run 'oneshowback calculate` to recalculate "\ + 'the showback') unless @showback_delete.empty? end # Check that etime from non last seq is 0 @@ -151,13 +155,6 @@ module OneDBFsck end end - # DATA: FIX: Remove possibly corrupte showback values - unless @showback_delete.empty? - log_error('Removing possibly corrupted records from VM showback '\ - "please run 'oneshowback calculate` to recalculate "\ - 'the showback') - end - @db.transaction do @showback_delete.each do |vid| @db[:vm_showback].where(:vmid => vid).delete diff --git a/src/onedb/fsck/image.rb b/src/onedb/fsck/image.rb index 76887ff5d6..85c80737dd 100644 --- a/src/onedb/fsck/image.rb +++ b/src/onedb/fsck/image.rb @@ -39,6 +39,8 @@ module OneDBFsck check_ugid(doc) + error = fix_permissions('IMAGE', row[:oid], doc) + persistent = ( doc.root.xpath('PERSISTENT').text == "1" ) current_state = doc.root.xpath('STATE').text.to_i @@ -53,6 +55,7 @@ module OneDBFsck if e.text != rvms.to_s log_error("Image #{oid} RUNNING_VMS has #{e.text} \tis\t#{rvms}") e.text = rvms + error = true end } @@ -69,8 +72,9 @@ module OneDBFsck counters_img[:vms].each do |id| id_elem = vms_elem.xpath("ID[.=#{id}]").remove - if id_elem.nil? + if id_elem.empty? log_error("VM #{id} is missing from Image #{oid} VM id list") + error = true end i_e = doc.create_element('ID') @@ -79,6 +83,7 @@ module OneDBFsck vms_elem.children.each do |id_elem| log_error("VM #{id_elem.text} is in Image #{oid} VM id list, but it should not") + error = true end end @@ -94,6 +99,7 @@ module OneDBFsck if e.text != n_cloning_ops.to_s log_error("Image #{oid} CLONING_OPS has #{e.text} \tis\t#{n_cloning_ops}") e.text = n_cloning_ops + error = true end } @@ -109,6 +115,7 @@ module OneDBFsck if id_elem.nil? log_error("Image #{id} is missing from Image #{oid} CLONES id list") + error = true end i_e = doc.create_element('ID') @@ -117,6 +124,7 @@ module OneDBFsck clones_elem.children.each do |id_elem| log_error("Image #{id_elem.text} is in Image #{oid} CLONES id list, but it should not") + error = true end # re-do list of Apps cloning this one @@ -132,6 +140,7 @@ module OneDBFsck if id_elem.nil? log_error("Marketplace App #{id} is missing from Image #{oid} APP_CLONES id list") + error = true end i_e = doc.create_element('ID') @@ -140,6 +149,7 @@ module OneDBFsck clones_elem.children.each do |id_elem| log_error("Marketplace App #{id_elem.text} is in Image #{oid} APP_CLONES id list, but it should not") + error = true end @@ -176,26 +186,16 @@ module OneDBFsck OpenNebula::Image::IMAGE_STATES[e.text.to_i] << " \tis\t#{OpenNebula::Image::IMAGE_STATES[state]}") e.text = state + error = true end } - fix_permissions('IMAGE', row[:oid], doc) - - # row[:body] = doc.root.to_s - - # # commit - # @db[:image_pool_new].insert(row) - - @fixes_image[oid] = doc.root.to_s + @fixes_image[oid] = doc.root.to_s if error end end end def fix_image - # # Rename table - # @db.run("DROP TABLE image_pool") - # @db.run("ALTER TABLE image_pool_new RENAME TO image_pool") - @fixes_image.each do |oid, body| @db[:image_pool].where(oid: oid).update(body: body) end diff --git a/src/onedb/fsck/marketplace.rb b/src/onedb/fsck/marketplace.rb index f7cd539b78..3a816ecfef 100644 --- a/src/onedb/fsck/marketplace.rb +++ b/src/onedb/fsck/marketplace.rb @@ -21,14 +21,14 @@ module OneDBFsck check_ugid(doc) + error = fix_permissions('MARKETPLACE', row[:oid], doc) + apps_elem = doc.root.at_xpath("MARKETPLACEAPPS") apps_elem.remove if !apps_elem.nil? apps_new_elem = doc.create_element("MARKETPLACEAPPS") doc.root.add_child(apps_new_elem) - error = false - # DATA: CHECK: are all apps in the marketplace? marketplace[market_id][:apps].each do |id| id_elem = apps_elem.at_xpath("ID[.=#{id}]") @@ -70,11 +70,7 @@ module OneDBFsck zone_elem.content = "0" end - error = fix_permissions('MARKETPLACE', row[:oid], doc) - - if (error) - @fixes_marketplace[row[:oid]] = doc.root.to_s - end + @fixes_marketplace[row[:oid]] = doc.root.to_s if error end end diff --git a/src/onedb/fsck/marketplaceapp.rb b/src/onedb/fsck/marketplaceapp.rb index c26880d0a7..b08c07e5cf 100644 --- a/src/onedb/fsck/marketplaceapp.rb +++ b/src/onedb/fsck/marketplaceapp.rb @@ -61,16 +61,14 @@ module OneDBFsck log_error("Marketplace App #{row[:oid]} has a wrong name for marketplace #{market_id}, #{market_name}. It will be changed to #{market_entry[:name]}") doc.root.xpath('MARKETPLACE').each do |e| - e.text = market_entry[:name] + e.content = market_entry[:name] end - apps_fix[row[:oid]] = doc.root.to_s - elsif error - @db[:marketplaceapp_pool].where( - :oid => row[:oid] - ).update(:body => doc.root.to_s) + error = true end + apps_fix[row[:oid]] = doc.root.to_s if error + # DATA: Add app to marketplace list. Used in marketplace check market_entry[:apps] << row[:oid] end diff --git a/src/onedb/fsck/network.rb b/src/onedb/fsck/network.rb index 2a2b4be4a2..73e2aa31a4 100644 --- a/src/onedb/fsck/network.rb +++ b/src/onedb/fsck/network.rb @@ -5,7 +5,7 @@ module OneDBFsck def check_network_cluster cluster = @data_cluster - @fixes_host_cluster = {} + @fixes_vnet_cluster = {} @db.fetch('SELECT oid,body FROM network_pool') do |row| doc = nokogiri_doc(row[:body], 'network_pool') @@ -13,6 +13,7 @@ module OneDBFsck doc.root.xpath('CLUSTERS/ID').each do |e| cluster_id = e.text.to_i cluster_entry = cluster[cluster_id] + vnet_id = row[:oid] if cluster_entry.nil? e.remove @@ -20,9 +21,22 @@ module OneDBFsck log_error("VNet #{row[:oid]} is in cluster " \ "#{cluster_id}, but it does not exist") - @fixes_host_cluster[row[:oid]] = { :body => doc.root.to_s } + @db.fetch('SELECT cid FROM cluster_network_relation where ' \ + "oid=#{vnet_id}") do |row2| + clusters = doc.root.at_xpath('CLUSTERS').children + + if clusters.find {|n| n.text == row2[:cid].to_s }.nil? + cluster_e = doc.create_element('ID') + + doc.root.at_xpath('CLUSTERS').add_child(cluster_e).content = row2[:cid] + end + + cluster[row2[:cid]][:vnets] << vnet_id + end + + @fixes_vnet_cluster[row[:oid]] = doc.root.to_s else - cluster_entry[:vnets] << row[:oid] + cluster_entry[:vnets] << vnet_id end end end @@ -73,27 +87,8 @@ module OneDBFsck # Fix network cluster def fix_network_cluster @db.transaction do - @fixes_host_cluster.each do |id, entry| - body = entry[:body] - - @db.fetch('SELECT cid FROM cluster_network_relation where ' \ - "oid=#{id}") do |row| - doc = nokogiri_doc(body, 'cluster_network_relation') - clusters = doc.root.at_xpath('CLUSTERS').children - - if clusters.find {|n| n.text == row[:cid].to_s }.nil? - cluster_e = doc.create_element('ID') - - doc.root.at_xpath('CLUSTERS') - .add_child(cluster_e).content = row[:cid] - end - - @data_cluster[row[:cid]][:vnets] << id - - body = doc.root.to_s - - @db[:network_pool].where(:oid => id).update(:body => body) - end + @fixes_vnet_cluster.each do |id, body| + @db[:network_pool].where(:oid => id).update(:body => body) end end end diff --git a/src/onedb/fsck/quotas.rb b/src/onedb/fsck/quotas.rb index b2bf1d4222..0d9abab9e7 100644 --- a/src/onedb/fsck/quotas.rb +++ b/src/onedb/fsck/quotas.rb @@ -2,49 +2,40 @@ # rubocop:disable Style/FormatStringToken module OneDBFsck - # Check and fix quotas + # Check quotas # # @param resource [String] user/group - # @param type [String] normal/running - def check_fix_quotas(resource, type = 'normal') + def check_quotas(resource) + fix_quotas = @fix_quotas[resource] = {} + table = "#{resource}_quotas" - @db.run "ALTER TABLE #{table} RENAME TO old_#{table};" + query = "SELECT * FROM #{table} WHERE #{resource}_oid>0" - create_table(table.to_sym) + @db.fetch(query) do |row| + @error = false + doc = nokogiri_doc(row[:body], table) + # resource[0] = u if user, g if group + id_field = "#{resource[0]}id" + oid = row["#{resource}_oid".to_sym] + params = [doc, "#{id_field}=#{oid}", resource.capitalize] + + calculate_running_quotas(*params) + calculate_quotas(*params) + + fix_quotas[oid] = doc.root.to_s if @error + end + end + + def fix_quotas(resource) @db.transaction do - query = "SELECT * FROM old_#{table} WHERE #{resource}_oid=0" - - # oneadmin does not have quotas - @db.fetch(query) do |row| - @db[table.to_sym].insert(row) - end - - query = "SELECT * FROM old_#{table} WHERE #{resource}_oid>0" - - @db.fetch(query) do |row| - doc = nokogiri_doc(row[:body], "old_#{table}") - - # resource[0] = u if user, g if group - id_field = "#{resource[0]}id" - oid = row["#{resource}_oid".to_sym] - params = [doc, "#{id_field}=#{oid}", resource.capitalize] - - if type == 'running' - calculate_running_quotas(*params) - else - calculate_quotas(*params) - end - - object = { "#{resource}_oid".to_sym => oid, - :body => doc.root.to_s } - - @db["#{resource}_quotas".to_sym].insert(object) + @fix_quotas[resource].each do |id, body| + @db["#{resource}_quotas".to_sym].where("#{resource}_oid".to_sym => id).update( + :body => body + ) end end - - @db.run "DROP TABLE old_#{table};" end # Calculate normal quotas @@ -156,6 +147,8 @@ module OneDBFsck ds_elem.xpath('IMAGES_USED').each do |e| next if e.text == images_used.to_s + @error = true + log_error("#{resource} #{oid} quotas: Datastore " \ "#{ds_id}\tIMAGES_USED has #{e.text} " \ "\tis\t#{images_used}") @@ -165,6 +158,8 @@ module OneDBFsck ds_elem.xpath('SIZE_USED').each do |e| next if e.text == size_used.to_s + @error = true + log_error("#{resource} #{oid} quotas: Datastore " \ "#{ds_id}\tSIZE_USED has #{e.text} " \ "\tis\t#{size_used}") @@ -173,6 +168,8 @@ module OneDBFsck end ds_usage.each do |ds_id, array| + @error = true + images_used, size_used = array log_error("#{resource} #{oid} quotas: Datastore " \ @@ -230,6 +227,8 @@ module OneDBFsck img_elem.xpath('RVMS_USED').each do |e| next if e.text == rvms.to_s + @error = true + log_error("#{resource} #{oid} quotas: Image " \ "#{img_id}\tRVMS has #{e.text} \tis\t#{rvms}") e.content = rvms.to_s @@ -237,6 +236,8 @@ module OneDBFsck end img_usage.each do |img_id, rvms| + @error = true + log_error("#{resource} #{oid} quotas: Image " \ "#{img_id}\tRVMS has 0 \tis\t#{rvms}") @@ -328,6 +329,8 @@ module OneDBFsck vm_elem.xpath('SYSTEM_DISK_SIZE_USED').each do |e| next if e.text == sys_used.to_s + @error = true + log_error("#{resource} #{oid} quotas: SYSTEM_DISK_SIZE_USED " \ "has #{e.text} \tis\t#{sys_used}") e.content = sys_used.to_s @@ -409,6 +412,8 @@ module OneDBFsck next unless different + @error = true + log_error("#{resource} #{oid} quotas: #{cpu}_USED has " \ "#{e.text} \tis\t#{cpu_used_str}") e.content = cpu_used_str @@ -420,6 +425,8 @@ module OneDBFsck vm_elem.xpath("#{quota_name}_USED").each do |e| next if e.text.to_i == quotas[quota_name].to_i + @error = true + log_error("#{resource} #{oid} quotas: #{quota_name}_USED has " \ "#{e.text} \tis\t#{quotas[quota_name]}") e.content = quotas[quota_name].to_s @@ -500,7 +507,7 @@ module OneDBFsck net_quota.xpath('NETWORK').each do |net_elem| # Check ID exists unless net_elem.at_xpath('ID') - log_error("#{resource} #{oid} quotas: NETWORK doesn't have ID") + log_error("#{resource} #{oid} quotas: NETWORK doesn't have ID", false) next end @@ -514,6 +521,8 @@ module OneDBFsck net_elem.xpath('LEASES_USED').each do |e| next if e.text == leases_used.to_s + @error = true + log_error("#{resource} #{oid} quotas: VNet " \ "#{vnet_id}\tLEASES_USED has #{e.text} " \ "\tis\t#{leases_used}") @@ -524,6 +533,8 @@ module OneDBFsck end vnet_usage.each do |vnet_id, leases_used| + @error = true + log_error("#{resource} #{oid} quotas: VNet " \ "#{vnet_id}\tLEASES_USED has 0 \tis\t#{leases_used}") diff --git a/src/onedb/fsck/template.rb b/src/onedb/fsck/template.rb index 11dfd60eec..a8c1788f6f 100644 --- a/src/onedb/fsck/template.rb +++ b/src/onedb/fsck/template.rb @@ -10,7 +10,10 @@ module OneDBFsck check_ugid(doc) + error_perm = fix_permissions('VMTEMPLATE', row[:oid], doc) + if boot.nil? || boot.text.downcase.match(/fd|hd|cdrom|network/).nil? + templates_fix[row[:oid]] = doc.root.to_s if error_perm next end @@ -61,16 +64,12 @@ module OneDBFsck else log_error("VM Template #{row[:oid]} OS/BOOT contains deprecated format \"#{boot.content}\", but it can't be parsed to be fixed automatically", false) error = true - end end if error - error = fix_permissions('VMTEMPLATE', row[:oid], doc) - - next unless error - - templates_fix[row[:oid]] = doc.root.to_s + # Unrepairable error detected + templates_fix[row[:oid]] = doc.root.to_s if error_perm next end @@ -81,8 +80,6 @@ module OneDBFsck boot.content = new_boot - fix_permissions('VMTEMPLATE', row[:oid], doc) - templates_fix[row[:oid]] = doc.root.to_s end end diff --git a/src/onedb/fsck/vm.rb b/src/onedb/fsck/vm.rb index 74a0e554a1..d280620b6e 100644 --- a/src/onedb/fsck/vm.rb +++ b/src/onedb/fsck/vm.rb @@ -42,7 +42,9 @@ module OneDBFsck @vms_ports[port][cid] << vm_doc.root.at_xpath('ID').text.to_i end - fix_permissions('VM', row[:oid], vm_doc) + error = fix_permissions('VM', row[:oid], vm_doc) + + vms_fix[row[:oid]] = vm_doc.root.to_s if error # DATA: Images used by this VM vm_doc.root.xpath("TEMPLATE/DISK/IMAGE_ID").each do |e| @@ -186,10 +188,6 @@ module OneDBFsck } vms_fix[row[:oid]] = vm_doc.root.to_s - else - @db[:vm_pool].where( - :oid => row[:oid] - ).update(:body => vm_doc.root.to_s) end # DATA: add resources to host counters @@ -209,11 +207,15 @@ module OneDBFsck hdoc.root.xpath("VMMMAD").each {|e| e.name = "VM_MAD" found = true + + log_error("VM #{row[:oid]} history #{hrow[:seq]} changing VMMMAD to VM_MAD") } hdoc.root.xpath("TMMAD").each {|e| e.name = "TM_MAD" found = true + + log_error("VM #{row[:oid]} history #{hrow[:seq]} changing TMMAD to TM_MAD") } # DATA: translate VMMMAD and TMMAD to VM_MAD and TM_MAD diff --git a/src/onedb/onedb b/src/onedb/onedb index e4ef206196..6635ee7d77 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -331,7 +331,7 @@ EXPR= { DRY= { :name => 'dry', :large => '--dry', - :description => 'Do not write in the database, output xml' + :description => 'Do not write in the database, output on stdout' } DELETE= { @@ -482,7 +482,7 @@ CommandParser::CmdParser.new(ARGV) do Checks the consistency of the DB, and fixes the problems found EOT - command :fsck, fsck_desc, :options=>[FORCE, BACKUP] do + command :fsck, fsck_desc, :options=>[FORCE, BACKUP, DRY] do begin helper = OneDB.new(options) helper.fsck(options) diff --git a/src/onedb/onedb.rb b/src/onedb/onedb.rb index a995931205..5ff1b86a10 100644 --- a/src/onedb/onedb.rb +++ b/src/onedb/onedb.rb @@ -359,8 +359,11 @@ class OneDB 'obtained separately.' puts - puts 'The database will be restored' - restore(ops[:backup], :force => true) + + if !ops.include?(:no_backup) + puts 'The database will be restored' + restore(ops[:backup], :force => true) + end -1 rescue Exception => e @@ -462,7 +465,7 @@ class OneDB if File.exist? file - one_not_running() + one_not_running() if ops[:dry].nil? load(file) @backend.extend OneDBFsck @@ -472,7 +475,7 @@ class OneDB ops[:backup] = @backend.bck_file if ops[:backup].nil? # FSCK will be executed, make DB backup - backup(ops[:backup], ops) + backup(ops[:backup], ops) if ops[:dry].nil? begin puts " > Running fsck" if ops[:verbose] @@ -481,7 +484,7 @@ class OneDB @backend.read_config - result = @backend.fsck + result = @backend.fsck(ops[:dry] || false) if !result raise "Error running fsck version #{ret[:version]}" @@ -506,7 +509,7 @@ class OneDB ops[:force] = true - restore(ops[:backup], ops) + restore(ops[:backup], ops) if ops[:dry].nil? return -1 end