Beispiel #1
0
func (getter clientGetterImpl) ContainerClient(localClient client.Client,
	stitchID int) (client.Client, error) {

	leaderClient, err := getter.LeaderClient(localClient)
	if err != nil {
		return nil, err
	}
	defer leaderClient.Close()

	containerInfo, err := util.GetContainer(leaderClient, stitchID)
	if err != nil {
		return nil, err
	}

	if containerInfo.Minion == "" {
		return nil, errors.New("container hasn't been scheduled yet")
	}

	containerIP, err := getPublicIP(localClient, containerInfo.Minion)
	if err != nil {
		return nil, err
	}

	return getter.Client(api.RemoteAddress(containerIP))
}
Beispiel #2
0
// Run finds the target continer, and executes the given command in it.
func (eCmd *Exec) Run() int {
	if eCmd.allocatePTY && !isTerminal() {
		log.Error("Cannot allocate pseudo-terminal without a terminal")
		return 1
	}

	localClient, err := eCmd.clientGetter.Client(eCmd.common.host)
	if err != nil {
		log.Error(err)
		return 1
	}
	defer localClient.Close()

	containerClient, err := eCmd.clientGetter.ContainerClient(
		localClient, eCmd.targetContainer)
	if err != nil {
		log.WithError(err).Error("Error getting container client")
		return 1
	}

	container, err := util.GetContainer(containerClient, eCmd.targetContainer)
	if err != nil {
		log.WithError(err).Error("Error getting container information")
		return 1
	}

	err = eCmd.SSHClient.Connect(containerClient.Host(), eCmd.privateKey)
	if err != nil {
		log.WithError(err).Info("Error opening SSH connection")
		return 1
	}
	defer eCmd.SSHClient.Disconnect()

	if eCmd.allocatePTY {
		if err = eCmd.SSHClient.RequestPTY(); err != nil {
			log.WithError(err).Info("Error requesting pseudo-terminal")
			return 1
		}
	}

	var flags string
	if eCmd.allocatePTY {
		flags = "-it"
	}
	command := strings.Join(
		[]string{"docker exec", flags, container.DockerID, eCmd.command}, " ")
	if err = eCmd.SSHClient.Run(command); err != nil {
		log.WithError(err).Info("Error running command over SSH")
		return 1
	}

	return 0
}
Beispiel #3
0
// Run finds the target continer and outputs logs.
func (lCmd *Log) Run() int {
	localClient, err := lCmd.clientGetter.Client(lCmd.common.host)
	if err != nil {
		log.Error(err)
		return 1
	}
	defer localClient.Close()

	containerClient, err := lCmd.clientGetter.ContainerClient(
		localClient, lCmd.targetContainer)
	if err != nil {
		log.WithError(err).Error("Error getting container client.")
		return 1
	}

	container, err := util.GetContainer(containerClient, lCmd.targetContainer)
	if err != nil {
		log.WithError(err).Error("Error getting container information.")
		return 1
	}

	dockerCmd := "docker logs"
	if lCmd.sinceTimestamp != "" {
		dockerCmd += fmt.Sprintf(" --since=%s", lCmd.sinceTimestamp)
	}
	if lCmd.showTimestamps {
		dockerCmd += " --timestamps"
	}
	if lCmd.shouldTail {
		dockerCmd += " --follow"
	}
	dockerCmd += " " + container.DockerID

	err = lCmd.SSHClient.Connect(containerClient.Host(), lCmd.privateKey)
	if err != nil {
		log.WithError(err).Info("Error opening SSH connection")
		return 1
	}
	defer lCmd.SSHClient.Disconnect()

	if err = lCmd.SSHClient.Run(dockerCmd); err != nil {
		log.WithError(err).Info("Error running command over SSH")
		return 1
	}

	return 0
}