Class: Dolphin::Postgres
- Inherits:
-
Base
- Object
- Thor
- Base
- Dolphin::Postgres
show all
- Defined in:
- lib/dolphin/ubuntu/postgres.rb
Overview
Instance Method Summary
collapse
Methods inherited from Base
#initialize
Constructor Details
This class inherits a constructor from Dolphin::Base
Instance Method Details
#backup ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 143
def backup
upload("#{@config_root}/postgres/slave/*", "/tmp")
= [
%{
mkdir -p ~/backups/postgresql
mv /tmp/pg_backup.* ~/backups/postgresql
# # add cron job to /var/spool/cron/crontabs/user
crontab /tmp/cron.txt
},
]
execute
end
|
#config ⇒ Object
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 44
def config
upload("#{@config_root}/postgres/shared/*", "/tmp")
master = @server_hash[:pgm]
slave = @server_hash[:pgs]
= [
%{
cp /etc/sysctl.conf /tmp/
echo kernel.shmmax = 2147483648 >> /tmp/sysctl.conf
echo kernel.shmall = 2097152 >> /tmp/sysctl.conf
echo kernel.shmmni = 4096 >> /tmp/sysctl.conf
sudo mv /tmp/sysctl.conf /etc/
sudo sysctl -p
},
%{
sudo mv /tmp/postgresql.conf /etc/postgresql/9.2/main/
sudo chown postgres:postgres /etc/postgresql/9.2/main/postgresql.conf
},
%{
echo hostssl replication replicator #{master}/32 trust >> /tmp/pg_hba.conf
echo hostssl replication replicator #{slave}/32 trust >> /tmp/pg_hba.conf
sudo mv /tmp/pg_hba.conf /etc/postgresql/9.2/main/
sudo chown postgres:postgres /etc/postgresql/9.2/main/pg_hba.conf
},
%{
sudo ufw allow from 192.168.0.0/16 to any port 5432
# only for replicator
sudo ufw allow from #{master}/32 to any port 5432
sudo ufw allow from #{slave}/32 to any port 5432
},
]
execute
end
|
#createdb(dbname) ⇒ Object
132
133
134
135
136
137
138
139
140
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 132
def createdb(dbname)
= [
%{
psql -d postgres -c 'CREATE DATABASE #{dbname}'
},
]
execute
end
|
#disable ⇒ Object
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 20
def disable
= [
"
sudo service postgresql stop
sudo update-rc.d postgresql disable
",
]
execute
end
|
#enable ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 32
def enable
= [
"
sudo update-rc.d postgresql enable
sudo service postgresql start
",
]
execute
end
|
#install ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 5
def install
= [
%{
sudo echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql postgresql-contrib libpq-dev
},
]
execute
end
|
#master(user, password) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 88
def master(user, password)
= [
%{
# plugin for pgadmin
sudo -u postgres psql -c 'CREATE EXTENSION adminpack;'
# create replicator user
sudo -u postgres psql -c "CREATE USER replicator REPLICATION LOGIN ENCRYPTED PASSWORD '#{password}';"
# create application user
sudo -u postgres psql -c "CREATE USER #{user} LOGIN ENCRYPTED PASSWORD '#{password}' NOSUPERUSER INHERIT CREATEDB CREATEROLE;"
sudo service postgresql restart
},
]
execute
end
|
#slave ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/dolphin/ubuntu/postgres.rb', line 108
def slave
upload("#{@config_root}/postgres/slave/*", "/tmp")
master = @server_hash[:pgm]
= [
%{
sudo service postgresql stop
sudo -u postgres rm -rf /var/lib/postgresql/9.2/main
sudo -u postgres pg_basebackup -h #{master} -D /var/lib/postgresql/9.2/main -U replicator -v -P
sed -i 's/MASTER/#{master}/' /tmp/recovery.conf
sudo mv /tmp/recovery.conf /var/lib/postgresql/9.2/main/
sudo chown postgres:postgres /var/lib/postgresql/9.2/main/recovery.conf
sudo service postgresql start
},
]
execute
end
|