Beispiel #1
0
func (t *Task) forwardSignals(client client.DockerClient, containerID string) chan<- os.Signal {
	chanSig := make(chan os.Signal, 128)

	// TODO: not all of these exist on windows?
	signal.Notify(chanSig, syscall.SIGINT, syscall.SIGTERM)

	kill := func(sig os.Signal) {
		t.logger().WithFields(log.Fields{"signal": sig}).Debug("received")

		intSig, ok := sig.(syscall.Signal)
		if !ok {
			t.logger().WithFields(log.Fields{"signal": sig}).Warnf(
				"Failed to convert signal from %T", sig)
			return
		}

		if err := client.KillContainer(docker.KillContainerOptions{
			ID:     containerID,
			Signal: docker.Signal(intSig),
		}); err != nil {
			t.logger().WithFields(log.Fields{"signal": sig}).Warnf(
				"Failed to send signal: %s", err)
		}
	}

	go func() {
		for sig := range chanSig {
			kill(sig)
		}
	}()
	return chanSig
}
Beispiel #2
0
func (t *Task) wait(client client.DockerClient, containerID string) error {
	status, err := client.WaitContainer(containerID)
	if err != nil {
		return fmt.Errorf("failed to wait on container exit: %s", err)
	}
	if status != 0 {
		return fmt.Errorf("exited with non-zero status code %d", status)
	}
	return nil
}
Beispiel #3
0
// RemoveContainer removes a container
func RemoveContainer(logger *log.Entry, client client.DockerClient, containerID string, expectContainer bool) {
	logger.Debug("Removing container")
	err := client.RemoveContainer(docker.RemoveContainerOptions{
		ID:            containerID,
		RemoveVolumes: true,
	})
	switch err.(type) {
	case *docker.NoSuchContainer:
		if !expectContainer {
			return
		}
	case nil:
		return
	}
	logger.WithFields(log.Fields{"container": containerID}).Warnf(
		"Failed to remove container: %s", err)
}