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

Clops 7816 #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 1 addition & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exposing a local *nix socket that routes to a downstream database on another hos
Requirements
============

Chef 0.7+
Chef 1.4+

Platform
--------
Expand Down Expand Up @@ -82,63 +82,11 @@ Multiple aliases may be supported on a single host.
action :stop
end

# TODO: include more examples

Recipes
=======

default
-------

Empty: this is a resource-only cookbook

example
-------

Example of how to use the resource; also exercised in the spec/ tests

Testing
=======

This cookbook has been "Tested in Production"™, but also has some basic RSpec tests.

**NOTE**: because Chef 10 has cookbook naming expectations, the root repo expects to be in a folder
named 'pgbouncer'.

bundle install
bundle exec rake spec

The cookbook is clean under FoodCritic.

bundle install
bundle exec rake foodcritic

To see the installation end to end, we've also got a rake task that spins up a [chef-zero](https://github.com/jkeiser/chef-zero)
local instance, uploads the cookbooks via berkshelf, and spins up a vagrant instance that pulls the data down. This is using a new
Vagrant plugin, created here at Whitepages, called [vagrant-chefzero](https://github.com/whitepages/vagrant-chefzero/).

vagrant plugin install vagrant-chefzero
bundle install
bundle exec rake vagrant_startup

License and Author(s)
=====================

- Author:: Owyn Richen (<[email protected]>)
- Author:: Jack Foy (<[email protected]>)
- Author:: Paul Kohan (<[email protected]>)
- Author:: Brian Engelman (<[email protected]>)

Copyright 2010-2013, [Whitepages Inc.](http://www.whitepages.com/)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
10 changes: 3 additions & 7 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
name "pgbouncer"
provides "pgbouncer"
maintainer "Whitepages Inc."
maintainer_email "[email protected]"
license "Apache 2.0"
maintainer "Jay Yu"
maintainer_email "[email protected]"
description "Installs/Configures pgbouncer"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "1.0.3"
recipe "pgbouncer", "the default recipe does nothing to hopefully indicate that this is an LWRP cookbook"
recipe "pgbouncer::example", "this gives an example of how one could consume the pgbouncer cookbook"

%w{ubuntu}.each do |os|
supports os
end

depends "apt"

78 changes: 19 additions & 59 deletions providers/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@
# Cookbook Name:: pgbouncer
# Provider:: connection
#
# Copyright 2010-2013, Whitepages Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'set'

Expand All @@ -25,30 +11,27 @@ def initialize(*args)
end

action :start do
service "pgbouncer-#{new_resource.db_alias}-start" do
service_name "pgbouncer-#{new_resource.db_alias}" # this is to eliminate warnings around http://tickets.opscode.com/browse/CHEF-3694
provider Chef::Provider::Service::Upstart
service "pgbouncer-start" do
service_name "pgbouncer" # this is to eliminate warnings around http://tickets.opscode.com/browse/CHEF-3694
provider Chef::Provider::Service::Systemd
action [:enable, :start, :reload]
end
new_resource.updated_by_last_action(true)
end

action :restart do
service "pgbouncer-#{new_resource.db_alias}-restart" do
service_name "pgbouncer-#{new_resource.db_alias}" # this is to eliminate warnings around http://tickets.opscode.com/browse/CHEF-3694
provider Chef::Provider::Service::Upstart
service "pgbouncer-restart" do
service_name "pgbouncer" # this is to eliminate warnings around http://tickets.opscode.com/browse/CHEF-3694
provider Chef::Provider::Service::Systemd
action [:enable, :restart]
end
new_resource.updated_by_last_action(true)
end

action :stop do
service "pgbouncer-#{new_resource.db_alias}-stop" do
service_name "pgbouncer-#{new_resource.db_alias}" # this is to eliminate warnings around http://tickets.opscode.com/browse/CHEF-3694
provider Chef::Provider::Service::Upstart
service "pgbouncer-stop" do
service_name "pgbouncer" # this is to eliminate warnings around http://tickets.opscode.com/browse/CHEF-3694
provider Chef::Provider::Service::Systemd
action :stop
end
new_resource.updated_by_last_action(true)
end

action :setup do
Expand Down Expand Up @@ -78,36 +61,18 @@ def initialize(*args)
action [:install, :upgrade]
end

service "pgbouncer-#{new_resource.db_alias}" do
provider Chef::Provider::Service::Upstart
service "pgbouncer" do
provider Chef::Provider::Service::Systemd
supports :enable => true, :start => true, :restart => true, :reload => true
action :nothing
end

# create the log, pid, db_sockets, /etc/pgbouncer, and application socket directories
Set.new([
new_resource.log_dir,
new_resource.pid_dir,
new_resource.socket_dir,
::File.expand_path(::File.join(new_resource.socket_dir, new_resource.db_alias)),
'/etc/pgbouncer'
]).each do |dir|
directory "#{new_resource.name}::#{dir}" do
path dir
action :create
recursive true
owner new_resource.user
group new_resource.group
mode 0775
end
end

# build the userlist, pgbouncer.ini, upstart conf and logrotate.d templates
{
"/etc/pgbouncer/userlist-#{new_resource.db_alias}.txt" => 'etc/pgbouncer/userlist.txt.erb',
"/etc/pgbouncer/pgbouncer-#{new_resource.db_alias}.ini" => 'etc/pgbouncer/pgbouncer.ini.erb',
"/etc/init/pgbouncer-#{new_resource.db_alias}.conf" => 'etc/init/pgbouncer.conf.erb',
"/etc/logrotate.d/pgbouncer-#{new_resource.db_alias}" => 'etc/logrotate.d/pgbouncer-logrotate.d.erb'
"/etc/pgbouncer/userlist.txt" => 'etc/pgbouncer/userlist.txt.erb',
"/etc/pgbouncer/pgbouncer.ini" => 'etc/pgbouncer/pgbouncer.ini.erb',
"/etc/init/pgbouncer.conf" => 'etc/init/pgbouncer.conf.erb',
"/etc/logrotate.d/pgbouncer" => 'etc/logrotate.d/pgbouncer-logrotate.d.erb'
}.each do |key, source_template|
## We are setting destination_file to a duplicate of key because the hash
## key is frozen and immutable.
Expand Down Expand Up @@ -135,9 +100,6 @@ def initialize(*args)
listen_port: new_resource.listen_port,
user: new_resource.user,
group: new_resource.group,
log_dir: new_resource.log_dir,
socket_dir: new_resource.socket_dir,
pid_dir: new_resource.pid_dir,
pool_mode: new_resource.pool_mode,
max_client_conn: new_resource.max_client_conn,
default_pool_size: new_resource.default_pool_size,
Expand All @@ -163,20 +125,18 @@ def initialize(*args)
end
end

new_resource.updated_by_last_action(true)
end

action :teardown do

{ "/etc/pgbouncer/userlist-#{new_resource.db_alias}.txt" => 'etc/pgbouncer/userlist.txt.erb',
"/etc/pgbouncer/pgbouncer-#{new_resource.db_alias}.ini" => 'etc/pgbouncer/pgbouncer.ini.erb',
"/etc/init/pgbouncer-#{new_resource.db_alias}.conf" => 'etc/pgbouncer/pgbouncer.conf',
"/etc/logrotate.d/pgbouncer-#{new_resource.db_alias}" => 'etc/logrotate.d/pgbouncer-logrotate.d'
{ "/etc/pgbouncer/userlist.txt" => 'etc/pgbouncer/userlist.txt.erb',
"/etc/pgbouncer/pgbouncer.ini" => 'etc/pgbouncer/pgbouncer.ini.erb',
"/etc/init/pgbouncer.conf" => 'etc/pgbouncer/pgbouncer.conf',
"/etc/logrotate.d/pgbouncer" => 'etc/logrotate.d/pgbouncer-logrotate.d'
}.each do |destination_file, source_template|
file destination_file do
action :delete
end
end

new_resource.updated_by_last_action(true)
end
18 changes: 0 additions & 18 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
#
# Cookbook Name:: pgbouncer
# Recipe:: default
#
# Copyright 2010-2013, Whitepages Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# disable default system pgbouncer service
if node['platform'] == 'ubuntu'
Expand Down
21 changes: 0 additions & 21 deletions recipes/example.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
#
# Cookbook Name:: pgbouncer
# Recipe:: example
#
# Copyright 2010-2013, Whitepages Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# NOTE:
# This is an example of how to leverage the included resource

pgbouncer_connection "database_example_com_ro" do
db_host "database.example.com"
Expand Down
11 changes: 11 additions & 0 deletions recipes/kb_spider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

pgbouncer_connection "kb_spider" do
db_host "localhost"
db_port "5433"
listen_port "5432"
db_name "kb_spider"
userlist "apitest" => "md5f72b9285fd6b4f628c78cb4a17488c2c", "readonly_user" => "md500000000000000000000000000000000"
max_client_conn 100
default_pool_size 20
action [ :setup, :restart ]
end
24 changes: 5 additions & 19 deletions resources/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@
# Cookbook Name:: pgbouncer
# Resource:: connection
#
# Copyright 2010-2013, Whitepages Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

actions :setup, :teardown, :start, :restart, :stop
default_action :setup
Expand All @@ -36,11 +22,11 @@
attribute :listen_addr, :kind_of => String, :default => '*'
attribute :listen_port, :kind_of => String

attribute :user, :kind_of => String, :default => 'pgbouncer'
attribute :group, :kind_of => String, :default => 'pgbouncer'
attribute :log_dir, :kind_of => String, :default => '/var/log/pgbouncer'
attribute :socket_dir, :kind_of => String, :default => '/var/run/pgbouncer'
attribute :pid_dir, :kind_of => String, :default => '/var/run/pgbouncer'
attribute :user, :kind_of => String, :default => 'postgres'
attribute :group, :kind_of => String, :default => 'postgres'
attribute :log_dir, :kind_of => String, :default => '/var/log/postgres'
attribute :socket_dir, :kind_of => String, :default => '/var/run/postgres'
attribute :pid_dir, :kind_of => String, :default => '/var/run/postgres'

attribute :pool_mode, :kind_of => String, :default => 'transaction'
attribute :max_client_conn, :kind_of => Integer, :default => 1000
Expand Down
9 changes: 5 additions & 4 deletions templates/default/etc/pgbouncer/pgbouncer.ini.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
;;; Administrative settings
;;;

logfile = <%= @log_dir %>/pgbouncer-<%= @db_alias %>.log
pidfile = <%= @pid_dir %>/pgbouncer-<%= @db_alias %>.pid
logfile = /var/log/postgresql/pgbouncer.log
pidfile = /var/run/postgresql/pgbouncer.pid

;;;
;;; Where to wait for clients
Expand All @@ -38,15 +38,16 @@ pidfile = <%= @pid_dir %>/pgbouncer-<%= @db_alias %>.pid

; unix socket is also used for -R.
; On debian it should be /var/run/postgresql
unix_socket_dir = <%= @socket_dir %>/<%= @db_alias %>/

unix_socket_dir = /var/run/postgresql

;;;
;;; Authentication settings
;;;

; any, trust, plain, crypt, md5
auth_type = md5
auth_file = /etc/pgbouncer/userlist-<%= @db_alias %>.txt
auth_file = /etc/pgbouncer/userlist.txt

;;;
;;; Users allowed into database 'pgbouncer'
Expand Down