Exemple #1
0
// getDockerClient obtains a new Docker client from the environment or
// from a Docker machine, starting it if necessary and permitted
func getDockerClient(out io.Writer, dockerMachine string, canStartDockerMachine bool) (*docker.Client, *dockerclient.Client, error) {
	if len(dockerMachine) > 0 {
		glog.V(2).Infof("Getting client for Docker machine %q", dockerMachine)
		dockerClient, engineAPIClient, err := getDockerMachineClient(dockerMachine, out, canStartDockerMachine)
		if err != nil {
			return nil, nil, errors.ErrNoDockerMachineClient(dockerMachine, err)
		}
		return dockerClient, engineAPIClient, nil
	}

	dockerTLSVerify := os.Getenv("DOCKER_TLS_VERIFY")
	dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
	if len(dockerTLSVerify) > 0 && len(dockerCertPath) == 0 {
		dockerCertPath = cliconfig.ConfigDir()
		os.Setenv("DOCKER_CERT_PATH", dockerCertPath)
	}

	if glog.V(4) {
		dockerHost := os.Getenv("DOCKER_HOST")
		if len(dockerHost) == 0 && len(dockerTLSVerify) == 0 && len(dockerCertPath) == 0 {
			glog.Infof("No Docker environment variables found. Will attempt default socket.")
		}
		if len(dockerHost) > 0 {
			glog.Infof("Will try Docker connection with host (DOCKER_HOST) %q", dockerHost)
		} else {
			glog.Infof("No Docker host (DOCKER_HOST) configured. Will attempt default socket.")
		}
		if len(dockerTLSVerify) > 0 {
			glog.Infof("DOCKER_TLS_VERIFY=%s", dockerTLSVerify)
		}
		if len(dockerCertPath) > 0 {
			glog.Infof("DOCKER_CERT_PATH=%s", dockerCertPath)
		}
	}
	dockerClient, _, err := dockerutil.NewHelper().GetClient()
	if err != nil {
		return nil, nil, errors.ErrNoDockerClient(err)
	}
	// FIXME: Workaround for docker engine API client on OS X - sets the default to
	// the wrong DOCKER_HOST string
	if runtime.GOOS == "darwin" {
		dockerHost := os.Getenv("DOCKER_HOST")
		if len(dockerHost) == 0 {
			os.Setenv("DOCKER_HOST", "unix:///var/run/docker.sock")
		}
	}
	engineAPIClient, err := dockerclient.NewEnvClient()
	if err != nil {
		return nil, nil, errors.ErrNoDockerClient(err)
	}
	if err = dockerClient.Ping(); err != nil {
		return nil, nil, errors.ErrCannotPingDocker(err)
	}
	glog.V(4).Infof("Docker ping succeeded")
	return dockerClient, engineAPIClient, nil
}
Exemple #2
0
// Status prints the OpenShift cluster status
func (c *ClientStatusConfig) Status(f *clientcmd.Factory, out io.Writer) error {
	dockerClient, _, err := getDockerClient(out, c.DockerMachine, false)
	if err != nil {
		return errors.ErrNoDockerClient(err)
	}
	helper := dockerhelper.NewHelper(dockerClient, nil)

	container, running, err := helper.GetContainerState(openshift.OpenShiftContainer)
	if err != nil {
		return errors.NewError("cannot get state of OpenShift container %s", openshift.OpenShiftContainer).WithCause(err)
	}

	if !running {
		return errors.NewError("OpenShift cluster is not running")
	}

	healthy, err := isHealthy(f)
	if err != nil {
		return err
	}
	if !healthy {
		return errors.NewError("OpenShift cluster health check failed")
	}

	config, err := openshift.GetConfigFromContainer(dockerClient)
	if err != nil {
		return err
	}

	fmt.Print(status(container, config))

	return nil
}
Exemple #3
0
func (c *ClientStopConfig) getDockerClient(out io.Writer) (*docker.Client, error) {
	// Get Docker client
	if len(c.DockerMachine) > 0 {
		return getDockerMachineClient(c.DockerMachine, out)
	}
	client, _, err := dockerutil.NewHelper().GetClient()
	if err != nil {
		return nil, errors.ErrNoDockerClient(err)
	}
	return client, nil
}
Exemple #4
0
// GetDockerClient will obtain a new Docker client from the environment or
// from a Docker machine, starting it if necessary
func (c *ClientStartConfig) GetDockerClient(out io.Writer) error {
	var err error

	if len(c.DockerMachine) > 0 {
		glog.V(2).Infof("Getting client for Docker machine %q", c.DockerMachine)
		c.dockerClient, err = getDockerMachineClient(c.DockerMachine, out)
		if err != nil {
			return errors.ErrNoDockerMachineClient(c.DockerMachine, err)
		}
		return nil
	}

	if glog.V(4) {
		dockerHost := os.Getenv("DOCKER_HOST")
		dockerTLSVerify := os.Getenv("DOCKER_TLS_VERIFY")
		dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
		if len(dockerHost) == 0 && len(dockerTLSVerify) == 0 && len(dockerCertPath) == 0 {
			glog.Infof("No Docker environment variables found. Will attempt default socket.")
		}
		if len(dockerHost) > 0 {
			glog.Infof("Will try Docker connection with host (DOCKER_HOST) %q", dockerHost)
		} else {
			glog.Infof("No Docker host (DOCKER_HOST) configured. Will attempt default socket.")
		}
		if len(dockerTLSVerify) > 0 {
			glog.Infof("DOCKER_TLS_VERIFY=%s", dockerTLSVerify)
		}
		if len(dockerCertPath) > 0 {
			glog.Infof("DOCKER_CERT_PATH=%s", dockerCertPath)
		}
	}
	c.dockerClient, _, err = dockerutil.NewHelper().GetClient()
	if err != nil {
		return errors.ErrNoDockerClient(err)
	}

	if err = c.dockerClient.Ping(); err != nil {
		return errors.ErrCannotPingDocker(err)
	}
	glog.V(4).Infof("Docker ping succeeded")
	return nil
}