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
137
138
139
140
141
142
143
144
|
# Load the appropriate libraries, or set a class indicating they aren't available
require 'facter'
require 'puppet'
module Puppet::Rails
Puppet.config.setdefaults(:puppetmaster,
:dblocation => { :default => "$statedir/clientconfigs.sqlite3",
:mode => 0660,
:owner => "$user",
:group => "$group",
:desc => "The database cache for client configurations. Used for
querying within the language."
},
:dbadapter => [ "sqlite3", "The type of database to use." ],
:dbmigrate => [ false, "Whether to automatically migrate the database." ],
:dbname => [ "puppet", "The name of the database to use." ],
:dbserver => [ "localhost", "The database server for Client caching. Only
used when networked databases are used."],
:dbuser => [ "puppet", "The database user for Client caching. Only
used when networked databases are used."],
:dbpassword => [ "puppet", "The database password for Client caching. Only
used when networked databases are used."],
:railslog => {:default => "$logdir/rails.log",
:mode => 0600,
:owner => "$user",
:group => "$group",
:desc => "Where Rails-specific logs are sent"
}
)
# The arguments for initializing the database connection.
def self.database_arguments
args = {:adapter => Puppet[:dbadapter]}
case Puppet[:dbadapter]
when "sqlite3":
args[:dbfile] = Puppet[:dblocation]
when "mysql", "postgresql":
args[:host] = Puppet[:dbserver]
args[:username] = Puppet[:dbuser]
args[:password] = Puppet[:dbpassword]
args[:database] = Puppet[:dbname]
else
raise ArgumentError, "Invalid db adapter %s" % Puppet[:dbadapter]
end
args
end
# Set up our database connection. It'd be nice to have a "use" system
# that could make callbacks.
def self.init
unless Puppet.features.rails?
raise Puppet::DevError, "No activerecord, cannot init Puppet::Rails"
end
# This global init does not work for testing, because we remove
# the state dir on every test.
unless ActiveRecord::Base.connected?
Puppet.config.use(:puppet)
ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
ActiveRecord::Base.allow_concurrency = true
begin
ActiveRecord::Base.establish_connection(database_arguments())
rescue => detail
if Puppet[:trace]
puts detail.backtrace
end
raise Puppet::Error, "Could not connect to database: %s" % detail
end
end
unless ActiveRecord::Base.connection.tables.include?("resources")
require 'puppet/rails/database/schema'
Puppet::Rails::Schema.init
end
if Puppet[:dbmigrate]
migrate()
end
# For now, we have to use :puppet, too, since non-puppetmasterd processes
# (including testing) put the logdir in :puppet, not in :puppetmasterd.
Puppet.config.use(:puppetmaster, :puppet)
# This has to come after we create the logdir with the :use above.
ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
end
# Migrate to the latest db schema.
def self.migrate
dbdir = nil
$:.each { |d|
tmp = File.join(d, "puppet/rails/database")
if FileTest.directory?(tmp)
dbdir = tmp
break
end
}
unless dbdir
raise Puppet::Error, "Could not find Puppet::Rails database dir"
end
begin
ActiveRecord::Migrator.migrate(dbdir)
rescue => detail
if Puppet[:trace]
puts detail.backtrace
end
raise Puppet::Error, "Could not migrate database: %s" % detail
end
end
# Tear down the database. Mostly only used during testing.
def self.teardown
unless Puppet.features.rails?
raise Puppet::DevError, "No activerecord, cannot init Puppet::Rails"
end
Puppet.config.use(:puppetmaster)
begin
ActiveRecord::Base.establish_connection(database_arguments())
rescue => detail
if Puppet[:trace]
puts detail.backtrace
end
raise Puppet::Error, "Could not connect to database: %s" % detail
end
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.drop_table t
end
end
end
if Puppet.features.rails?
require 'puppet/rails/host'
end
# $Id$
|