-
Notifications
You must be signed in to change notification settings - Fork 28
/
postgres.rb
62 lines (57 loc) · 1.87 KB
/
postgres.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
dep 'existing postgres db', :username, :db_name do
requires 'postgres access'.with(:username => username)
met? {
!shell("psql -l") {|shell|
shell.stdout.split("\n").grep(/^\s*#{db_name}\s+\|/)
}.empty?
}
meet {
shell "createdb -O '#{username}' '#{db_name}'"
}
end
dep 'existing data', :username, :db_name do
requires 'existing postgres db'.with(username, db_name)
met? {
shell("psql #{db_name} -c '\\d'").scan(/\((\d+) rows?\)/).flatten.first.tap {|rows|
if rows && rows.to_i > 0
log "There are already #{rows} tables."
else
unmeetable! <<-MSG
The '#{db_name}' database is empty. Load a database dump with:
$ cat #{db_name}.psql | ssh #{username}@#{shell('hostname -f')} 'psql #{db_name}'
MSG
end
}
}
end
dep 'pg.gem' do
requires 'postgres.managed'
provides []
end
dep 'postgres access', :username, :flags do
requires 'postgres.managed'
requires 'user exists'.with(:username => username)
username.default(shell('whoami'))
flags.default!('-SdR')
met? { !sudo("echo '\\du' | #{which 'psql'}", :as => 'postgres').split("\n").grep(/^\W*\b#{username}\b/).empty? }
meet { sudo "createuser #{flags} #{username}", :as => 'postgres' }
end
dep 'postgres backups' do
requires 'postgres.managed'
met? { shell? "test -x /etc/cron.hourly/psql_git" }
meet {
render_erb 'postgres/psql_git.rb.erb', :to => '/usr/local/bin/psql_git', :perms => '755', :sudo => true
sudo "ln -sf /usr/local/bin/psql_git /etc/cron.hourly/"
}
end
dep 'postgres.managed', :version do
version.default('9.2')
# Assume the installed version if there is one
version.default!(shell('psql --version').val_for('psql (PostgreSQL)')[/^\d\.\d/]) if which('psql')
requires 'common:set.locale'
installs {
via :apt, ["postgresql-#{owner.version}", "libpq-dev"]
via :brew, "postgresql"
}
provides "psql ~> #{version}.0"
end