~hadware/magicicada-server/trusty-support

« back to all changes in this revision

Viewing changes to src/backends/db/scripts/dev/start-postgres.py

  • Committer: Facundo Batista
  • Date: 2015-08-05 13:10:02 UTC
  • Revision ID: facundo@taniquetil.com.ar-20150805131002-he7b7k704d8o7js6
First released version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Copyright 2008-2015 Canonical
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Affero General Public License as
 
7
# published by the Free Software Foundation, either version 3 of the
 
8
# License, or (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU Affero General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU Affero General Public License
 
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
17
#
 
18
# For further info, check  http://launchpad.net/filesync-server
 
19
 
 
20
"""Script to start Postgres."""
 
21
 
 
22
import os
 
23
import sys
 
24
 
 
25
import utilities.localendpoints as local
 
26
 
 
27
from utilities.utils import os_exec, is_running, get_tmpdir
 
28
 
 
29
 
 
30
def start():
 
31
    """Start Postgres."""
 
32
    sock_dir = os.path.join(get_tmpdir(), "db1")
 
33
    data_dir = os.path.join(sock_dir, "data")
 
34
    pidfile = os.path.join(data_dir, "postmaster.pid")
 
35
 
 
36
    if is_running("postgres", pidfile, "postgres"):
 
37
        print "Postgres already up & running."
 
38
        return 0
 
39
 
 
40
    pg_bin = None
 
41
    for path in ("/usr/lib/postgresql/9.1/bin",
 
42
                 "/usr/lib/postgresql/8.4/bin"):
 
43
        if os.path.isdir(path):
 
44
            pg_bin = path
 
45
            break
 
46
 
 
47
    if pg_bin is None:
 
48
        print "Cannot find valid parent for PGBINDIR"
 
49
        return 1
 
50
 
 
51
    pg_port = local.allocate_ports(1)[0]
 
52
    local.register_local_port("postgres", pg_port)
 
53
    os_exec(os.path.join(pg_bin, "postgres"),
 
54
            "-D", data_dir,
 
55
            "-k", sock_dir,
 
56
            "-i", "-h", "127.0.0.1",
 
57
            "-p", str(pg_port))
 
58
 
 
59
if __name__ == '__main__':
 
60
    sys.exit(start())