From 56f40032558276a0ed895094096def0d91a7eb16 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 28 Dec 2017 17:56:15 +0100 Subject: [PATCH] F #1614: Add update-history and show-history commands to onedb (cherry picked from commit ce4b3b9e7361cf9a4ef85ed04b740254dd206e4d) --- src/onedb/onedb | 66 +++++++++++++++++++++++++++++++++++++++++ src/onedb/onedb_live.rb | 66 +++++++++++++++++++++++++++++++++++------ 2 files changed, 123 insertions(+), 9 deletions(-) diff --git a/src/onedb/onedb b/src/onedb/onedb index ec83683102..61f446b62b 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -632,6 +632,39 @@ cmd=CommandParser::CmdParser.new(ARGV) do 0 # exit code end + ########################################################################### + # Update body of a history record + ########################################################################### + update_history_desc = <<-EOT.unindent + Update 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 + + For example to update the 3rd record of VM 0 + onedb update-history --id 0 --seq 3 + + #{LIVE_ACTION_HELP} + EOT + + command :'update-history', update_history_desc, :options => [ID, SEQ] do + begin + if !options[:id] || !options[:seq] + puts "Missing VM ID or SEQ number" + return -1 + end + + action = OneDBLive.new + action.update_history_cli(options[:id], options[:seq]) + rescue Exception => e + puts e.message + [-1, e.message] + end + + 0 # exit code + end + ########################################################################### # Show object body ########################################################################### @@ -664,4 +697,37 @@ cmd=CommandParser::CmdParser.new(ARGV) do 0 # exit code end + + ########################################################################### + # Show body of a history record + ########################################################################### + show_history_desc = <<-EOT.unindent + Show body of a history record. 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 + + For example to show the 3rd record of VM 0 + onedb update-history --id 0 --seq 3 + + #{LIVE_ACTION_HELP} + EOT + + command :'show-history', show_history_desc, :options => [ID, SEQ] do + begin + if !options[:id] || !options[:seq] + puts "Missing VM ID or SEQ" + return -1 + end + + action = OneDBLive.new + STDOUT.puts action.show_history_cli(options[:id], options[:seq]) + rescue Exception => e + puts e.message + [-1, e.message] + end + + 0 # exit code + end end diff --git a/src/onedb/onedb_live.rb b/src/onedb/onedb_live.rb index 221d7355d6..c04977371c 100644 --- a/src/onedb/onedb_live.rb +++ b/src/onedb/onedb_live.rb @@ -361,20 +361,12 @@ class OneDBLive def change_history(vid, seq, xpath, value, options) begin - db_data = select("history", "vid = #{vid} and seq = #{seq}") + doc = get_history_body(vid, seq) rescue => e - STDERR.puts "Error getting history recored #{seq} for VM #{vid}" STDERR.puts e.message 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 @@ -515,6 +507,31 @@ class OneDBLive end end + def update_history_cli(vid, seq) + begin + doc = get_history_body(vid, seq) + rescue => e + STDERR.puts e.message + return + end + + doc = editor_body(doc.root.to_s) + + begin + xml_doc = Nokogiri::XML(doc, nil, NOKOGIRI_ENCODING) do |c| + c.default_xml.noblanks + c.strict + end + + xml = xml_doc.root.to_xml + + update_body("history", xml, "vid = #{vid} and seq = #{seq}", false) + rescue => e + STDERR.puts "Error updating history record #{seq} for VM #{vid}" + STDERR.puts e.message + end + end + def show_body_cli(object, id) table, object, federate = get_pool_config(object) @@ -535,5 +552,36 @@ class OneDBLive doc.root.to_s end + + def show_history_cli(vid, seq) + begin + doc = get_history_body(vid, seq) + rescue => e + STDERR.puts e.message + return + end + + doc.root.to_s + end + + def get_history_body(vid, seq) + begin + db_data = select("history", "vid = #{vid} and seq = #{seq}") + rescue => e + error_str = "Error getting history record #{seq} for VM #{vid}" + error_str << e.message + + raise error_str + end + + row = db_data.first + body = Base64.decode64(row['body64']) + + doc = Nokogiri::XML(body, nil, NOKOGIRI_ENCODING) do |c| + c.default_xml.noblanks + end + + return doc + end end