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

Clean up PostgreSQL DB creation logic #904

Merged
merged 9 commits into from
Nov 7, 2023
14 changes: 0 additions & 14 deletions manifests/database/mysql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
true => "cd ${schema_path} && if [ -f server.sql.gz ]; then gunzip -f server.sql.gz ; fi && mysql -h '${database_host}' -u '${database_user}' -p'${database_password}' ${port}-D '${database_name}' < server.sql && touch /etc/zabbix/.schema.done",
false => "cd ${schema_path} && if [ -f create.sql.gz ]; then gunzip -f create.sql.gz ; fi && mysql -h '${database_host}' -u '${database_user}' -p'${database_password}' ${port}-D '${database_name}' < create.sql && touch /etc/zabbix/.schema.done"
}
$zabbix_server_images_sql = 'touch /etc/zabbix/.images.done'
$zabbix_server_data_sql = 'touch /etc/zabbix/.data.done'
}
}

Expand All @@ -74,18 +72,6 @@
unless => 'test -f /etc/zabbix/.schema.done',
provider => 'shell',
}
-> exec { 'zabbix_server_images.sql':
command => $zabbix_server_images_sql,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => 'test -f /etc/zabbix/.images.done',
provider => 'shell',
}
-> exec { 'zabbix_server_data.sql':
command => $zabbix_server_data_sql,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => 'test -f /etc/zabbix/.data.done',
provider => 'shell',
}
}
default : {
fail 'We do not work.'
Expand Down
104 changes: 31 additions & 73 deletions manifests/database/postgresql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -23,90 +23,48 @@
) inherits zabbix::params {
assert_private()

if ($database_schema_path == false) or ($database_schema_path == '') {
if $facts['os']['family'] == 'RedHat' and $facts['os']['release']['major'] == '7' {
if versioncmp($zabbix_version, '6.0') >= 0 {
$schema_path = '/usr/share/zabbix-sql-scripts/postgresql/'
} else {
$schema_path = "/usr/share/doc/zabbix-*-pgsql-${zabbix_version}*/"
}
} else {
if versioncmp($zabbix_version, '6.0') >= 0 {
$schema_path = '/usr/share/zabbix-sql-scripts/postgresql/'
} else {
$schema_path = '/usr/share/doc/zabbix-*-pgsql'
}
}
} else {
if $database_schema_path != false and $database_schema_path != '' {
$schema_path = $database_schema_path
} elsif versioncmp($zabbix_version, '6.0') >= 0 {
$schema_path = '/usr/share/zabbix-sql-scripts/postgresql/'
} elsif $facts['os']['family'] == 'RedHat' and $facts['os']['release']['major'] == '7' {
$schema_path = "/usr/share/doc/zabbix-*-pgsql-${zabbix_version}*/"
} else {
$schema_path = '/usr/share/doc/zabbix-*-pgsql'
}

case $zabbix_type {
$done_file = '/etc/zabbix/.schema.done'
$schema_file = case $zabbix_type {
'proxy': {
$zabbix_proxy_create_sql = versioncmp($zabbix_version, '6.0') >= 0 ? {
true => "cd ${schema_path} && psql -h '${database_host}' -U '${database_user}' -p ${database_port} -d '${database_name}' -f proxy.sql && touch /etc/zabbix/.schema.done",
false => "cd ${schema_path} && if [ -f schema.sql.gz ]; then gunzip -f schema.sql.gz ; fi && psql -h '${database_host}' -U '${database_user}' -p ${database_port} -d '${database_name}' -f schema.sql && touch /etc/zabbix/.schema.done"
if versioncmp($zabbix_version, '6.0') >= 0 {
'proxy.sql'
} else {
'schema.sql'
}
}
default: {
$zabbix_server_create_sql = versioncmp($zabbix_version, '6.0') >= 0 ? {
true => "cd ${schema_path} && if [ -f server.sql.gz ]; then gunzip -f server.sql.gz ; fi && psql -h '${database_host}' -U '${database_user}' -p ${database_port} -d '${database_name}' -f server.sql && touch /etc/zabbix/.schema.done",
false => "cd ${schema_path} && if [ -f create.sql.gz ]; then gunzip -f create.sql.gz ; fi && psql -h '${database_host}' -U '${database_user}' -p ${database_port} -d '${database_name}' -f create.sql && touch /etc/zabbix/.schema.done"
if versioncmp($zabbix_version, '6.0') >= 0 {
'server.sql'
} else {
'create.sql'
}
$zabbix_server_images_sql = 'touch /etc/zabbix/.images.done'
$zabbix_server_data_sql = 'touch /etc/zabbix/.data.done'
}
}

exec { 'update_pgpass':
command => "echo ${database_host}:${database_port}:${database_name}:${database_user}:${database_password} >> /root/.pgpass",
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => "grep \"${database_host}:${database_port}:${database_name}:${database_user}:${database_password}\" /root/.pgpass",
require => File['/root/.pgpass'],
}

file { '/root/.pgpass':
ensure => file,
mode => '0600',
owner => 'root',
group => 'root',
}
$command = "cd ${schema_path} && if [ -f ${schema_file}.gz ]; then zcat ${schema_file}.gz | psql ; else psql -f ${schema_file}; fi && touch ${done_file}"
$exec_env = [
"PGHOST=${database_host}",
"PGPORT=${database_port}",
"PGUSER=${database_user}",
"PGPASSWORD=${database_password}",
"PGDATABASE=${database_name}",
]

case $zabbix_type {
'proxy': {
exec { 'zabbix_proxy_create.sql':
command => $zabbix_proxy_create_sql,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => 'test -f /etc/zabbix/.schema.done',
provider => 'shell',
require => Exec['update_pgpass'],
}
}
'server': {
exec { 'zabbix_server_create.sql':
command => $zabbix_server_create_sql,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => 'test -f /etc/zabbix/.schema.done',
provider => 'shell',
require => Exec['update_pgpass'],
}
-> exec { 'zabbix_server_images.sql':
command => $zabbix_server_images_sql,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => 'test -f /etc/zabbix/.images.done',
provider => 'shell',
require => Exec['update_pgpass'],
}
-> exec { 'zabbix_server_data.sql':
command => $zabbix_server_data_sql,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
unless => 'test -f /etc/zabbix/.data.done',
provider => 'shell',
require => Exec['update_pgpass'],
}
}
default: {
fail 'We do not work.'
}
exec { 'zabbix_create.sql':
command => $command,
path => "/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:${database_path}",
creates => $done_file,
provider => 'shell',
environment => $exec_env,
}
}
4 changes: 0 additions & 4 deletions spec/classes/database_mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
it { is_expected.to contain_class('zabbix::database::mysql') }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && mysql -h 'rspec.puppet.com' -u 'zabbix-server' -p'zabbix-server' -P 3306 -D 'zabbix-server' < #{sql_server} && touch /etc/zabbix/.schema.done") }
it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') }
it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') }
end

describe 'and no database_port is defined' do
Expand All @@ -74,8 +72,6 @@
it { is_expected.to contain_class('zabbix::database::mysql') }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && mysql -h 'rspec.puppet.com' -u 'zabbix-server' -p'zabbix-server' -D 'zabbix-server' < #{sql_server} && touch /etc/zabbix/.schema.done") }
it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') }
it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') }
end
end

Expand Down
190 changes: 78 additions & 112 deletions spec/classes/database_postgresql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
'rspec.puppet.com'
end

let :pre_condition do
'include postgresql::server'
end

on_supported_os(baseline_os_hash).each do |os, facts|
next if facts[:os]['name'] == 'windows'

Expand All @@ -19,6 +15,18 @@
facts
end

let :params do
{
database_host: 'node01.example.com',
}
end

let :expected_environment do
[
'PGHOST=node01.example.com',
]
end

supported_versions.each do |zabbix_version|
path = if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
# Path on EL7
Expand All @@ -41,117 +49,75 @@
'create.sql'
end

describe "when zabbix_type is server and version is #{zabbix_version}" do
let :params do
{
database_name: 'zabbix-server',
database_user: 'zabbix-server',
database_password: 'zabbix-server',
database_host: 'node01.example.com',
database_port: 5432,
zabbix_type: 'server',
zabbix_version: zabbix_version
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:5432:zabbix-server:zabbix-server:zabbix-server >> /root/.pgpass') }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-server' -p 5432 -d 'zabbix-server' -f #{sql_server} && touch /etc/zabbix/.schema.done") }
it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') }
it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') }
it { is_expected.to contain_file('/root/.pgpass') }
it { is_expected.to contain_class('zabbix::params') }
end

describe "when zabbix_type is server and version is #{zabbix_version} and no port is defined" do
let :params do
{
database_name: 'zabbix-server',
database_user: 'zabbix-server',
database_password: 'zabbix-server',
database_host: 'node01.example.com',
zabbix_type: 'server',
zabbix_version: zabbix_version
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:5432:zabbix-server:zabbix-server:zabbix-server >> /root/.pgpass') }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-server' -p 5432 -d 'zabbix-server' -f #{sql_server} && touch /etc/zabbix/.schema.done") }
it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') }
it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') }
it { is_expected.to contain_file('/root/.pgpass') }
it { is_expected.to contain_class('zabbix::params') }
end

describe "when zabbix_type is server and version is #{zabbix_version} and custom port is defined" do
let :params do
{
database_name: 'zabbix-server',
database_user: 'zabbix-server',
database_password: 'zabbix-server',
database_host: 'node01.example.com',
database_port: 6432,
zabbix_type: 'server',
zabbix_version: zabbix_version
}
describe "when version is #{zabbix_version}" do
let(:params) { super().merge(zabbix_version: zabbix_version) }

describe 'when zabbix_type is server' do
let(:params) do
super().merge(
database_name: 'zabbix-server',
database_user: 'zabbix-server',
database_password: 'zabbix-server',
zabbix_type: 'server'
)
end
let(:expected_environment) do
super() + [
'PGPORT=5432',
'PGUSER=zabbix-server',
'PGPASSWORD=zabbix-server',
'PGDATABASE=zabbix-server',
]
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then zcat #{sql_server}.gz | psql ; else psql -f #{sql_server}; fi && touch /etc/zabbix/.schema.done").with_environment(expected_environment) }
it { is_expected.to contain_class('zabbix::params') }

describe 'with custom port is defined' do
let(:params) { super().merge(database_port: 6432) }

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_create.sql').with_environment(expected_environment.map { |v| v.start_with?('PGPORT=') ? 'PGPORT=6432' : v }) }
it { is_expected.to contain_class('zabbix::params') }
end
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:6432:zabbix-server:zabbix-server:zabbix-server >> /root/.pgpass') }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-server' -p 6432 -d 'zabbix-server' -f #{sql_server} && touch /etc/zabbix/.schema.done") }
it { is_expected.to contain_exec('zabbix_server_images.sql').with_command('touch /etc/zabbix/.images.done') }
it { is_expected.to contain_exec('zabbix_server_data.sql').with_command('touch /etc/zabbix/.data.done') }
it { is_expected.to contain_file('/root/.pgpass') }
it { is_expected.to contain_class('zabbix::params') }
end

describe "when zabbix_type is proxy and version is #{zabbix_version}" do
let :params do
{
database_name: 'zabbix-proxy',
database_user: 'zabbix-proxy',
database_password: 'zabbix-proxy',
database_host: 'node01.example.com',
database_port: 5432,
zabbix_type: 'proxy',
zabbix_version: zabbix_version
}
describe 'when zabbix_type is proxy' do
let :params do
super().merge(
database_name: 'zabbix-proxy',
database_user: 'zabbix-proxy',
database_password: 'zabbix-proxy',
zabbix_type: 'proxy'
)
end
let(:expected_environment) do
super() + [
'PGPORT=5432',
'PGUSER=zabbix-proxy',
'PGPASSWORD=zabbix-proxy',
'PGDATABASE=zabbix-proxy',
]
end

it { is_expected.to compile.with_all_deps }

if Puppet::Util::Package.versioncmp(zabbix_version, '6.0') < 0
it { is_expected.to contain_exec('zabbix_create.sql').with_command("cd #{path} && if [ -f schema.sql.gz ]; then zcat schema.sql.gz | psql ; else psql -f schema.sql; fi && touch /etc/zabbix/.schema.done").with_environment(expected_environment) }
else
it { is_expected.to contain_exec('zabbix_create.sql').with_command("cd #{path} && if [ -f proxy.sql.gz ]; then zcat proxy.sql.gz | psql ; else psql -f proxy.sql; fi && touch /etc/zabbix/.schema.done").with_environment(expected_environment) }
end
it { is_expected.to contain_class('zabbix::params') }

describe 'with a custom port' do
let(:params) { super().merge(database_port: 6432) }

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_create.sql').with_environment(expected_environment.map { |v| v.start_with?('PGPORT=') ? 'PGPORT=6432' : v }) }
it { is_expected.to contain_class('zabbix::params') }
end
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:5432:zabbix-proxy:zabbix-proxy:zabbix-proxy >> /root/.pgpass') }

if Puppet::Util::Package.versioncmp(zabbix_version, '6.0') < 0
it { is_expected.to contain_exec('zabbix_proxy_create.sql').with_command("cd #{path} && if [ -f schema.sql.gz ]; then gunzip -f schema.sql.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-proxy' -p 5432 -d 'zabbix-proxy' -f schema.sql && touch /etc/zabbix/.schema.done") }
else
it { is_expected.to contain_exec('zabbix_proxy_create.sql').with_command("cd #{path} && psql -h 'node01.example.com' -U 'zabbix-proxy' -p 5432 -d 'zabbix-proxy' -f proxy.sql && touch /etc/zabbix/.schema.done") }
end
it { is_expected.to contain_class('zabbix::params') }
end

describe "when zabbix_type is proxy and version is #{zabbix_version} and no port is defined" do
let :params do
{
database_name: 'zabbix-proxy',
database_user: 'zabbix-proxy',
database_password: 'zabbix-proxy',
database_host: 'node01.example.com',
zabbix_type: 'proxy',
zabbix_version: zabbix_version
}
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('update_pgpass').with_command('echo node01.example.com:5432:zabbix-proxy:zabbix-proxy:zabbix-proxy >> /root/.pgpass') }

if Puppet::Util::Package.versioncmp(zabbix_version, '6.0') < 0
it { is_expected.to contain_exec('zabbix_proxy_create.sql').with_command("cd #{path} && if [ -f schema.sql.gz ]; then gunzip -f schema.sql.gz ; fi && psql -h 'node01.example.com' -U 'zabbix-proxy' -p 5432 -d 'zabbix-proxy' -f schema.sql && touch /etc/zabbix/.schema.done") }
else
it { is_expected.to contain_exec('zabbix_proxy_create.sql').with_command("cd #{path} && psql -h 'node01.example.com' -U 'zabbix-proxy' -p 5432 -d 'zabbix-proxy' -f proxy.sql && touch /etc/zabbix/.schema.done") }
end

it { is_expected.to contain_class('zabbix::params') }
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/classes/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
it { is_expected.to contain_file('/etc/zabbix/zabbix_proxy.conf.d').with_require('File[/etc/zabbix/zabbix_proxy.conf]') }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_exec('update_pgpass') }
it { is_expected.to contain_exec('zabbix_proxy_create.sql') }
it { is_expected.to contain_file('/root/.pgpass') }
it { is_expected.to contain_exec('zabbix_create.sql') }
it { is_expected.to contain_postgresql__server__pg_hba_rule('Allow zabbix-proxy to access database') }

describe 'when manage_repo is true and zabbix version is unset' do
Expand Down
Loading