Class: Dolphin::Postgres

Inherits:
Base
  • Object
show all
Defined in:
lib/dolphin/ubuntu/postgres.rb

Overview

postgres related tasks

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Dolphin::Base

Instance Method Details

#backupObject



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 files
  upload("#{@config_root}/postgres/slave/*", "/tmp")

  menu = [
    %{
      mkdir -p ~/backups/postgresql
      mv /tmp/pg_backup.* ~/backups/postgresql
      # # add cron job to /var/spool/cron/crontabs/user
      crontab /tmp/cron.txt
    },

  ]

  execute menu
end

#configObject



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 files
  upload("#{@config_root}/postgres/shared/*", "/tmp")

  master = @server_hash[:pgm]
  slave = @server_hash[:pgs]
  menu = [
    # set kernel sysctl
    %{
      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
    },

    # conf
    %{
      sudo mv /tmp/postgresql.conf /etc/postgresql/9.2/main/
      sudo chown postgres:postgres /etc/postgresql/9.2/main/postgresql.conf
    },

    # auth for replication
    %{
      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
    },

    # firewall
    %{
      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 menu
end

#createdb(dbname) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/dolphin/ubuntu/postgres.rb', line 132

def createdb(dbname)
  menu = [
    %{
      psql -d postgres -c 'CREATE DATABASE #{dbname}'
    },
  ]

  execute menu
end

#disableObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/dolphin/ubuntu/postgres.rb', line 20

def disable
  menu = [
    "
      sudo service postgresql stop
      sudo update-rc.d postgresql disable
    ",
  ]

  execute menu
end

#enableObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/dolphin/ubuntu/postgres.rb', line 32

def enable
  menu = [
    "
      sudo update-rc.d postgresql enable
      sudo service postgresql start
    ",
  ]

  execute menu
end

#installObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dolphin/ubuntu/postgres.rb', line 5

def install
  menu = [
    %{
      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 menu
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)
  menu = [
    %{
      # 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 menu
end

#slaveObject



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 files
  upload("#{@config_root}/postgres/slave/*", "/tmp")

  master = @server_hash[:pgm]
  menu = [
    %{
      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 menu
end