-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.go
More file actions
337 lines (286 loc) · 8.65 KB
/
agent.go
File metadata and controls
337 lines (286 loc) · 8.65 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"log"
"strconv"
"github.com/docker/docker/api/types/filters"
"google.golang.org/grpc"
"github.com/buger/jsonparser"
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
consul "github.com/hashicorp/consul/api"
pb "github.com/opencopilot/agent/agent"
managerPb "github.com/opencopilot/agent/manager"
"github.com/opencopilot/consulkvjson"
"gopkg.in/yaml.v2"
)
// Service is a specification for a service running on the device
type Service string
// Services is a list of Service
type Services []Service
// Agent handles agent functionality
type Agent struct {
dockerCli *docker.Client
consulCli *consul.Client
}
// AgentGetStatus returns the status of a running service
func (agent *Agent) AgentGetStatus(ctx context.Context) (*pb.AgentStatus, error) {
status := &pb.AgentStatus{InstanceId: InstanceID, Services: []*pb.AgentStatus_AgentService{}}
containers, err := agent.dockerCli.ContainerList(ctx, dockerTypes.ContainerListOptions{})
if err != nil {
log.Fatal(err)
}
for _, container := range containers {
status.Services = append(status.Services, &pb.AgentStatus_AgentService{Id: container.ID, Image: container.Image})
}
return status, nil
}
func (agent *Agent) startConfigHandler(queue chan consul.KVPairs) {
for {
kvs := <-queue
agent.sync(kvs)
}
}
func (agent *Agent) sync(kvs consul.KVPairs) {
m, err := consulkvjson.ConsulKVsToJSON(kvs)
if err != nil {
log.Panic(err)
}
jsonString, err := json.Marshal(m)
if err != nil {
log.Panic(err)
}
servicesMapString, valueType, _, err := jsonparser.Get(jsonString, "instances", InstanceID, "services")
if valueType == jsonparser.NotExist {
agent.ensureServices(Services{})
return
}
if err != nil {
log.Fatal(err)
}
incomingServices := Services{}
jsonparser.ObjectEach(servicesMapString, func(key, value []byte, dataType jsonparser.ValueType, offset int) error {
service := Service(key)
incomingServices = append(incomingServices, service)
return nil
})
agent.ensureServices(incomingServices)
localServices, err := agent.getLocalServices()
if err != nil {
log.Fatal(err)
}
agent.configureServices(localServices)
}
func (agent *Agent) getLocalServices() (Services, error) {
args := filters.NewArgs(
filters.Arg("label", "com.opencopilot.managed"),
)
containers, err := agent.dockerCli.ContainerList(context.Background(), dockerTypes.ContainerListOptions{
Filters: args,
})
if err != nil {
log.Panic(err)
}
localServices := Services{}
for _, container := range containers {
serviceName, found := container.Labels["com.opencopilot.service-manager"]
if !found {
continue
}
// fmt.Printf("%s %s %v\n", container.ID[:10], container.Image, serviceName)
service := Service(serviceName)
localServices = append(localServices, service)
}
return localServices, nil
}
func (agent *Agent) ensureServices(incomingServices Services) {
localServices, err := agent.getLocalServices()
if err != nil {
log.Panicln(err)
}
for _, incomingService := range incomingServices {
// For every service we should have, go check all the services we're currently running
existsLocally := false
for _, localService := range localServices {
// If an incoming service is already running, do nothing
if incomingService == localService {
existsLocally = true
}
}
// If we didn't find that this incoming service exists locally, start it
if existsLocally {
break
} else {
err := agent.startService(incomingService)
if err != nil {
// TODO: do something else here
log.Println(err)
}
}
}
for _, localService := range localServices {
// For every locally running service, check if it should be running from the incoming services
existsIncoming := false
for _, incomingService := range incomingServices {
if localService == incomingService {
existsIncoming = true
}
}
// If we didn't find that this local service exists in the incoming specification, stop it
if existsIncoming {
break
} else {
err := agent.stopService(localService)
if err != nil {
// TODO: do something else here
log.Println(err)
}
}
}
}
func (agent *Agent) startService(service Service) error {
log.Printf("adding service: %s\n", string(service))
ctx := context.Background()
serviceImageYaml, err := ioutil.ReadFile("./services.yaml")
if err != nil {
log.Fatal(err)
}
serviceImageMap := make(map[interface{}]interface{})
yamlErr := yaml.Unmarshal(serviceImageYaml, &serviceImageMap)
if yamlErr != nil {
log.Fatal(yamlErr)
}
if serviceImageMap[string(service)] == nil {
return errors.New("invalid service specified")
}
containerConfig := &container.Config{
Labels: map[string]string{
"com.opencopilot.managed": "",
"com.opencopilot.service-manager": string(service),
},
Image: serviceImageMap[string(service)].(string),
}
reader, err := agent.dockerCli.ImagePull(ctx, containerConfig.Image, dockerTypes.ImagePullOptions{})
if err != nil {
return err
}
defer reader.Close()
if _, err := ioutil.ReadAll(reader); err != nil {
log.Panic(err)
}
containerConfig.Env = []string{"CONFIG_DIR=" + ConfigDir, "INSTANCE_ID=" + InstanceID}
res, err := agent.dockerCli.ContainerCreate(ctx, containerConfig, &container.HostConfig{
AutoRemove: true, // Important to remove container after it's stopped, so that we can start a new one up with the same name if this service gets re-added
Privileged: true, // So that the manager containers can start other docker containers,
Binds: []string{ // So that the manager containers have access to Docker on the host
"/var/run/docker.sock:/var/run/docker.sock",
ConfigDir + ":" + ConfigDir,
},
PublishAllPorts: true,
}, nil, "com.opencopilot.service-manager."+string(service))
if err != nil {
return err
}
startErr := agent.dockerCli.ContainerStart(ctx, res.ID, dockerTypes.ContainerStartOptions{})
if startErr != nil {
return startErr
}
return nil
}
func (agent *Agent) stopService(service Service) error {
log.Printf("stopping service: %s\n", string(service))
ctx := context.Background()
args := filters.NewArgs(
filters.Arg("label", "com.opencopilot.managed"),
filters.Arg("name", "com.opencopilot.service-manager."+string(service)),
)
containers, err := agent.dockerCli.ContainerList(ctx, dockerTypes.ContainerListOptions{
Filters: args,
})
if err != nil {
log.Fatal(err)
}
for _, container := range containers {
agent.dockerCli.ContainerStop(ctx, container.ID, nil)
}
return nil
}
func (agent *Agent) getServiceConfig(service Service) ([]byte, error) {
kv := agent.consulCli.KV()
kvs, _, err := kv.List("instances/"+InstanceID+"/services/"+string(service), &consul.QueryOptions{})
if err != nil {
log.Fatal(err)
}
configMap, err := consulkvjson.ConsulKVsToJSON(kvs)
if err != nil {
log.Fatal(err)
}
configString, err := json.Marshal(configMap)
if err != nil {
log.Fatal(err)
}
serviceConfig, dataType, _, err := jsonparser.Get(configString, "instances", InstanceID, "services", string(service))
if err != nil {
log.Fatal(err)
}
if dataType == jsonparser.NotExist {
log.Println(errors.New("invalid JSON"))
}
return serviceConfig, nil
}
func (agent *Agent) getServiceGRPCPort(service Service) (uint16, error) {
ctx := context.Background()
args := filters.NewArgs(
filters.Arg("label", "com.opencopilot.managed"),
filters.Arg("name", "com.opencopilot.service-manager."+string(service)),
)
containers, err := agent.dockerCli.ContainerList(ctx, dockerTypes.ContainerListOptions{
Filters: args,
})
if err != nil {
return 0, err
}
for _, container := range containers {
for _, portPair := range container.Ports {
if portPair.PrivatePort == 50052 {
return portPair.PublicPort, nil
}
}
}
return 0, errors.New("Could not find gRPC port")
}
func (agent *Agent) configureService(service Service) error {
serviceConfig, err := agent.getServiceConfig(service)
if err != nil {
return err
}
gRPCPort, err := agent.getServiceGRPCPort(service)
if err != nil {
return err
}
conn, err := grpc.Dial("localhost:"+strconv.Itoa(int(gRPCPort)), grpc.WithInsecure())
if err != nil {
return err
}
defer conn.Close()
client := managerPb.NewManagerClient(conn)
_, errConfiguring := client.Configure(context.Background(), &managerPb.ConfigureRequest{Config: string(serviceConfig)})
if errConfiguring != nil {
return errConfiguring
}
return nil
}
func (agent *Agent) configureServices(services Services) []error {
var errorList []error
for _, service := range services {
err := agent.configureService(service)
if err != nil {
errorList = append(errorList, err)
}
}
return errorList
}