summaryrefslogtreecommitdiffstats
path: root/lib/puppet/rails.rb
blob: 2a5b31408fa0844c1bb73af78030f31ded374b47 (plain)
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
# Load the appropriate libraries, or set a class indicating they aren't available

require 'facter'
require 'puppet'

begin
    require 'active_record'
rescue LoadError => detail
    if Facter["operatingsystem"].value == "Debian"
        count = 0
        Dir.entries("/usr/share/rails").each do |dir|
            libdir = File.join("/usr/share/rails", dir, "lib")
            if FileTest.exists?(libdir) and ! $:.include?(libdir)
                count += 1
                $: << libdir
            end
        end

        if count > 0
            retry
        end
    end
end

module Puppet::Rails
    Puppet.config.setdefaults(:puppetmaster,
        :dblocation => { :default => "$statedir/clientconfigs.sqlite3",
            :mode => 0600,
            :desc => "The database cache for client configurations.  Used for
                querying within the language."
        },
        :dbadapter => [ "sqlite3", "The type of database to use." ],
        :dbname => [ "puppet", "The name of the database to use." ],
        :dbserver => [ "puppet", "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/puppetrails.log",
            :mode => 0600,
            :desc => "Where Rails-specific logs are sent"
        }
    )

    # Set up our database connection.  It'd be nice to have a "use" system
    # that could make callbacks.
    def self.init
        unless defined? @inited and @inited
            Puppet.config.use(:puppet)
            Puppet.config.use(:puppetmaster)

            ActiveRecord::Base.logger = Logger.new(Puppet[:railslog])
            args = {:adapter => Puppet[:dbadapter]}

            case Puppet[:dbadapter]
            when "sqlite3":
                args[:database] = Puppet[:dblocation]
            when "mysql":
                args[:host]     = Puppet[:dbserver]
                args[:username] = Puppet[:dbuser]
                args[:password] = Puppet[:dbpassword]
                args[:database] = Puppet[:dbname]
            end

            ActiveRecord::Base.establish_connection(args)

            unless FileTest.exists?(args[:database])
                require 'puppet/rails/database'
                Puppet::Rails::Database.up
            end

            @inited = true
        end
    end
end

if defined? ActiveRecord::Base
    require 'puppet/rails/host'
end

# $Id$