@@ -123,10 +123,26 @@ func NewNode() (*Node, error) {
123
123
}
124
124
125
125
func (n * Node ) Init (ctx context.Context ) error {
126
+ // Ensure directory and files have proper permissions
126
127
if err := setDirOwnership (); err != nil {
127
128
return err
128
129
}
129
130
131
+ // Initiate a restore
132
+ if os .Getenv ("FLY_RESTORED_FROM" ) != "" {
133
+ // Check to see if there's an active restore.
134
+ active , err := isRestoreActive ()
135
+ if err != nil {
136
+ return err
137
+ }
138
+
139
+ if active {
140
+ if err := Restore (ctx , n ); err != nil {
141
+ return fmt .Errorf ("failed to issue restore: %s" , err )
142
+ }
143
+ }
144
+ }
145
+
130
146
if ZombieLockExists () {
131
147
fmt .Println ("Zombie lock detected!" )
132
148
primaryStr , err := readZombieLock ()
@@ -188,7 +204,7 @@ func (n *Node) Init(ctx context.Context) error {
188
204
}
189
205
}
190
206
191
- err := WriteSSHKey ()
207
+ err := writeSSHKey ()
192
208
if err != nil {
193
209
return fmt .Errorf ("failed initialize ssh. %v" , err )
194
210
}
@@ -211,12 +227,10 @@ func (n *Node) Init(ctx context.Context) error {
211
227
212
228
if ! clusterInitialized {
213
229
// Initialize ourselves as the primary.
214
- fmt .Println ("Initializing postgres" )
215
230
if err := n .initializePG (); err != nil {
216
231
return fmt .Errorf ("failed to initialize postgres %s" , err )
217
232
}
218
233
219
- fmt .Println ("Setting default HBA" )
220
234
if err := n .setDefaultHBA (); err != nil {
221
235
return fmt .Errorf ("failed updating pg_hba.conf: %s" , err )
222
236
}
@@ -233,7 +247,6 @@ func (n *Node) Init(ctx context.Context) error {
233
247
}
234
248
}
235
249
236
- fmt .Println ("Initializing Postgres configuration" )
237
250
if err := n .configurePostgres (store ); err != nil {
238
251
return fmt .Errorf ("failed to configure postgres: %s" , err )
239
252
}
@@ -245,44 +258,6 @@ func (n *Node) Init(ctx context.Context) error {
245
258
return nil
246
259
}
247
260
248
- func WriteSSHKey () error {
249
- err := os .Mkdir ("/data/.ssh" , 0700 )
250
- if err != nil && ! os .IsExist (err ) {
251
- return err
252
- }
253
-
254
- key := os .Getenv ("SSH_KEY" )
255
-
256
- keyFile , err := os .Create ("/data/.ssh/id_rsa" )
257
- if err != nil {
258
- return err
259
- }
260
- defer keyFile .Close ()
261
- _ , err = keyFile .Write ([]byte (key ))
262
- if err != nil {
263
- return err
264
- }
265
-
266
- cert := os .Getenv ("SSH_CERT" )
267
-
268
- certFile , err := os .Create ("/data/.ssh/id_rsa-cert.pub" )
269
- if err != nil {
270
- return err
271
- }
272
- defer certFile .Close ()
273
- _ , err = certFile .Write ([]byte (cert ))
274
- if err != nil {
275
- return err
276
- }
277
-
278
- err = setSSHOwnership ()
279
- if err != nil {
280
- return err
281
- }
282
-
283
- return nil
284
- }
285
-
286
261
// PostInit are operations that should be executed against a running Postgres on boot.
287
262
func (n * Node ) PostInit (ctx context.Context ) error {
288
263
if ZombieLockExists () {
@@ -529,17 +504,14 @@ func (n *Node) isPGInitialized() bool {
529
504
}
530
505
531
506
func (n * Node ) configure (ctx context.Context , store * state.Store ) error {
532
- fmt .Println ("Initializing internal config" )
533
507
if err := n .configureInternal (store ); err != nil {
534
508
return fmt .Errorf ("failed to set internal config: %s" , err )
535
509
}
536
510
537
- fmt .Println ("Initializing replication manager" )
538
511
if err := n .configureRepmgr (store ); err != nil {
539
512
return fmt .Errorf ("failed to configure repmgr config: %s" , err )
540
513
}
541
514
542
- fmt .Println ("Initializing pgbouncer" )
543
515
if err := n .configurePGBouncer (store ); err != nil {
544
516
return fmt .Errorf ("failed to configure pgbouncer: %s" , err )
545
517
}
@@ -553,6 +525,44 @@ func (n *Node) configure(ctx context.Context, store *state.Store) error {
553
525
return nil
554
526
}
555
527
528
+ func writeSSHKey () error {
529
+ err := os .Mkdir ("/data/.ssh" , 0700 )
530
+ if err != nil && ! os .IsExist (err ) {
531
+ return err
532
+ }
533
+
534
+ key := os .Getenv ("SSH_KEY" )
535
+
536
+ keyFile , err := os .Create ("/data/.ssh/id_rsa" )
537
+ if err != nil {
538
+ return err
539
+ }
540
+ defer keyFile .Close ()
541
+ _ , err = keyFile .Write ([]byte (key ))
542
+ if err != nil {
543
+ return err
544
+ }
545
+
546
+ cert := os .Getenv ("SSH_CERT" )
547
+
548
+ certFile , err := os .Create ("/data/.ssh/id_rsa-cert.pub" )
549
+ if err != nil {
550
+ return err
551
+ }
552
+ defer certFile .Close ()
553
+ _ , err = certFile .Write ([]byte (cert ))
554
+ if err != nil {
555
+ return err
556
+ }
557
+
558
+ err = setSSHOwnership ()
559
+ if err != nil {
560
+ return err
561
+ }
562
+
563
+ return nil
564
+ }
565
+
556
566
func (n * Node ) configureInternal (store * state.Store ) error {
557
567
if err := n .InternalConfig .initialize (); err != nil {
558
568
return fmt .Errorf ("failed to initialize internal config: %s" , err )
0 commit comments