Skip to content

Commit

Permalink
Refactored NewMachine and Start functions
Browse files Browse the repository at this point in the history
Signed-off-by: David Son <[email protected]>
  • Loading branch information
sondavidb committed Aug 11, 2022
1 parent b6809bb commit bce5434
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/snapshotting.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ m.PauseVM(ctx)
m.CreateSnapshot(ctx, memPath, snapPath)
```

The snapshot can be loaded at any later time at startup of a machine via the machine's `Start()` function, using `WithSnapshot()` as an option. The VM must then be resumed before attempting to use it.
The snapshot can be loaded at any later time at creation of a machine via the machine's `NewMachine()` function, using `WithSnapshot()` as an option. Upon starting, the VM loads the snapshot and must then be resumed before attempting to use it.

```
ctx := context.Background()
Expand All @@ -49,9 +49,9 @@ cfg := sdk.Config{
...
}
m, _ := sdk.NewMachine(ctx, cfg)
m, _ := sdk.NewMachine(ctx, cfg, sdk.WithSnapshot(memPath, snapPath))
m.Start(ctx, sdk.WithSnapshot(memPath, snapPath))
m.Start(ctx)
m.ResumeVM(ctx)
```

Expand Down
4 changes: 2 additions & 2 deletions examples/cmd/snapshotting/example_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ func loadSnapshotSSH(ctx context.Context, socketPath, memPath, snapPath, ipToRes
// Use the firecracker binary
cmd := sdk.VMCommandBuilder{}.WithSocketPath(socketFile).WithBin(filepath.Join(dir, "firecracker")).Build(ctx)

m, err := sdk.NewMachine(ctx, cfg, sdk.WithProcessRunner(cmd))
m, err := sdk.NewMachine(ctx, cfg, sdk.WithProcessRunner(cmd), sdk.WithSnapshot(memPath, snapPath))
if err != nil {
log.Fatal(err)
}
defer os.Remove(socketFile)

err = m.Start(ctx, sdk.WithSnapshot(memPath, snapPath))
err = m.Start(ctx)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions examples/cmd/snapshotting/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ require (
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/firecracker-microvm/firecracker-go-sdk => ../../..
14 changes: 5 additions & 9 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
m.cmd = configureBuilder(defaultFirecrackerVMMCommandBuilder, cfg).Build(ctx)
}

for _, opt := range opts {
opt(m)
}

if m.logger == nil {
logger := log.New()

Expand Down Expand Up @@ -370,6 +366,10 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
m.Cfg.NetNS = m.defaultNetNSPath()
}

for _, opt := range opts {
opt(m)
}

m.logger.Debug("Called NewMachine()")
return m, nil
}
Expand All @@ -382,7 +382,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
// handlers succeed, then this will start the VMM instance.
// Start may only be called once per Machine. Subsequent calls will return
// ErrAlreadyStarted.
func (m *Machine) Start(ctx context.Context, opts ...StartOpt) error {
func (m *Machine) Start(ctx context.Context) error {
m.logger.Debug("Called Machine.Start()")
alreadyStarted := true
m.startOnce.Do(func() {
Expand All @@ -403,10 +403,6 @@ func (m *Machine) Start(ctx context.Context, opts ...StartOpt) error {
}
}()

for _, opt := range opts {
opt(m)
}

err = m.Handlers.Run(ctx, m)
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1945,10 +1945,10 @@ func TestLoadSnapshot(t *testing.T) {
// some unexported members
args := m.cmd.Args[1:]
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
}, WithLogger(logrus.NewEntry(machineLogger)))
}, WithLogger(logrus.NewEntry(machineLogger)), WithSnapshot(memPath, snapPath))
require.NoError(t, err)

err = m.Start(ctx, WithSnapshot(memPath, snapPath))
err = m.Start(ctx)
require.NoError(t, err)

err = m.ResumeVM(ctx)
Expand All @@ -1971,10 +1971,10 @@ func TestLoadSnapshot(t *testing.T) {
// some unexported members
args := m.cmd.Args[1:]
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
}, WithLogger(logrus.NewEntry(machineLogger)))
}, WithLogger(logrus.NewEntry(machineLogger)), WithSnapshot(memPath, snapPath))
require.NoError(t, err)

err = m.Start(ctx, WithSnapshot(memPath, snapPath))
err = m.Start(ctx)
require.Error(t, err)
},
},
Expand Down Expand Up @@ -2085,10 +2085,10 @@ func TestLoadSnapshot(t *testing.T) {

cmd := VMCommandBuilder{}.WithSocketPath(fmt.Sprintf("%s.load", socketPath)).WithBin(getFirecrackerBinaryPath()).Build(ctx)

m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd))
m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd), WithSnapshot(memPath, snapPath))
require.NoError(t, err)

err = m.Start(ctx, WithSnapshot(memPath, snapPath))
err = m.Start(ctx)
require.NoError(t, err)
defer m.StopVMM()

Expand Down
2 changes: 1 addition & 1 deletion machineiface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var _ MachineIface = (*Machine)(nil)
// MachineIface can be used for mocking and testing of the Machine. The Machine
// is subject to change, meaning this interface would change.
type MachineIface interface {
Start(context.Context, ...StartOpt) error
Start(context.Context) error
StopVMM() error
Shutdown(context.Context) error
Wait(context.Context) error
Expand Down
3 changes: 1 addition & 2 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

// Opt represents a functional option to help modify functionality of a Machine.
type Opt func(*Machine)
type StartOpt func(*Machine)

// WithClient will use the client in place rather than the client constructed
// during bootstrapping of the machine. This option is useful for mocking out
Expand Down Expand Up @@ -54,7 +53,7 @@ func WithProcessRunner(cmd *exec.Cmd) Opt {
type WithSnapshotOpt func(*SnapshotConfig)

// WithSnapshot will allow for the machine to start using a given snapshot.
func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) StartOpt {
func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) Opt {
return func(m *Machine) {
m.Cfg.Snapshot.MemFilePath = memFilePath
m.Cfg.Snapshot.SnapshotPath = snapshotPath
Expand Down

0 comments on commit bce5434

Please sign in to comment.