|
1 | 1 | #!/usr/bin/env python |
2 | | -""" |
3 | | -Entry point for pg_withcluster. |
4 | | -""" |
5 | | -import tempfile |
6 | | -import subprocess as sp |
7 | | -import getpass |
8 | | -from optparse import OptionParser |
9 | | -from postgresql.cluster import Cluster |
10 | | -from postgresql.installation import Installation |
11 | | - |
12 | | -def main(args = sys.argv): |
13 | | - op = OptionParser( |
14 | | - "%prog [-p] [--version] {[setting=value], ...} [pg_config] command", |
15 | | - version = '1.0' |
16 | | - ) |
17 | | - op.allow_interspersed_args = False |
18 | | - op.add_option( |
19 | | - '-l', |
20 | | - dest = 'logfile', |
21 | | - help = 'logfile for the PostgreSQL daemon', |
22 | | - default = '/dev/stderr', |
23 | | - ) |
24 | | - op.add_option( |
25 | | - '-p', |
26 | | - dest = 'look_in_path', |
27 | | - help = 'Look for "pg_config" in the PATH environment', |
28 | | - action = 'store_true', |
29 | | - default = False |
30 | | - ) |
31 | | - op.add_option( |
32 | | - '-N', |
33 | | - dest = 'max_connections', |
34 | | - help = 'Number', |
35 | | - type = 'int', |
36 | | - default = 4, |
37 | | - ) |
38 | | - co, ca = op.parse_args(args[1:]) |
39 | | - if not ca: |
40 | | - sys.stderr.write("no command given to run in cluster context") |
41 | | - sys.exit(1) |
42 | | - |
43 | | - max_con = co.max_connections |
44 | | - if max_con <= 0: |
45 | | - max_con = 4 |
46 | | - i = 0 |
47 | | - for x in ca: |
48 | | - if '=' not in x: |
49 | | - break |
50 | | - i += 1 |
51 | | - settings = ca[0:i] |
52 | | - command = ca[i:] |
53 | | - d = tempfile.mkdtemp() |
54 | | - try: |
55 | | - c = Cluster(d, inn) |
56 | | - c.init( |
57 | | - user = getpass.getuser(), |
58 | | - ) |
59 | | - c.settings.update([ |
60 | | - x.split('=', 1) for x in settings |
61 | | - ]) |
62 | | - c.settings.update({ |
63 | | - 'port' : '14327', |
64 | | - 'listen_addresses' : 'localhost', |
65 | | - 'max_connections' : str(co.max_connections), |
66 | | - 'shared_buffers' : str((co.max_connections * 2) + 50), |
67 | | - }) |
68 | | - c.start(logfile = sys.stderr) |
69 | | - c.wait_until_started() |
70 | | - with c.connect() as con: |
71 | | - pass |
72 | | - try: |
73 | | - p = sp.Popen( |
74 | | - command, |
75 | | - env = { |
76 | | - 'PGUSER' : getpass.getuser(), |
77 | | - 'PGHOST' : 'localhost', |
78 | | - 'PGPORT' : c.settings['port'], |
79 | | - 'PGDATABASE' : getpass.getuser(), |
80 | | - 'PGINSTALLATION' : c.installation.pg_config_path, |
81 | | - 'PGDATA' : d, |
82 | | - 'PG_WITHCLUSTER' : '1', |
83 | | - }, |
84 | | - ) |
85 | | - while True: |
86 | | - try: |
87 | | - return p.wait() |
88 | | - except OSError as e: |
89 | | - if e.errno != errno.ESRCH: |
90 | | - raise |
91 | | - # sigh. |
92 | | - finally: |
93 | | - c.stop() |
94 | | - finally: |
95 | | - c.drop() |
96 | | - |
97 | | -if __name__ == '__main__': |
98 | | - sys.exit(main()) |
| 2 | +import sys |
| 3 | +import postgresql.bin.pg_withcluster as cmd |
| 4 | +sys.exit(cmd.command(sys.argv)) |
0 commit comments