-
Notifications
You must be signed in to change notification settings - Fork 54
/
driver.go
202 lines (169 loc) · 4.26 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package daemon
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/bblfsh/bblfshd/daemon/protocol"
"github.com/bblfsh/bblfshd/runtime"
"github.com/opencontainers/runc/libcontainer/configs"
"google.golang.org/grpc"
sdk "gopkg.in/bblfsh/sdk.v1/protocol"
)
type Driver interface {
ID() string
Start() error
Stop() error
Status() (protocol.Status, error)
State() (*protocol.DriverInstanceState, error)
Service() sdk.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 sdk.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
}
// ID returns the container id.
func (i *DriverInstance) ID() string {
return i.Container.ID()
}
// 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 = sdk.NewProtocolServiceClient(conn)
return err
}
func (i *DriverInstance) loadVersion() error {
_, err := i.srv.Version(context.Background(), &sdk.VersionRequest{})
if err != nil {
return err
}
return nil
}
// Status returns the current status of the container.
func (i *DriverInstance) Status() (protocol.Status, error) {
s, err := i.Container.Status()
return protocol.Status(s), err
}
// State returns the current state of the driver instance.
func (i *DriverInstance) State() (*protocol.DriverInstanceState, error) {
status, err := i.Status()
if err != nil {
return nil, err
}
pid, err := i.Container.Processes()
if err != nil {
return nil, err
}
state, err := i.Container.State()
if err != nil {
return nil, err
}
return &protocol.DriverInstanceState{
ID: i.ID(),
Image: i.Image.Name(),
Status: status,
Processes: pid,
Created: state.Created,
}, nil
}
// 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() sdk.ProtocolServiceClient {
return i.srv
}
func logFields(containerID, language string) string {
js, _ := json.Marshal(map[string]string{
"id": containerID,
"language": language,
})
return string(js)
}