Skip to content

Commit b1f90fd

Browse files
committed
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.
1 parent 1e926b4 commit b1f90fd

32 files changed

+553
-438
lines changed

Modulefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name 'puppetlabs-mysql'
2-
version '0.0.1'
2+
version '0.1.0'
33
source 'git://github.com/puppetlabs/puppetlabs-mysql.git'
44
author 'Puppet Labs'
55
license 'Apache'

README.md

+59-44
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,83 @@
11
# Mysql module for Puppet
22

3+
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.
34

45
## Description
5-
This module has evolved and is originally based on work by David Schmitt.
6-
If anyone else was involved in the development of this module
7-
and wants credit, let Puppetlabs know.
6+
7+
This module is based on work by David Schmitt. The following contributor have contributed patches to this module (beyond Puppet Labs):
8+
9+
* Christian G. Warden
10+
* Daniel Black
11+
* Justin Ellison
12+
* Lowe Schmidt
13+
* Matthias Pigulla
14+
* William Van Hevelingen
815

916
## Usage
1017

1118
### mysql
1219
Installs the mysql-client package.
13-
<pre>
14-
class { 'mysql': }
15-
</pre>
20+
21+
class { 'mysql': }
1622

1723
### mysql::python
1824
Installs mysql bindings for python.
19-
<pre>
20-
class { 'mysql::python': }
21-
</pre>
25+
26+
class { 'mysql::python': }
2227

2328
### mysql::ruby
2429
Installs mysql bindings for ruby.
25-
<pre>
26-
class { 'mysql::ruby': }
27-
</pre>
30+
31+
class { 'mysql::ruby': }
2832

2933
### mysql::server
30-
Installs mysql-server, starts service, sets `root_pw`, and sets root.
31-
<pre>
32-
class { 'mysql::server':
33-
config_hash => { 'root_password' => 'foo' }
34-
}
35-
</pre>
34+
Installs mysql-server packages, configures my.cnf and starts mysqld service:
35+
36+
class { 'mysql::server':
37+
config_hash => { 'root_password' => 'foo' }
38+
}
3639

37-
Login information in `/etc/.my.cnf` and `/root/.my.cnf`.
40+
Database login information stored in `/root/.my.cnf`.
3841

3942
### mysql::db
4043
Creates a database with a user and assign some privileges.
4144

42-
<pre>
43-
mysql::db { 'mydb':
44-
user => 'myuser',
45-
password => 'mypass',
46-
host => 'localhost',
47-
grant => ['all'],
48-
}
49-
</pre>
45+
mysql::db { 'mydb':
46+
user => 'myuser',
47+
password => 'mypass',
48+
host => 'localhost',
49+
grant => ['all'],
50+
}
5051

5152
### Providers for database types:
52-
<pre>
53-
database { 'mydb':
54-
charset => 'latin1',
55-
}
56-
</pre>
57-
58-
<pre>
59-
database_user { 'bob@localhost':
60-
password_hash => mysql_password('foo')
61-
}
62-
</pre>
63-
64-
<pre>
65-
database_grant { 'user@localhost/database':
66-
privileges => ['all'] ,
67-
}
68-
</pre>
53+
MySQL provider supports puppet resources command:
54+
55+
$ puppet resource database
56+
database { 'information_schema':
57+
ensure => 'present',
58+
charset => 'utf8',
59+
}
60+
database { 'mysql':
61+
ensure => 'present',
62+
charset => 'latin1',
63+
}
64+
65+
The custom resources can be used in any other manifests:
66+
67+
database { 'mydb':
68+
charset => 'latin1',
69+
}
70+
71+
database_user { 'bob@localhost':
72+
password_hash => mysql_password('foo')
73+
}
74+
75+
database_grant { 'user@localhost/database':
76+
privileges => ['all'] ,
77+
}
78+
79+
A resource default can be specified to handle dependency:
80+
81+
Database {
82+
require => Class['mysql::server'],
83+
}

Rakefile

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
require 'rubygems'
12
require 'rake'
3+
require 'rspec/core/rake_task'
24
require 'fileutils'
35

4-
begin
5-
require 'rspec/core/rake_task'
6-
HAVE_RSPEC = true
7-
rescue LoadError
8-
HAVE_RSPEC = false
6+
task :default do
7+
system("rake -T")
98
end
109

11-
task :default => [:build]
10+
desc "Run all rspec-puppet tests"
11+
RSpec::Core::RakeTask.new(:spec) do |t|
12+
t.rspec_opts = ['--color']
13+
# ignores fixtures directory.
14+
t.pattern = 'spec/{classes,defines,unit}/**/*_spec.rb'
15+
end
1216

