Exemple #1
0
// We only want to attach to containers associated with a particular
// Image name
func (api *DockerApi) Attach(containerId string) ContainerChannel {
	out := make(ContainerChannel) // channel to send back
	go CaptureUserCancel(&out, nil)

	go func() {
		endpoint := api.configFile.DockerEndpoint

		baseUrl := strings.Join(
			[]string{
				endpoint,
				"containers",
				containerId,
				"attach",
			},
			"/",
		)

		params := strings.Join(
			[]string{
				"stdout=true",
				"stderr=true",
				"stream=true",
			},
			"&",
		)
		url := baseUrl + "?" + params
		Logger.Trace("Attach() - Api call to:", url)

		// jsonResult := ""
		byteData := []byte{}
		bytesReader := bytes.NewReader(byteData)
		resp, err := http.Post(url, "text/json", bytesReader)
		defer resp.Body.Close()
		if err != nil {
			Logger.Error("Could not submit request, err:", err)
		}

		reader := bufio.NewReader(resp.Body)

		for {
			if line, err := reader.ReadBytes('\n'); err != nil {
				if err == io.EOF {
					break
				} else {
					msg := "Error reading from stream on attached container, error:" + err.Error()
					Logger.Error(msg)
					// close(out)
					// return error.New(msg)
				}
			} else {
				Logger.ConsoleChannel(string(bytes.TrimSpace(line)[:]))
			}
		}

		close(out)
	}()

	return out
}
Exemple #2
0
func (api *DockerApi) BuildImage(
	buffer *bytes.Buffer,
	imageName string,
	watch *ContainerChannel,
	wg *sync.WaitGroup,
) {
	// out := make(ContainerChannel) // channel to send back
	go CaptureUserCancel(watch, wg)

	go func() error {
		endpoint := api.configFile.DockerEndpoint

		baseUrl := strings.Join(
			[]string{
				endpoint,
				"build",
			},
			"/",
		)

		params := strings.Join(
			[]string{
				"t=" + imageName,
			},
			"&",
		)
		url := baseUrl + "?" + params
		Logger.Trace("BuildImage() - Api call to:", url)

		type BuildStream struct {
			Stream string
		}
		var jsonResult BuildStream

		bytesReader := bytes.NewReader(buffer.Bytes())
		resp, err := http.Post(url, "application/tar", bytesReader)
		defer resp.Body.Close()
		if err != nil {
			Logger.Error("Could not submit request, err:", err)
			return err
		}

		reader := bufio.NewReader(resp.Body)

		for {
			if line, err := reader.ReadBytes('\n'); err != nil {
				if err == io.EOF {
					break
				} else {
					msg := "Error reading from stream on building image, error:" + err.Error()
					Logger.Error(msg)
					*watch <- "ok"
					return errors.New(msg)
				}
			} else {
				if err := json.Unmarshal(line, &jsonResult); err != nil {
					Logger.Error(err)
				} else {
					Logger.ConsoleChannel(strings.TrimSpace(jsonResult.Stream))
				}
			}
		}

		*watch <- "ok"
		wg.Done()
		return nil
	}()
}