client.go 911 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package docker
  2. import (
  3. "time"
  4. log "github.com/Sirupsen/logrus"
  5. dockerClient "github.com/fsouza/go-dockerclient"
  6. "github.com/rancherio/os/config"
  7. )
  8. const (
  9. MAX_WAIT = 30000
  10. INTERVAL = 100
  11. )
  12. func NewSystemClient() (*dockerClient.Client, error) {
  13. return NewClient(config.DOCKER_SYSTEM_HOST)
  14. }
  15. func NewDefaultClient() (*dockerClient.Client, error) {
  16. return NewClient(config.DOCKER_HOST)
  17. }
  18. func NewClient(endpoint string) (*dockerClient.Client, error) {
  19. client, err := dockerClient.NewClient(endpoint)
  20. if err != nil {
  21. return nil, err
  22. }
  23. retry := false
  24. for i := 0; i < (MAX_WAIT / INTERVAL); i++ {
  25. _, err = client.Info()
  26. if err == nil {
  27. break
  28. }
  29. retry = true
  30. log.Infof("Waiting for Docker at %s", endpoint)
  31. time.Sleep(INTERVAL * time.Millisecond)
  32. }
  33. if err != nil {
  34. return nil, err
  35. }
  36. if retry {
  37. log.Infof("Connected to Docker at %s", endpoint)
  38. }
  39. return client, nil
  40. }