2015-08-04 21:45:38 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2018-10-23 04:13:32 +00:00
|
|
|
"github.com/rancher/os/config"
|
|
|
|
"github.com/rancher/os/pkg/log"
|
|
|
|
"github.com/rancher/os/pkg/util"
|
2016-05-24 00:21:28 +00:00
|
|
|
|
|
|
|
dockerclient "github.com/docker/engine-api/client"
|
|
|
|
composeClient "github.com/docker/libcompose/docker/client"
|
2015-08-04 21:45:38 +00:00
|
|
|
"github.com/docker/libcompose/project"
|
2018-10-23 04:13:32 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-08-04 21:45:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClientFactory struct {
|
2016-05-24 00:21:28 +00:00
|
|
|
userClient dockerclient.APIClient
|
|
|
|
systemClient dockerclient.APIClient
|
2015-08-04 21:45:38 +00:00
|
|
|
userOnce sync.Once
|
|
|
|
systemOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:21:28 +00:00
|
|
|
func NewClientFactory(opts composeClient.Options) (project.ClientFactory, error) {
|
2015-08-04 21:45:38 +00:00
|
|
|
userOpts := opts
|
|
|
|
systemOpts := opts
|
|
|
|
|
2016-11-28 08:06:00 +00:00
|
|
|
userOpts.Host = config.DockerHost
|
|
|
|
systemOpts.Host = config.SystemDockerHost
|
2015-08-04 21:45:38 +00:00
|
|
|
|
2016-05-24 00:21:28 +00:00
|
|
|
userClient, err := composeClient.Create(userOpts)
|
2015-08-04 21:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:21:28 +00:00
|
|
|
systemClient, err := composeClient.Create(systemOpts)
|
2015-08-04 21:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClientFactory{
|
|
|
|
userClient: userClient,
|
|
|
|
systemClient: systemClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:21:28 +00:00
|
|
|
func (c *ClientFactory) Create(service project.Service) dockerclient.APIClient {
|
2015-08-04 21:45:38 +00:00
|
|
|
if IsSystemContainer(service.Config()) {
|
2016-11-28 08:06:00 +00:00
|
|
|
waitFor(&c.systemOnce, c.systemClient, config.SystemDockerHost)
|
2015-08-04 21:45:38 +00:00
|
|
|
return c.systemClient
|
|
|
|
}
|
|
|
|
|
2016-11-28 08:06:00 +00:00
|
|
|
waitFor(&c.userOnce, c.userClient, config.DockerHost)
|
2015-08-04 21:45:38 +00:00
|
|
|
return c.userClient
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:21:28 +00:00
|
|
|
func waitFor(once *sync.Once, client dockerclient.APIClient, endpoint string) {
|
2015-08-04 21:45:38 +00:00
|
|
|
once.Do(func() {
|
|
|
|
err := ClientOK(endpoint, func() bool {
|
2016-05-24 00:21:28 +00:00
|
|
|
_, err := client.Info(context.Background())
|
2015-08-04 21:45:38 +00:00
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClientOK(endpoint string, test func() bool) error {
|
|
|
|
backoff := util.Backoff{}
|
|
|
|
defer backoff.Close()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
retry := false
|
|
|
|
for ok := range backoff.Start() {
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("Timeout waiting for Docker at %s", endpoint)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if test() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
retry = true
|
|
|
|
log.Infof("Waiting for Docker at %s", endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if retry {
|
|
|
|
log.Infof("Connected to Docker at %s", endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|