Permalink
Browse files

Major refactor of mysql module.

This is a major change to the module and would be released as a new
version.

* Add self.instances to database and database_user for puppet resource.
* Update database provider to use flush method.
* Update module to conform to puppet-lint recommendations.
* Cleanup some unecessary logic in mysql::db define type.
* Move mysql_restart to config class.
* Use class to class dependency instead of resource dependency.
* Change appropriate rspec-puppet tests.
* Add fixtures directory to simplify testing.
* Update raketask and spec_helper to reflect fixture changes.
* Update mysql_password function to support validation.
* Move client installation to a separate class.
* Update documentation and readme.
  • Loading branch information...
1 parent 1e926b4 commit b1f90fd1d2581186c6fb07c10a4a76f5c5a073b1 Nan Liu committed Feb 9, 2012
View
@@ -1,5 +1,5 @@
name 'puppetlabs-mysql'
-version '0.0.1'
+version '0.1.0'
source 'git://github.com/puppetlabs/puppetlabs-mysql.git'
author 'Puppet Labs'
license 'Apache'
View
103 README.md
@@ -1,68 +1,83 @@
# Mysql module for Puppet
+This module manages mysql on Linux (RedHat/Debian) distros. A native mysql provider implements database resource type to handle database, database user, and database permission.
## Description
-This module has evolved and is originally based on work by David Schmitt.
-If anyone else was involved in the development of this module
-and wants credit, let Puppetlabs know.
+
+This module is based on work by David Schmitt. The following contributor have contributed patches to this module (beyond Puppet Labs):
+
+* Christian G. Warden
+* Daniel Black
+* Justin Ellison
+* Lowe Schmidt
+* Matthias Pigulla
+* William Van Hevelingen
## Usage
### mysql
Installs the mysql-client package.
-<pre>
-class { 'mysql': }
-</pre>
+
+ class { 'mysql': }
### mysql::python
Installs mysql bindings for python.
-<pre>
-class { 'mysql::python': }
-</pre>
+
+ class { 'mysql::python': }
### mysql::ruby
Installs mysql bindings for ruby.
-<pre>
-class { 'mysql::ruby': }
-</pre>
+
+ class { 'mysql::ruby': }
### mysql::server
-Installs mysql-server, starts service, sets `root_pw`, and sets root.
-<pre>
-class { 'mysql::server':
- config_hash => { 'root_password' => 'foo' }
-}
-</pre>
+Installs mysql-server packages, configures my.cnf and starts mysqld service:
+
+ class { 'mysql::server':
+ config_hash => { 'root_password' => 'foo' }
+ }
-Login information in `/etc/.my.cnf` and `/root/.my.cnf`.
+Database login information stored in `/root/.my.cnf`.
### mysql::db
Creates a database with a user and assign some privileges.
-<pre>
-mysql::db { 'mydb':
- user => 'myuser',
- password => 'mypass',
- host => 'localhost',
- grant => ['all'],
-}
-</pre>
+ mysql::db { 'mydb':
+ user => 'myuser',
+ password => 'mypass',
+ host => 'localhost',
+ grant => ['all'],
+ }
### Providers for database types:
-<pre>
-database { 'mydb':
- charset => 'latin1',
-}
-</pre>
-
-<pre>
-database_user { 'bob@localhost':
- password_hash => mysql_password('foo')
-}
-</pre>
-
-<pre>
-database_grant { 'user@localhost/database':
- privileges => ['all'] ,
-}
-</pre>
+MySQL provider supports puppet resources command:
+
+ $ puppet resource database
+ database { 'information_schema':
+ ensure => 'present',
+ charset => 'utf8',
+ }
+ database { 'mysql':
+ ensure => 'present',
+ charset => 'latin1',
+ }
+
+The custom resources can be used in any other manifests:
+
+ database { 'mydb':
+ charset => 'latin1',
+ }
+
+ database_user { 'bob@localhost':
+ password_hash => mysql_password('foo')
+ }
+
+ database_grant { 'user@localhost/database':
+ privileges => ['all'] ,
+ }
+
+A resource default can be specified to handle dependency:
+
+ Database {
+ require => Class['mysql::server'],
+ }
View
@@ -1,14 +1,18 @@
+require 'rubygems'
require 'rake'
+require 'rspec/core/rake_task'
require 'fileutils'
-begin
- require 'rspec/core/rake_task'
- HAVE_RSPEC = true
-rescue LoadError
- HAVE_RSPEC = false
+task :default do
+ system("rake -T")
end
-task :default => [:build]
+desc "Run all rspec-puppet tests"
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.rspec_opts = ['--color']
+ # ignores fixtures directory.
+ t.pattern = 'spec/{classes,defines,unit}/**/*_spec.rb'
+end
def update_module_version
gitdesc = %x{git describe}.chomp
@@ -39,9 +43,9 @@ task :clean do
FileUtils.rm_rf("pkg/")
end
-if HAVE_RSPEC then
- desc 'Run all module spec tests (Requires rspec-puppet gem)'
- task :spec do
- system 'rspec --format d spec/'
- end
+desc "Check puppet manifests with puppet-lint"
+task :lint do
+ # This requires pull request: https://github.com/rodjek/puppet-lint/pull/81
+ system("puppet-lint manifests")
+ system("puppet-lint tests")
end
@@ -2,8 +2,14 @@
require 'digest/sha1'
module Puppet::Parser::Functions
- newfunction(:mysql_password, :type => :rvalue) do |args|
- '*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase
- end
-end
+ newfunction(:mysql_password, :type => :rvalue, :doc => <<-EOS
+ Returns the mysql password hash from the clear text password.
+ EOS
+ ) do |args|
+
+ raise(Puppet::ParseError, "mysql_password(): Wrong number of arguments " +
+ "given (#{args.size} for 1)") if args.size != 1
+ '*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase
+ end
+end
@@ -1,7 +1,11 @@
Puppet::Type.type(:database).provide(:default) do
desc "This is a default provider that does nothing. This allows us to install mysql on the same puppet run where we want to use it."
-
+
+ def self.instances
+ []
+ end
+
def create
return false
end
@@ -14,14 +18,11 @@ def exists?
fail('This is just the default provider for database, all it does is fail')
end
-
def charset
return false
end
def charset=(value)
return false
end
- # retrieve the current set of mysql databases
end
-
@@ -1,21 +1,34 @@
Puppet::Type.type(:database).provide(:mysql) do
- desc "Create mysql database."
+ desc "Manages MySQL database."
defaultfor :kernel => 'Linux'
- optional_commands :mysqladmin => 'mysqladmin'
optional_commands :mysql => 'mysql'
- optional_commands :mysqlshow => 'mysqlshow'
-
+ optional_commands :mysqladmin => 'mysqladmin'
+
+ def self.instances
+ mysql('-NBe', "show databases").split("\n").collect do |name|
+ new(:name => name)
+ end
+ end
+
def create
- mysql('-NBe', "CREATE DATABASE #{@resource[:name]} CHARACTER SET #{resource[:charset]}")
+ mysql('-NBe', "create database #{@resource[:name]} character set #{resource[:charset]}")
end
def destroy
mysqladmin('-f', 'drop', @resource[:name])
end
+ def charset
+ mysql('-NBe', "show create database #{resource[:name]}").match(/.*?(\S+)\s\*\//)[1]
+ end
+
+ def charset=(value)
+ mysql('-NBe', "alter database #{resource[:name]} CHARACTER SET #{value}")
+ end
+
def exists?
begin
mysql('-NBe', "show databases").match(/^#{@resource[:name]}$/)
@@ -24,14 +37,6 @@ def exists?
return nil
end
end
-
- def charset
- mysql('-NBe', "show create database #{resource[:name]}").match(/.*?(\S+)\s\*\//)[1]
- end
- def charset=(value)
- mysql('-NBe', "alter database #{resource[:name]} CHARACTER SET #{value}")
- end
- # retrieve the current set of mysql databases
end
@@ -1,21 +1,27 @@
# A grant is either global or per-db. This can be distinguished by the syntax
# of the name:
-# user@host => global
-# user@host/db => per-db
+# user@host => global
+# user@host/db => per-db
Puppet::Type.type(:database_grant).provide(:default) do
desc "Uses mysql as database."
+ def self.instances
+ []
+ end
+
def destroy
return false
end
+
def create
return false
end
- def exists?
+
+ def exists?
fail('Default provider for database_grant should never be used')
end
-
+
end
Oops, something went wrong.

0 comments on commit b1f90fd

Please sign in to comment.