github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/bindings/containers/checkpoint.go (about) 1 package containers 2 3 import ( 4 "context" 5 "io" 6 "net/http" 7 "os" 8 9 "github.com/hanks177/podman/v4/pkg/bindings" 10 "github.com/hanks177/podman/v4/pkg/domain/entities" 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. 15 func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions) (*entities.CheckpointReport, error) { 16 var report entities.CheckpointReport 17 if options == nil { 18 options = new(CheckpointOptions) 19 } 20 conn, err := bindings.GetClient(ctx) 21 if err != nil { 22 return nil, err 23 } 24 params, err := options.ToParams() 25 if err != nil { 26 return nil, err 27 } 28 29 // "export" is a bool for the server so override it in the parameters 30 // if set. 31 export := false 32 if options.Export != nil && *options.Export != "" { 33 export = true 34 params.Set("export", "true") 35 } 36 response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/checkpoint", params, nil, nameOrID) 37 if err != nil { 38 return nil, err 39 } 40 defer response.Body.Close() 41 42 if !export { 43 return &report, response.Process(&report) 44 } 45 46 f, err := os.OpenFile(*options.Export, os.O_RDWR|os.O_CREATE, 0600) 47 if err != nil { 48 return nil, err 49 } 50 defer f.Close() 51 if _, err := io.Copy(f, response.Body); err != nil { 52 return nil, err 53 } 54 55 return &entities.CheckpointReport{}, nil 56 } 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. 60 func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*entities.RestoreReport, error) { 61 var report entities.RestoreReport 62 if options == nil { 63 options = new(RestoreOptions) 64 } 65 conn, err := bindings.GetClient(ctx) 66 if err != nil { 67 return nil, err 68 } 69 params, err := options.ToParams() 70 if err != nil { 71 return nil, err 72 } 73 74 for _, p := range options.PublishPorts { 75 params.Add("publishPorts", p) 76 } 77 78 params.Del("ImportArchive") // The import key is a reserved golang term 79 80 // Open the to-be-imported archive if needed. 81 var r io.Reader 82 i := options.GetImportArchive() 83 if i == "" { 84 // backwards compat, ImportAchive is a typo but we still have to 85 // support this to avoid breaking users 86 // TODO: remove ImportAchive with 5.0 87 i = options.GetImportAchive() 88 } 89 if i != "" { 90 params.Set("import", "true") 91 r, err = os.Open(i) 92 if err != nil { 93 return nil, err 94 } 95 // Hard-code the name since it will be ignored in any case. 96 nameOrID = "import" 97 } 98 99 response, err := conn.DoRequest(ctx, r, http.MethodPost, "/containers/%s/restore", params, nil, nameOrID) 100 if err != nil { 101 return nil, err 102 } 103 defer response.Body.Close() 104 105 return &report, response.Process(&report) 106 }