From a40167e9b4008e79cad319af53b6ad9f0b8aaab9 Mon Sep 17 00:00:00 2001 From: Alejandro Huertas Herrero Date: Fri, 13 May 2022 13:38:32 +0200 Subject: [PATCH] F #4959: onedb update-body from a XML file (#2047) --- src/onedb/onedb | 20 ++++++++++++++++++-- src/onedb/onedb_live.rb | 38 +++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/onedb/onedb b/src/onedb/onedb index ae51f82cc9..e3c0c70985 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -354,6 +354,13 @@ PAGES = { :format => Integer } +FILE = { + :name => 'file', + :large => '--file file', + :description => 'File with object XML to update directly in the database', + :format => String +} + CommandParser::CmdParser.new(ARGV) do description <<-EOT.unindent This command enables the user to manage the OpenNebula database. It @@ -733,15 +740,24 @@ CommandParser::CmdParser.new(ARGV) do #{LIVE_ACTION_HELP} EOT - command :'update-body', update_body_desc, :object, :options => [ID] do + command :'update-body', update_body_desc, :object, :options => [ID, FILE] do begin if !options[:id] puts 'Missing object ID' return -1 end + if options[:file] && !File.exist?(options[:file]) + STDERR.puts "File '#{options[:file]}' doesn't exist" + exit(-1) + end + action = OneDBLive.new - rc = action.update_body_cli(args[0], options[:id]) + rc = action.update_body_cli( + args[0], + options[:id], + options[:file] + ) if OpenNebula.is_error?(rc) STDERR.puts "ERROR: #{rc.message}" diff --git a/src/onedb/onedb_live.rb b/src/onedb/onedb_live.rb index ba555e1d91..96b0e5e350 100644 --- a/src/onedb/onedb_live.rb +++ b/src/onedb/onedb_live.rb @@ -492,27 +492,31 @@ class OneDBLive File.read(tmp.path) end - def update_body_cli(object, id) + def update_body_cli(object, id, file) table, _object, federate = get_pool_config(object) - # Get body from the database - begin - db_data = select(table, "oid = #{id}") - rescue StandardError => e - STDERR.puts "Error getting #{object} with id #{id}" - STDERR.puts e.message - exit(-1) + if file + doc = File.read(file) + else + # Get body from the database + begin + db_data = select(table, "oid = #{id}") + rescue StandardError => e + STDERR.puts "Error getting #{object} with id #{id}" + STDERR.puts e.message + exit(-1) + 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 = editor_body(doc.root.to_s) 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 = editor_body(doc.root.to_s) - begin xml_doc = Nokogiri::XML(doc, nil, NOKOGIRI_ENCODING) do |c| c.default_xml.noblanks