@@ -32,22 +32,22 @@ type Node struct {
32
32
PrivateIP string
33
33
DataDir string
34
34
Region string
35
- PGPort int
36
- ProxyPort int
35
+ Port int
36
+
37
+ PGBouncer PGBouncer
37
38
38
39
SUCredentials Credentials
39
40
OperatorCredentials Credentials
40
- ManagerCredentials Credentials
41
41
42
+ ManagerCredentials Credentials
42
43
ManagerConfigPath string
43
44
ManagerDatabaseName string
44
45
}
45
46
46
47
func NewNode () (* Node , error ) {
47
48
node := & Node {
48
49
AppName : "local" ,
49
- PGPort : 5433 ,
50
- ProxyPort : 5432 ,
50
+ Port : 5433 ,
51
51
DataDir : "/data/postgresql" ,
52
52
ManagerDatabaseName : "repmgr" ,
53
53
ManagerConfigPath : "/data/repmgr.conf" ,
@@ -64,14 +64,14 @@ func NewNode() (*Node, error) {
64
64
}
65
65
node .PrivateIP = ipv6 .String ()
66
66
67
- machineID := os .Getenv ("FLY_ALLOC_ID" )
68
67
// Generate a random, reconstructable signed int32
68
+ machineID := os .Getenv ("FLY_ALLOC_ID" )
69
69
seed := binary .LittleEndian .Uint64 ([]byte (machineID ))
70
70
rand .Seed (int64 (seed ))
71
71
node .ID = rand .Int31 ()
72
72
73
73
if port , err := strconv .Atoi (os .Getenv ("PG_PORT" )); err == nil {
74
- node .PGPort = port
74
+ node .Port = port
75
75
}
76
76
77
77
// Internal user
@@ -92,6 +92,14 @@ func NewNode() (*Node, error) {
92
92
Password : "supersecret" ,
93
93
}
94
94
95
+ node .PGBouncer = PGBouncer {
96
+ PrivateIP : node .PrivateIP ,
97
+ Port : 5432 ,
98
+ ForwardPort : 5433 ,
99
+ ConfigPath : "/data/pgbouncer" ,
100
+ Credentials : node .OperatorCredentials ,
101
+ }
102
+
95
103
return node , nil
96
104
}
97
105
@@ -116,6 +124,12 @@ func (n *Node) Init() error {
116
124
fmt .Printf ("Failed to initialize replmgr: %s\n " , err .Error ())
117
125
}
118
126
127
+ // Initialize PGBouncer
128
+ fmt .Println ("Initializing PGBouncer" )
129
+ if err := n .PGBouncer .initialize (); err != nil {
130
+ return err
131
+ }
132
+
119
133
switch primaryIP {
120
134
case n .PrivateIP :
121
135
// Noop
@@ -163,11 +177,6 @@ func (n *Node) Init() error {
163
177
return fmt .Errorf ("failed to configure postgres %s" , err )
164
178
}
165
179
166
- fmt .Println ("Configuring pgbouncer auth" )
167
- if err := n .ConfigurePGBouncerAuth (); err != nil {
168
- return fmt .Errorf ("failed to configure pgbouncer auth %s" , err )
169
- }
170
-
171
180
return nil
172
181
}
173
182
@@ -270,31 +279,25 @@ func (n *Node) PostInit() error {
270
279
return fmt .Errorf ("failed to query current primary: %s" , err )
271
280
}
272
281
273
- fmt .Println ("Configuring pgbouncer primary" )
274
- if err := n .ConfigurePGBouncerPrimary (primaryIP , false ); err != nil {
275
- return fmt .Errorf ("failed to configure pgbouncer primary %s" , err )
282
+ if err := n .PGBouncer .ConfigurePrimary (primaryIP , true ); err != nil {
283
+ return fmt .Errorf ("failed to configure pgbouncer's primary: %s" , err )
276
284
}
277
285
278
286
return nil
279
287
}
280
288
281
- func (n * Node ) NewPGBouncerConnection (ctx context.Context ) (* pgx.Conn , error ) {
282
- host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .ProxyPort ))
283
- return openConnection (ctx , host , "pgbouncer" , n .SUCredentials )
284
- }
285
-
286
289
func (n * Node ) NewLocalConnection (ctx context.Context ) (* pgx.Conn , error ) {
287
- host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .PGPort ))
288
- return openConnection (ctx , host , "postgres" , n .SUCredentials )
290
+ host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .Port ))
291
+ return openConnection (ctx , host , "postgres" , n .OperatorCredentials )
289
292
}
290
293
291
294
func (n * Node ) NewRepLocalConnection (ctx context.Context ) (* pgx.Conn , error ) {
292
- host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .PGPort ))
295
+ host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .Port ))
293
296
return openConnection (ctx , host , "repmgr" , n .ManagerCredentials )
294
297
}
295
298
296
299
func (n * Node ) NewRepRemoteConnection (ctx context.Context , hostname string ) (* pgx.Conn , error ) {
297
- host := net .JoinHostPort (hostname , strconv .Itoa (n .PGPort ))
300
+ host := net .JoinHostPort (hostname , strconv .Itoa (n .Port ))
298
301
return openConnection (ctx , host , "repmgr" , n .ManagerCredentials )
299
302
}
300
303
@@ -370,53 +373,6 @@ func (n *Node) initializePostgres() error {
370
373
return nil
371
374
}
372
375
373
- func (n * Node ) ConfigurePGBouncerAuth () error {
374
- path := fmt .Sprintf ("%s/pgbouncer.auth" , "/data" )
375
- file , err := os .OpenFile (path , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0644 )
376
- if err != nil {
377
- return err
378
- }
379
- contents := fmt .Sprintf ("\" %s\" \" %s\" " , n .OperatorCredentials .Username , n .OperatorCredentials .Password )
380
- _ , err = file .Write ([]byte (contents ))
381
- if err != nil {
382
- return err
383
- }
384
- return nil
385
- }
386
-
387
- func (n * Node ) ConfigurePGBouncerPrimary (primary string , reload bool ) error {
388
- path := fmt .Sprintf ("%s/pgbouncer.database.ini" , "/data" )
389
- file , err := os .OpenFile (path , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0644 )
390
- if err != nil {
391
- return err
392
- }
393
- contents := fmt .Sprintf ("[databases]\n * = host=%s port=%d\n " , primary , n .PGPort )
394
- _ , err = file .Write ([]byte (contents ))
395
- if err != nil {
396
- return err
397
- }
398
-
399
- if reload {
400
- err = n .ReloadPGBouncerConfig ()
401
- if err != nil {
402
- fmt .Printf ("failed to reconfigure pgbouncer primary %s\n " , err )
403
- }
404
- }
405
- return nil
406
- }
407
-
408
- func (n * Node ) ReloadPGBouncerConfig () error {
409
- conn , err := n .NewPGBouncerConnection (context .TODO ())
410
- if err != nil {
411
- return err
412
- }
413
- _ , err = conn .Exec (context .TODO (), "RELOAD;" )
414
- if err != nil {
415
- return err
416
- }
417
- return nil
418
- }
419
-
420
376
func (n * Node ) configurePostgres () error {
421
377
cmdStr := fmt .Sprintf ("sed -i \" s/#shared_preload_libraries.*/shared_preload_libraries = 'repmgr'/\" /data/postgresql/postgresql.conf" )
422
378
return runCommand (cmdStr )
0 commit comments