diff --git a/src/onedb/onedb b/src/onedb/onedb index 433489013e..b052c1424b 100755 --- a/src/onedb/onedb +++ b/src/onedb/onedb @@ -44,6 +44,8 @@ else LOCK_FILE = VAR_LOCATION + '/.lock' end +ONED_CONF = "#{ETC_LOCATION}/oned.conf" + if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) $LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ } diff --git a/src/onedb/onedb.rb b/src/onedb/onedb.rb index ab1b3f77e6..36308263cb 100644 --- a/src/onedb/onedb.rb +++ b/src/onedb/onedb.rb @@ -23,8 +23,10 @@ class OneDB attr_accessor :backend def initialize(ops) - # Set MySQL backend as default if any connection option is provided and --type is not - if ops[:backend].nil? and (!ops[:server].nil? || !ops[:port].nil? || !ops[:user].nil? || !ops[:password].nil? || !ops[:db_name].nil? || !ops[:encoding].nil?) + if ops[:backend].nil? && ops[:server].nil? && ops[:port].nil? && ops[:user].nil? && ops[:password].nil? && ops[:db_name].nil? + ops = read_credentials(ops) + elsif ops[:backend].nil? and (!ops[:server].nil? || !ops[:port].nil? || !ops[:user].nil? || !ops[:password].nil? || !ops[:db_name].nil? || !ops[:encoding].nil?) + # Set MySQL backend as default if any connection option is provided and --type is not ops[:backend] = :mysql end @@ -48,12 +50,8 @@ class OneDB end passwd = ops[:passwd] - if !passwd - passwd = ENV['ONE_DB_PASSWORD'] - end - if !passwd - passwd = get_password - end + passwd = ENV['ONE_DB_PASSWORD'] unless passwd + passwd = get_password unless passwd @backend = BackEndMySQL.new( :server => ops[:server], @@ -72,10 +70,10 @@ class OneDB exit -1 end - passwd = ops[:passwd] - if !passwd - passwd = get_password("PostgreSQL Password: ") - end + passwd = ops[:passwd] + passwd = ENV['ONE_DB_PASSWORD'] unless passwd + passwd = get_password("PostgreSQL Password: ") unless passwd + ops[:port] = 5432 if ops[:port] == 0 @backend = BackEndPostgreSQL.new( :server => ops[:server], @@ -101,6 +99,55 @@ class OneDB return passwd end + def read_credentials(ops) + begin + gem 'augeas', '~> 0.6' + require 'augeas' + rescue Gem::LoadError + STDERR.puts( + 'Augeas gem is not installed, run `gem install ' \ + 'augeas -v \'0.6\'` to install it' + ) + exit(-1) + end + + work_file_dir = File.dirname(ONED_CONF) + work_file_name = File.basename(ONED_CONF) + + aug = Augeas.create(:no_modl_autoload => true, + :no_load => true, + :root => work_file_dir, + :loadpath => ONED_CONF) + + aug.clear_transforms + aug.transform(:lens => 'Oned.lns', :incl => work_file_name) + aug.context = "/files/#{work_file_name}" + aug.load + + ops[:backend] = aug.get('DB/BACKEND') + ops[:server] = aug.get('DB/SERVER') + ops[:port] = aug.get('DB/PORT') + ops[:user] = aug.get('DB/USER') + ops[:passwd] = aug.get('DB/PASSWD') + ops[:db_name] = aug.get('DB/DB_NAME') + + ops = ops.transform_values do |v| + next unless v + + v.chomp('"').reverse.chomp('"').reverse + end + + ops.each {|_, v| v.gsub!("\\", '') if v } + + ops[:backend] = ops[:backend].to_sym + ops[:port] = ops[:port].to_i + + ops + rescue StandardError => e + STDERR.puts "Unable to parse oned.conf: #{e}" + exit(-1) + end + def backup(bck_file, ops, backend=@backend) bck_file = backend.bck_file(ops[:federated]) if bck_file.nil?