1317
def update_module_version
1418
gitdesc = %x{git describe}.chomp
@@ -39,9 +43,9 @@ task :clean do
3943
FileUtils.rm_rf("pkg/")
4044
end
4145

42-
if HAVE_RSPEC then
43-
desc 'Run all module spec tests (Requires rspec-puppet gem)'
44-
task :spec do
45-
system 'rspec --format d spec/'
46-
end
46+
desc "Check puppet manifests with puppet-lint"
47+
task :lint do
48+
# This requires pull request: https://github.com/rodjek/puppet-lint/pull/81
49+
system("puppet-lint manifests")
50+
system("puppet-lint tests")
4751
end

lib/puppet/parser/functions/mysql_password.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
require 'digest/sha1'
33

44
module Puppet::Parser::Functions
5-
newfunction(:mysql_password, :type => :rvalue) do |args|
6-
'*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase
7-
end
8-
end
5+
newfunction(:mysql_password, :type => :rvalue, :doc => <<-EOS
6+
Returns the mysql password hash from the clear text password.
7+
EOS
8+
) do |args|
9+
10+
raise(Puppet::ParseError, "mysql_password(): Wrong number of arguments " +
11+
"given (#{args.size} for 1)") if args.size != 1
912

13+
'*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase
14+
end
15+
end

lib/puppet/provider/database/default.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
Puppet::Type.type(:database).provide(:default) do
22

33
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."
4-
4+
5+
def self.instances
6+
[]
7+
end
8+
59
def create
610
return false
711
end
@@ -14,14 +18,11 @@ def exists?
1418
fail('This is just the default provider for database, all it does is fail')
1519
end
1620

17-
1821
def charset
1922
return false
2023
end
2124

2225
def charset=(value)
2326
return false
2427
end
25-
# retrieve the current set of mysql databases
2628
end
27-

lib/puppet/provider/database/mysql.rb

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
Puppet::Type.type(:database).provide(:mysql) do
22

3-
desc "Create mysql database."
3+
desc "Manages MySQL database."
44

55
defaultfor :kernel => 'Linux'
66

7-
optional_commands :mysqladmin => 'mysqladmin'
87
optional_commands :mysql => 'mysql'
9-
optional_commands :mysqlshow => 'mysqlshow'
10-
8+
optional_commands :mysqladmin => 'mysqladmin'
9+
10+
def self.instances
11+
mysql('-NBe', "show databases").split("\n").collect do |name|
12+
new(:name => name)
13+
end
14+
end
15+
1116
def create
12-
mysql('-NBe', "CREATE DATABASE #{@resource[:name]} CHARACTER SET #{resource[:charset]}")
17+
mysql('-NBe', "create database #{@resource[:name]} character set #{resource[:charset]}")
1318
end
1419

1520
def destroy
1621
mysqladmin('-f', 'drop', @resource[:name])
1722
end
1823

24+
def charset
25+
mysql('-NBe', "show create database #{resource[:name]}").match(/.*?(\S+)\s\*\//)[1]
26+
end
27+
28+
def charset=(value)
29+
mysql('-NBe', "alter database #{resource[:name]} CHARACTER SET #{value}")
30+
end
31+
1932
def exists?
2033
begin
2134
mysql('-NBe', "show databases").match(/^#{@resource[:name]}$/)
@@ -24,14 +37,6 @@ def exists?
2437
return nil
2538
end
2639
end
27-
28-
def charset
29-
mysql('-NBe', "show create database #{resource[:name]}").match(/.*?(\S+)\s\*\//)[1]
30-
end
3140

32-
def charset=(value)
33-
mysql('-NBe', "alter database #{resource[:name]} CHARACTER SET #{value}")
34-
end
35-
# retrieve the current set of mysql databases
3641
end
3742

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
# A grant is either global or per-db. This can be distinguished by the syntax
22
# of the name:
3-
# user@host => global
4-
# user@host/db => per-db
3+
# user@host => global
4+
# user@host/db => per-db
55

66
Puppet::Type.type(:database_grant).provide(:default) do
77

88
desc "Uses mysql as database."
99

10+
def self.instances
11+
[]
12+
end
13+
1014
def destroy
1115
return false
1216
end
17+
1318
def create
1419
return false
1520
end
16-
def exists?
21+
22+
def exists?
1723
fail('Default provider for database_grant should never be used')
1824
end
19-
25+
2026
end
2127

0 commit comments

Comments
 (0)