Exemple #1
0
// ContainerSetStdinHandler returns the stdin
func (i *InteractionHandlersImpl) ContainerSetStdinHandler(params interaction.ContainerSetStdinParams) middleware.Responder {
	defer trace.End(trace.Begin(params.ID))

	var ctxDeadline time.Time
	var timeout time.Duration

	// Calculate the timeout for the attach if the caller specified a deadline.  This deadline
	if params.Deadline != nil {
		ctxDeadline = time.Time(*params.Deadline)
		timeout = ctxDeadline.Sub(time.Now())
		log.Debugf("Attempting to get ssh session for container %s stdin with deadline %s", params.ID, ctxDeadline.Format(time.UnixDate))
		if timeout < 0 {
			e := &models.Error{Message: fmt.Sprintf("Deadline for stdin already passed for container %s", params.ID)}
			return interaction.NewContainerSetStdinInternalServerError().WithPayload(e)
		}
	} else {
		log.Debugf("Attempting to get ssh session for container %s stdin", params.ID)
		timeout = interactionTimeout
	}

	session, err := i.attachServer.Get(context.Background(), params.ID, timeout)
	if err != nil {
		log.Errorf("%s", err.Error())

		e := &models.Error{
			Message: fmt.Sprintf("No stdin connection found (id: %s): %s", params.ID, err.Error()),
		}
		return interaction.NewContainerSetStdinNotFound().WithPayload(e)
	}
	// Remove the connection from the map
	defer func() {
		// io.EOF is expected if the channel is already closed so ignore it
		if err := i.attachServer.Remove(params.ID); err != nil && err != io.EOF {
			log.Errorf("Removing the connection from the map failed with %s", err)
		}
	}()

	detachableIn := NewFlushingReaderWithInitBytes(params.RawStream, []byte(attachStdinInitString))
	_, err = io.Copy(session.Stdin(), detachableIn)
	if err != nil {
		log.Errorf("%s", err.Error())

		// FIXME (caglar10ur): Do not return an error here - https://github.com/vmware/vic/issues/2594
		/*
			e := &models.Error{
				Message: fmt.Sprintf("Error copying stdin (id: %s): %s", params.ID, err.Error()),
			}
			return interaction.NewContainerSetStdinInternalServerError().WithPayload(e)
		*/
	}

	log.Debugf("Done copying stdin")

	return interaction.NewContainerSetStdinOK()
}
Exemple #2
0
func (i *InteractionHandlersImpl) ContainerSetStdinHandler(params interaction.ContainerSetStdinParams) middleware.Responder {
	defer trace.End(trace.Begin(params.ID))

	var ctxDeadline time.Time
	var timeout time.Duration

	// Calculate the timeout for the attach if the caller specified a deadline.  This deadline

	if params.Deadline != nil {
		ctxDeadline = time.Time(*params.Deadline)
		timeout = ctxDeadline.Sub(time.Now())
		log.Printf("Attempting to get ssh session for container %s stdin with deadline %s", params.ID, ctxDeadline.Format(time.UnixDate))
		if timeout < 0 {
			e := &models.Error{Message: fmt.Sprintf("Deadline for stdin already passed for container %s", params.ID)}
			return interaction.NewContainerGetStdoutInternalServerError().WithPayload(e)
		}
	} else {
		log.Printf("Attempting to get ssh session for container %s stdin", params.ID)
		timeout = interactionTimeout
	}

	contConn, err := i.attachServer.Get(context.Background(), params.ID, timeout)
	if err != nil {
		err = fmt.Errorf("No stdin found (id:%s): %s", params.ID, err.Error())
		log.Errorf("%s", err.Error())

		return interaction.NewContainerSetStdinNotFound()
	}

	detachableIn := NewFlushingReader(params.RawStream)
	_, err = io.Copy(contConn.Stdin(), detachableIn)
	if err != nil {
		err = fmt.Errorf("Error copying stdin (id:%s): %s", params.ID, err.Error())
		log.Errorf("%s", err.Error())
	}

	log.Printf("Done copying stdin")

	return interaction.NewContainerSetStdinOK()
}