@@ -130,7 +130,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
130
130
log .Msg (fmt .Sprintf ("Run job: %s. Options: %v" , r .Name (), r .CopyOptions ))
131
131
132
132
defer func () {
133
- if err ! = nil && r .CopyOptions .SyncInstance {
133
+ if err = = nil && r .CopyOptions .SyncInstance {
134
134
if syncErr := r .runSyncInstance (ctx ); syncErr != nil {
135
135
log .Err ("Failed to run sync instance" , syncErr )
136
136
}
@@ -148,10 +148,6 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
148
148
return nil
149
149
}
150
150
151
- if err := tools .PullImage (ctx , r .dockerClient , r .CopyOptions .DockerImage ); err != nil {
152
- return errors .Wrap (err , "failed to scan image pulling response" )
153
- }
154
-
155
151
contID , err := r .startContainer (ctx , r .restoreContainerName ())
156
152
if err != nil {
157
153
return errors .Wrapf (err , "failed to create container: %s" , r .restoreContainerName ())
@@ -216,7 +212,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
216
212
}
217
213
218
214
// Start PostgreSQL instance.
219
- startCommand , err := r .dockerClient .ContainerExecCreate (ctx , contID , startingPostgresConfig (r .globalCfg .DataDir ))
215
+ startCommand , err := r .dockerClient .ContainerExecCreate (ctx , contID , startingPostgresConfig (r .globalCfg .DataDir , pgVersion ))
220
216
221
217
if err != nil {
222
218
return errors .Wrap (err , "failed to create an exec command" )
@@ -241,7 +237,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
241
237
}
242
238
243
239
func (r * RestoreJob ) startContainer (ctx context.Context , containerName string ) (string , error ) {
244
- hostConfig , err := r .buildHostConfig ()
240
+ hostConfig , err := r .buildHostConfig (ctx )
245
241
if err != nil {
246
242
return "" , errors .Wrap (err , "failed to build container host config" )
247
243
}
@@ -251,6 +247,10 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName string) (
251
247
return "" , errors .Wrap (err , "failed to generate PostgreSQL password" )
252
248
}
253
249
250
+ if err := tools .PullImage (ctx , r .dockerClient , r .CopyOptions .DockerImage ); err != nil {
251
+ return "" , errors .Wrap (err , "failed to scan image pulling response" )
252
+ }
253
+
254
254
syncInstance , err := r .dockerClient .ContainerCreate (ctx ,
255
255
r .buildContainerConfig (pwd ),
256
256
hostConfig ,
@@ -269,12 +269,15 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName string) (
269
269
return syncInstance .ID , nil
270
270
}
271
271
272
- func startingPostgresConfig (pgDataDir string ) types.ExecConfig {
272
+ func startingPostgresConfig (pgDataDir , pgVersion string ) types.ExecConfig {
273
+ command := fmt .Sprintf ("/usr/lib/postgresql/%s/bin/postgres" , pgVersion )
274
+
273
275
return types.ExecConfig {
274
276
AttachStdout : true ,
275
277
AttachStderr : true ,
276
- Cmd : []string {"postgres" , "-D" , pgDataDir },
278
+ Cmd : []string {command , "-D" , pgDataDir },
277
279
User : defaults .Username ,
280
+ Env : os .Environ (),
278
281
}
279
282
}
280
283
@@ -284,11 +287,17 @@ func isDatabaseReady(input io.Reader) error {
284
287
timer := time .NewTimer (time .Minute )
285
288
defer timer .Stop ()
286
289
287
- for scanner .Scan () {
290
+ LOOP:
291
+ for {
288
292
select {
289
293
case <- timer .C :
290
294
return errors .New ("timeout exceeded" )
291
295
default :
296
+ if ! scanner .Scan () {
297
+ break LOOP
298
+ }
299
+
300
+ timer .Reset (time .Minute )
292
301
}
293
302
294
303
text := scanner .Text ()
@@ -335,16 +344,29 @@ func (r *RestoreJob) runSyncInstance(ctx context.Context) error {
335
344
return err
336
345
}
337
346
338
- startSyncCommand , err := r .dockerClient .ContainerExecCreate (ctx , syncInstanceID , startingPostgresConfig (r .globalCfg .DataDir ))
347
+ // Set permissions.
348
+ if err := tools .ExecCommand (ctx , r .dockerClient , syncInstanceID , types.ExecConfig {
349
+ Cmd : []string {"chown" , "-R" , "postgres" , r .globalCfg .DataDir },
350
+ }); err != nil {
351
+ return errors .Wrap (err , "failed to set permissions" )
352
+ }
353
+
354
+ pgVersion , err := tools .DetectPGVersion (r .globalCfg .DataDir )
355
+ if err != nil {
356
+ return err
357
+ }
358
+
359
+ startSyncCommand , err := r .dockerClient .ContainerExecCreate (ctx , syncInstanceID , startingPostgresConfig (r .globalCfg .DataDir , pgVersion ))
339
360
if err != nil {
340
361
return errors .Wrap (err , "failed to create exec command" )
341
362
}
342
363
343
- if err = r .dockerClient .ContainerExecStart (ctx , startSyncCommand .ID , types.ExecStartCheck {Tty : true }); err != nil {
364
+ if err = r .dockerClient .ContainerExecStart (ctx , startSyncCommand .ID , types.ExecStartCheck {
365
+ Detach : true , Tty : true }); err != nil {
344
366
return errors .Wrap (err , "failed to attach to exec command" )
345
367
}
346
368
347
- if err := tools .InspectCommandResponse (ctx , r .dockerClient , startSyncCommand . ID , startSyncCommand .ID ); err != nil {
369
+ if err := tools .InspectCommandResponse (ctx , r .dockerClient , syncInstanceID , startSyncCommand .ID ); err != nil {
348
370
return errors .Wrap (err , "failed to perform exec command" )
349
371
}
350
372
@@ -373,16 +395,16 @@ func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
373
395
return & container.Config {
374
396
Labels : map [string ]string {"label" : tools .DBLabControlLabel },
375
397
Env : r .getEnvironmentVariables (password ),
376
- Image : r .DockerImage ,
398
+ Image : r .CopyOptions . DockerImage ,
377
399
}
378
400
}
379
401
380
- func (r * RestoreJob ) buildHostConfig () (* container.HostConfig , error ) {
402
+ func (r * RestoreJob ) buildHostConfig (ctx context. Context ) (* container.HostConfig , error ) {
381
403
hostConfig := & container.HostConfig {
382
404
Mounts : r .restorer .GetMounts (),
383
405
}
384
406
385
- if err := tools .AddVolumesToHostConfig (hostConfig , r .globalCfg .DataDir ); err != nil {
407
+ if err := tools .AddVolumesToHostConfig (ctx , r . dockerClient , hostConfig , r .globalCfg .DataDir ); err != nil {
386
408
return nil , err
387
409
}
388
410
0 commit comments