Class: Puppet::Provider::Mysql

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/mysql.rb

Overview

Puppet provider for mysql

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cmd_options(options) ⇒ Object

Take in potential options and build up a query string with them.



170
171
172
173
174
175
176
# File 'lib/puppet/provider/mysql.rb', line 170

def self.cmd_options(options)
  option_string = ''
  options.each do |opt|
    option_string += ' WITH GRANT OPTION' if opt == 'GRANT'
  end
  option_string
end

.cmd_privs(privileges) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/puppet/provider/mysql.rb', line 159

def self.cmd_privs(privileges)
  return 'ALL PRIVILEGES' if privileges.include?('ALL')
  priv_string = ''
  privileges.each do |priv|
    priv_string += "#{priv}, "
  end
  # Remove trailing , from the last element.
  priv_string.sub(%r{, $}, '')
end

.cmd_table(table) ⇒ Object

Take root.* and return ON ‘root`.*



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/puppet/provider/mysql.rb', line 144

def self.cmd_table(table)
  table_string = ''

  # We can't escape *.* so special case this.
  table_string += if table == '*.*'
                    '*.*'
                  # Special case also for FUNCTIONs and PROCEDUREs
                  elsif table.start_with?('FUNCTION ', 'PROCEDURE ')
                    table.sub(%r{^(FUNCTION|PROCEDURE) (.*)(\..*)}, '\1 `\2`\3')
                  else
                    table.sub(%r{^(.*)(\..*)}, '`\1`\2')
                  end
  table_string
end

.cmd_user(user) ⇒ Object

Take root@localhost and munge it to ‘root’@‘localhost’ Take root@id123@localhost and munge it to ‘root@id123’@‘localhost’



139
140
141
# File 'lib/puppet/provider/mysql.rb', line 139

def self.cmd_user(user)
  "'#{user.reverse.sub('@', "'@'").reverse}'"
end

.defaults_fileObject

Optional defaults file



47
48
49
# File 'lib/puppet/provider/mysql.rb', line 47

def self.defaults_file
  "--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf" if File.file?("#{Facter.value(:root_home)}/.my.cnf")
end

.mysql_caller(text_of_sql, type) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet/provider/mysql.rb', line 104

def self.mysql_caller(text_of_sql, type)
  if type.eql? 'system'
    if File.file?("#{Facter.value(:root_home)}/.mylogin.cnf")
      ENV['MYSQL_TEST_LOGIN_FILE'] = "#{Facter.value(:root_home)}/.mylogin.cnf"
      mysql_raw([system_database, '-e', text_of_sql].flatten.compact).scrub
    else
      mysql_raw([defaults_file, system_database, '-e', text_of_sql].flatten.compact).scrub
    end
  elsif type.eql? 'regular'
    if File.file?("#{Facter.value(:root_home)}/.mylogin.cnf")
      ENV['MYSQL_TEST_LOGIN_FILE'] = "#{Facter.value(:root_home)}/.mylogin.cnf"
      mysql_raw(['-NBe', text_of_sql].flatten.compact).scrub
    else
      mysql_raw([defaults_file, '-NBe', text_of_sql].flatten.compact).scrub
    end
  else
    raise Puppet::Error, _("#mysql_caller: Unrecognised type '%{type}'" % { type: type })
  end
end

.mysqld_typeObject



51
52
53
54
55
56
# File 'lib/puppet/provider/mysql.rb', line 51

def self.mysqld_type
  # find the mysql "dialect" like mariadb / mysql etc.
  mysqld_version_string.scan(%r{mariadb}i) { return 'mariadb' }
  mysqld_version_string.scan(%r{\s\(percona}i) { return 'percona' }
  'mysql'
end

.mysqld_versionObject



73
74
75
76
77
78
# File 'lib/puppet/provider/mysql.rb', line 73

def self.mysqld_version
  # NOTE: be prepared for '5.7.6-rc-log' etc results
  #       versioncmp detects 5.7.6-log to be newer then 5.7.6
  #       this is why we need the trimming.
  mysqld_version_string&.scan(%r{\d+\.\d+\.\d+})&.first
end

.mysqld_version_stringObject



62
63
64
65
66
67
# File 'lib/puppet/provider/mysql.rb', line 62

def self.mysqld_version_string
  # As the possibility of the mysqld being remote we need to allow the version string to be overridden,
  # this can be done by facter.value as seen below. In the case that it has not been set and the facter
  # value is nil we use the mysql -v command to ensure we report the correct version of mysql for later use cases.
  @mysqld_version_string ||= Facter.value(:mysqld_version) || mysqld('-V')
end

.newer_than(forks_versions) ⇒ Object



84
85
86
# File 'lib/puppet/provider/mysql.rb', line 84

def self.newer_than(forks_versions)
  forks_versions.key?(mysqld_type) && Puppet::Util::Package.versioncmp(mysqld_version, forks_versions[mysqld_type]) >= 0
end

.older_than(forks_versions) ⇒ Object



92
93
94
# File 'lib/puppet/provider/mysql.rb', line 92

def self.older_than(forks_versions)
  forks_versions.key?(mysqld_type) && Puppet::Util::Package.versioncmp(mysqld_version, forks_versions[mysqld_type]) < 0
end

.system_databaseObject

Optional parameter to run a statement on the MySQL system database.



129
130
131
# File 'lib/puppet/provider/mysql.rb', line 129

def self.system_database
  '--database=mysql'
end

.usersObject



124
125
126
# File 'lib/puppet/provider/mysql.rb', line 124

def self.users
  mysql_caller("SELECT CONCAT(User, '@',Host) AS User FROM mysql.user", 'regular').split("\n")
end

Instance Method Details

#defaults_fileObject



100
101
102
# File 'lib/puppet/provider/mysql.rb', line 100

def defaults_file
  self.class.defaults_file
end

#mysqld_typeObject



58
59
60
# File 'lib/puppet/provider/mysql.rb', line 58

def mysqld_type
  self.class.mysqld_type
end

#mysqld_versionObject



80
81
82
# File 'lib/puppet/provider/mysql.rb', line 80

def mysqld_version
  self.class.mysqld_version
end

#mysqld_version_stringObject



69
70
71
# File 'lib/puppet/provider/mysql.rb', line 69

def mysqld_version_string
  self.class.mysqld_version_string
end

#newer_than(forks_versions) ⇒ Object



88
89
90
# File 'lib/puppet/provider/mysql.rb', line 88

def newer_than(forks_versions)
  self.class.newer_than(forks_versions)
end

#older_than(forks_versions) ⇒ Object



96
97
98
# File 'lib/puppet/provider/mysql.rb', line 96

def older_than(forks_versions)
  self.class.older_than(forks_versions)
end

#system_databaseObject



133
134
135
# File 'lib/puppet/provider/mysql.rb', line 133

def system_database
  self.class.system_database
end