mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-21 14:50:08 +03:00
bug #724: Improve onedb error handling
This commit is contained in:
parent
25d4fa1e35
commit
3470ecf7e1
@ -160,8 +160,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
EOT
|
||||
|
||||
command :backup, backup_desc, [:output_file, nil], :options=>FORCE do
|
||||
helper = OneDB.new(options)
|
||||
helper.backup(args[0], options)
|
||||
begin
|
||||
helper = OneDB.new(options)
|
||||
helper.backup(args[0], options)
|
||||
rescue Exception => e
|
||||
[-1, e.message]
|
||||
end
|
||||
end
|
||||
|
||||
###########################################################################
|
||||
@ -173,8 +177,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
EOT
|
||||
|
||||
command :version , version_desc do
|
||||
helper = OneDB.new(options)
|
||||
helper.version(options)
|
||||
begin
|
||||
helper = OneDB.new(options)
|
||||
helper.version(options)
|
||||
rescue Exception => e
|
||||
[-1, e.message]
|
||||
end
|
||||
end
|
||||
|
||||
###########################################################################
|
||||
@ -185,8 +193,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
EOT
|
||||
|
||||
command :history , history_desc do
|
||||
helper = OneDB.new(options)
|
||||
helper.history
|
||||
begin
|
||||
helper = OneDB.new(options)
|
||||
helper.history
|
||||
rescue Exception => e
|
||||
[-1, e.message]
|
||||
end
|
||||
end
|
||||
|
||||
###########################################################################
|
||||
@ -198,8 +210,12 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
EOT
|
||||
|
||||
command :restore , restore_desc, [:backup_file, nil], :options=>FORCE do
|
||||
helper = OneDB.new(options)
|
||||
helper.restore(args[0], options)
|
||||
begin
|
||||
helper = OneDB.new(options)
|
||||
helper.restore(args[0], options)
|
||||
rescue Exception => e
|
||||
[-1, e.message]
|
||||
end
|
||||
end
|
||||
|
||||
###########################################################################
|
||||
@ -212,7 +228,11 @@ cmd=CommandParser::CmdParser.new(ARGV) do
|
||||
EOT
|
||||
|
||||
command :upgrade , upgrade_desc, [:version, nil], :options=>[FORCE,BACKUP] do
|
||||
helper = OneDB.new(options)
|
||||
helper.upgrade(args[0], options)
|
||||
begin
|
||||
helper = OneDB.new(options)
|
||||
helper.upgrade(args[0], options)
|
||||
rescue Exception => e
|
||||
[-1, e.message]
|
||||
end
|
||||
end
|
||||
end
|
@ -50,8 +50,8 @@ class OneDB
|
||||
bck_file = @backend.bck_file if bck_file.nil?
|
||||
|
||||
if !ops[:force] && File.exists?(bck_file)
|
||||
puts "File #{bck_file} exists, backup aborted. Use -f to overwrite."
|
||||
raise
|
||||
raise "File #{bck_file} exists, backup aborted. Use -f " <<
|
||||
"to overwrite."
|
||||
end
|
||||
|
||||
@backend.backup(bck_file)
|
||||
@ -62,8 +62,7 @@ class OneDB
|
||||
bck_file = @backend.bck_file if bck_file.nil?
|
||||
|
||||
if !File.exists?(bck_file)
|
||||
puts "File #{bck_file} doesn't exist, backup restoration aborted."
|
||||
return -1
|
||||
raise "File #{bck_file} doesn't exist, backup restoration aborted."
|
||||
end
|
||||
|
||||
one_not_running
|
||||
@ -115,7 +114,6 @@ class OneDB
|
||||
# At least one upgrade will be executed, make DB backup
|
||||
backup(ops[:backup], ops)
|
||||
|
||||
|
||||
puts " > Running migrator #{file}" if ops[:verbose]
|
||||
|
||||
load(file)
|
||||
@ -123,9 +121,8 @@ class OneDB
|
||||
result = @backend.up
|
||||
|
||||
if !result
|
||||
puts "Error while upgrading from #{version} to " <<
|
||||
" #{@backend.db_version}"
|
||||
return -1
|
||||
raise "Error while upgrading from #{version} to " <<
|
||||
" #{@backend.db_version}"
|
||||
end
|
||||
|
||||
puts " > Done" if ops[:verbose]
|
||||
@ -148,8 +145,7 @@ class OneDB
|
||||
config = Configuration.new("#{ETC_LOCATION}/oned.conf")
|
||||
|
||||
if config[:db] == nil
|
||||
puts "No DB defined."
|
||||
raise
|
||||
raise "No DB defined."
|
||||
end
|
||||
|
||||
if config[:db]["BACKEND"].upcase.include? "SQLITE"
|
||||
@ -164,9 +160,8 @@ class OneDB
|
||||
:db_name => config[:db]["DB_NAME"]
|
||||
)
|
||||
else
|
||||
puts "Could not load DB configuration from " <<
|
||||
"#{ETC_LOCATION}/oned.conf"
|
||||
raise
|
||||
raise "Could not load DB configuration from " <<
|
||||
"#{ETC_LOCATION}/oned.conf"
|
||||
end
|
||||
|
||||
return 0
|
||||
@ -174,8 +169,7 @@ class OneDB
|
||||
|
||||
def one_not_running()
|
||||
if File.exists?(LOCK_FILE)
|
||||
puts "First stop OpenNebula. Lock file found: #{LOCK_FILE}"
|
||||
raise
|
||||
raise "First stop OpenNebula. Lock file found: #{LOCK_FILE}"
|
||||
end
|
||||
end
|
||||
end
|
@ -37,18 +37,16 @@ class OneDBBacKEnd
|
||||
rescue
|
||||
# If the DB doesn't have db_version table, it means it is empty or a 2.x
|
||||
if !db_exists?
|
||||
puts "Database schema does not look to be created by OpenNebula:"
|
||||
puts "table user_pool is missing or empty."
|
||||
raise
|
||||
raise "Database schema does not look to be created by " <<
|
||||
"OpenNebula: table user_pool is missing or empty."
|
||||
end
|
||||
|
||||
begin
|
||||
# Table image_pool is present only in 2.X DBs
|
||||
@db.fetch("SELECT * FROM image_pool") { |row| }
|
||||
rescue
|
||||
puts "Database schema looks to be created by OpenNebula 1.X."
|
||||
puts "This tool only works with databases created by 2.X versions."
|
||||
raise
|
||||
raise "Database schema looks to be created by OpenNebula 1.X." <<
|
||||
"This tool only works with databases created by 2.X versions."
|
||||
end
|
||||
|
||||
comment = "Could not read any previous db_versioning data, " <<
|
||||
@ -73,8 +71,7 @@ class OneDBBacKEnd
|
||||
puts ""
|
||||
end
|
||||
rescue Exception => e
|
||||
puts "No version records found. Error message:"
|
||||
puts e.message
|
||||
raise "No version records found. Error message: " + e.message
|
||||
end
|
||||
end
|
||||
|
||||
@ -130,8 +127,7 @@ class BackEndMySQL < OneDBBacKEnd
|
||||
(error = true; missing = "DBNAME") if @db_name == nil
|
||||
|
||||
if error
|
||||
puts "MySQL option #{missing} is needed"
|
||||
exit -1
|
||||
raise "MySQL option #{missing} is needed"
|
||||
end
|
||||
|
||||
# Check for defaults:
|
||||
@ -156,8 +152,7 @@ class BackEndMySQL < OneDBBacKEnd
|
||||
|
||||
rc = system(cmd)
|
||||
if !rc
|
||||
puts "Unknown error running '#{cmd}'"
|
||||
raise
|
||||
raise "Unknown error running '#{cmd}'"
|
||||
end
|
||||
|
||||
puts "MySQL dump stored in #{bck_file}"
|
||||
@ -169,9 +164,8 @@ class BackEndMySQL < OneDBBacKEnd
|
||||
connect_db
|
||||
|
||||
if !force && db_exists?
|
||||
puts "MySQL database #{@db_name} at #{@server} exists," <<
|
||||
" use -f to overwrite."
|
||||
raise
|
||||
raise "MySQL database #{@db_name} at #{@server} exists," <<
|
||||
" use -f to overwrite."
|
||||
end
|
||||
|
||||
mysql_cmd = "mysql -u #{@user} -p#{@passwd} -h #{@server} -P #{@port} "
|
||||
@ -179,22 +173,19 @@ class BackEndMySQL < OneDBBacKEnd
|
||||
drop_cmd = mysql_cmd + "-e 'DROP DATABASE IF EXISTS #{@db_name};'"
|
||||
rc = system(drop_cmd)
|
||||
if !rc
|
||||
puts "Error dropping MySQL DB #{@db_name} at #{@server}."
|
||||
raise
|
||||
raise "Error dropping MySQL DB #{@db_name} at #{@server}."
|
||||
end
|
||||
|
||||
create_cmd = mysql_cmd+"-e 'CREATE DATABASE IF NOT EXISTS #{@db_name};'"
|
||||
rc = system(create_cnd)
|
||||
if !rc
|
||||
puts "Error creating MySQL DB #{@db_name} at #{@server}."
|
||||
raise
|
||||
raise "Error creating MySQL DB #{@db_name} at #{@server}."
|
||||
end
|
||||
|
||||
restore_cmd = mysql_cmd + "#{@db_name} < #{bck_file}"
|
||||
rc = system(restore_cmd)
|
||||
if !rc
|
||||
puts "Error while restoring MySQL DB #{@db_name} at #{@server}."
|
||||
raise
|
||||
raise "Error while restoring MySQL DB #{@db_name} at #{@server}."
|
||||
end
|
||||
|
||||
puts "MySQL DB #{@db_name} at #{@server} restored."
|
||||
@ -204,13 +195,24 @@ class BackEndMySQL < OneDBBacKEnd
|
||||
|
||||
def connect_db
|
||||
endpoint = "mysql://#{@user}:#{@passwd}@#{@server}:#{@port}/#{@db_name}"
|
||||
@db = Sequel.connect(endpoint)
|
||||
|
||||
begin
|
||||
@db = Sequel.connect(endpoint)
|
||||
rescue Exception => e
|
||||
raise "Error connecting to DB: " + e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class BackEndSQLite < OneDBBacKEnd
|
||||
require 'fileutils'
|
||||
|
||||
def initialize(file)
|
||||
@sqlite_file = file
|
||||
|
||||
if !File.exists?(@sqlite_file)
|
||||
raise "File #{@sqlite_file} doesn't exist"
|
||||
end
|
||||
end
|
||||
|
||||
def bck_file
|
||||
@ -218,20 +220,14 @@ class BackEndSQLite < OneDBBacKEnd
|
||||
end
|
||||
|
||||
def backup(bck_file)
|
||||
if !File.exists?(@sqlite_file)
|
||||
puts "File #{@sqlite_file} doesn't exist, backup aborted."
|
||||
raise
|
||||
end
|
||||
|
||||
FileUtils.cp(@sqlite_file, "#{bck_file}")
|
||||
puts "Sqlite database backup stored in #{bck_file}"
|
||||
puts "Use 'onedb restore' or copy the file back to restore the DB."
|
||||
end
|
||||
|
||||
def restore(bck_file, force=nil)
|
||||
if !force && File.exists?(@sqlite_file)
|
||||
puts "File #{@sqlite_file} exists, use -f to overwrite."
|
||||
raise
|
||||
if !force
|
||||
raise "File #{@sqlite_file} exists, use -f to overwrite."
|
||||
end
|
||||
|
||||
FileUtils.cp(bck_file, @sqlite_file)
|
||||
@ -241,11 +237,10 @@ class BackEndSQLite < OneDBBacKEnd
|
||||
private
|
||||
|
||||
def connect_db
|
||||
if !File.exists?(@sqlite_file)
|
||||
puts "File #{@sqlite_file} doesn't exist."
|
||||
raise
|
||||
begin
|
||||
@db = Sequel.sqlite(@sqlite_file)
|
||||
rescue Exception => e
|
||||
raise "Error connecting to DB: " + e.message
|
||||
end
|
||||
|
||||
@db = Sequel.sqlite(@sqlite_file)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user