Ejemplo n.º 1
0
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)
}
Ejemplo n.º 2
0
func (p *dockerProvisioner) ImageDeploy(app provision.App, imageId string, evt *event.Event) (string, error) {
	cluster := p.Cluster()
	if !strings.Contains(imageId, ":") {
		imageId = fmt.Sprintf("%s:latest", imageId)
	}
	w := evt
	fmt.Fprintln(w, "---- Pulling image to tsuru ----")
	pullOpts := docker.PullImageOptions{
		Repository:        imageId,
		OutputStream:      w,
		InactivityTimeout: net.StreamInactivityTimeout,
	}
	nodes, err := cluster.NodesForMetadata(map[string]string{"pool": app.GetPool()})
	if err != nil {
		return "", err
	}
	node, _, err := p.scheduler.minMaxNodes(nodes, app.GetName(), "")
	if err != nil {
		return "", err
	}
	err = cluster.PullImage(pullOpts, docker.AuthConfiguration{}, node)
	if err != nil {
		return "", err
	}
	fmt.Fprintln(w, "---- Getting process from image ----")
	cmd := "cat /home/application/current/Procfile || cat /app/user/Procfile || cat /Procfile"
	var outBuf bytes.Buffer
	err = p.runCommandInContainer(imageId, cmd, app, &outBuf, nil)
	if err != nil {
		return "", err
	}
	newImage, err := dockercommon.PrepareImageForDeploy(dockercommon.PrepareImageArgs{
		Client:      cluster,
		App:         app,
		ProcfileRaw: outBuf.String(),
		ImageId:     imageId,
		AuthConfig:  p.RegistryAuthConfig(),
		Out:         w,
	})
	if err != nil {
		return "", err
	}
	app.SetUpdatePlatform(true)
	return newImage, p.deploy(app, newImage, evt)
}
Ejemplo n.º 3
0
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)
}