-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds full integration for managing virtual machines in development via https://github.com/lima-vm/lima as an alternative to Vagrant. Lima stands for "Linux virtual machines (on macOS, in most cases)" and that's exactly our use case. Lima isn't just a replacement for Vagrant, it also replaces the VM provider like VirtualBox or Parallels too. Lima supports two ways of running guest machines: * [QEMU](https://www.qemu.org/) * macOS Virtualization.Framework ("vz") For this initial integration, we're only supporting the macOS Virtualization.Framework because it offers near-native performance, and that includes file syncing via `virtiofs`. For macOS Ventura (13.0+) users, we'll eventually recommend using trellis-cli's VM feature over Vagrant as the default since it's easier and faster. We'll look into expanding support for Linux users as well depending on performance of the file mounts on the VM. Requirements: * Intel or Apple Silicon * macOS 13 (Ventura) * Lima >= 0.14 Usage: There's 5 new commands: * `trellis vm start` * `trellis vm stop` * `trellis vm delete` * `trellis vm shell` * `trellis vm sudoers` Under the hood, those commands wrap equivalent `limactl` features. Just like the previous Vagrant integration, you can always run `limactl` directly to manage your VMs. For default use cases, `trellis vm start` can be run without any customization first. It will create a new virtual machine (using Lima) from a generated config file (`project/trellis/.trellis/lima/config/<name>.yml`). The site's `local_path` will be automatically mounted on the VM and your `/etc/hosts` file will be updated. Note: run `trellis vm sudoers -h` to make `/etc/hosts` file passwordless: ```bash $ trellis sudoers | sudo tee /etc/sudoers.d/trellis ``` Configuration: Right now configuration options for the VM are limited. The intention is to the most common use cases without any configuration needed. A trellis-cli config file (global or project level) supports a new `vm` option. The only useful config option right now is `images` (to switch between Ubuntu 20.04 and 22.04 for example). 20.04. Here's an example of specifying Jammy 22.04 for ARM (`aarch64`) only, since that's what I use): ```yml vm: images: - location: https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-arm64.img arch: aarch64 ```
- Loading branch information
1 parent
d02471d
commit d3894d6
Showing
36 changed files
with
2,550 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/mitchellh/cli" | ||
"github.com/roots/trellis-cli/pkg/lima" | ||
"github.com/roots/trellis-cli/pkg/vm" | ||
"github.com/roots/trellis-cli/trellis" | ||
) | ||
|
||
func newVmManager(trellis *trellis.Trellis, ui cli.Ui) (manager vm.Manager, err error) { | ||
switch trellis.CliConfig.Vm.Manager { | ||
case "auto": | ||
switch runtime.GOOS { | ||
case "darwin": | ||
return lima.NewManager(trellis, ui) | ||
default: | ||
return nil, fmt.Errorf("No VM managers are supported on %s yet.", runtime.GOOS) | ||
} | ||
case "lima": | ||
return lima.NewManager(trellis, ui) | ||
case "mock": | ||
return vm.NewMockManager(trellis, ui) | ||
} | ||
|
||
return nil, fmt.Errorf("VM manager not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"strings" | ||
|
||
"github.com/manifoldco/promptui" | ||
"github.com/mitchellh/cli" | ||
"github.com/posener/complete" | ||
"github.com/roots/trellis-cli/trellis" | ||
) | ||
|
||
type VmDeleteCommand struct { | ||
UI cli.Ui | ||
Trellis *trellis.Trellis | ||
flags *flag.FlagSet | ||
force bool | ||
} | ||
|
||
func NewVmDeleteCommand(ui cli.Ui, trellis *trellis.Trellis) *VmDeleteCommand { | ||
c := &VmDeleteCommand{UI: ui, Trellis: trellis} | ||
c.init() | ||
return c | ||
} | ||
|
||
func (c *VmDeleteCommand) init() { | ||
c.flags = flag.NewFlagSet("", flag.ContinueOnError) | ||
c.flags.Usage = func() { c.UI.Info(c.Help()) } | ||
c.flags.BoolVar(&c.force, "force", false, "Delete VM without confirmation.") | ||
} | ||
|
||
func (c *VmDeleteCommand) Run(args []string) int { | ||
if err := c.Trellis.LoadProject(); err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
c.Trellis.CheckVirtualenv(c.UI) | ||
|
||
if err := c.flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
args = c.flags.Args() | ||
|
||
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0} | ||
commandArgumentErr := commandArgumentValidator.validate(args) | ||
if commandArgumentErr != nil { | ||
c.UI.Error(commandArgumentErr.Error()) | ||
c.UI.Output(c.Help()) | ||
return 1 | ||
} | ||
|
||
siteName, err := c.Trellis.FindSiteNameFromEnvironment("development", "") | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
manager, err := newVmManager(c.Trellis, c.UI) | ||
if err != nil { | ||
c.UI.Error("Error: " + err.Error()) | ||
return 1 | ||
} | ||
|
||
if c.force || c.confirmDeletion() { | ||
if err := manager.DeleteInstance(siteName); err != nil { | ||
c.UI.Error("Error: " + err.Error()) | ||
return 1 | ||
} | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (c *VmDeleteCommand) Synopsis() string { | ||
return "Deletes the development virtual machine." | ||
} | ||
|
||
func (c *VmDeleteCommand) Help() string { | ||
helpText := ` | ||
Usage: trellis vm delete [options] | ||
Deletes the development virtual machine. | ||
VMs must be in a stopped state before they can be deleted. | ||
Delete without prompting for confirmation: | ||
$ trellis vm delete --force | ||
Options: | ||
--force Delete VM without confirmation. | ||
-h, --help Show this help | ||
` | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *VmDeleteCommand) AutocompleteFlags() complete.Flags { | ||
return complete.Flags{ | ||
"--force": complete.PredictNothing, | ||
} | ||
} | ||
|
||
func (c *VmDeleteCommand) confirmDeletion() bool { | ||
prompt := promptui.Prompt{ | ||
Label: "Delete virtual machine", | ||
IsConfirm: true, | ||
} | ||
|
||
_, err := prompt.Run() | ||
|
||
if err != nil { | ||
c.UI.Info("Aborted. Not deleting virtual machine.") | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cmd | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/mitchellh/cli" | ||
"github.com/roots/trellis-cli/trellis" | ||
) | ||
|
||
func TestVmDeleteRunValidations(t *testing.T) { | ||
defer trellis.LoadFixtureProject(t)() | ||
|
||
cases := []struct { | ||
name string | ||
projectDetected bool | ||
args []string | ||
out string | ||
code int | ||
}{ | ||
{ | ||
"no_project", | ||
false, | ||
nil, | ||
"No Trellis project detected", | ||
1, | ||
}, | ||
{ | ||
"too_many_args", | ||
true, | ||
[]string{"foo"}, | ||
"Error: too many arguments", | ||
1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ui := cli.NewMockUi() | ||
trellis := trellis.NewMockTrellis(tc.projectDetected) | ||
vmDeleteCommand := NewVmDeleteCommand(ui, trellis) | ||
|
||
code := vmDeleteCommand.Run(tc.args) | ||
|
||
if code != tc.code { | ||
t.Errorf("expected code %d to be %d", code, tc.code) | ||
} | ||
|
||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String() | ||
|
||
if !strings.Contains(combined, tc.out) { | ||
t.Errorf("expected output %q to contain %q", combined, tc.out) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.