Example #1
0
func (builder *STI) commitContainer(containerID, cmd, user string, env []string, labels map[string]string) (string, error) {
	opts := dockerpkg.CommitContainerOptions{
		Command:     []string{cmd},
		Env:         env,
		ContainerID: containerID,
		Repository:  builder.config.Tag,
		User:        user,
		Labels:      labels,
	}

	imageID, err := builder.docker.CommitContainer(opts)
	if err != nil {
		return "", errors.NewCommitError(builder.config.Tag, err)
	}

	return imageID, nil
}
Example #2
0
func commitContainer(docker dockerpkg.Docker, containerID, cmd, user, tag string, env, entrypoint []string, labels map[string]string) (string, error) {
	opts := dockerpkg.CommitContainerOptions{
		Command:     []string{cmd},
		Env:         env,
		Entrypoint:  entrypoint,
		ContainerID: containerID,
		Repository:  tag,
		User:        user,
		Labels:      labels,
	}

	imageID, err := docker.CommitContainer(opts)
	if err != nil {
		return "", s2ierr.NewCommitError(tag, err)
	}

	return imageID, nil
}
Example #3
0
// PostExecute allows to execute post-build actions after the Docker build
// finishes.
func (builder *STI) PostExecute(containerID, location string) error {
	var (
		err             error
		previousImageID string
	)

	if builder.incremental && builder.config.RemovePreviousImage {
		if previousImageID, err = builder.docker.GetImageID(builder.config.Tag); err != nil {
			glog.Errorf("Error retrieving previous image's metadata: %v", err)
		}
	}

	env, err := scripts.GetEnvironment(builder.config)
	if err != nil {
		glog.V(1).Infof("No user environment provided (%v)", err)
	}

	buildEnv := append(scripts.ConvertEnvironment(env), builder.generateConfigEnv()...)

	runCmd := builder.scriptsURL[api.Run]
	if strings.HasPrefix(runCmd, "image://") {
		// scripts from inside of the image, we need to strip the image part
		// NOTE: We use path.Join instead of filepath.Join to avoid converting the
		// path to UNC (Windows) format as we always run this inside container.
		runCmd = strings.TrimPrefix(runCmd, "image://")
	} else {
		// external scripts, in which case we're taking the directory to which they
		// were extracted and append scripts dir and name
		runCmd = path.Join(location, "scripts", api.Run)
	}
	existingLabels, err := builder.docker.GetLabels(builder.config.BuilderImage)
	if err != nil {
		glog.Errorf("Unable to read existing labels from current builder image %s", builder.config.BuilderImage)
	}

	buildImageUser, err := builder.docker.GetImageUser(builder.config.BuilderImage)
	if err != nil {
		return err
	}

	resultLabels := mergeLabels(util.GenerateOutputImageLabels(builder.sourceInfo, builder.config), existingLabels)
	opts := dockerpkg.CommitContainerOptions{
		Command:     append([]string{}, runCmd),
		Env:         buildEnv,
		ContainerID: containerID,
		Repository:  builder.config.Tag,
		User:        buildImageUser,
		Labels:      resultLabels,
	}

	imageID, err := builder.docker.CommitContainer(opts)
	if err != nil {
		return errors.NewCommitError(builder.config.Tag, err)
	}

	builder.result.Success = true
	builder.result.ImageID = imageID

	if len(builder.config.Tag) > 0 {
		glog.V(1).Infof("Successfully built %s", builder.config.Tag)
	} else {
		glog.V(1).Infof("Successfully built %s", imageID)
	}

	if builder.incremental && builder.config.RemovePreviousImage && previousImageID != "" {
		glog.V(1).Infof("Removing previously-tagged image %s", previousImageID)
		if err = builder.docker.RemoveImage(previousImageID); err != nil {
			glog.Errorf("Unable to remove previous image: %v", err)
		}
	}

	if builder.config.CallbackURL != "" {
		builder.result.Messages = builder.callbackInvoker.ExecuteCallback(builder.config.CallbackURL,
			builder.result.Success, resultLabels, builder.result.Messages)
	}

	return nil
}