diff options
author | root <root@mysql.edv-bus.at> | 2007-10-18 11:28:00 +0200 |
---|---|---|
committer | root <root@mysql.edv-bus.at> | 2007-10-18 11:28:00 +0200 |
commit | 813025c80066504345e72ead609075751fbc5b80 (patch) | |
tree | c004c9d674fd2e65c817ac77e56991d5ad9d5c7e /plugins/puppet/provider/mysql_user/mysql.rb | |
parent | 38081c9c5e26933946fb1c666218dc66c6f6bde1 (diff) | |
download | puppet-mysql-813025c80066504345e72ead609075751fbc5b80.tar.gz puppet-mysql-813025c80066504345e72ead609075751fbc5b80.tar.xz puppet-mysql-813025c80066504345e72ead609075751fbc5b80.zip |
mysql: prefetching, tests, order independent privileges
This commit implements prefetching for the mysql_database and
the mysql_user types. This enables them to e.g. set the password_hash
right when creating the user.
There is now a directory with tests.
Privileges are now order independent and do not cause spurious notices
anymore.
Diffstat (limited to 'plugins/puppet/provider/mysql_user/mysql.rb')
-rw-r--r-- | plugins/puppet/provider/mysql_user/mysql.rb | 61 |
1 files changed, 51 insertions, 10 deletions
diff --git a/plugins/puppet/provider/mysql_user/mysql.rb b/plugins/puppet/provider/mysql_user/mysql.rb index 1c48d71..8238620 100644 --- a/plugins/puppet/provider/mysql_user/mysql.rb +++ b/plugins/puppet/provider/mysql_user/mysql.rb @@ -1,27 +1,68 @@ -Puppet::Type.type(:mysql_user).provide(:mysql) do +require 'puppet/provider/package' + +Puppet::Type.type(:mysql_user).provide(:mysql, + # T'is funny business, this code is quite generic + :parent => Puppet::Provider::Package) do + desc "Use mysql as database." commands :mysql => '/usr/bin/mysql' + # retrieve the current set of mysql users + def self.instances + users = [] + + cmd = "#{command(:mysql)} mysql -NBe 'select concat(user, \"@\", host), password from user'" + execpipe(cmd) do |process| + process.each do |line| + users << new( query_line_to_hash(line) ) + end + end + return users + end + + def self.query_line_to_hash(line) + fields = line.chomp.split(/\t/) + { + :name => fields[0], + :password_hash => fields[1], + :ensure => :present + } + end + + def query + result = {} + + cmd = "#{command(:mysql)} -NBe 'select concat(user, \"@\", host), password from user where concat(user, \"@\", host) = \"%s\"'" % @resource[:name] + execpipe(cmd) do |process| + process.each do |line| + unless result.empty? + raise Puppet::Error, + "Got multiple results for user '%s'" % @resource[:name] + end + result = query_line_to_hash(line) + end + end + result + end + def create - mysql "mysql", "-e", "create user '%s@%s' identified by '%s'" % [ @resource[:name], @resource[:host], @resource[:password] ] + mysql "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.should(:password_hash) ] end + def destroy - mysql "mysql", "-e", "drop user '%s@%s'" % [ @resource[:name], @resource[:host] ] + mysql "mysql", "-e", "drop user '%s'" % @resource[:name].sub("@", "'@'") end + def exists? - if /^#{@resource[:name]}@#{@resource[:host]}$/.match( mysql( "mysql", "-Be", 'SELECT CONCAT(user, "@", host) FROM user' ) ) - true - else - false - end + not mysql("mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource[:name]).empty? end def password_hash - mysql("mysql", "-NBe", "select password from user where user='#{@resource[:name]}' and host='#{@resource[:host]}'").chomp + @property_hash[:password_hash] end def password_hash=(string) - mysql "mysql", "-e", "SET PASSWORD FOR '#{@resource[:name]}'@'#{@resource[:host]}' = '#{string}'" + mysql "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] end end |