コード例 #1
0
ファイル: provisioner.go プロジェクト: zhenruyan/tsuru
func (p *dockerProvisioner) ImageDeploy(app provision.App, imageId string, w io.Writer) (string, error) {
	cluster := p.Cluster()
	pullOpts := docker.PullImageOptions{
		Repository: imageId,
	}
	err := cluster.PullImage(pullOpts, docker.AuthConfiguration{})
	if err != nil {
		return "", err
	}
	cmd := "cat /home/application/current/Procfile || cat /app/user/Procfile || cat /Procfile"
	output, err := p.runCommandInContainer(imageId, cmd, app)
	if err != nil {
		return "", err
	}
	procfile := getProcessesFromProcfile(output.String())
	if len(procfile) == 0 {
		imageInspect, inspectErr := cluster.InspectImage(imageId)
		if inspectErr != nil {
			return "", inspectErr
		}
		if len(imageInspect.Config.Entrypoint) == 0 {
			return "", ErrEntrypointOrProcfileNotFound
		}
		procfile["web"] = strings.Join(imageInspect.Config.Entrypoint, " ")
	}
	newImage, err := appNewImageName(app.GetName())
	if err != nil {
		return "", err
	}
	imageInfo := strings.Split(newImage, ":")
	err = cluster.TagImage(imageId, docker.TagImageOptions{Repo: strings.Join(imageInfo[:len(imageInfo)-1], ":"), Tag: imageInfo[len(imageInfo)-1], Force: true})
	if err != nil {
		return "", err
	}
	registry, err := config.GetString("docker:registry")
	if err != nil {
		return "", err
	}
	pushOpts := docker.PushImageOptions{
		Name:     strings.Join(imageInfo[:len(imageInfo)-1], ":"),
		Tag:      imageInfo[len(imageInfo)-1],
		Registry: registry,
	}
	err = cluster.PushImage(pushOpts, mainDockerProvisioner.RegistryAuthConfig())
	if err != nil {
		return "", err
	}
	imageData := createImageMetadata(newImage, procfile)
	err = saveImageCustomData(newImage, imageData.CustomData)
	if err != nil {
		return "", err
	}
	app.SetUpdatePlatform(true)
	return newImage, p.deploy(app, newImage, w)
}
コード例 #2
0
ファイル: provisioner.go プロジェクト: ggarnier/tsuru
func (p *dockerProvisioner) ImageDeploy(app provision.App, imageId string, w io.Writer) (string, error) {
	cluster := p.Cluster()
	if !strings.Contains(imageId, ":") {
		imageId = fmt.Sprintf("%s:latest", imageId)
	}
	fmt.Fprintln(w, "---- Pulling image to tsuru ----")
	pullOpts := docker.PullImageOptions{
		Repository:   imageId,
		OutputStream: w,
	}
	err := cluster.PullImage(pullOpts, docker.AuthConfiguration{})
	if err != nil {
		return "", err
	}
	fmt.Fprintln(w, "---- Getting process from image ----")
	cmd := "cat /home/application/current/Procfile || cat /app/user/Procfile || cat /Procfile"
	output, _ := p.runCommandInContainer(imageId, cmd, app)
	procfile := getProcessesFromProcfile(output.String())
	if len(procfile) == 0 {
		fmt.Fprintln(w, "  ---> Procfile not found, trying to get entrypoint")
		imageInspect, inspectErr := cluster.InspectImage(imageId)
		if inspectErr != nil {
			return "", inspectErr
		}
		if len(imageInspect.Config.Entrypoint) == 0 {
			return "", ErrEntrypointOrProcfileNotFound
		}
		webProcess := imageInspect.Config.Entrypoint[0]
		for _, c := range imageInspect.Config.Entrypoint[1:] {
			webProcess += fmt.Sprintf(" %q", c)
		}
		procfile["web"] = webProcess
	}
	for k, v := range procfile {
		fmt.Fprintf(w, "  ---> Process %s found with command: %v\n", k, v)
	}
	newImage, err := appNewImageName(app.GetName())
	if err != nil {
		return "", err
	}
	imageInfo := strings.Split(newImage, ":")
	err = cluster.TagImage(imageId, docker.TagImageOptions{Repo: strings.Join(imageInfo[:len(imageInfo)-1], ":"), Tag: imageInfo[len(imageInfo)-1], Force: true})
	if err != nil {
		return "", err
	}
	registry, err := config.GetString("docker:registry")
	if err != nil {
		return "", err
	}
	fmt.Fprintln(w, "---- Pushing image to tsuru ----")
	pushOpts := docker.PushImageOptions{
		Name:         strings.Join(imageInfo[:len(imageInfo)-1], ":"),
		Tag:          imageInfo[len(imageInfo)-1],
		Registry:     registry,
		OutputStream: w,
	}
	err = cluster.PushImage(pushOpts, mainDockerProvisioner.RegistryAuthConfig())
	if err != nil {
		return "", err
	}
	imageData := createImageMetadata(newImage, procfile)
	err = saveImageCustomData(newImage, imageData.CustomData)
	if err != nil {
		return "", err
	}
	app.SetUpdatePlatform(true)
	return newImage, p.deploy(app, newImage, w)
}