Class: PuppetHerald::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-herald/database.rb

Constant Summary collapse

@@dbconn =
nil
@@passfile =
nil

Class Method Summary collapse

Class Method Details

.dbconn=(dbconn) ⇒ Object



9
10
11
# File 'lib/puppet-herald/database.rb', line 9

def self.dbconn= dbconn
  @@dbconn = dbconn
end

.passfile=(passfile) ⇒ Object



13
14
15
# File 'lib/puppet-herald/database.rb', line 13

def self.passfile= passfile
  @@passfile = passfile
end

.setup(app) ⇒ Object



46
47
48
# File 'lib/puppet-herald/database.rb', line 46

def self.setup app
  ActiveRecord::Base.establish_connection(validate!)
end

.validate!(echo = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet-herald/database.rb', line 17

def self.validate! echo=false
  db = URI.parse(@@dbconn)
  connection = {}
  if db.scheme.to_s.empty?
    raise "Database scheme couldn't be empty! Connection string given: #{@@dbconn}"
  end
  if ['sqlite', 'sqlite3'].include? db.scheme
    file = File.expand_path("#{db.host}#{db.path}")
    FileUtils.touch file
    connection[:adapter]  = 'sqlite3'
    connection[:database] = file
  else
    dbname = db.path[1..-1]
    connection[:adapter]  = db.scheme == 'postgres' ? 'postgresql' : db.scheme
    connection[:host]     = db.host
    connection[:port]     = db.port unless db.port.nil?
    connection[:username] = db.user.nil? ? dbname : db.user
    connection[:password] = File.read(@@passfile).strip
    connection[:database] = dbname
    connection[:encoding] = 'utf8'
  end
  if echo
    copy = connection.dup
    copy[:password] = '***' unless copy[:password].nil?
    puts "Using #{copy.inspect} for database."
  end
  return connection
end