@@ -224,28 +224,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
224
224
return errors .Wrap (err , "failed to adjust by init parameters" )
225
225
}
226
226
227
- // Start PostgreSQL instance.
228
- startCommand , err := r .dockerClient .ContainerExecCreate (ctx , contID ,
229
- pgCommandConfig ("postgres" , dataDir , pgVersion ))
230
-
231
- if err != nil {
232
- return errors .Wrap (err , "failed to create an exec command" )
233
- }
234
-
235
- log .Msg ("Running refresh command" )
236
-
237
- attachResponse , err := r .dockerClient .ContainerExecAttach (ctx , startCommand .ID , types.ExecStartCheck {})
238
- if err != nil {
239
- return errors .Wrap (err , "failed to attach to the exec command" )
240
- }
241
-
242
- defer attachResponse .Close ()
243
-
244
- if err := isDatabaseReady (attachResponse .Reader ); err != nil {
245
- return errors .Wrap (err , "failed to refresh data" )
246
- }
247
-
248
- log .Msg ("Refresh command has been finished" )
227
+ log .Msg ("Configuration has been finished" )
249
228
250
229
return nil
251
230
}
@@ -283,53 +262,6 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName, containe
283
262
return syncInstance .ID , nil
284
263
}
285
264
286
- func pgCommandConfig (cmd , pgDataDir , pgVersion string ) types.ExecConfig {
287
- command := fmt .Sprintf ("/usr/lib/postgresql/%s/bin/%s" , pgVersion , cmd )
288
-
289
- return types.ExecConfig {
290
- AttachStdout : true ,
291
- AttachStderr : true ,
292
- Cmd : []string {command , "-D" , pgDataDir },
293
- User : defaults .Username ,
294
- Env : os .Environ (),
295
- }
296
- }
297
-
298
- func isDatabaseReady (input io.Reader ) error {
299
- scanner := bufio .NewScanner (input )
300
-
301
- timer := time .NewTimer (time .Minute )
302
- defer timer .Stop ()
303
-
304
- LOOP:
305
- for {
306
- select {
307
- case <- timer .C :
308
- return errors .New ("timeout exceeded" )
309
- default :
310
- if ! scanner .Scan () {
311
- break LOOP
312
- }
313
-
314
- timer .Reset (time .Minute )
315
- }
316
-
317
- text := scanner .Text ()
318
-
319
- if strings .Contains (text , readyLogLine ) {
320
- return nil
321
- }
322
-
323
- fmt .Println (text )
324
- }
325
-
326
- if err := scanner .Err (); err != nil {
327
- return err
328
- }
329
-
330
- return errors .New ("database instance is not running" )
331
- }
332
-
333
265
func (r * RestoreJob ) syncInstanceName () string {
334
266
return cont .SyncInstanceContainerPrefix + r .globalCfg .InstanceID
335
267
}
@@ -358,34 +290,61 @@ func (r *RestoreJob) runSyncInstance(ctx context.Context) error {
358
290
return err
359
291
}
360
292
361
- // Set permissions.
362
- if err := tools .ExecCommand (ctx , r .dockerClient , syncInstanceID , types.ExecConfig {
363
- Cmd : []string {"chown" , "-R" , "postgres" , r .globalCfg .DataDir ()},
364
- }); err != nil {
365
- return errors .Wrap (err , "failed to set permissions" )
293
+ log .Msg ("Starting PostgreSQL" )
294
+
295
+ if err := tools .RunPostgres (ctx , r .dockerClient , syncInstanceID , r .globalCfg .DataDir ()); err != nil {
296
+ return errors .Wrap (err , "failed to start PostgreSQL instance" )
366
297
}
367
298
368
- pgVersion , err := tools .DetectPGVersion (r .globalCfg .DataDir ())
299
+ logs , err := r .dockerClient .ContainerLogs (ctx , syncInstanceID , types.ContainerLogsOptions {
300
+ ShowStdout : true ,
301
+ Follow : true ,
302
+ })
369
303
if err != nil {
370
- return err
304
+ return errors . Wrap ( err , "failed to get container logs" )
371
305
}
372
306
373
- startSyncCommand , err := r . dockerClient . ContainerExecCreate ( ctx , syncInstanceID ,
374
- pgCommandConfig ( "postgres" , r . globalCfg . DataDir (), pgVersion ))
375
- if err != nil {
376
- return errors .Wrap (err , "failed to create exec command " )
307
+ defer func () { _ = logs . Close () }()
308
+
309
+ if err := isDatabaseReady ( logs ); err != nil {
310
+ return errors .Wrap (err , "failed to run PostgreSQL " )
377
311
}
378
312
379
- if err = r .dockerClient .ContainerExecStart (ctx , startSyncCommand .ID , types.ExecStartCheck {
380
- Detach : true , Tty : true }); err != nil {
381
- return errors .Wrap (err , "failed to attach to exec command" )
313
+ return nil
314
+ }
315
+
316
+ func isDatabaseReady (input io.Reader ) error {
317
+ scanner := bufio .NewScanner (input )
318
+
319
+ timer := time .NewTimer (time .Minute )
320
+ defer timer .Stop ()
321
+
322
+ LOOP:
323
+ for {
324
+ select {
325
+ case <- timer .C :
326
+ return errors .New ("timeout exceeded" )
327
+ default :
328
+ if ! scanner .Scan () {
329
+ break LOOP
330
+ }
331
+
332
+ timer .Reset (time .Minute )
333
+ }
334
+
335
+ text := scanner .Text ()
336
+
337
+ if strings .Contains (text , readyLogLine ) {
338
+ log .Msg (text )
339
+ return nil
340
+ }
382
341
}
383
342
384
- if err := tools . InspectCommandResponse ( ctx , r . dockerClient , syncInstanceID , startSyncCommand . ID ); err != nil {
385
- return errors . Wrap ( err , "failed to perform exec command" )
343
+ if err := scanner . Err ( ); err != nil {
344
+ return err
386
345
}
387
346
388
- return nil
347
+ return errors . New ( "database instance is not running" )
389
348
}
390
349
391
350
func (r * RestoreJob ) getEnvironmentVariables (password string ) []string {
@@ -467,12 +426,11 @@ func (r *RestoreJob) adjustRecoveryConfiguration(pgVersion, pgDataDir string) er
467
426
recoveryFilename = "recovery.conf"
468
427
}
469
428
470
- return r . appendConfigFile (path .Join (pgDataDir , recoveryFilename ), r .restorer .GetRecoveryConfig ())
429
+ return appendConfigFile (path .Join (pgDataDir , recoveryFilename ), r .restorer .GetRecoveryConfig ())
471
430
}
472
431
473
432
func (r * RestoreJob ) applyInitParams (ctx context.Context , contID , pgVersion , dataDir string ) error {
474
- initConfCmd , err := r .dockerClient .ContainerExecCreate (ctx , contID ,
475
- pgCommandConfig ("pg_controldata" , dataDir , pgVersion ))
433
+ initConfCmd , err := r .dockerClient .ContainerExecCreate (ctx , contID , pgControlDataConfig (dataDir , pgVersion ))
476
434
477
435
if err != nil {
478
436
return errors .Wrap (err , "failed to create an exec command" )
@@ -487,15 +445,27 @@ func (r *RestoreJob) applyInitParams(ctx context.Context, contID, pgVersion, dat
487
445
488
446
defer attachResponse .Close ()
489
447
490
- initParams , err := r . extractInitParams (ctx , attachResponse .Reader )
448
+ initParams , err := extractInitParams (ctx , attachResponse .Reader )
491
449
if err != nil {
492
450
return err
493
451
}
494
452
495
- return r .appendInitConfigs (initParams , dataDir )
453
+ return appendInitConfigs (initParams , dataDir )
454
+ }
455
+
456
+ func pgControlDataConfig (pgDataDir , pgVersion string ) types.ExecConfig {
457
+ command := fmt .Sprintf ("/usr/lib/postgresql/%s/bin/pg_controldata" , pgVersion )
458
+
459
+ return types.ExecConfig {
460
+ AttachStdout : true ,
461
+ AttachStderr : true ,
462
+ Cmd : []string {command , "-D" , pgDataDir },
463
+ User : defaults .Username ,
464
+ Env : os .Environ (),
465
+ }
496
466
}
497
467
498
- func ( r * RestoreJob ) extractInitParams (ctx context.Context , read io.Reader ) (map [string ]string , error ) {
468
+ func extractInitParams (ctx context.Context , read io.Reader ) (map [string ]string , error ) {
499
469
extractedConfigs := make (map [string ]string )
500
470
scanner := bufio .NewScanner (read )
501
471
@@ -528,7 +498,7 @@ func (r *RestoreJob) extractInitParams(ctx context.Context, read io.Reader) (map
528
498
return extractedConfigs , nil
529
499
}
530
500
531
- func ( r * RestoreJob ) appendInitConfigs (initConfiguration map [string ]string , pgDataDir string ) error {
501
+ func appendInitConfigs (initConfiguration map [string ]string , pgDataDir string ) error {
532
502
if len (initConfiguration ) == 0 {
533
503
return nil
534
504
}
@@ -539,10 +509,10 @@ func (r *RestoreJob) appendInitConfigs(initConfiguration map[string]string, pgDa
539
509
buffer .WriteString (fmt .Sprintf ("%s = '%s'\n " , key , value ))
540
510
}
541
511
542
- return r . appendConfigFile (path .Join (pgDataDir , "postgresql.conf" ), buffer .Bytes ())
512
+ return appendConfigFile (path .Join (pgDataDir , "postgresql.conf" ), buffer .Bytes ())
543
513
}
544
514
545
- func ( r * RestoreJob ) appendConfigFile (file string , data []byte ) error {
515
+ func appendConfigFile (file string , data []byte ) error {
546
516
configFile , err := os .OpenFile (file , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
547
517
if err != nil {
548
518
return err
0 commit comments