diff --git a/install.sh b/install.sh index 7d4b41e81a..536d583045 100755 --- a/install.sh +++ b/install.sh @@ -229,6 +229,7 @@ LIB_DIRS="$LIB_LOCATION/ruby \ $LIB_LOCATION/ruby/onedb \ $LIB_LOCATION/ruby/onedb/shared \ $LIB_LOCATION/ruby/onedb/local \ + $LIB_LOCATION/ruby/onedb/patches \ $LIB_LOCATION/ruby/vendors \ $LIB_LOCATION/ruby/vendors/rbvmomi \ $LIB_LOCATION/ruby/vendors/rbvmomi/lib \ @@ -367,6 +368,7 @@ INSTALL_FILES=( ONEDB_FILES:$LIB_LOCATION/ruby/onedb ONEDB_SHARED_MIGRATOR_FILES:$LIB_LOCATION/ruby/onedb/shared ONEDB_LOCAL_MIGRATOR_FILES:$LIB_LOCATION/ruby/onedb/local + ONEDB_PATCH_FILES:$LIB_LOCATION/ruby/onedb/patches MADS_LIB_FILES:$LIB_LOCATION/mads IM_PROBES_FILES:$VAR_LOCATION/remotes/im IM_PROBES_KVM_FILES:$VAR_LOCATION/remotes/im/kvm.d @@ -1214,6 +1216,8 @@ ONEDB_LOCAL_MIGRATOR_FILES="src/onedb/local/4.5.80_to_4.7.80.rb \ src/onedb/local/4.10.3_to_4.11.80.rb \ src/onedb/local/4.11.80_to_4.13.80.rb" +ONEDB_PATCH_FILES="src/onedb/patches/4.14_monitoring.rb" + #------------------------------------------------------------------------------- # Configuration files for OpenNebula, to be installed under $ETC_LOCATION #------------------------------------------------------------------------------- diff --git a/src/onedb/local/4.11.80_to_4.13.80.rb b/src/onedb/local/4.11.80_to_4.13.80.rb index 043428bf4e..ef027fecb8 100644 --- a/src/onedb/local/4.11.80_to_4.13.80.rb +++ b/src/onedb/local/4.11.80_to_4.13.80.rb @@ -86,57 +86,67 @@ module Migrator # 3718 - # Move monitoring attributes in VM pool table + # Move monitoring attributes in VM pool table. VMs in the DONE state + # will be processed by the onedb patch command. @db.run "ALTER TABLE vm_pool RENAME TO old_vm_pool;" @db.run "CREATE TABLE vm_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, gid INTEGER, last_poll INTEGER, state INTEGER, lcm_state INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER);" + @db.run "INSERT INTO vm_pool SELECT * FROM old_vm_pool WHERE state=6;" + + log_time() + @db.transaction do - @db.fetch("SELECT * FROM old_vm_pool") do |row| - doc = Nokogiri::XML(row[:body],nil,NOKOGIRI_ENCODING){|c| c.default_xml.noblanks} + @db.fetch("SELECT * FROM old_vm_pool WHERE state<>6") do |row| + doc = Nokogiri::XML(row[:body],nil,NOKOGIRI_ENCODING){|c| c.default_xml.noblanks} - update_monitoring(doc.root.at_xpath("/VM")) + update_monitoring(doc.root.at_xpath("/VM")) - @db[:vm_pool].insert( - :oid => row[:oid], - :name => row[:name], - :body => doc.root.to_s, - :uid => row[:uid], - :gid => row[:gid], - :last_poll => row[:last_poll], - :state => row[:state], - :lcm_state => row[:lcm_state], - :owner_u => row[:owner_u], - :group_u => row[:group_u], - :other_u => row[:other_u]) - end + @db[:vm_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => doc.root.to_s, + :uid => row[:uid], + :gid => row[:gid], + :last_poll => row[:last_poll], + :state => row[:state], + :lcm_state => row[:lcm_state], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u]) + end end @db.run "DROP TABLE old_vm_pool;" log_time() - # Move monitoring attributes in the history table + # Move monitoring attributes in the history table. Closed records + # will be processed by the onedb patch command. @db.run "ALTER TABLE history RENAME TO old_history;" @db.run "CREATE TABLE history (vid INTEGER, seq INTEGER, body MEDIUMTEXT, stime INTEGER, etime INTEGER,PRIMARY KEY(vid,seq));" + @db.run "INSERT INTO history SELECT * FROM old_history WHERE etime<>0;" + + log_time() + @db.transaction do - @db.fetch("SELECT * FROM old_history") do |row| - doc = Nokogiri::XML(row[:body],nil,NOKOGIRI_ENCODING){|c| c.default_xml.noblanks} + @db.fetch("SELECT * FROM old_history WHERE etime=0") do |row| + doc = Nokogiri::XML(row[:body],nil,NOKOGIRI_ENCODING){|c| c.default_xml.noblanks} - elem = doc.root.at_xpath("/HISTORY/VM") - if !elem.nil? - update_monitoring(elem) - end - - @db[:history].insert( - :vid => row[:vid], - :seq => row[:seq], - :body => doc.root.to_s, - :stime => row[:stime], - :etime => row[:etime]) + elem = doc.root.at_xpath("/HISTORY/VM") + if !elem.nil? + update_monitoring(elem) end + + @db[:history].insert( + :vid => row[:vid], + :seq => row[:seq], + :body => doc.root.to_s, + :stime => row[:stime], + :etime => row[:etime]) + end end @db.run "DROP TABLE old_history;" diff --git a/src/onedb/onedb b/src/onedb/onedb index a10c95d325..cd00e0d93b 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -339,4 +339,21 @@ cmd=CommandParser::CmdParser.new(ARGV) do [-1, e.message] end end + + ########################################################################### + # Patch + ########################################################################### + patch_desc = <<-EOT.unindent + Applies a database patch file + EOT + + command :patch , patch_desc, :file, :options=>[BACKUP] do + begin + helper = OneDB.new(options) + helper.patch(args[0], options) + rescue Exception => e + [-1, e.message] + end + end + end diff --git a/src/onedb/onedb.rb b/src/onedb/onedb.rb index 0ea5c1a84c..471fedec07 100644 --- a/src/onedb/onedb.rb +++ b/src/onedb/onedb.rb @@ -451,6 +451,71 @@ is preserved. end end + def patch(file, ops) + ret = @backend.read_db_version + + if ops[:verbose] + pretty_print_db_version(ret) + puts "" + end + + if File.exists? file + + load(file) + @backend.extend OneDBPatch + + if (!@backend.is_hot_patch(ops)) + one_not_running() + end + + @backend.check_db_version(ops) + + ops[:backup] = @backend.bck_file if ops[:backup].nil? + + if (!@backend.is_hot_patch(ops)) + backup(ops[:backup], ops) + end + + begin + puts " > Running patch #{file}" if ops[:verbose] + + time0 = Time.now + + result = @backend.patch(ops) + + if !result + raise "Error running patch #{file}" + end + + puts " > Done" if ops[:verbose] + puts "" if ops[:verbose] + + time1 = Time.now + + puts " > Total time: #{"%0.02f" % (time1 - time0).to_s}s" if ops[:verbose] + + return 0 + rescue Exception => e + puts + puts e.message + puts e.backtrace.join("\n") + puts + + puts "Error running patch #{file}" + if (!@backend.is_hot_patch(ops)) + puts "The database will be restored" + + ops[:force] = true + + restore(ops[:backup], ops) + end + + return -1 + end + else + raise "File was not found: #{file}" + end + end private