Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/limactl/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func cloneAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/limactl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
}
return err
}
if err := instance.Delete(cmd.Context(), inst, force); err != nil {
if err := instance.Delete(ctx, inst, force); err != nil {
return fmt.Errorf("failed to delete instance %q: %w", instName, err)
}
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
Expand All @@ -60,7 +60,7 @@ func deleteAction(cmd *cobra.Command, args []string) error {
}
logrus.Infof("Deleted %q (%q)", instName, inst.Dir)
}
return networks.Reconcile(cmd.Context(), "")
return networks.Reconcile(ctx, "")
}

func deleteBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func editAction(cmd *cobra.Command, args []string) error {
var yBytes []byte
if len(yqExprs) > 0 {
yq := yqutil.Join(yqExprs)
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
yBytes, err = yqutil.EvaluateExpression(ctx, yq, yContent)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
}
}()
defer srv.Close()
return ha.Run(cmd.Context())
return ha.Run(ctx)
}

// syncer is implemented by *os.File.
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func listAction(cmd *cobra.Command, args []string) error {
}
} else {
var res []byte
if res, err = yqutil.EvaluateExpression(yqExpr, buf.Bytes()); err != nil {
if res, err = yqutil.EvaluateExpression(ctx, yqExpr, buf.Bytes()); err != nil {
return err
}
str = string(res)
Expand Down
13 changes: 8 additions & 5 deletions cmd/limactl/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -174,6 +175,8 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
return err
}

ctx := cmd.Context()

switch mode {
case networks.ModeBridged:
if gateway != "" {
Expand All @@ -183,7 +186,7 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("network mode %q requires specifying interface", mode)
}
yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"interface":%q}`, name, mode, intf)
return networkApplyYQ(yq)
return networkApplyYQ(ctx, yq)
default:
if gateway == "" {
return fmt.Errorf("network mode %q requires specifying gateway", mode)
Expand All @@ -208,11 +211,11 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
// TODO: check IP range collision

yq := fmt.Sprintf(`.networks.%q = {"mode":%q,"gateway":%q,"netmask":%q,"interface":%q}`, name, mode, gwIP.String(), gwMaskStr, intf)
return networkApplyYQ(yq)
return networkApplyYQ(ctx, yq)
}
}

func networkApplyYQ(yq string) error {
func networkApplyYQ(ctx context.Context, yq string) error {
filePath, err := networks.ConfigFile()
if err != nil {
return err
Expand All @@ -221,7 +224,7 @@ func networkApplyYQ(yq string) error {
if err != nil {
return err
}
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
if err != nil {
return err
}
Expand Down Expand Up @@ -263,7 +266,7 @@ func networkDeleteAction(cmd *cobra.Command, args []string) error {
yq += " | "
}
}
return networkApplyYQ(yq)
return networkApplyYQ(cmd.Context(), yq)
}

func networkBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
Expand Down
18 changes: 9 additions & 9 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
return nil, err
}
} else {
tmpl, err = limatmpl.Read(cmd.Context(), name, arg)
tmpl, err = limatmpl.Read(ctx, name, arg)
if err != nil {
return nil, err
}
Expand All @@ -321,7 +321,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
}
}

if err := tmpl.Embed(cmd.Context(), true, true); err != nil {
if err := tmpl.Embed(ctx, true, true); err != nil {
return nil, err
}
yqExprs, err := editflags.YQExpressions(flags, true)
Expand All @@ -331,18 +331,18 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string, createOnly bool) (*
yq := yqutil.Join(yqExprs)
if tty {
var err error
tmpl, err = chooseNextCreatorState(cmd.Context(), tmpl, yq)
tmpl, err = chooseNextCreatorState(ctx, tmpl, yq)
if err != nil {
return nil, err
}
} else {
logrus.Info("Terminal is not available, proceeding without opening an editor")
if err := modifyInPlace(tmpl, yq); err != nil {
if err := modifyInPlace(ctx, tmpl, yq); err != nil {
return nil, err
}
}
saveBrokenYAML := tty
return instance.Create(cmd.Context(), tmpl.Name, tmpl.Bytes, saveBrokenYAML)
return instance.Create(ctx, tmpl.Name, tmpl.Bytes, saveBrokenYAML)
}

func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Instance, yq string) (*limatype.Instance, error) {
Expand All @@ -355,7 +355,7 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Ins
return nil, err
}
logrus.Debugf("Applying yq expression %q to an existing instance %q", yq, inst.Name)
yBytes, err := yqutil.EvaluateExpression(yq, yContent)
yBytes, err := yqutil.EvaluateExpression(ctx, yq, yContent)
if err != nil {
return nil, err
}
Expand All @@ -381,8 +381,8 @@ func applyYQExpressionToExistingInstance(ctx context.Context, inst *limatype.Ins
return store.Inspect(ctx, inst.Name)
}

func modifyInPlace(st *limatmpl.Template, yq string) error {
out, err := yqutil.EvaluateExpression(yq, st.Bytes)
func modifyInPlace(ctx context.Context, st *limatmpl.Template, yq string) error {
out, err := yqutil.EvaluateExpression(ctx, yq, st.Bytes)
if err != nil {
return err
}
Expand All @@ -407,7 +407,7 @@ func (exitSuccessError) ExitCode() int {

func chooseNextCreatorState(ctx context.Context, tmpl *limatmpl.Template, yq string) (*limatmpl.Template, error) {
for {
if err := modifyInPlace(tmpl, yq); err != nil {
if err := modifyInPlace(ctx, tmpl, yq); err != nil {
logrus.WithError(err).Warn("Failed to evaluate yq expression")
return tmpl, err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/limactl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func templateCopyAction(cmd *cobra.Command, args []string) error {
if embed && verbatim {
return errors.New("--verbatim cannot be used with any of --embed, --embed-all, or --fill")
}
tmpl, err := limatmpl.Read(cmd.Context(), "", source)
tmpl, err := limatmpl.Read(ctx, "", source)
if err != nil {
return err
}
Expand All @@ -135,11 +135,11 @@ func templateCopyAction(cmd *cobra.Command, args []string) error {
if !verbatim {
if embed {
// Embed default base.yaml only when fill is true.
if err := tmpl.Embed(cmd.Context(), embedAll, fill); err != nil {
if err := tmpl.Embed(ctx, embedAll, fill); err != nil {
return err
}
} else {
if err := tmpl.UseAbsLocators(); err != nil {
if err := tmpl.UseAbsLocators(ctx); err != nil {
return err
}
}
Expand Down Expand Up @@ -197,14 +197,14 @@ func templateYQAction(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
locator := args[0]
expr := args[1]
tmpl, err := limatmpl.Read(cmd.Context(), "", locator)
tmpl, err := limatmpl.Read(ctx, "", locator)
if err != nil {
return err
}
if len(tmpl.Bytes) == 0 {
return fmt.Errorf("don't know how to interpret %q as a template locator", locator)
}
if err := tmpl.Embed(cmd.Context(), true, true); err != nil {
if err := tmpl.Embed(ctx, true, true); err != nil {
return err
}
if err := fillDefaults(ctx, tmpl); err != nil {
Expand Down Expand Up @@ -241,7 +241,7 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
}

for _, arg := range args {
tmpl, err := limatmpl.Read(cmd.Context(), "", arg)
tmpl, err := limatmpl.Read(ctx, "", arg)
if err != nil {
return err
}
Expand All @@ -252,7 +252,7 @@ func templateValidateAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("can't determine instance name from template locator %q", arg)
}
// Embed default base.yaml only when fill is true.
if err := tmpl.Embed(cmd.Context(), true, fill); err != nil {
if err := tmpl.Embed(ctx, true, fill); err != nil {
return err
}
// Load() will merge the template with override.yaml and default.yaml via FillDefaults().
Expand Down
8 changes: 4 additions & 4 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Lifecycle interface {
type GUI interface {
// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates
// It returns error if there are any failures
RunGUI() error
RunGUI(context.Context) error

ChangeDisplayPassword(ctx context.Context, password string) error
DisplayConnection(ctx context.Context) (string, error)
Expand All @@ -63,7 +63,7 @@ type SnapshotManager interface {
// GuestAgent defines operations for the guest agent.
type GuestAgent interface {
// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent.
ForwardGuestAgent() bool
ForwardGuestAgent(context.Context) bool

// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
GuestAgentConn(_ context.Context) (net.Conn, string, error)
Expand All @@ -76,12 +76,12 @@ type Driver interface {
SnapshotManager
GuestAgent

Info() Info
Info(context.Context) Info

// Configure sets the configuration for the instance.
// TODO: merge Configure and FillConfig?
// Or come up with a better name to clarify the difference.
Configure(inst *limatype.Instance) *ConfiguredDriver
Configure(_ context.Context, inst *limatype.Instance) *ConfiguredDriver

// FillConfig fills and validates the configuration for the instance.
// The config is not set to the instance.
Expand Down
16 changes: 8 additions & 8 deletions pkg/driver/external/client/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ func (d *DriverClient) FillConfig(_ context.Context, _ *limatype.LimaYAML, _ str
return errors.New("pre-configured driver action not implemented in client driver")
}

func (d *DriverClient) RunGUI() error {
func (d *DriverClient) RunGUI(ctx context.Context) error {
d.logger.Debug("Running GUI for the driver instance")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

_, err := d.DriverSvc.RunGUI(ctx, &emptypb.Empty{})
Expand Down Expand Up @@ -221,10 +221,10 @@ func (d *DriverClient) ListSnapshots(ctx context.Context) (string, error) {
return resp.Snapshots, nil
}

func (d *DriverClient) ForwardGuestAgent() bool {
func (d *DriverClient) ForwardGuestAgent(ctx context.Context) bool {
d.logger.Debug("Checking if guest agent needs to be forwarded")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

resp, err := d.DriverSvc.ForwardGuestAgent(ctx, &emptypb.Empty{})
Expand All @@ -247,10 +247,10 @@ func (d *DriverClient) GuestAgentConn(ctx context.Context) (net.Conn, string, er
return nil, "", nil
}

func (d *DriverClient) Info() driver.Info {
func (d *DriverClient) Info(ctx context.Context) driver.Info {
d.logger.Debug("Getting driver info")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

resp, err := d.DriverSvc.Info(ctx, &emptypb.Empty{})
Expand All @@ -269,7 +269,7 @@ func (d *DriverClient) Info() driver.Info {
return info
}

func (d *DriverClient) Configure(inst *limatype.Instance) *driver.ConfiguredDriver {
func (d *DriverClient) Configure(ctx context.Context, inst *limatype.Instance) *driver.ConfiguredDriver {
d.logger.Debugf("Setting config for instance %s with SSH local port %d", inst.Name, inst.SSHLocalPort)

instJSON, err := inst.MarshalJSON()
Expand All @@ -278,7 +278,7 @@ func (d *DriverClient) Configure(inst *limatype.Instance) *driver.ConfiguredDriv
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

_, err = d.DriverSvc.Configure(ctx, &pb.SetConfigRequest{
Expand Down
18 changes: 9 additions & 9 deletions pkg/driver/external/server/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *DriverServer) Start(_ *emptypb.Empty, stream pb.Driver_StartServer) err
}
}

func (s *DriverServer) Configure(_ context.Context, req *pb.SetConfigRequest) (*emptypb.Empty, error) {
func (s *DriverServer) Configure(ctx context.Context, req *pb.SetConfigRequest) (*emptypb.Empty, error) {
s.logger.Debugf("Received SetConfig request")
var inst limatype.Instance

Expand All @@ -63,7 +63,7 @@ func (s *DriverServer) Configure(_ context.Context, req *pb.SetConfigRequest) (*
return &emptypb.Empty{}, err
}

_ = s.driver.Configure(&inst)
_ = s.driver.Configure(ctx, &inst)

return &emptypb.Empty{}, nil
}
Expand All @@ -77,7 +77,7 @@ func (s *DriverServer) GuestAgentConn(ctx context.Context, _ *emptypb.Empty) (*e
}

if connType != "unix" {
proxySocketPath := filepath.Join(s.driver.Info().InstanceDir, filenames.GuestAgentSock)
proxySocketPath := filepath.Join(s.driver.Info(ctx).InstanceDir, filenames.GuestAgentSock)

var lc net.ListenConfig
listener, err := lc.Listen(ctx, "unix", proxySocketPath)
Expand All @@ -103,9 +103,9 @@ func (s *DriverServer) GuestAgentConn(ctx context.Context, _ *emptypb.Empty) (*e
return &emptypb.Empty{}, nil
}

func (s *DriverServer) Info(_ context.Context, _ *emptypb.Empty) (*pb.InfoResponse, error) {
func (s *DriverServer) Info(ctx context.Context, _ *emptypb.Empty) (*pb.InfoResponse, error) {
s.logger.Debug("Received GetInfo request")
info := s.driver.Info()
info := s.driver.Info(ctx)

infoJSON, err := json.Marshal(info)
if err != nil {
Expand Down Expand Up @@ -162,9 +162,9 @@ func (s *DriverServer) Stop(ctx context.Context, empty *emptypb.Empty) (*emptypb
return empty, nil
}

func (s *DriverServer) RunGUI(_ context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
func (s *DriverServer) RunGUI(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
s.logger.Debug("Received RunGUI request")
err := s.driver.RunGUI()
err := s.driver.RunGUI(ctx)
if err != nil {
s.logger.Errorf("RunGUI failed: %v", err)
return empty, err
Expand Down Expand Up @@ -280,7 +280,7 @@ func (s *DriverServer) ListSnapshots(ctx context.Context, _ *emptypb.Empty) (*pb
return &pb.ListSnapshotsResponse{Snapshots: snapshots}, nil
}

func (s *DriverServer) ForwardGuestAgent(_ context.Context, _ *emptypb.Empty) (*pb.ForwardGuestAgentResponse, error) {
func (s *DriverServer) ForwardGuestAgent(ctx context.Context, _ *emptypb.Empty) (*pb.ForwardGuestAgentResponse, error) {
s.logger.Debug("Received ForwardGuestAgent request")
return &pb.ForwardGuestAgentResponse{ShouldForward: s.driver.ForwardGuestAgent()}, nil
return &pb.ForwardGuestAgentResponse{ShouldForward: s.driver.ForwardGuestAgent(ctx)}, nil
}
Loading
Loading