-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for ProxySQL v2.x galera functionality
- Loading branch information
1 parent
028d97e
commit 747421d
Showing
10 changed files
with
344 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
lib/puppet/provider/proxy_mysql_galera_hostgroup/proxysql.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'proxysql')) | ||
Puppet::Type.type(:proxy_mysql_galera_hostgroup).provide(:proxysql, parent: Puppet::Provider::Proxysql) do | ||
desc 'Manage galera hostgroup for a ProxySQL instance.' | ||
commands mysql: 'mysql' | ||
|
||
# Build a property_hash containing all the discovered information about MySQL | ||
# Galera servers. | ||
def self.instances | ||
instances = [] | ||
hostgroups = mysql([defaults_file, '-NBe', | ||
'SELECT `writer_hostgroup`, `backup_writer_hostgroup`, `reader_hostgroup`, `offline_hostgroup`, `active`,`max_writers`,`writer_is_also_reader`,`max_transactions_behind`, `comment` FROM `mysql_galera_hostgroups`'].compact).split(%r{\n}) | ||
|
||
# To reduce the number of calls to MySQL we collect all the properties in | ||
# one big swoop. | ||
hostgroups.each do |line| | ||
writer_hostgroup, backup_writer_hostgroup, reader_hostgroup, offline_hostgroup, active, max_writers, writer_is_also_reader, max_transactions_behind, comment = line.split(%r{\t}) | ||
name = "#{writer_hostgroup}-#{backup_writer_hostgroup}-#{reader_hostgroup}-#{offline_hostgroup}" | ||
|
||
instances << new( | ||
name: name, | ||
ensure: :present, | ||
writer_hostgroup: writer_hostgroup, | ||
backup_writer_hostgroup: backup_writer_hostgroup, | ||
reader_hostgroup: reader_hostgroup, | ||
offline_hostgroup: offline_hostgroup, | ||
active: active, | ||
max_writers: max_writers, | ||
writer_is_also_reader: writer_is_also_reader, | ||
max_transactions_behind: max_transactions_behind, | ||
comment: comment | ||
) | ||
end | ||
instances | ||
end | ||
|
||
# We iterate over each proxy_mysql_galera_hostgroup entry in the catalog and compare it against | ||
# the contents of the property_hash generated by self.instances | ||
def self.prefetch(resources) | ||
hostgroups = instances | ||
resources.keys.each do |name| | ||
provider = hostgroups.find { |hostgroup| hostgroup.name == name } | ||
resources[name].provider = provider if provider | ||
end | ||
end | ||
|
||
def create | ||
_name = @resource[:name] | ||
writer_hostgroup = @resource.value(:writer_hostgroup) | ||
backup_writer_hostgroup = @resource.value(:backup_writer_hostgroup) | ||
reader_hostgroup = @resource.value(:reader_hostgroup) | ||
offline_hostgroup = @resource.value(:offline_hostgroup) | ||
active = @resource.value(:active) | ||
max_writers = @resource.value(:max_writers) | ||
writer_is_also_reader = @resource.value(:writer_is_also_reader) | ||
max_transactions_behind = @resource.value(:max_transactions_behind) | ||
comment = @resource.value(:comment) || '' | ||
|
||
query = 'INSERT INTO `mysql_galera_hostgroups` (`writer_hostgroup`, `backup_writer_hostgroup`, `reader_hostgroup`, `offline_hostgroup`, `active`,`max_writers`,`writer_is_also_reader`,`max_transactions_behind`, `comment`)' | ||
query << " VALUES (#{writer_hostgroup}, #{backup_writer_hostgroup}, #{reader_hostgroup}, #{offline_hostgroup}, #{active}, #{max_writers}, #{writer_is_also_reader}, #{max_transactions_behind}, '#{comment}')" | ||
mysql([defaults_file, '-e', query].compact) | ||
@property_hash[:ensure] = :present | ||
|
||
exists? ? (return true) : (return false) | ||
end | ||
|
||
def destroy | ||
writer_hostgroup = @resource.value(:writer_hostgroup) | ||
backup_writer_hostgroup = @resource.value(:backup_writer_hostgroup) | ||
reader_hostgroup = @resource.value(:reader_hostgroup) | ||
offline_hostgroup = @resource.value(:offline_hostgroup) | ||
query = 'DELETE FROM `mysql_galera_hostgroups`' | ||
query << " WHERE `writer_hostgroup` = #{writer_hostgroup} AND `backup_writer_hostgroup` = #{backup_writer_hostgroup} `reader_hostgroup` = #{reader_hostgroup} AND `offline_hostgroup` = #{offline_hostgroup}" | ||
mysql([defaults_file, '-e', query].compact) | ||
|
||
@property_hash.clear | ||
exists? ? (return false) : (return true) | ||
end | ||
|
||
def exists? | ||
@property_hash[:ensure] == :present || false | ||
end | ||
|
||
def flush | ||
@property_hash.clear | ||
load_to_runtime = @resource[:load_to_runtime] | ||
mysql([defaults_file, '-NBe', 'LOAD MYSQL SERVERS TO RUNTIME'].compact) if load_to_runtime == :true | ||
|
||
save_to_disk = @resource[:save_to_disk] | ||
mysql([defaults_file, '-NBe', 'SAVE MYSQL SERVERS TO DISK'].compact) if save_to_disk == :true | ||
end | ||
|
||
# Generates method for all properties of the property_hash | ||
mk_resource_methods | ||
|
||
def comment=(value) | ||
writer_hostgroup = @resource.value(:writer_hostgroup) | ||
backup_writer_hostgroup = @resource.value(:backup_writer_hostgroup) | ||
reader_hostgroup = @resource.value(:reader_hostgroup) | ||
offline_hostgroup = @resource.value(:offline_hostgroup) | ||
|
||
query = "UPDATE mysql_galera_hostgroups SET `comment` = '#{value}'" | ||
query << " WHERE `writer_hostgroup` = #{writer_hostgroup} AND `backup_writer_hostgroup` = #{backup_writer_hostgroup}AND `reader_hostgroup` = #{reader_hostgroup} AND `offline_hostgroup` = #{offline_hostgroup}" | ||
mysql([defaults_file, '-e', query].compact) | ||
|
||
@property_hash.clear | ||
exists? ? (return false) : (return true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# This has to be a separate type to enable collecting | ||
Puppet::Type.newtype(:proxy_mysql_galera_hostgroup) do | ||
@doc = 'Manage a ProxySQL mysql_galera_hostgroup.' | ||
|
||
ensurable | ||
|
||
autorequire(:file) { '/root/.my.cnf' } | ||
autorequire(:class) { 'mysql::client' } | ||
autorequire(:service) { 'proxysql' } | ||
|
||
validate do | ||
raise('writer_hostgroup parameter is required.') if (self[:ensure] == :present) && self[:writer_hostgroup].nil? | ||
raise('backup_writer_hostgroup parameter is required.') if (self[:ensure] == :present) && self[:backup_writer_hostgroup].nil? | ||
raise('reader_hostgroup parameter is required.') if (self[:ensure] == :present) && self[:reader_hostgroup].nil? | ||
raise('offline_hostgroup parameter is required.') if (self[:ensure] == :present) && self[:offline_hostgroup].nil? | ||
raise('name must match writer_hostgroup-backup_writer_hostgroup-reader_hostgroup-offline_hostgroup parameters') if self[:name] != "#{self[:writer_hostgroup]}-#{self[:backup_writer_hostgroup]}-#{self[:reader_hostgroup]}-#{self[:offline_hostgroup]}" | ||
end | ||
|
||
newparam(:name, namevar: true) do | ||
desc 'name to describe the hostgroup config' | ||
end | ||
|
||
newparam(:load_to_runtime) do | ||
desc 'Load this entry to the active runtime.' | ||
defaultto :true | ||
newvalues(:true, :false) | ||
end | ||
|
||
newparam(:save_to_disk) do | ||
desc 'Perist this entry to the disk.' | ||
defaultto :true | ||
newvalues(:true, :false) | ||
end | ||
|
||
newproperty(:writer_hostgroup) do | ||
desc 'Writer hostgroup.' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:backup_writer_hostgroup) do | ||
desc 'Backup Writer hostgroup.' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:reader_hostgroup) do | ||
desc 'Reader hostgroup.' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:offline_hostgroup) do | ||
desc 'Offline hostgroup.' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:active) do | ||
desc 'active' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:max_writers) do | ||
desc 'Maximum Writers' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:writer_is_also_reader) do | ||
desc 'A writer is also used for reading' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:max_transactions_behind) do | ||
desc 'Maximum Transaction Galera Node out-of-sync' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:comment) do | ||
desc 'text field can be used to store any arbitrary data.' | ||
newvalue(%r{[\w+]}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.