|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + |
| 12 | + metadata "github.com/checkpoint-restore/checkpointctl/lib" |
| 13 | +) |
| 14 | + |
| 15 | +type ImageBuilder struct { |
| 16 | + imageName string |
| 17 | + checkpointPath string |
| 18 | +} |
| 19 | + |
| 20 | +func NewImageBuilder(imageName, checkpointPath string) *ImageBuilder { |
| 21 | + return &ImageBuilder{ |
| 22 | + imageName: imageName, |
| 23 | + checkpointPath: checkpointPath, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func runBuildahCommand(args ...string) (string, error) { |
| 28 | + cmd := exec.Command("buildah", args...) |
| 29 | + |
| 30 | + var out bytes.Buffer |
| 31 | + var stderr bytes.Buffer |
| 32 | + cmd.Stdout = &out |
| 33 | + cmd.Stderr = &stderr |
| 34 | + |
| 35 | + err := cmd.Run() |
| 36 | + if err != nil { |
| 37 | + return "", fmt.Errorf("buildah command failed: error: %w, stderr: %s", err, stderr.String()) |
| 38 | + } |
| 39 | + |
| 40 | + return out.String(), nil |
| 41 | +} |
| 42 | + |
| 43 | +func (ic *ImageBuilder) CreateImageFromCheckpoint(ctx context.Context) error { |
| 44 | + // Step 1: Create a new container from scratch |
| 45 | + newContainer, err := runBuildahCommand("from", "scratch") |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("creating container failed: %w", err) |
| 48 | + } |
| 49 | + newContainer = strings.TrimSpace(newContainer) |
| 50 | + |
| 51 | + // Ensure the container is removed in case of failure |
| 52 | + defer func() { |
| 53 | + if newContainer != "" { |
| 54 | + _, err := runBuildahCommand("rm", newContainer) |
| 55 | + if err != nil { |
| 56 | + fmt.Printf("Warning: failed to remove container %s: %v\n", newContainer, err) |
| 57 | + } |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + // Step 2: Add checkpoint files to the container |
| 62 | + _, err = runBuildahCommand("add", newContainer, ic.checkpointPath) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("adding files to container failed: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Step 3: Apply checkpoint annotations |
| 68 | + checkpointImageAnnotations, err := ic.getCheckpointAnnotations() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("extracting checkpoint annotations failed: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + for key, value := range checkpointImageAnnotations { |
| 74 | + _, err := runBuildahCommand("config", "--annotation", fmt.Sprintf("%s=%s", key, value), newContainer) |
| 75 | + if err != nil { |
| 76 | + fmt.Printf("Error setting annotation %s=%s: %v\n", key, value, err) |
| 77 | + } else { |
| 78 | + fmt.Printf("Added annotation: %s=%s\n", key, value) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Step 4: Commit the container to an image |
| 83 | + _, err = runBuildahCommand("commit", newContainer, ic.imageName) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("committing container annotations failed: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (ic *ImageBuilder) getCheckpointAnnotations() (map[string]string, error) { |
| 92 | + checkpointImageAnnotations := map[string]string{} |
| 93 | + |
| 94 | + // Create a temporary directory |
| 95 | + tempDir, err := os.MkdirTemp("", "checkpoint-extract-") |
| 96 | + if err != nil { |
| 97 | + log.Printf("Error creating temporary directory: %v\n", err) |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + defer os.RemoveAll(tempDir) |
| 101 | + |
| 102 | + filesToExtract := []string{"spec.dump", "config.dump"} |
| 103 | + if err = UntarFiles(ic.checkpointPath, tempDir, filesToExtract); err != nil { |
| 104 | + log.Printf("Error extracting files from archive %s: %v\n", ic.checkpointPath, err) |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + info := &checkpointInfo{} |
| 109 | + info.configDump, _, err = metadata.ReadContainerCheckpointConfigDump(tempDir) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + info.specDump, _, err = metadata.ReadContainerCheckpointSpecDump(tempDir) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + info.containerInfo, err = getContainerInfo(info.specDump, info.configDump) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + checkpointImageAnnotations[metadata.CheckpointAnnotationEngine] = info.containerInfo.Engine |
| 125 | + checkpointImageAnnotations[metadata.CheckpointAnnotationName] = info.containerInfo.Name |
| 126 | + checkpointImageAnnotations[metadata.CheckpointAnnotationPod] = info.containerInfo.Pod |
| 127 | + checkpointImageAnnotations[metadata.CheckpointAnnotationNamespace] = info.containerInfo.Namespace |
| 128 | + checkpointImageAnnotations[metadata.CheckpointAnnotationRootfsImageUserRequested] = info.configDump.RootfsImage |
| 129 | + checkpointImageAnnotations[metadata.CheckpointAnnotationRootfsImageName] = info.configDump.RootfsImageName |
| 130 | + checkpointImageAnnotations[metadata.CheckpointAnnotationRootfsImageID] = info.configDump.RootfsImageRef |
| 131 | + checkpointImageAnnotations[metadata.CheckpointAnnotationRuntimeName] = info.configDump.OCIRuntime |
| 132 | + |
| 133 | + return checkpointImageAnnotations, nil |
| 134 | +} |
0 commit comments