Ejemplo n.º 1
0
// Build implements Builder. It consumes the docker build API endpoint and sends
// a tar of the specified service build context.
func (d *DaemonBuilder) Build(imageName string, p *project.Project, service project.Service) error {
	if service.Config().Build == "" {
		return fmt.Errorf("Specified service does not have a build section")
	}

	context, err := CreateTar(p, service.Name())
	if err != nil {
		return err
	}

	defer context.Close()

	client := d.context.ClientFactory.Create(service)

	logrus.Infof("Building %s...", imageName)

	return client.BuildImage(dockerclient.BuildImageOptions{
		InputStream:    context,
		OutputStream:   os.Stdout,
		RawJSONStream:  false,
		Name:           imageName,
		RmTmpContainer: true,
		Dockerfile:     service.Config().Dockerfile,
		NoCache:        d.context.NoCache,
	})
}
Ejemplo n.º 2
0
// Build implements Builder. It consumes the docker build API endpoint and sends
// a tar of the specified service build context.
func (d *DaemonBuilder) Build(p *project.Project, service project.Service) (string, error) {
	if service.Config().Build == "" {
		return service.Config().Image, nil
	}

	tag := fmt.Sprintf("%s_%s", p.Name, service.Name())
	context, err := CreateTar(p, service.Name())
	if err != nil {
		return "", err
	}

	defer context.Close()

	client := d.context.ClientFactory.Create(service)

	logrus.Infof("Building %s...", tag)

	err = client.BuildImage(dockerclient.BuildImageOptions{
		InputStream:    context,
		OutputStream:   os.Stdout,
		RawJSONStream:  false,
		Name:           tag,
		RmTmpContainer: true,
		Dockerfile:     service.Config().Dockerfile,
		NoCache:        d.context.NoCache,
	})

	if err != nil {
		return "", err
	}

	return tag, nil
}
Ejemplo n.º 3
0
// Build implements Builder. It consumes the docker build API endpoint and sends
// a tar of the specified service build context.
func (d *DaemonBuilder) Build(imageName string, p *project.Project, service project.Service) error {
	if service.Config().Build == "" {
		return fmt.Errorf("Specified service does not have a build section")
	}

	ctx, err := CreateTar(p, service.Name())
	if err != nil {
		return err
	}
	defer ctx.Close()

	var progBuff io.Writer = os.Stdout
	var buildBuff io.Writer = os.Stdout

	// Setup an upload progress bar
	progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(progBuff, true)

	var body io.Reader = progress.NewProgressReader(ctx, progressOutput, 0, "", "Sending build context to Docker daemon")

	client := d.context.ClientFactory.Create(service)

	logrus.Infof("Building %s...", imageName)

	outFd, isTerminalOut := term.GetFdInfo(os.Stdout)

	response, err := client.ImageBuild(context.Background(), types.ImageBuildOptions{
		Context:     body,
		Tags:        []string{imageName},
		NoCache:     d.context.NoCache,
		Remove:      true,
		Dockerfile:  service.Config().Dockerfile,
		AuthConfigs: d.context.ConfigFile.AuthConfigs,
	})

	err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, outFd, isTerminalOut, nil)
	if err != nil {
		if jerr, ok := err.(*jsonmessage.JSONError); ok {
			// If no error code is set, default to 1
			if jerr.Code == 0 {
				jerr.Code = 1
			}
			fmt.Fprintf(os.Stderr, "%s%s", progBuff, buildBuff)
			return fmt.Errorf("Status: %s, Code: %d", jerr.Message, jerr.Code)
		}
	}
	return err
}
Ejemplo n.º 4
0
// Build implements Builder. It consumes the docker build API endpoint and sends
// a tar of the specified service build context.
func (d *DaemonBuilder) Build(p *project.Project, service project.Service) (string, error) {
	if service.Config().Build == "" {
		return service.Config().Image, nil
	}

	tag := fmt.Sprintf("%s_%s", p.Name, service.Name())
	context, err := CreateTar(p, service.Name())
	if err != nil {
		return "", err
	}

	defer context.Close()

	client := d.context.ClientFactory.Create(service)

	logrus.Infof("Building %s...", tag)
	output, err := client.BuildImage(&dockerclient.BuildImage{
		Context:        context,
		RepoName:       tag,
		Remove:         true,
		DockerfileName: service.Config().Dockerfile,
	})
	if err != nil {
		return "", err
	}

	defer output.Close()

	// Don't really care about errors in the scanner
	scanner := bufio.NewScanner(output)
	for scanner.Scan() {
		text := scanner.Text()
		data := map[string]interface{}{}
		err := json.Unmarshal([]byte(text), &data)
		if stream, ok := data["stream"]; ok && err == nil {
			fmt.Print(stream)
		}
	}

	return tag, nil
}