Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few modification to puppet-proxysql module #57

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 61 additions & 10 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ You can override any configuration setting by using the `override_config_setting
mysql_servers => {
'mysql1' => {
'address' => '127.0.0.1',
        'port' => 33061,
      },
'port' => 33061,
},
'mysql2' => {
'address' => '127.0.0.1',
        'port' => 33062,
      },
'port' => 33062,
},
...
},
mysql_users => { ... },
Expand Down Expand Up @@ -164,12 +164,23 @@ The username ProxySQL will use to connect to the configured mysql_servers. Defau
##### `monitor_password`
The password ProxySQL will use to connect to the configured mysql_servers. Defaults to 'monitor'

##### `config_file`
The file where the ProxySQL configuration is saved. This will only be configured if `manage_config_file` is set to `true`.
Defaults to '/etc/proxysql.cnf'
##### `main_config_file`
The file where the main ProxySQL configuration is saved (datadir, admin and mysql variable).
This will only be configured if `manage_main_config_file` is set to `true`. Defaults to '/etc/proxysql.cn

##### `manage_config_file`
Determines wheter this module will configure the ProxySQL configuration file. Defaults to 'true'
##### `manage_main_config_file`
Determines wheter this module will update the ProxySQL main configuration file. Defaults to 'tru

##### `proxy_config_file`
The file where servers, users, hostgroups, rules the ProxySQL configuration is saved.
This will only be configured if `manage_proxy_config_file` is set to `true`. Defaults to '/etc/proxysql.cn

##### `manage_proxy_config_file`
Determines wheter this module will update the ProxySQL proxy configuration file. Defaults to 'tru

##### `config_directory`
Path where proxy_config_file file will be stored.
Defaults to '/etc/proxysql.d

##### `mycnf_file_name`
Path of the my.cnf file where the connections details for the admin interface is save. This is required for the providers to work.
Expand Down Expand Up @@ -226,6 +237,34 @@ key utl for the yumrepo-resource in RedHat-based systems, defaults to 'https://w
##### `override_config_settings`
Which configuration variables should be overriden. Hash, defaults to {} (empty hash).

##### `cluster_name`
If set, proxysql_servers with the same cluster_name will be automatically added to the same cluster and will synchronize their configuration parameters.
Defaults to ''

##### `cluster_username`
The username ProxySQL will use to connect to the configured mysql_clusters
Defaults to 'cluster'

##### `cluster_password`
The password ProxySQL will use to connect to the configured mysql_clusters. Defaults to 'cluster'

##### `admin_users`
Array of users, for which .my.cnf file will be copied to their home directory. Defaults to []

##### `mysql_servers`
Array of mysql_servers, that will be created in ProxySQL. Defaults to undef

##### `mysql_users`
Array of mysql_users, that will be created in ProxySQL. Defaults to undef

##### `mysql_hostgroups`
Array of mysql_hostgroups, that will be created in ProxySQL. Defaults to undef

##### `mysql_rules`
Array of mysql_rules, that will be created in ProxySQL. Defaults to undef

##### `schedulers`
Array of schedulers, that will be created in ProxySQL. Defaults to undef

## Types
#### proxy_global_variable
Expand All @@ -243,6 +282,18 @@ Specifies wheter the resource should be immediately save to disk. Boolean, defau
##### `value`
The value of the variable.

#### proxy_cluster
`proxy_cluster` manages an entry in the ProxySQL `proxysql_clusters` admin table.

##### `name`
The name of the resource.

##### `hostname`
Hostname of the server. Required.

##### `port`
Port of the server. Required. Defaults to 3306.

#### proxy_mysql_replication_hostgroup
`proxy_mysql_replication_hostgroup` manages an entry in the ProxySQL `mysql_replication_hostgroups` admin table.

Expand Down Expand Up @@ -515,4 +566,4 @@ We are open to feature requests, bug reports, contributions, etc...

## Contributors

Original author: Matthias Crauwels
Original author: Matthias Crauwels
31 changes: 31 additions & 0 deletions examples/init.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
# lint:ignore:80chars
# lint:ignore:2sp_soft_tabs
# variant 1

class { '::proxysql':
cluster_name => 'cluster11',
mysql_servers => [ { 'db1' => { 'port' => 3306,
'hostgroup_id' => 1, } },
{ 'db2' => { 'hostgroup_id' => 2, } },
],
mysql_users => [ { 'app' => { 'password' => '*92C74DFBDA5D60ABD41EFD7EB0DAE389F4646ABB',
'default_hostgroup' => 1, } },
{ 'ro' => { 'password' => '*86935F2843252CFAAC4CE713C0D5FF80CF444F3B',
' default_hostgroup' => 2, } },
],
mysql_hostgroups => [ { 'hostgroup 1' => { 'writer_hostgroup' => 1,
'reader_hostgroup' => 2, } },
],
mysql_rules => [ { 'testable to test DB' => { 'rule_id' => 1,
'match_pattern' => 'testtable',
'replace_pattern' => 'test.newtable',
'apply' => 1,
'active' => 1, } },
],
schedulers => [ { 'test scheduler' => { 'scheduler_id' => 1,
'active' => 0,
'filename' => '/usr/bin/whoami', } },
],
}
# lint:endignore

# variant 2

class { '::proxysql':
listen_port => 3306,
Expand Down
128 changes: 128 additions & 0 deletions lib/puppet/provider/proxy_cluster/proxysql.rb
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
Loading