-
-
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.
Merge pull request #58 from MaxFedotov/proxysql_cluster
add support for Proxysql cluster
- Loading branch information
Showing
8 changed files
with
280 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'proxysql')) | ||
Puppet::Type.type(:proxy_cluster).provide(:proxysql, parent: Puppet::Provider::Proxysql) do | ||
desc 'Manage cluster for a ProxySQL instance.' | ||
commands mysql: 'mysql' | ||
|
||
def self.mysql_running | ||
system("mysql #{defaults_file} -NBe 'SELECT 1' >out 2>&1", out: '/dev/null') | ||
end | ||
|
||
# Build a property_hash containing all the discovered information about MySQL | ||
# servers. | ||
def self.instances | ||
instances = [] | ||
if mysql_running | ||
servers = mysql([defaults_file, '-NBe', | ||
'SELECT `hostname`, `port` FROM `proxysql_servers`'].compact).split(%r{\n}) | ||
|
||
# To reduce the number of calls to MySQL we collect all the properties in | ||
# one big swoop. | ||
servers.each do |line| | ||
hostname, port = line.split(%r{\t}) | ||
query = 'SELECT `hostname`, `port`, `weight`, `comment`' | ||
query << ' FROM `proxysql_servers`' | ||
query << " WHERE `hostname` = '#{hostname}' AND `port` = #{port}" | ||
|
||
@hostname, @port, @weight, @comment = mysql([defaults_file, '-NBe', query].compact).chomp.split(%r{\t}) | ||
name = "#{hostname}:#{port}" | ||
|
||
instances << new( | ||
name: name, | ||
ensure: :present, | ||
hostname: @hostname, | ||
port: @port, | ||
weight: @weight, | ||
comment: @comment | ||
) | ||
end | ||
end | ||
instances | ||
end | ||
|
||
# We iterate over each proxy_mysql_server entry in the catalog and compare it against | ||
# the contents of the property_hash generated by self.instances | ||
def self.prefetch(resources) | ||
servers = instances | ||
resources.keys.each do |name| | ||
provider = servers.find { |server| server.name == name } | ||
resources[name].provider = provider if provider | ||
end | ||
end | ||
|
||
def create | ||
_name = @resource[:name] | ||
hostname = @resource.value(:hostname) | ||
port = @resource.value(:port) || 6032 | ||
weight = @resource.value(:weight) || 0 | ||
comment = @resource.value(:comment) || '' | ||
|
||
query = 'INSERT INTO proxysql_servers (`hostname`, `port`, `weight`, `comment`)' | ||
query << " VALUES ('#{hostname}', #{port}, #{weight}, '#{comment}')" | ||
mysql([defaults_file, '-e', query].compact) | ||
@property_hash[:ensure] = :present | ||
|
||
exists? ? (return true) : (return false) | ||
end | ||
|
||
def destroy | ||
hostname = @property_hash[:hostname] | ||
port = @property_hash[:port] | ||
query = 'DELETE FROM `proxysql_servers`' | ||
query << " WHERE `hostname` = '#{hostname}' AND `port` = #{port}" | ||
mysql([defaults_file, '-e', query].compact) | ||
|
||
@property_hash.clear | ||
exists? ? (return false) : (return true) | ||
end | ||
|
||
def exists? | ||
@property_hash[:ensure] == :present || false | ||
end | ||
|
||
def initialize(value = {}) | ||
super(value) | ||
@property_flush = {} | ||
end | ||
|
||
def flush | ||
update_server(@property_flush) if @property_flush | ||
@property_hash.clear | ||
|
||
load_to_runtime = @resource[:load_to_runtime] | ||
mysql([defaults_file, '-NBe', 'LOAD PROXYSQL SERVERS TO RUNTIME'].compact) if load_to_runtime == :true | ||
|
||
save_to_disk = @resource[:save_to_disk] | ||
mysql([defaults_file, '-NBe', 'SAVE PROXYSQL SERVERS TO DISK'].compact) if save_to_disk == :true | ||
end | ||
|
||
def update_server(properties) | ||
hostname = @resource.value(:hostname) | ||
port = @resource.value(:port) | ||
|
||
return false if properties.empty? | ||
|
||
values = [] | ||
properties.each do |field, value| | ||
values.push("`#{field}` = '#{value}'") | ||
end | ||
|
||
query = 'UPDATE proxysql_servers SET ' | ||
query << values.join(', ') | ||
query << " WHERE `hostname` = '#{hostname}' AND `port` = #{port}" | ||
mysql([defaults_file, '-e', query].compact) | ||
|
||
@property_hash.clear | ||
exists? ? (return false) : (return true) | ||
end | ||
|
||
# Generates method for all properties of the property_hash | ||
mk_resource_methods | ||
|
||
def weight=(value) | ||
@property_flush[:weight] = value | ||
end | ||
|
||
def comment=(value) | ||
@property_flush[:comment] = value | ||
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,52 @@ | ||
# This has to be a separate type to enable collecting | ||
Puppet::Type.newtype(:proxy_cluster) do | ||
@doc = 'Manage a ProxySQL cluster.' | ||
|
||
ensurable | ||
|
||
autorequire(:file) { '/root/.my.cnf' } | ||
autorequire(:class) { 'mysql::client' } | ||
autorequire(:service) { 'proxysql' } | ||
|
||
validate do | ||
raise('name parameter is required.') if (self[:ensure] == :present) && self[:name].nil? | ||
raise('hostname parameter is required.') if (self[:ensure] == :present) && self[:hostname].nil? | ||
raise('port parameter is required.') if (self[:ensure] == :present) && self[:port].nil? | ||
end | ||
|
||
newparam(:name, namevar: true) do | ||
desc 'name for cluster to manage.' | ||
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(:hostname) do | ||
desc 'The hostname of the server.' | ||
newvalue(%r{\w+}) | ||
end | ||
|
||
newproperty(:port) do | ||
desc 'The port of the server.' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:weight) do | ||
desc 'Currently unused, but in the roadmap for future enhancements.' | ||
newvalue(%r{\d+}) | ||
end | ||
|
||
newproperty(:comment) do | ||
desc 'free form comment field.' | ||
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
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