Module: Blue::Postgresql

Defined in:
lib/blue/postgresql.rb,
lib/blue/postgresql/version.rb

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/blue/postgresql.rb', line 85

def self.included(klass)
  klass.add_role(:db)
  klass.add_role(:postgresql)
  klass.class_eval do
    recipe :postgresql
  end
end

Instance Method Details

#postgresql(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/blue/postgresql.rb', line 14

def postgresql(options = {})
  file "/etc/apt/sources.list.d/pgdg.list",
    :ensure  => :present,
    :content => "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main"

  exec 'get gpg key',
    :command => "wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -",
    :require => file("/etc/apt/sources.list.d/pgdg.list"),
    :unless => "test -x /usr/bin/psql"

  exec 'apt-get update',
    :command => 'apt-get update',
    :require => exec("get gpg key"),
    :unless => "test -x /usr/bin/psql"


  packages = ["postgresql-#{Blue.config.postgresql.version}", "postgresql-client-#{Blue.config.postgresql.version}", "postgresql-contrib-#{Blue.config.postgresql.version}", "libpq-dev"]
  packages.each do |pkg|
    package pkg,
      :ensure => :installed,
      :require => exec("apt-get update")
  end

  file "/etc/postgresql/#{Blue.config.postgresql.version}/main/pg_hba.conf",
    :ensure  => :present,
    :content => template(File.join(File.dirname(__FILE__), '..', '..', 'templates', 'pg_hba.conf.erb'), binding),
    :require => package("postgresql-#{Blue.config.postgresql.version}"),
    :mode    => '600',
    :owner   => 'postgres',
    :group   => 'postgres',
    :notify  => service("postgresql-#{Blue.config.postgresql.version}")

  file "/etc/postgresql/#{Blue.config.postgresql.version}/main/postgresql.conf",
    :ensure  => :present,
    :content => template(File.join(File.dirname(__FILE__), '..', '..', 'templates', 'postgresql.conf.erb'), binding),
    :require => package("postgresql-#{Blue.config.postgresql.version}"),
    :mode    => '600',
    :owner   => 'postgres',
    :group   => 'postgres',
    :notify  => service("postgresql-#{Blue.config.postgresql.version}")

  service "postgresql",
    :alias      => "postgresql-#{Blue.config.postgresql.version}",
    :hasstatus  => true,
    :require    => packages.map{|pkg| package(pkg)}

  psql "CREATE USER #{Blue.config.database.username} WITH PASSWORD '#{Blue.config.database.password}'",
    :alias    => "postgresql_user",
    :unless   => psql_query('\\\\du') + "| grep #{Blue.config.database.username}",
    :require  => service("postgresql")

  exec "postgresql_database",
    :command  => "/usr/bin/createdb -O #{Blue.config.database.username} #{Blue.config.database.database}",
    :unless   => "/usr/bin/psql -l | grep #{Blue.config.database.database}",
    :user     => 'postgres',
    :require  => exec('postgresql_user')
end

#psql(query, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/blue/postgresql.rb', line 72

def psql(query, options = {})
  name = options.delete(:alias) || "psql #{query}"
  hash = {
    :command => psql_query(query),
    :user => 'postgres'
  }.merge(options)
  exec(name,hash)
end

#psql_query(sql) ⇒ Object



81
82
83
# File 'lib/blue/postgresql.rb', line 81

def psql_query(sql)
  %(/usr/bin/psql -c "#{sql}")
end