1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

F #5521: change-history for onedb

This commit is contained in:
Ruben S. Montero 2017-10-31 17:10:25 +01:00
parent 801f9cf27f
commit cda099cc7d
2 changed files with 83 additions and 0 deletions

View File

@ -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

View File

@ -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