1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# Load the appropriate libraries, or set a class indicating they aren't available
require 'facter'
require 'puppet'
module Puppet::Rails
TIME_DEBUG = true
def self.connect
# This global init does not work for testing, because we remove
# the state dir on every test.
return if ActiveRecord::Base.connected?
Puppet.settings.use(:main, :rails, :master)
ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
begin
loglevel = Logger.const_get(Puppet[:rails_loglevel].upcase)
ActiveRecord::Base.logger.level = loglevel
rescue => detail
Puppet.warning "'#{Puppet[:rails_loglevel]}' is not a valid Rails log level; using debug"
ActiveRecord::Base.logger.level = Logger::DEBUG
end
if (::ActiveRecord::VERSION::MAJOR == 2 and ::ActiveRecord::VERSION::MINOR <= 1)
ActiveRecord::Base.allow_concurrency = true
end
ActiveRecord::Base.verify_active_connections!
begin
args = database_arguments
Puppet.info "Connecting to #{args[:adapter]} database: #{args[:database]}"
ActiveRecord::Base.establish_connection(args)
rescue => detail
puts detail.backtrace if Puppet[:trace]
raise Puppet::Error, "Could not connect to database: #{detail}"
end
end
# The arguments for initializing the database connection.
def self.database_arguments
adapter = Puppet[:dbadapter]
args = {:adapter => adapter, :log_level => Puppet[:rails_loglevel]}
case adapter
when "sqlite3"
args[:database] = Puppet[:dblocation]
when "mysql", "postgresql"
args[:host] = Puppet[:dbserver] unless Puppet[:dbserver].to_s.empty?
args[:port] = Puppet[:dbport] unless Puppet[:dbport].to_s.empty?
args[:username] = Puppet[:dbuser] unless Puppet[:dbuser].to_s.empty?
args[:password] = Puppet[:dbpassword] unless Puppet[:dbpassword].to_s.empty?
args[:database] = Puppet[:dbname]
args[:reconnect]= true
socket = Puppet[:dbsocket]
args[:socket] = socket unless socket.to_s.empty?
connections = Puppet[:dbconnections].to_i
args[:pool] = connections if connections > 0
when "oracle_enhanced":
args[:database] = Puppet[:dbname] unless Puppet[:dbname].to_s.empty?
args[:username] = Puppet[:dbuser] unless Puppet[:dbuser].to_s.empty?
args[:password] = Puppet[:dbpassword] unless Puppet[:dbpassword].to_s.empty?
connections = Puppet[:dbconnections].to_i
args[:pool] = connections if connections > 0
else
raise ArgumentError, "Invalid db adapter #{adapter}"
end
args
end
# Set up our database connection. It'd be nice to have a "use" system
# that could make callbacks.
def self.init
raise Puppet::DevError, "No activerecord, cannot init Puppet::Rails" unless Puppet.features.rails?
connect()
unless ActiveRecord::Base.connection.tables.include?("resources")
require 'puppet/rails/database/schema'
Puppet::Rails::Schema.init
end
migrate() if Puppet[:dbmigrate]
end
# Migrate to the latest db schema.
def self.migrate
dbdir = nil
$LOAD_PATH.each { |d|
tmp = File.join(d, "puppet/rails/database")
if FileTest.directory?(tmp)
dbdir = tmp
break
end
}
raise Puppet::Error, "Could not find Puppet::Rails database dir" unless dbdir
raise Puppet::Error, "Database has problems, can't migrate." unless ActiveRecord::Base.connection.tables.include?("resources")
Puppet.notice "Migrating"
begin
ActiveRecord::Migrator.migrate(dbdir)
rescue => detail
puts detail.backtrace if Puppet[:trace]
raise Puppet::Error, "Could not migrate database: #{detail}"
end
end
# Tear down the database. Mostly only used during testing.
def self.teardown
raise Puppet::DevError, "No activerecord, cannot init Puppet::Rails" unless Puppet.features.rails?
Puppet.settings.use(:master, :rails)
begin
ActiveRecord::Base.establish_connection(database_arguments())
rescue => detail
puts detail.backtrace if Puppet[:trace]
raise Puppet::Error, "Could not connect to database: #{detail}"
end
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.drop_table t
end
end
end
require 'puppet/rails/host' if Puppet.features.rails?
|