daytona/apps/runner/pkg/docker/snapshot_pull.go
Luka Brecic cb6628bc6d
fix(runner): pulling snapshot on runner (#4177)
Signed-off-by: Luka Brecic <luka.brecic3@gmail.com>
2026-03-25 13:53:02 +01:00

70 lines
1.7 KiB
Go

// Copyright 2025 Daytona Platforms Inc.
// SPDX-License-Identifier: AGPL-3.0
package docker
import (
"context"
"errors"
"fmt"
"strings"
common_errors "github.com/daytonaio/common-go/pkg/errors"
"github.com/daytonaio/runner/pkg/api/dto"
)
func (d *DockerClient) PullSnapshot(ctx context.Context, req dto.PullSnapshotRequestDTO) error {
// Pull the image using the pull registry (or none for public images)
_, err := d.PullImage(ctx, req.Snapshot, req.Registry, nil)
if err != nil {
return err
}
if req.NewTag != nil {
err = d.TagImage(ctx, req.Snapshot, *req.NewTag)
if err != nil {
return err
}
}
if req.DestinationRegistry != nil {
if req.DestinationRegistry.Project == nil {
return common_errors.NewBadRequestError(errors.New("project is required when pushing to registry"))
}
var targetRef string
// If destination ref is provided, use it directly; otherwise build it from the image info
if req.DestinationRef != nil {
targetRef = *req.DestinationRef
} else {
// Get image info to retrieve the hash
imageInfo, err := d.GetImageInfo(ctx, req.Snapshot)
if err != nil {
return err
}
ref := "daytona-" + getHashWithoutPrefix(imageInfo.Hash) + ":daytona"
targetRef = fmt.Sprintf("%s/%s/%s", req.DestinationRegistry.Url, *req.DestinationRegistry.Project, ref)
}
// Tag the image for the target registry
err = d.TagImage(ctx, req.Snapshot, targetRef)
if err != nil {
return err
}
// Push the tagged image
err = d.PushImage(ctx, targetRef, req.DestinationRegistry)
if err != nil {
return err
}
}
return nil
}
func getHashWithoutPrefix(hash string) string {
return strings.TrimPrefix(hash, "sha256:")
}