mirror of
https://github.com/LANCommander/Docker.Network.DHCP.git
synced 2026-08-01 03:07:47 -04:00
42 lines
838 B
Go
42 lines
838 B
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/client"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
OptionsKeyGeneric = "com.docker.network.generic"
|
|
)
|
|
|
|
func AwaitContainerInspect(ctx context.Context, docker *client.Client, id string, interval time.Duration) (types.ContainerJSON, error) {
|
|
var err error
|
|
ctrChan := make(chan types.ContainerJSON)
|
|
go func() {
|
|
for {
|
|
var ctr types.ContainerJSON
|
|
ctr, err = docker.ContainerInspect(ctx, id)
|
|
if err == nil {
|
|
ctrChan <- ctr
|
|
return
|
|
}
|
|
|
|
time.Sleep(interval)
|
|
}
|
|
}()
|
|
|
|
var dummy types.ContainerJSON
|
|
select {
|
|
case link := <-ctrChan:
|
|
return link, nil
|
|
case <-ctx.Done():
|
|
if err != nil {
|
|
log.WithError(err).WithField("id", id).Error("Failed to await container by ID")
|
|
}
|
|
return dummy, ctx.Err()
|
|
}
|
|
}
|