diff --git a/src/onedb/onedb b/src/onedb/onedb index 96094601e7..52fe8440a0 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -254,6 +254,13 @@ ID = { :format => Numeric } +SEQ= { + :name => "seq", + :large => "--seq SEQ" , + :description => "Sequence number of a hitory record", + :format => Numeric +} + XPATH = { :name => "xpath", :short => "-x ID", @@ -553,4 +560,44 @@ cmd=CommandParser::CmdParser.new(ARGV) do 0 # exit code end + + ########################################################################### + # Change value in a history record + ########################################################################### + change_history_desc = <<-EOT.unindent + Changes a value from a history record of a VM. History records are + defined by its sequence number (SEQ) and VM ID. + + * --id: vm id, example: 156 + * --seq: sequence number of the record, example: 0 + + The new value for this elemente is set in the third parameter. + + Change the the hostname of first record: + + onedb change-history --id 3 --seq 0 '/HISTORY/HOSTNAME' new_host + + #{LIVE_ACTION_HELP} + EOT + command :'change-history', change_history_desc, :xpath, :value, + :options => [ID, SEQ, DRY] do + begin + vid = options[:id] + seq = options[:seq] + + if !vid || !seq + puts "Missing VM ID or SEQ number" + return -1 + end + + action = OneDBLive.new + action.change_history(vid, seq, args[0], args[1], options) + rescue Exception => e + puts e.message + pp e.backtrace + [-1, e.message] + end + + 0 # exit code + end end diff --git a/src/onedb/onedb_live.rb b/src/onedb/onedb_live.rb index 3cf5739308..1e042e12f9 100644 --- a/src/onedb/onedb_live.rb +++ b/src/onedb/onedb_live.rb @@ -257,6 +257,42 @@ class OneDBLive res end + def change_history(vid, seq, xpath, value, options) + begin + db_data = select("history", "vid = #{vid} and seq = #{seq}") + rescue => e + STDERR.puts "Error getting history recored #{seq} for VM #{vid}" + STDERR.puts e.message + STDERR.puts e.backtrace + return + end + + row = db_data.first + body = Base64.decode64(row['body64']) + + doc = Nokogiri::XML(body, nil, NOKOGIRI_ENCODING) do |c| + c.default_xml.noblanks + end + + doc.xpath(xpath).each do |e| + e.content = value + end + + xml = doc.root.to_xml + + if options[:dry] + puts xml + else + begin + update_body("history", xml, "vid = #{vid} and seq = #{seq}", false) + rescue => e + STDERR.puts "Error updating history recored #{seq} for VM #{vid}" + STDERR.puts e.message + STDERR.puts e.backtrace + end + end + end + def change_body(object, xpath, value, options = {}) case (object||'').downcase.strip.to_sym when :vm