GetImageFile downloads an image from the server, returning an ImageFileResponse struct.
(fingerprint string, req ImageFileRequest)
| 129 | |
| 130 | // GetImageFile downloads an image from the server, returning an ImageFileResponse struct. |
| 131 | func (r *ProtocolOCI) GetImageFile(fingerprint string, req ImageFileRequest) (*ImageFileResponse, error) { |
| 132 | ctx := context.Background() |
| 133 | |
| 134 | // Get the cached entry. |
| 135 | info, ok := r.cache[fingerprint] |
| 136 | if !ok { |
| 137 | _, err := exec.LookPath("skopeo") |
| 138 | if err != nil { |
| 139 | return nil, errors.New("OCI container handling requires \"skopeo\" be present on the system") |
| 140 | } |
| 141 | |
| 142 | err, ok := r.errors[fingerprint] |
| 143 | if ok { |
| 144 | return nil, err |
| 145 | } |
| 146 | |
| 147 | return nil, errors.New("Image not found") |
| 148 | } |
| 149 | |
| 150 | // Quick checks. |
| 151 | if req.MetaFile == nil && req.RootfsFile == nil { |
| 152 | return nil, errors.New("No file requested") |
| 153 | } |
| 154 | |
| 155 | if os.Geteuid() != 0 { |
| 156 | return nil, errors.New("OCI image export currently requires root access") |
| 157 | } |
| 158 | |
| 159 | // Get some temporary storage. |
| 160 | ociPath, err := os.MkdirTemp(r.tempPath, "incus-oci-") |
| 161 | if err != nil { |
| 162 | return nil, err |
| 163 | } |
| 164 | |
| 165 | defer logger.WarnOnError(func() error { return os.RemoveAll(ociPath) }, "Failed to remove temporary directory") |
| 166 | |
| 167 | err = os.Mkdir(filepath.Join(ociPath, "oci"), 0o700) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | |
| 172 | err = os.Mkdir(filepath.Join(ociPath, "image"), 0o700) |
| 173 | if err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | |
| 177 | // Copy the image. |
| 178 | if req.ProgressHandler != nil { |
| 179 | req.ProgressHandler(ioprogress.ProgressData{Text: "Retrieving OCI image from registry"}) |
| 180 | } |
| 181 | |
| 182 | imageTag := "latest" |
| 183 | |
| 184 | stdout, err := r.runSkopeo( |
| 185 | "copy", info.Alias, |
| 186 | "--remove-signatures", |
| 187 | fmt.Sprintf("oci:%s:%s", filepath.Join(ociPath, "oci"), imageTag), |
| 188 | ) |
nothing calls this directly
no test coverage detected