1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

M #-: read DB credentials with augeas (#4624)

This commit is contained in:
Alejandro Huertas Herrero 2020-04-28 18:35:04 +02:00 committed by GitHub
parent 9a5d637c4f
commit 12cc18d64b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 12 deletions

View File

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

View File

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