-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathvm_start.go
140 lines (110 loc) · 3.35 KB
/
vm_start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cmd
import (
"errors"
"flag"
"strings"
"github.com/mitchellh/cli"
"github.com/roots/trellis-cli/pkg/vm"
"github.com/roots/trellis-cli/trellis"
)
type VmStartCommand struct {
UI cli.Ui
Trellis *trellis.Trellis
flags *flag.FlagSet
}
func NewVmStartCommand(ui cli.Ui, trellis *trellis.Trellis) *VmStartCommand {
c := &VmStartCommand{UI: ui, Trellis: trellis}
c.init()
return c
}
func (c *VmStartCommand) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.Usage = func() { c.UI.Info(c.Help()) }
}
func (c *VmStartCommand) 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
}
// CHANGE: Use GetVMInstanceName instead of MainSiteFromEnvironment
siteName, err := c.Trellis.GetVMInstanceName()
if err != nil {
c.UI.Error("Error: could not get VM name: " + err.Error())
return 1
}
if siteName == "" {
c.UI.Error("Error: could not automatically set VM name. No site found in development environment.")
return 1
}
manager, err := newVmManager(c.Trellis, c.UI)
if err != nil {
c.UI.Error("Error: " + err.Error())
return 1
}
err = manager.StartInstance(siteName)
if err == nil {
c.printInstanceInfo()
return 0
}
if !errors.Is(err, vm.VmNotFoundErr) {
c.UI.Error("Error starting VM.")
c.UI.Error(err.Error())
return 1
}
// VM doesn't exist yet, create and start it
if err = manager.CreateInstance(siteName); err != nil {
c.UI.Error("Error creating VM.")
c.UI.Error(err.Error())
return 1
}
// Save the instance name for future reference
if err = c.Trellis.SaveVMInstanceName(siteName); err != nil {
c.UI.Warn("Warning: Failed to save VM instance name. VM was created successfully, but future commands may not recognize it.")
}
if err = manager.StartInstance(siteName); err != nil {
c.UI.Error("Error starting VM.")
c.UI.Error(err.Error())
return 1
}
c.UI.Info("\nProvisioning VM...")
provisionCmd := NewProvisionCommand(c.UI, c.Trellis)
code := provisionCmd.Run([]string{"development"})
if code == 0 {
c.printInstanceInfo()
}
return code
}
func (c *VmStartCommand) Synopsis() string {
return "Starts a development virtual machine."
}
func (c *VmStartCommand) Help() string {
helpText := `
Usage: trellis vm start [options]
Starts a development virtual machine.
If a VM doesn't exist yet, it will be created. If a VM already exists, it will be started.
Note: VM management (under the 'trellis vm' subcommands) is currently only available for macOS Ventura (13.0) and later.
Lima (https://lima-vm.io/) is the underlying VM manager which requires macOS's new virtualization framework.
Options:
-h, --help show this help
`
return strings.TrimSpace(helpText)
}
func (c *VmStartCommand) printInstanceInfo() {
c.UI.Info(`
Your Trellis VM is ready to use!
* Composer and WP-CLI commands need to be run on the virtual machine for any post-provision modifications.
* You can SSH into the machine with 'trellis vm shell'
* Then navigate to your WordPress sites at '/srv/www'`)
}