Files
podman/pkg/bindings/containers/checkpoint.go
Paul Holzinger 0db184fb08 fix breaking change in pkg/bindings
pkg/bindings is considered stable. We cannot make changes that would
break any users. If someone uses this field their code would fail to
compile. Since the fix is obviously correct we will keep it but also
add the old field back in to keep compatibility with old code.

When both fields are set ImportArchive is preferred over ImportAchive.

Fixes changes from commit 217197340c8f

[NO NEW TESTS NEEDED]

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-03-15 15:06:42 +01:00

107 lines
2.8 KiB
Go

package containers
import (
"context"
"io"
"net/http"
"os"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
)
// Checkpoint checkpoints the given container (identified by nameOrID). All additional
// options are options and allow for more fine grained control of the checkpoint process.
func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions) (*entities.CheckpointReport, error) {
var report entities.CheckpointReport
if options == nil {
options = new(CheckpointOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params, err := options.ToParams()
if err != nil {
return nil, err
}
// "export" is a bool for the server so override it in the parameters
// if set.
export := false
if options.Export != nil && *options.Export != "" {
export = true
params.Set("export", "true")
}
response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/checkpoint", params, nil, nameOrID)
if err != nil {
return nil, err
}
defer response.Body.Close()
if !export {
return &report, response.Process(&report)
}
f, err := os.OpenFile(*options.Export, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, err
}
defer f.Close()
if _, err := io.Copy(f, response.Body); err != nil {
return nil, err
}
return &entities.CheckpointReport{}, nil
}
// Restore restores a checkpointed container to running. The container is identified by the nameOrID option. All
// additional options are optional and allow finer control of the restore process.
func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*entities.RestoreReport, error) {
var report entities.RestoreReport
if options == nil {
options = new(RestoreOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params, err := options.ToParams()
if err != nil {
return nil, err
}
for _, p := range options.PublishPorts {
params.Add("publishPorts", p)
}
params.Del("ImportArchive") // The import key is a reserved golang term
// Open the to-be-imported archive if needed.
var r io.Reader
i := options.GetImportArchive()
if i == "" {
// backwards compat, ImportAchive is a typo but we still have to
// support this to avoid breaking users
// TODO: remove ImportAchive with 5.0
i = options.GetImportAchive()
}
if i != "" {
params.Set("import", "true")
r, err = os.Open(i)
if err != nil {
return nil, err
}
// Hard-code the name since it will be ignored in any case.
nameOrID = "import"
}
response, err := conn.DoRequest(ctx, r, http.MethodPost, "/containers/%s/restore", params, nil, nameOrID)
if err != nil {
return nil, err
}
defer response.Body.Close()
return &report, response.Process(&report)
}