From 749dace1e20107979a57fe421446a2ee383f808f Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 17 Jun 2011 13:17:33 +0200 Subject: [PATCH] Add error codes to onedb and fix constant issue --- src/cli/command_parser.rb | 2 +- src/onedb/onedb.rb | 22 +++++++++++++++------- src/onedb/onedb_backend.rb | 28 ++++++++++++++-------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index 317e97c7dd..0152b1c4f9 100755 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -160,7 +160,7 @@ module CommandParser begin rc = comm[:proc].call rescue Exception =>e - #puts e.message + puts e.message exit -1 end diff --git a/src/onedb/onedb.rb b/src/onedb/onedb.rb index e071834a6c..381215648e 100644 --- a/src/onedb/onedb.rb +++ b/src/onedb/onedb.rb @@ -20,7 +20,7 @@ require 'onedb_backend' class OneDB def initialize(ops) if ops[:backend]==nil - @backend = from_onedconf + from_onedconf else if ops[:backend] == :sqlite @backend = BackEndSQLite.new(ops[:sqlite]) @@ -51,10 +51,11 @@ class OneDB if !ops[:force] && File.exists?(bck_file) puts "File #{bck_file} exists, backup aborted. Use -f to overwrite." - exit -1 + return -1 end @backend.backup(bck_file) + return 0 end def restore(bck_file, ops) @@ -62,12 +63,13 @@ class OneDB if !File.exists?(bck_file) puts "File #{bck_file} doesn't exist, backup restoration aborted." - exit -1 + return -1 end one_not_running @backend.restore(bck_file, ops[:force]) + return 0 end def version(ops) @@ -82,6 +84,8 @@ class OneDB else puts version end + + return 0 end def history @@ -107,7 +111,7 @@ class OneDB if ops[:backup] bck_file = ops[:backup] else - bck_file = @backend.bck_file(VAR_LOCATION) + bck_file = @backend.bck_file end @backend.backup(bck_file) @@ -141,6 +145,8 @@ class OneDB else puts "Database already uses version #{version}" end + + return 0 end private @@ -150,7 +156,7 @@ class OneDB if config[:db] == nil puts "No DB defined." - exit -1 + raise end if config[:db]["BACKEND"].upcase.include? "SQLITE" @@ -167,14 +173,16 @@ class OneDB else puts "Could not load DB configuration from " << "#{ETC_LOCATION}/oned.conf" - exit -1 + raise end + + return 0 end def one_not_running() if File.exists?(LOCK_FILE) puts "First stop OpenNebula. Lock file found: #{LOCK_FILE}" - exit -1 + raise end end end \ No newline at end of file diff --git a/src/onedb/onedb_backend.rb b/src/onedb/onedb_backend.rb index 8a249286dc..5db64124fc 100644 --- a/src/onedb/onedb_backend.rb +++ b/src/onedb/onedb_backend.rb @@ -39,7 +39,7 @@ class OneDBBacKEnd if !db_exists? puts "Database schema does not look to be created by OpenNebula:" puts "table user_pool is missing or empty." - exit -1 + raise end begin @@ -48,7 +48,7 @@ class OneDBBacKEnd rescue puts "Database schema looks to be created by OpenNebula 1.X." puts "This tool only works with databases created by 2.X versions." - exit -1 + raise end comment = "Could not read any previous db_versioning data, " << @@ -146,8 +146,8 @@ class OneDBBackEndMySQL < OneDBBacKEnd @db_name = @db_name[1..-2] if @db_name[0] == ?" end - def bck_file(var_location) - "#{var_location}/mysql_#{@server}_#{@db_name}.sql" + def bck_file + "#{VAR_LOCATION}/mysql_#{@server}_#{@db_name}.sql" end def backup(bck_file) @@ -157,7 +157,7 @@ class OneDBBackEndMySQL < OneDBBacKEnd rc = system(cmd) if !rc puts "Unknown error running '#{cmd}'" - exit -1 + raise end puts "MySQL dump stored in #{bck_file}" @@ -171,7 +171,7 @@ class OneDBBackEndMySQL < OneDBBacKEnd if !force && db_exists? puts "MySQL database #{@db_name} at #{@server} exists," << " use -f to overwrite." - exit -1 + raise end mysql_cmd = "mysql -u #{@user} -p#{@passwd} -h #{@server} -P #{@port} " @@ -180,21 +180,21 @@ class OneDBBackEndMySQL < OneDBBacKEnd rc = system(drop_cmd) if !rc puts "Error dropping MySQL DB #{@db_name} at #{@server}." - exit -1 + raise 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}." - exit -1 + raise 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}." - exit -1 + raise end puts "MySQL DB #{@db_name} at #{@server} restored." @@ -213,14 +213,14 @@ class BackEndSQLite < OneDBBacKEnd @sqlite_file = file end - def bck_file(var_location) - "#{var_location}/one.db.bck" + def bck_file + "#{VAR_LOCATION}/one.db.bck" end def backup(bck_file) if !File.exists?(@sqlite_file) puts "File #{@sqlite_file} doesn't exist, backup aborted." - exit -1 + raise end FileUtils.cp(@sqlite_file, "#{bck_file}") @@ -231,7 +231,7 @@ class BackEndSQLite < OneDBBacKEnd def restore(bck_file, force=nil) if !force && File.exists?(@sqlite_file) puts "File #{@sqlite_file} exists, use -f to overwrite." - exit -1 + raise end FileUtils.cp(bck_file, @sqlite_file) @@ -243,7 +243,7 @@ class BackEndSQLite < OneDBBacKEnd def connect_db if !File.exists?(@sqlite_file) puts "File #{@sqlite_file} doesn't exist." - exit -1 + raise end @db = Sequel.sqlite(@sqlite_file)