Skip to content

Commit

Permalink
store: add StatusSuspended to the Instance Status
Browse files Browse the repository at this point in the history
Signed-off-by: Norio Nomura <[email protected]>
  • Loading branch information
norio-nomura committed Nov 16, 2024
1 parent 79c0277 commit 8b4dc0f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmd/limactl/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func copyAction(cmd *cobra.Command, args []string) error {
}
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
} else if inst.Status == store.StatusSuspended {
return fmt.Errorf("instance %q is suspended, run `limactl start %s` to resume the instance", instName, instName)
}
if legacySSH {
scpFlags = append(scpFlags, "-P", fmt.Sprintf("%d", inst.SSHLocalPort))
Expand Down
5 changes: 5 additions & 0 deletions cmd/limactl/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ func diskUnlockAction(_ *cobra.Command, args []string) error {
if inst.Status == store.StatusRunning {
logrus.Warnf("Cannot unlock disk %q used by running instance %q", diskName, disk.Instance)
continue
} else if inst.Status == store.StatusSuspended {
logrus.Warnf("Cannot unlock disk %q used by suspended instance %q", diskName, disk.Instance)
continue
}
}
if err := disk.Unlock(); err != nil {
Expand Down Expand Up @@ -387,6 +390,8 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
if err == nil {
if inst.Status == store.StatusRunning {
return fmt.Errorf("cannot resize disk %q used by running instance %q. Please stop the VM instance", diskName, disk.Instance)
} else if inst.Status == store.StatusSuspended {
return fmt.Errorf("cannot resize disk %q used by suspended instance %q. Please resume and stop the VM instance", diskName, disk.Instance)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func editAction(cmd *cobra.Command, args []string) error {

if inst.Status == store.StatusRunning {
return errors.New("cannot edit a running instance")
} else if inst.Status == store.StatusSuspended {
return errors.New("cannot edit a suspended instance")
}
filePath = filepath.Join(inst.Dir, filenames.LimaYAML)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func shellAction(cmd *cobra.Command, args []string) error {
}
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
} else if inst.Status == store.StatusSuspended {
return fmt.Errorf("instance %q is suspended, run `limactl start %s` to resume the instance", instName, instName)
}

// When workDir is explicitly set, the shell MUST have workDir as the cwd, or exit with an error.
Expand Down
2 changes: 1 addition & 1 deletion cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func startAction(cmd *cobra.Command, args []string) error {
inst.Name, instance.LimactlShellCmd(inst.Name))
// Not an error
return nil
case store.StatusStopped:
case store.StatusStopped, store.StatusSuspended:
// NOP
default:
logrus.Warnf("expected status %q, got %q", store.StatusStopped, inst.Status)
Expand Down
2 changes: 2 additions & 0 deletions cmd/limactl/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
}
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
} else if inst.Status == store.StatusSuspended {
return fmt.Errorf("instance %q is suspended, run `limactl start %s` to resume the instance", instName, instName)
}

if port == 0 {
Expand Down
1 change: 1 addition & 0 deletions pkg/instance/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

func StopGracefully(inst *store.Instance) error {
// TODO: support store.StatusSuspended
if inst.Status != store.StatusRunning {
return fmt.Errorf("expected status %q, got %q (maybe use `limactl stop -f`?)", store.StatusRunning, inst.Status)
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
StatusBroken Status = "Broken"
StatusStopped Status = "Stopped"
StatusRunning Status = "Running"
StatusSuspended Status = "Suspended"
)

type Instance struct {
Expand Down Expand Up @@ -198,7 +199,15 @@ func inspectStatusWithPIDFiles(instDir string, inst *Instance, y *limayaml.LimaY
case inst.HostAgentPID > 0 && inst.DriverPID > 0:
inst.Status = StatusRunning
case inst.HostAgentPID == 0 && inst.DriverPID == 0:
inst.Status = StatusStopped
_, err = os.Stat(filepath.Join(instDir, filenames.VzMachineState))
if err == nil {
inst.Status = StatusSuspended
} else if errors.Is(err, os.ErrNotExist) {
inst.Status = StatusStopped
} else {
inst.Errors = append(inst.Errors, err)
inst.Status = StatusBroken
}
case inst.HostAgentPID > 0 && inst.DriverPID == 0:
inst.Errors = append(inst.Errors, errors.New("host agent is running but driver is not"))
inst.Status = StatusBroken
Expand Down

0 comments on commit 8b4dc0f

Please sign in to comment.