@@ -4,11 +4,16 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"io"
7
+ "net"
8
+ "net/http"
7
9
"os"
8
10
"os/signal"
9
11
"strconv"
10
12
13
+ "github.com/gorilla/mux"
11
14
"github.com/lima-vm/lima/pkg/hostagent"
15
+ "github.com/lima-vm/lima/pkg/hostagent/api/server"
16
+ "github.com/sirupsen/logrus"
12
17
"github.com/spf13/cobra"
13
18
)
14
19
@@ -21,6 +26,7 @@ func newHostagentCommand() *cobra.Command {
21
26
Hidden : true ,
22
27
}
23
28
hostagentCommand .Flags ().StringP ("pidfile" , "p" , "" , "write pid to file" )
29
+ hostagentCommand .Flags ().String ("socket" , "" , "hostagent socket" )
24
30
return hostagentCommand
25
31
}
26
32
@@ -38,6 +44,13 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
38
44
}
39
45
defer os .RemoveAll (pidfile )
40
46
}
47
+ socket , err := cmd .Flags ().GetString ("socket" )
48
+ if err != nil {
49
+ return err
50
+ }
51
+ if socket == "" {
52
+ return fmt .Errorf ("socket must be specified (limactl version mismatch?)" )
53
+ }
41
54
42
55
instName := args [0 ]
43
56
@@ -51,6 +64,28 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
51
64
if err != nil {
52
65
return err
53
66
}
67
+
68
+ backend := & server.Backend {
69
+ Agent : ha ,
70
+ }
71
+ r := mux .NewRouter ()
72
+ server .AddRoutes (r , backend )
73
+ srv := & http.Server {Handler : r }
74
+ err = os .RemoveAll (socket )
75
+ if err != nil {
76
+ return err
77
+ }
78
+ l , err := net .Listen ("unix" , socket )
79
+ if err != nil {
80
+ return err
81
+ }
82
+ go func () {
83
+ defer os .RemoveAll (socket )
84
+ defer srv .Close ()
85
+ if serveErr := srv .Serve (l ); serveErr != nil {
86
+ logrus .WithError (serveErr ).Warn ("hostagent API server exited with an error" )
87
+ }
88
+ }()
54
89
return ha .Run (cmd .Context ())
55
90
}
56
91
0 commit comments