Skip to content

Commit 3cfbc8c

Browse files
committed
added pause, resume, create snapshot
Signed-off-by: Plamen Petrov <[email protected]>
1 parent 539fdb5 commit 3cfbc8c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

firecracker.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,42 @@ func (f *Client) PutGuestVsock(ctx context.Context, vsock *models.Vsock, opts ..
229229
return f.client.Operations.PutGuestVsock(params)
230230
}
231231

232+
// PatchVMOpt is a functional option to be used for the
233+
// PatchVM API in setting any additional optional fields.
234+
type PatchVMOpt func(*ops.PatchVMParams)
235+
236+
// PatchVM is a wrapper for the swagger generated client to make
237+
// calling of the API easier.
238+
func (f *Client) PatchVM(ctx context.Context, vm *models.VM, opts ...PatchVMOpt) (*ops.PatchVMNoContent, error) {
239+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
240+
defer cancel()
241+
242+
params := ops.NewPatchVMParamsWithContext(timeout)
243+
params.SetBody(vm)
244+
for _, opt := range opts {
245+
opt(params)
246+
}
247+
248+
return f.client.Operations.PatchVM(params)
249+
}
250+
251+
// CreateSnapshotOpt is a functional option to be used for the
252+
// CreateSnapshot API in setting any additional optional fields.
253+
type CreateSnapshotOpt func(*ops.CreateSnapshotParams)
254+
255+
// CreateSnapshot is a wrapper for the swagger generated client to make
256+
// calling of the API easier.
257+
func (f *Client) CreateSnapshot(ctx context.Context, snapshotParams *models.SnapshotCreateParams, opts ...CreateSnapshotOpt) (*ops.CreateSnapshotNoContent, error) {
258+
params := ops.NewCreateSnapshotParamsWithContext(ctx)
259+
params.SetBody(snapshotParams)
260+
261+
for _, opt := range opts {
262+
opt(params)
263+
}
264+
265+
return f.client.Operations.CreateSnapshot(params)
266+
}
267+
232268
// CreateSyncActionOpt is a functional option to be used for the
233269
// CreateSyncAction API in setting any additional optional fields.
234270
type CreateSyncActionOpt func(*ops.CreateSyncActionParams)

machine.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,3 +1002,50 @@ func (m *Machine) setupSignals() {
10021002
close(sigchan)
10031003
}()
10041004
}
1005+
1006+
// PauseVM Pauses the VM
1007+
func (m *Machine) PauseVM(ctx context.Context, opts ...PatchVMOpt) error {
1008+
vm := &models.VM{
1009+
State: String(models.VMStatePaused),
1010+
}
1011+
1012+
if _, err := m.client.PatchVM(ctx, vm, opts...); err != nil {
1013+
m.logger.Errorf("failed to pause the VM: %v", err)
1014+
return err
1015+
}
1016+
1017+
m.logger.Debug("VM paused successfully")
1018+
return nil
1019+
}
1020+
1021+
// ResumeVM Resumes the VM
1022+
func (m *Machine) ResumeVM(ctx context.Context, opts ...PatchVMOpt) error {
1023+
vm := &models.VM{
1024+
State: String(models.VMStateResumed),
1025+
}
1026+
1027+
if _, err := m.client.PatchVM(ctx, vm, opts...); err != nil {
1028+
m.logger.Errorf("failed to resume the VM: %v", err)
1029+
return err
1030+
}
1031+
1032+
m.logger.Debug("VM resumed successfully")
1033+
return nil
1034+
}
1035+
1036+
// CreateSnapshot Creates a snapshot of the VM
1037+
func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath string, opts ...CreateSnapshotOpt) error {
1038+
snapshotParams := &models.SnapshotCreateParams{
1039+
MemFilePath: String(memFilePath),
1040+
SnapshotPath: String(snapshotPath),
1041+
}
1042+
1043+
if _, err := m.client.CreateSnapshot(ctx, snapshotParams, opts...); err != nil {
1044+
m.logger.Errorf("failed to create a snapshot of the VM: %v", err)
1045+
return err
1046+
}
1047+
1048+
m.logger.Debug("snapshot created successfully")
1049+
1050+
return nil
1051+
}

0 commit comments

Comments
 (0)