summaryrefslogtreecommitdiff
path: root/lib/puppet/provider/mysql_database/mysql.rb
blob: 18b0a5af67ee4e12cc4694b289cd0f203876d4a2 (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
require 'puppet/provider/package'

Puppet::Type.type(:mysql_database).provide(:mysql,
    :parent => Puppet::Provider::Package) do

  desc "Use mysql as database."
  commands :mysqladmin => '/usr/bin/mysqladmin'
  commands :mysql => '/usr/bin/mysql'

  # retrieve the current set of mysql users
  def self.instances
    dbs = []

    cmd = "#{command(:mysql)} mysql -NBe 'show databases'"
    execpipe(cmd) do |process|
      process.each do |line|
        dbs << new( { :ensure => :present, :name => line.chomp } )
      end
    end
    return dbs
  end

  def query
    result = {
      :name => @resource[:name],
      :ensure => :absent
    }

    cmd = "#{command(:mysql)} mysql -NBe 'show databases'"
    execpipe(cmd) do |process|
      process.each do |line|
        if line.chomp.eql?(@resource[:name])
          result[:ensure] = :present
        end
      end
    end
    result
  end

  def create
    mysqladmin "create", @resource[:name]
  end
  def destroy
    mysqladmin "-f", "drop", @resource[:name]
  end

  def exists?
    if mysql("mysql", "-NBe", "show databases").match(/^#{@resource[:name]}$/)
      true
    else
      false
    end
  end
end