podman
106 строк · 2.8 Кб
1package containers2
3import (4"context"5"io"6"net/http"7"os"8
9"github.com/containers/podman/v5/pkg/bindings"10"github.com/containers/podman/v5/pkg/domain/entities/types"11)
12
13// Checkpoint checkpoints the given container (identified by nameOrID). All additional
14// options are options and allow for more fine grained control of the checkpoint process.
15func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions) (*types.CheckpointReport, error) {16var report types.CheckpointReport17if options == nil {18options = new(CheckpointOptions)19}20conn, err := bindings.GetClient(ctx)21if err != nil {22return nil, err23}24params, err := options.ToParams()25if err != nil {26return nil, err27}28
29// "export" is a bool for the server so override it in the parameters30// if set.31export := false32if options.Export != nil && *options.Export != "" {33export = true34params.Set("export", "true")35}36response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/checkpoint", params, nil, nameOrID)37if err != nil {38return nil, err39}40defer response.Body.Close()41
42if response.StatusCode != http.StatusOK || !export {43return &report, response.Process(&report)44}45
46f, err := os.OpenFile(*options.Export, os.O_RDWR|os.O_CREATE, 0600)47if err != nil {48return nil, err49}50defer f.Close()51if _, err := io.Copy(f, response.Body); err != nil {52return nil, err53}54
55return &types.CheckpointReport{}, nil56}
57
58// Restore restores a checkpointed container to running. The container is identified by the nameOrID option. All
59// additional options are optional and allow finer control of the restore process.
60func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*types.RestoreReport, error) {61var report types.RestoreReport62if options == nil {63options = new(RestoreOptions)64}65conn, err := bindings.GetClient(ctx)66if err != nil {67return nil, err68}69params, err := options.ToParams()70if err != nil {71return nil, err72}73
74for _, p := range options.PublishPorts {75params.Add("publishPorts", p)76}77
78params.Del("ImportArchive") // The import key is a reserved golang term79
80// Open the to-be-imported archive if needed.81var r io.Reader82i := options.GetImportArchive()83if i == "" {84// backwards compat, ImportAchive is a typo but we still have to85// support this to avoid breaking users86// TODO: remove ImportAchive with 5.087i = options.GetImportAchive()88}89if i != "" {90params.Set("import", "true")91r, err = os.Open(i)92if err != nil {93return nil, err94}95// Hard-code the name since it will be ignored in any case.96nameOrID = "import"97}98
99response, err := conn.DoRequest(ctx, r, http.MethodPost, "/containers/%s/restore", params, nil, nameOrID)100if err != nil {101return nil, err102}103defer response.Body.Close()104
105return &report, response.Process(&report)106}
107