4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "log"
7
8
"net"
8
9
"os"
9
10
"os/exec"
@@ -163,7 +164,7 @@ func (n *Node) Init(ctx context.Context) error {
163
164
}
164
165
165
166
if ! clusterInitialized {
166
- fmt .Println ("Provisioning primary" )
167
+ log .Println ("Provisioning primary" )
167
168
// TODO - This should probably run on boot in case the password changes.
168
169
if err := n .PGConfig .writePasswordFile (n .OperatorCredentials .Password ); err != nil {
169
170
return fmt .Errorf ("failed to write pg password file: %s" , err )
@@ -173,7 +174,7 @@ func (n *Node) Init(ctx context.Context) error {
173
174
return fmt .Errorf ("failed to initialize postgres %s" , err )
174
175
}
175
176
} else {
176
- fmt .Println ("Provisioning standby" )
177
+ log .Println ("Provisioning standby" )
177
178
cloneTarget , err := n .RepMgr .ResolveMemberOverDNS (ctx )
178
179
if err != nil {
179
180
return fmt .Errorf ("failed to resolve member over dns: %s" , err )
@@ -182,7 +183,7 @@ func (n *Node) Init(ctx context.Context) error {
182
183
if err := n .RepMgr .clonePrimary (cloneTarget .Hostname ); err != nil {
183
184
// Clean-up the directory so it can be retried.
184
185
if rErr := os .Remove (n .DataDir ); rErr != nil {
185
- fmt .Printf ("failed to cleanup postgresql dir after clone error: %s\n " , rErr )
186
+ log .Printf ("[ERROR] failed to cleanup postgresql dir after clone error: %s\n " , rErr )
186
187
}
187
188
188
189
return fmt .Errorf ("failed to clone primary: %s" , err )
@@ -208,8 +209,8 @@ func (n *Node) Init(ctx context.Context) error {
208
209
// PostInit are operations that need to be executed against a running Postgres on boot.
209
210
func (n * Node ) PostInit (ctx context.Context ) error {
210
211
if ZombieLockExists () {
211
- fmt .Println ("Manual intervention required. Delete the zombie.lock file and restart the machine to force a retry." )
212
- fmt .Println ("Sleeping for 5 minutes." )
212
+ log .Println ("[ERROR] Manual intervention required. Delete the zombie.lock file and restart the machine to force a retry." )
213
+ log .Println ("[ERROR] Sleeping for 5 minutes." )
213
214
time .Sleep (5 * time .Minute )
214
215
return fmt .Errorf ("unrecoverable zombie" )
215
216
}
@@ -219,8 +220,8 @@ func (n *Node) PostInit(ctx context.Context) error {
219
220
if err != nil {
220
221
// Check to see if this is an authentication error.
221
222
if strings .Contains (err .Error (), "28P01" ) {
222
- fmt .Println ("WARNING: `postgres` user password does not match the `OPERATOR_PASSWORD` secret" )
223
- fmt .Printf ("HINT: Use `fly secrets set OPERATOR_PASSWORD=<password> --app %s` to resolve the issue\n " , n .AppName )
223
+ log .Println ("[WARN] `postgres` user password does not match the `OPERATOR_PASSWORD` secret" )
224
+ log .Printf ("[WARN] Use `fly secrets set OPERATOR_PASSWORD=<password> --app %s` to resolve the issue\n " , n .AppName )
224
225
}
225
226
226
227
return fmt .Errorf ("failed to establish connection to local node: %s" , err )
@@ -251,13 +252,13 @@ func (n *Node) PostInit(ctx context.Context) error {
251
252
// Verify cluster state to ensure we are the actual primary and not a zombie.
252
253
primary , err := PerformScreening (ctx , conn , n )
253
254
if errors .Is (err , ErrZombieDiagnosisUndecided ) {
254
- fmt .Println ("Unable to confirm that we are the true primary!" )
255
+ log .Println ("[ERROR] Unable to confirm that we are the true primary!" )
255
256
// Turn member read-only
256
257
if err := Quarantine (ctx , n , primary ); err != nil {
257
258
return fmt .Errorf ("failed to quarantine failed primary: %s" , err )
258
259
}
259
260
} else if errors .Is (err , ErrZombieDiscovered ) {
260
- fmt .Printf ("The majority of registered members agree that '%s' is the real primary.\n " , primary )
261
+ log .Printf ("[ERROR] The majority of registered members agree that '%s' is the real primary.\n " , primary )
261
262
// Turn member read-only
262
263
if err := Quarantine (ctx , n , primary ); err != nil {
263
264
return fmt .Errorf ("failed to quarantine failed primary: %s" , err )
@@ -285,7 +286,7 @@ func (n *Node) PostInit(ctx context.Context) error {
285
286
case StandbyRoleName :
286
287
// Register existing standby to take-on any configuration changes.
287
288
if err := n .RepMgr .registerStandby (); err != nil {
288
- fmt .Printf ("failed to register existing standby: %s\n " , err )
289
+ return fmt .Errorf ("failed to register existing standby: %s" , err )
289
290
}
290
291
default :
291
292
return fmt .Errorf ("member has unknown role: %q" , member .Role )
@@ -312,7 +313,7 @@ func (n *Node) PostInit(ctx context.Context) error {
312
313
313
314
if ! clusterInitialized {
314
315
// Configure as primary
315
- fmt .Println ("Registering primary" )
316
+ log .Println ("Registering primary" )
316
317
317
318
// Verify we reside within the clusters primary region
318
319
if ! n .RepMgr .eligiblePrimary () {
@@ -349,9 +350,9 @@ func (n *Node) PostInit(ctx context.Context) error {
349
350
}
350
351
} else {
351
352
// Configure as standby
352
- fmt .Println ("Registering standby" )
353
+ log .Println ("Registering standby" )
353
354
if err := n .RepMgr .registerStandby (); err != nil {
354
- fmt .Printf ("failed to register new standby: %s\n " , err )
355
+ return fmt .Errorf ("failed to register new standby: %s" , err )
355
356
}
356
357
357
358
// Let the boot process know that we've already been configured.
0 commit comments