# # Cookbook Name:: zabbix # Recipe:: database # # Copyright 2017, Linux-Help.org # Authors: # Eric Renfro # yum_package 'postgresql94-server' directory '/tmp/database' do owner 'root' group 'root' mode '0755' action :create only_if { Dir.glob('/var/lib/pgsql/9.4/data/*').empty? } end template "/tmp/database/schema.sql" do source "schema.sql.erb" mode "0644" owner "root" group "root" action :create only_if { Dir.exist?('/tmp/database') } end template "/tmp/database/images.sql" do source "images.sql.erb" mode "0644" owner "root" group "root" action :create only_if { Dir.exist?('/tmp/database') } end template "/tmp/database/data.sql" do source "data.sql.erb" mode "0644" owner "root" group "root" action :create only_if { Dir.exist?('/tmp/database') } end execute 'initializingdb' do command 'service postgresql-9.4 initdb' action :run only_if { Dir.glob('/var/lib/pgsql/9.4/data/*').empty? } end template "/var/lib/pgsql/9.4/data/pg_hba.conf" do source "pg_hba.conf.erb" mode "0600" owner "postgres" group "postgres" notifies :restart, "service[postgresql-9.4]", :immediately end service 'postgresql-9.4' do action [:start, :enable] end bash 'configure_postgres' do user 'root' sensitive true credentials = Chef::EncryptedDataBagItem.load("odhp_credentials", "credentials") code <<-EOH su postgres -l -c "psql -U postgres -c \\"CREATE DATABASE zabbix WITH ENCODING='UTF-8';\\"" su postgres -l -c "psql -U postgres -c \\"CREATE USER zabbixmaster WITH PASSWORD '#{credentials['zabbix']['postgres_password']}';\\"" su postgres -l -c "psql -U postgres -c \\"GRANT ALL PRIVILEGES ON DATABASE zabbix to zabbixmaster;\\"" EOH only_if { Dir.exist?('/tmp/database') } end bash 'configure_schema' do credentials = Chef::EncryptedDataBagItem.load("odhp_credentials", "credentials") sensitive true environment ({ "PGPASSWORD" => credentials['zabbix']['postgres_password'] }) code <<-EOH cd /tmp/database/ psql -h 127.0.0.1 -U zabbixmaster -d zabbix -f /tmp/database/schema.sql; psql -h 127.0.0.1 -U zabbixmaster -d zabbix -f /tmp/database/images.sql; psql -h 127.0.0.1 -U zabbixmaster -d zabbix -f /tmp/database/data.sql; EOH only_if { Dir.exist?('/tmp/database') } end directory "cleanup" do path "/tmp/database" recursive true action :delete end