diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..8b1202a --- /dev/null +++ b/Rakefile @@ -0,0 +1,47 @@ +require 'rake' +require 'fileutils' + +begin + require 'rspec/core/rake_task' + HAVE_RSPEC = true +rescue LoadError + HAVE_RSPEC = false +end + +task :default => [:build] + +def update_module_version + gitdesc = %x{git describe}.chomp + semver = gitdesc.gsub(/v?(\d+\.\d+\.\d+)-?(.*)/) do + newver = "#{$1}" + newver << "git-#{$2}" unless $2.empty? + newver + end + modulefile = File.read("Modulefile") + modulefile.gsub!(/^\s*version\s+'.*?'/, "version '#{semver}'") + File.open("Modulefile", 'w') do |f| + f.write(modulefile) + end + semver +end + +desc "Build Puppet Module Package" +task :build do + system("gimli README*.markdown") + FileUtils.cp "Modulefile", "Modulefile.bak" + update_module_version + system("puppet-module build") + FileUtils.mv "Modulefile.bak", "Modulefile" +end + +desc "Clean the package directory" +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 +end diff --git a/manifests/db.pp b/manifests/db.pp index 4c07aa8..42ae0f7 100644 --- a/manifests/db.pp +++ b/manifests/db.pp @@ -31,32 +31,46 @@ define mysql::db ( $charset = 'utf8', $host = 'localhost', $grant='all', + $enforce_sql = false, $sql='' ) { + if $grant == 'all' { + $safe_grant = [ 'alter_priv','alter_routine_priv','create_priv','create_routine_priv','create_tmp_table_priv','create_view_priv','delete_priv','drop_priv','event_priv','execute_priv','grant_priv','index_priv','insert_priv','lock_tables_priv','references_priv','select_priv','show_view_priv','trigger_priv','update_priv'] + } else { + $safe_grant = $grant + } + database { $name: - ensure => present, - charset => $charset, + ensure => present, + charset => $charset, provider => 'mysql', - require => Class['mysql::server'] + require => Class['mysql::server'], + notify => Exec["${name}-import-import"], } + database_user{"${user}@${host}": - ensure => present, + ensure => present, password_hash => mysql_password($password), - provider => 'mysql', - require => Database[$name], + provider => 'mysql', + require => Database[$name], } + database_grant{"${user}@${host}/${name}": # privileges => [ 'alter_priv', 'insert_priv', 'select_priv', 'update_priv' ], - privileges => $grant, - provider => 'mysql', - require => Database_user["${user}@${host}"], + privileges => $safe_grant, + provider => 'mysql', + require => Database_user["${user}@${host}"], } + if($sql) { exec{"${name}-import-import": - command => "/usr/bin/mysql -u ${user} -p${password} -h ${host} ${name} < ${sql}", - logoutput => true, - require => Database_grant["${user}@${host}/${name}"], + command => "/usr/bin/mysql -u ${user} -p${password} -h ${host} ${name} < ${sql}", + logoutput => true, + refreshonly => $enforce_sql ? { + true => false, + false => true, + }, } } -} +} diff --git a/spec/classes/mysql_init_spec.rb b/spec/classes/mysql_init_spec.rb new file mode 100644 index 0000000..8ccf864 --- /dev/null +++ b/spec/classes/mysql_init_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' + +describe 'mysql' do + + it { should contain_class 'mysql' } +end diff --git a/spec/defines/mysql_db_spec.rb b/spec/defines/mysql_db_spec.rb new file mode 100644 index 0000000..a31ba49 --- /dev/null +++ b/spec/defines/mysql_db_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'mysql::db', :type => :define do + let(:title) { 'test_db' } + let(:params) { + {'user' => 'testuser', + 'password' => 'testpass', + 'enforce_sql' => false, + 'sql' => 'test_sql', + } + } + + it 'should set load of sql script to refreshonly' do + should create_resource('exec', 'test_db-import-import').with_param('refreshonly', true) + end +end diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 0000000..91cd642 --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1,6 @@ +--format +s +--colour +--loadby +mtime +--backtrace diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..c4c41bf --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,7 @@ +require 'puppet' +require 'rubygems' +require 'rspec-puppet' + +RSpec.configure do |c| + c.module_path = File.join(File.dirname(__FILE__), '../../') +end