Mercurial > repos > other > Puppet
annotate modules/mysql/lib/puppet/provider/mysql.rb @ 482:d83de9b3a62b default tip
Update hiera.yaml within Puppet config
Forgot that we manage it from here. Now has content to match
new packages
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 30 Aug 2024 16:10:36 +0100 |
parents | adf6fe9bbc17 |
children |
rev | line source |
---|---|
389 | 1 # frozen_string_literal: true |
2 | |
244 | 3 # Puppet provider for mysql |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 class Puppet::Provider::Mysql < Puppet::Provider |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 # Without initvars commands won't work. |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 initvars |
244 | 7 |
8 # Make sure we find mysql commands on CentOS and FreeBSD | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
389
diff
changeset
|
9 ENV['PATH'] = "#{ENV.fetch('PATH', nil)}:/usr/libexec:/usr/local/libexec:/usr/local/bin" |
244 | 10 ENV['LD_LIBRARY_PATH'] = [ |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
389
diff
changeset
|
11 ENV.fetch('LD_LIBRARY_PATH', nil), |
244 | 12 '/usr/lib', |
13 '/usr/lib64', | |
14 '/opt/rh/rh-mysql56/root/usr/lib', | |
15 '/opt/rh/rh-mysql56/root/usr/lib64', | |
16 '/opt/rh/rh-mysql57/root/usr/lib', | |
17 '/opt/rh/rh-mysql57/root/usr/lib64', | |
389 | 18 '/opt/rh/rh-mysql80/root/usr/lib', |
19 '/opt/rh/rh-mysql80/root/usr/lib64', | |
244 | 20 '/opt/rh/rh-mariadb100/root/usr/lib', |
21 '/opt/rh/rh-mariadb100/root/usr/lib64', | |
22 '/opt/rh/rh-mariadb101/root/usr/lib', | |
23 '/opt/rh/rh-mariadb101/root/usr/lib64', | |
24 '/opt/rh/rh-mariadb102/root/usr/lib', | |
25 '/opt/rh/rh-mariadb102/root/usr/lib64', | |
26 '/opt/rh/rh-mariadb103/root/usr/lib', | |
27 '/opt/rh/rh-mariadb103/root/usr/lib64', | |
28 '/opt/rh/mysql55/root/usr/lib', | |
29 '/opt/rh/mysql55/root/usr/lib64', | |
30 '/opt/rh/mariadb55/root/usr/lib', | |
31 '/opt/rh/mariadb55/root/usr/lib64', | |
32 '/usr/mysql/5.5/lib', | |
33 '/usr/mysql/5.5/lib64', | |
34 '/usr/mysql/5.6/lib', | |
35 '/usr/mysql/5.6/lib64', | |
36 '/usr/mysql/5.7/lib', | |
37 '/usr/mysql/5.7/lib64', | |
38 ].join(':') | |
39 | |
40 # rubocop:disable Style/HashSyntax | |
41 commands :mysql_raw => 'mysql' | |
42 commands :mysqld => 'mysqld' | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 commands :mysqladmin => 'mysqladmin' |
244 | 44 # rubocop:enable Style/HashSyntax |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 # Optional defaults file |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 def self.defaults_file |
244 | 48 "--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf" if File.file?("#{Facter.value(:root_home)}/.my.cnf") |
49 end | |
50 | |
51 def self.mysqld_type | |
52 # find the mysql "dialect" like mariadb / mysql etc. | |
53 mysqld_version_string.scan(%r{mariadb}i) { return 'mariadb' } | |
54 mysqld_version_string.scan(%r{\s\(percona}i) { return 'percona' } | |
55 'mysql' | |
56 end | |
57 | |
58 def mysqld_type | |
59 self.class.mysqld_type | |
60 end | |
61 | |
62 def self.mysqld_version_string | |
63 # As the possibility of the mysqld being remote we need to allow the version string to be overridden, | |
64 # this can be done by facter.value as seen below. In the case that it has not been set and the facter | |
65 # value is nil we use the mysql -v command to ensure we report the correct version of mysql for later use cases. | |
66 @mysqld_version_string ||= Facter.value(:mysqld_version) || mysqld('-V') | |
67 end | |
68 | |
69 def mysqld_version_string | |
70 self.class.mysqld_version_string | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 end |
244 | 72 |
73 def self.mysqld_version | |
389 | 74 # NOTE: be prepared for '5.7.6-rc-log' etc results |
244 | 75 # versioncmp detects 5.7.6-log to be newer then 5.7.6 |
76 # this is why we need the trimming. | |
389 | 77 mysqld_version_string&.scan(%r{\d+\.\d+\.\d+})&.first |
244 | 78 end |
79 | |
80 def mysqld_version | |
81 self.class.mysqld_version | |
82 end | |
83 | |
84 def self.newer_than(forks_versions) | |
389 | 85 forks_versions.key?(mysqld_type) && Puppet::Util::Package.versioncmp(mysqld_version, forks_versions[mysqld_type]) >= 0 |
244 | 86 end |
87 | |
88 def newer_than(forks_versions) | |
89 self.class.newer_than(forks_versions) | |
90 end | |
91 | |
92 def self.older_than(forks_versions) | |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
389
diff
changeset
|
93 forks_versions.key?(mysqld_type) && Puppet::Util::Package.versioncmp(mysqld_version, forks_versions[mysqld_type]).negative? |
244 | 94 end |
95 | |
96 def older_than(forks_versions) | |
97 self.class.older_than(forks_versions) | |
98 end | |
99 | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 def defaults_file |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
101 self.class.defaults_file |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
103 |
244 | 104 def self.mysql_caller(text_of_sql, type) |
105 if type.eql? 'system' | |
106 if File.file?("#{Facter.value(:root_home)}/.mylogin.cnf") | |
107 ENV['MYSQL_TEST_LOGIN_FILE'] = "#{Facter.value(:root_home)}/.mylogin.cnf" | |
389 | 108 mysql_raw([system_database, '-e', text_of_sql].flatten.compact).scrub |
244 | 109 else |
389 | 110 mysql_raw([defaults_file, system_database, '-e', text_of_sql].flatten.compact).scrub |
244 | 111 end |
112 elsif type.eql? 'regular' | |
113 if File.file?("#{Facter.value(:root_home)}/.mylogin.cnf") | |
114 ENV['MYSQL_TEST_LOGIN_FILE'] = "#{Facter.value(:root_home)}/.mylogin.cnf" | |
389 | 115 mysql_raw(['-NBe', text_of_sql].flatten.compact).scrub |
244 | 116 else |
389 | 117 mysql_raw([defaults_file, '-NBe', text_of_sql].flatten.compact).scrub |
244 | 118 end |
119 else | |
120 raise Puppet::Error, _("#mysql_caller: Unrecognised type '%{type}'" % { type: type }) | |
121 end | |
122 end | |
123 | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
124 def self.users |
244 | 125 mysql_caller("SELECT CONCAT(User, '@',Host) AS User FROM mysql.user", 'regular').split("\n") |
126 end | |
127 | |
128 # Optional parameter to run a statement on the MySQL system database. | |
129 def self.system_database | |
130 '--database=mysql' | |
131 end | |
132 | |
133 def system_database | |
134 self.class.system_database | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
135 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
136 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
137 # Take root@localhost and munge it to 'root'@'localhost' |
244 | 138 # Take root@id123@localhost and munge it to 'root@id123'@'localhost' |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
139 def self.cmd_user(user) |
244 | 140 "'#{user.reverse.sub('@', "'@'").reverse}'" |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
141 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
142 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
143 # Take root.* and return ON `root`.* |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
144 def self.cmd_table(table) |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
145 table_string = '' |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
146 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
147 # We can't escape *.* so special case this. |
389 | 148 table_string += if table == '*.*' |
244 | 149 '*.*' |
150 # Special case also for FUNCTIONs and PROCEDUREs | |
151 elsif table.start_with?('FUNCTION ', 'PROCEDURE ') | |
152 table.sub(%r{^(FUNCTION|PROCEDURE) (.*)(\..*)}, '\1 `\2`\3') | |
153 else | |
154 table.sub(%r{^(.*)(\..*)}, '`\1`\2') | |
155 end | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
156 table_string |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
157 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
158 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
159 def self.cmd_privs(privileges) |
244 | 160 return 'ALL PRIVILEGES' if privileges.include?('ALL') |
478
adf6fe9bbc17
Update Puppet modules to latest versions
IBBoard <dev@ibboard.co.uk>
parents:
389
diff
changeset
|
161 |
244 | 162 priv_string = '' |
163 privileges.each do |priv| | |
389 | 164 priv_string += "#{priv}, " |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
165 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
166 # Remove trailing , from the last element. |
244 | 167 priv_string.sub(%r{, $}, '') |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
168 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
169 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
170 # Take in potential options and build up a query string with them. |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
171 def self.cmd_options(options) |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
172 option_string = '' |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
173 options.each do |opt| |
389 | 174 option_string += ' WITH GRANT OPTION' if opt == 'GRANT' |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
175 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
176 option_string |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
177 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
178 end |