class PuppetHerald::Database
A class for a database configuration
Attributes
dbconn[W]
Sets a database connection @return [String] a dbconnection string
passfile[W]
Sets a passfile @return [String] a password file
Public Class Methods
new()
click to toggle source
# File lib/puppet-herald/database.rb, line 8 def initialize @dbconn = nil @passfile = nil end
Public Instance Methods
spec(log = false)
click to toggle source
Compiles a spec for database creation
@param log [Boolean] should log on screen? @return [Hash] a database configuration
# File lib/puppet-herald/database.rb, line 25 def spec(log = false) connection = process_spec print_config(connection, log) connection end
Private Instance Methods
pgport(connection, port)
click to toggle source
# File lib/puppet-herald/database.rb, line 84 def pgport(connection, port) connection[:port] = port unless port.nil? end
pgscheme(scheme)
click to toggle source
# File lib/puppet-herald/database.rb, line 88 def pgscheme(scheme) scheme == 'postgres' ? 'postgresql' : scheme end
pguser(user, dbname)
click to toggle source
# File lib/puppet-herald/database.rb, line 92 def pguser(user, dbname) user.nil? ? dbname : user end
postgresql(conn, passfile)
click to toggle source
# File lib/puppet-herald/database.rb, line 70 def postgresql(conn, passfile) connection = {} db = URI.parse conn dbname = db.path[1..-1] connection[:adapter] = pgscheme(db.scheme) connection[:host] = db.host connection[:username] = pguser(db.user, dbname) connection[:password] = File.read(passfile).strip connection[:database] = dbname connection[:encoding] = 'utf8' pgport(connection, db.port) connection end
print_config(connection, log)
click to toggle source
# File lib/puppet-herald/database.rb, line 51 def print_config(connection, log) return unless log copy = connection.dup copy[:password] = '***' unless copy[:password].nil? PuppetHerald.logger.info "Using #{copy.inspect} for database." end
process_spec()
click to toggle source
# File lib/puppet-herald/database.rb, line 33 def process_spec match = validate_conn(@dbconn) if %w(sqlite sqlite3).include? match[1] connection = sqlite(match) else connection = postgresql(@dbconn, @passfile) end connection end
sqlite(match)
click to toggle source
# File lib/puppet-herald/database.rb, line 58 def sqlite(match) connection = {} dbname = match[2] unless dbname.match(/^(?:file:)?:mem/) dbname = File.expand_path(dbname) FileUtils.touch dbname end connection[:adapter] = 'sqlite3' connection[:database] = dbname connection end
validate_conn(dbconn)
click to toggle source
# File lib/puppet-herald/database.rb, line 43 def validate_conn(dbconn) fail 'Connection is not set, can not validate database connection' if dbconn.nil? match = dbconn.match(%r{^(sqlite3?|postgres(?:ql)?)://(.+)$}) fail "Invalid database connection string given: #{dbconn}" if match.nil? match end