@@ -29,6 +29,7 @@ import (
2929 "os"
3030 "path/filepath"
3131 "strconv"
32+ "time"
3233
3334 "github.com/charmbracelet/glamour"
3435 "github.com/charmbracelet/log"
@@ -141,8 +142,29 @@ func (c *cmdLaunch) launch(app string, instanceName string) error {
141142 launchSettings .InstallMethod = installMethod
142143
143144 }
144- // choose ssh options
145+
146+ // select install method
145147 form := huh .NewForm (
148+
149+ huh .NewGroup (
150+ huh .NewConfirm ().
151+ Title ("Launch As VM?" ).
152+ Value (& launchSettings .VM ).
153+ Affirmative ("Yes" ).
154+ Negative ("No" ),
155+ ),
156+ ).WithAccessible (accessible )
157+
158+ err = form .Run ()
159+ if err != nil {
160+ fmt .Println ("form error:" , err )
161+ os .Exit (1 )
162+ }
163+ launchSettings .Image = "images:" + application .InstallMethods [installMethod ].Resources .Image ()
164+ launchSettings .InstallMethod = installMethod
165+
166+ // choose ssh options
167+ form = huh .NewForm (
146168 huh .NewGroup (
147169 huh .NewConfirm ().
148170 Title ("Pass through GPU?" ).
@@ -354,7 +376,7 @@ func (c *cmdLaunch) launch(app string, instanceName string) error {
354376
355377 createInstance := func () {
356378 // create the instance
357- err := c .global .client .Launch (launchSettings .Image , launchSettings .Name , launchSettings .Profiles , extraConfigs , false , false )
379+ err := c .global .client .Launch (launchSettings .Image , launchSettings .Name , launchSettings .Profiles , extraConfigs , launchSettings . VM , false )
358380 if err != nil {
359381 fmt .Println ("Error creating instance:" , err )
360382 os .Exit (1 )
@@ -380,6 +402,39 @@ func (c *cmdLaunch) launch(app string, instanceName string) error {
380402 fmt .Println ("Error starting instance:" , err )
381403 os .Exit (1 )
382404 }
405+ if launchSettings .VM {
406+ log .Info ("VM started, waiting for agent..." )
407+ const maxAttempts = 5
408+ const waitTime = 2
409+ getState := func () (bool , error ) {
410+ time .Sleep (waitTime * time .Second )
411+ state , err := c .global .client .InstanceState (context .Background (), launchSettings .Name )
412+ if err != nil {
413+ fmt .Println ("Error waiting for vm agent:" , err )
414+ return false , err
415+ }
416+ if state .State .Processes > 2 {
417+ return true , nil
418+ }
419+ return false , nil
420+ }
421+ attempts := 0
422+ for {
423+ success , err := getState ()
424+ if err != nil {
425+ fmt .Println ("Error waiting for vm agent:" , err )
426+ os .Exit (1 )
427+ }
428+ if success {
429+ break
430+ }
431+ attempts ++
432+ if attempts >= maxAttempts {
433+ fmt .Println ("Error waiting for vm agent: max attempts reached" )
434+ os .Exit (1 )
435+ }
436+ }
437+ }
383438 }
384439
385440 if doit {
0 commit comments