-
Notifications
You must be signed in to change notification settings - Fork 54
/
driver.go
167 lines (139 loc) · 3.54 KB
/
driver.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package daemon
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/bblfsh/server/runtime"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/configs"
"google.golang.org/grpc"
"gopkg.in/bblfsh/sdk.v1/protocol"
)
type Driver interface {
Start() error
Stop() error
Status() (libcontainer.Status, error)
Service() protocol.ProtocolServiceClient
}
// DriverInstance represents an instance of a driver.
type DriverInstance struct {
Language string
Process *runtime.Process
Container runtime.Container
Image runtime.DriverImage
ctx context.Context
conn *grpc.ClientConn
srv protocol.ProtocolServiceClient
tmp string
}
const (
DriverBinary = "/opt/driver/bin/driver"
GRPCSocket = "rpc.sock"
TmpPathPattern = "/tmp/%s"
ConnectionTimeout = 5 * time.Second
)
type Options struct {
LogLevel string
LogFormat string
}
// NewDriverInstance represents a running Driver in the runtime. Its holds the
// container and the connection to the internal grpc server.
func NewDriverInstance(r *runtime.Runtime, lang string, i runtime.DriverImage, o *Options) (*DriverInstance, error) {
id := strings.ToLower(runtime.NewULID().String())
p := &runtime.Process{
Args: []string{
DriverBinary,
"--log-level", o.LogLevel,
"--log-format", o.LogFormat,
"--log-fields", logFields(id, lang),
"--network", "unix",
"--address", fmt.Sprintf(TmpPathPattern, GRPCSocket),
},
Stdout: os.Stdout,
Stderr: os.Stderr,
}
tmp := filepath.Join(r.Root, fmt.Sprintf(TmpPathPattern, id))
f := func(containerID string) *configs.Config {
cfg := runtime.ContainerConfigFactory(containerID)
cfg.Mounts = append(cfg.Mounts, &configs.Mount{
Source: tmp,
Destination: "/tmp/",
Device: "bind",
Flags: syscall.MS_BIND | syscall.MS_REC | syscall.MS_NOSUID,
PremountCmds: []configs.Command{
{Path: "mkdir", Args: []string{"-p", tmp}},
},
})
return cfg
}
c, err := r.Container(id, i, p, f)
if err != nil {
return nil, err
}
return &DriverInstance{
Language: lang,
Process: p,
Container: c,
Image: i,
ctx: context.Background(),
tmp: tmp,
}, nil
}
// Start starts a container and connects to it.
func (i *DriverInstance) Start() error {
if err := i.Container.Start(); err != nil {
return err
}
if err := i.dial(); err != nil {
return err
}
if err := i.loadVersion(); err != nil {
return err
}
return nil
}
func (i *DriverInstance) dial() error {
addr := filepath.Join(i.tmp, GRPCSocket)
conn, err := grpc.Dial(addr,
grpc.WithDialer(func(addr string, t time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, t)
}),
grpc.WithBlock(),
grpc.WithTimeout(ConnectionTimeout),
grpc.WithInsecure(),
)
i.conn = conn
i.srv = protocol.NewProtocolServiceClient(conn)
return err
}
func (i *DriverInstance) loadVersion() error {
_, err := i.srv.Version(context.Background(), &protocol.VersionRequest{})
if err != nil {
return err
}
return nil
}
func (i *DriverInstance) Status() (libcontainer.Status, error) {
return i.Container.Status()
}
// Stop stops the inner running container.
func (i *DriverInstance) Stop() error {
return i.Container.Stop()
}
// Service returns the client using the grpc connection.
func (i *DriverInstance) Service() protocol.ProtocolServiceClient {
return i.srv
}
func logFields(containerID, language string) string {
js, _ := json.Marshal(map[string]string{
"id": containerID,
"language": language,
})
return string(js)
}