forked from control-center/serviced
/
agent.go
917 lines (798 loc) · 29.8 KB
/
agent.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
// Copyright 2014, The Serviced Authors. All rights reserved.
// Use of this source code is governed by a
// license that can be found in the LICENSE file.
// Package agent implements a service that runs on a serviced node. It is
// responsible for ensuring that a particular node is running the correct services
// and reporting the state and health of those services back to the master
// serviced.
package serviced
import (
"github.com/samuel/go-zookeeper/zk"
"github.com/zenoss/serviced/circular"
"github.com/zenoss/serviced/dao"
"github.com/zenoss/serviced/volume"
"github.com/zenoss/serviced/zzk"
"encoding/json"
"errors"
"fmt"
"github.com/zenoss/glog"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
)
/*
glog levels:
0: important info that should always be shown
1: info that might be important for debugging
2: very verbose debug info
3: trace level info
*/
const (
circularBufferSize = 1000
)
// An instance of the control plane Agent.
type HostAgent struct {
master string // the connection string to the master agent
hostId string // the hostID of the current host
varPath string // directory to store serviced data
mount []string // each element is in the form: container_image:host_path:container_path
zookeepers []string
currentServices map[string]*exec.Cmd // the current running services
mux TCPMux
closing chan chan error
}
// assert that this implemenents the Agent interface
var _ Agent = &HostAgent{}
// Create a new HostAgent given the connection string to the
func NewHostAgent(master string, varPath string, mount []string, zookeepers []string, mux TCPMux) (*HostAgent, error) {
// save off the arguments
agent := &HostAgent{}
agent.master = master
agent.varPath = varPath
agent.mount = mount
agent.zookeepers = zookeepers
if len(agent.zookeepers) == 0 {
defaultZK := "127.0.0.1:2181"
glog.V(1).Infoln("Zookeepers not specified: using default of ", defaultZK)
agent.zookeepers = []string{defaultZK}
}
agent.mux = mux
if agent.mux.Enabled {
go agent.mux.ListenAndMux()
}
agent.closing = make(chan chan error)
hostId, err := HostId()
if err != nil {
panic("Could not get hostid")
}
agent.hostId = hostId
agent.currentServices = make(map[string]*exec.Cmd)
go agent.start()
return agent, err
}
// Use the Context field of the given template to fill in all the templates in
// the Command fields of the template's ServiceDefinitions
func injectContext(s *dao.Service, cp dao.ControlPlane) error {
return s.EvaluateStartupTemplate(cp)
}
func (a *HostAgent) Shutdown() error {
glog.V(2).Info("Issuing shutdown signal")
errc := make(chan error)
a.closing <- errc
return <-errc
}
// Attempts to attach to a running container
func (a *HostAgent) attachToService(conn *zk.Conn, procFinished chan<- int, serviceState *dao.ServiceState, hss *zzk.HostServiceState) (bool, error) {
// get docker status
containerState, err := getDockerState(serviceState.DockerId)
glog.V(2).Infof("Agent.updateCurrentState got container state for docker ID %s: %v", serviceState.DockerId, containerState)
switch {
case err == nil && !containerState.State.Running:
glog.V(1).Infof("Container does not appear to be running: %s", serviceState.Id)
return false, errors.New("Container not running for " + serviceState.Id)
case err != nil && strings.HasPrefix(err.Error(), "no container"):
glog.Warningf("Error retrieving container state: %s", serviceState.Id)
return false, err
}
cmd := exec.Command("docker", "attach", serviceState.DockerId)
go a.waitForProcessToDie(conn, cmd, procFinished, serviceState)
return true, nil
}
func markTerminated(conn *zk.Conn, hss *zzk.HostServiceState) {
ssPath := zzk.ServiceStatePath(hss.ServiceId, hss.ServiceStateId)
_, stats, err := conn.Get(ssPath)
if err != nil {
glog.V(0).Infof("Unable to get service state %s for delete because: %v", ssPath, err)
return
}
err = conn.Delete(ssPath, stats.Version)
if err != nil {
glog.V(0).Infof("Unable to delete service state %s because: %v", ssPath, err)
return
}
hssPath := zzk.HostServiceStatePath(hss.HostId, hss.ServiceStateId)
_, stats, err = conn.Get(hssPath)
if err != nil {
glog.V(0).Infof("Unable to get host service state %s for delete becaus: %v", hssPath, err)
return
}
err = conn.Delete(hssPath, stats.Version)
if err != nil {
glog.V(0).Infof("Unable to delete host service state %s", hssPath)
}
}
// Terminate a particular service instance (serviceState) on the localhost.
func (a *HostAgent) terminateInstance(conn *zk.Conn, serviceState *dao.ServiceState) error {
err := a.dockerTerminate(serviceState.Id)
if err != nil {
return err
}
markTerminated(conn, zzk.SsToHss(serviceState))
return nil
}
func (a *HostAgent) terminateAttached(conn *zk.Conn, procFinished <-chan int, ss *dao.ServiceState) error {
err := a.dockerTerminate(ss.Id)
if err != nil {
return err
}
<-procFinished
markTerminated(conn, zzk.SsToHss(ss))
return nil
}
func (a *HostAgent) dockerRemove(dockerId string) error {
glog.V(1).Infof("Ensuring that container %s does not exist", dockerId)
cmd := exec.Command("docker", "rm", dockerId)
err := cmd.Run()
if err != nil {
glog.V(1).Infof("problem removing container instance %s", dockerId)
return err
}
glog.V(2).Infof("Successfully removed %s", dockerId)
return nil
}
func (a *HostAgent) dockerTerminate(dockerId string) error {
glog.V(1).Infof("Killing container %s", dockerId)
cmd := exec.Command("docker", "kill", dockerId)
err := cmd.Run()
if err != nil {
glog.V(1).Infof("problem killing container instance %s", dockerId)
return err
}
glog.V(2).Infof("Successfully killed %s", dockerId)
return nil
}
// Get the state of the docker container given the dockerId
func getDockerState(dockerId string) (containerState ContainerState, err error) {
// get docker status
cmd := exec.Command("docker", "inspect", dockerId)
output, err := cmd.Output()
if err != nil {
glog.V(2).Infof("problem getting docker state: %s", dockerId)
return containerState, err
}
var containerStates []ContainerState
err = json.Unmarshal(output, &containerStates)
if err != nil {
glog.Errorf("bad state happened: %v, \n\n\n%s", err, string(output))
return containerState, dao.ControlPlaneError{"no state"}
}
if len(containerStates) < 1 {
return containerState, dao.ControlPlaneError{"no container"}
}
return containerStates[0], err
}
func dumpOut(stdout, stderr io.Reader, size int) {
dumpBuffer(stdout, size, "stdout")
dumpBuffer(stderr, size, "stderr")
}
func dumpBuffer(reader io.Reader, size int, name string) {
buffer := make([]byte, size)
if n, err := reader.Read(buffer); err != nil {
glog.V(1).Infof("Unable to read %s of dump", name)
} else {
glog.V(0).Infof("Process %s:\n%s", name, string(buffer[:n]))
}
}
func (a *HostAgent) waitForProcessToDie(conn *zk.Conn, cmd *exec.Cmd, procFinished chan<- int, serviceState *dao.ServiceState) {
a.dockerRemove(serviceState.Id)
defer func() {
procFinished <- 1
}()
// save the last circularBufferSize bytes from each container run
lastStdout := circular.NewBuffer(circularBufferSize)
lastStderr := circular.NewBuffer(circularBufferSize)
if stdout, err := cmd.StdoutPipe(); err != nil {
glog.Errorf("Unable to read standard out for service state %s: %v", serviceState.Id, err)
return
} else {
go io.Copy(lastStdout, stdout)
}
if stderr, err := cmd.StderrPipe(); err != nil {
glog.Errorf("Unable to read standard error for service state %s: %v", serviceState.Id, err)
return
} else {
go io.Copy(lastStderr, stderr)
}
if err := cmd.Start(); err != nil {
glog.Errorf("Problem starting command '%s %s': %v", cmd.Path, cmd.Args, err)
dumpOut(lastStdout, lastStderr, circularBufferSize)
return
}
// We are name the container the same as its service state ID, so use that as an alias
dockerId := serviceState.Id
serviceState.DockerId = dockerId
time.Sleep(1 * time.Second) // Sleep to give docker a chance to start
var containerState ContainerState
var err error
for i := 0; i < 30; i++ {
if containerState, err = getDockerState(dockerId); err != nil {
time.Sleep(3 * time.Second) // Sleep to give docker a chance to start
glog.V(2).Infof("Problem getting service state for %s :%v", serviceState.Id, err)
a.dockerTerminate(dockerId)
dumpOut(lastStdout, lastStderr, circularBufferSize)
} else {
break
}
}
if err != nil {
return
}
if err = zzk.LoadAndUpdateServiceState(conn, serviceState.ServiceId, serviceState.Id, func(ss *dao.ServiceState) {
ss.DockerId = containerState.ID
ss.Started = time.Now()
ss.Terminated = time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)
ss.PrivateIp = containerState.NetworkSettings.IPAddress
ss.PortMapping = containerState.NetworkSettings.Ports
}); err != nil {
glog.Warningf("Unable to update service state %s: %v", serviceState.Id, err)
} else {
glog.V(1).Infof("SSPath: %s, PortMapping: %v", zzk.ServiceStatePath(serviceState.ServiceId, serviceState.Id), serviceState.PortMapping)
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
statusCode := status.ExitStatus()
switch {
case statusCode == 137:
glog.V(1).Infof("Docker process killed: %s", serviceState.Id)
case statusCode == 2:
glog.V(1).Infof("Docker process stopped: %s", serviceState.Id)
default:
glog.V(0).Infof("Docker process %s exited with code %d", serviceState.Id, statusCode)
dumpOut(lastStdout, lastStderr, circularBufferSize)
}
}
} else {
glog.V(1).Info("Unable to determine exit code for %s", serviceState.Id)
}
} else {
glog.V(0).Infof("Process for service state %s finished", serviceState.Id)
}
if err = zzk.ResetServiceState(conn, serviceState.ServiceId, serviceState.Id); err != nil {
glog.Errorf("Caught error marking process termination time for %s: %v", serviceState.Id, err)
}
}
}
func getSubvolume(varPath, poolId, tenantId string) (vol volume.Volume, err error) {
baseDir, _ := filepath.Abs(path.Join(varPath, "volumes"))
volume.New(baseDir, poolId)
baseDir, _ = filepath.Abs(path.Join(varPath, "volumes", poolId))
return volume.New(baseDir, tenantId)
}
/*
writeConfFile is responsible for writing contents out to a file
Input string prefix : cp_cd67c62b-e462-5137-2cd8-38732db4abd9_zenmodeler_logstash_forwarder_conf_
Input string id : Service ID (example cd67c62b-e462-5137-2cd8-38732db4abd9)
Input string filename: zenmodeler_logstash_forwarder_conf
Input string content : the content that you wish to write to a file
Output *os.File f : file handler to the file that you've just opened and written the content to
Example name of file that is written: /tmp/cp_cd67c62b-e462-5137-2cd8-38732db4abd9_zenmodeler_logstash_forwarder_conf_592084261
*/
func writeConfFile(prefix string, id string, filename string, content string) (*os.File, error) {
f, err := ioutil.TempFile("", prefix)
if err != nil {
glog.Errorf("Could not generate tempfile for config %s %s", id, filename)
return f, err
}
_, err = f.WriteString(content)
if err != nil {
glog.Errorf("Could not write out config file %s %s", id, filename)
return f, err
}
return f, nil
}
/*
chownConfFile is responsible for changing the owner of a file
Input *os.File f : file handler to a file that has already been opened
Input string id : Service ID (example cd67c62b-e462-5137-2cd8-38732db4abd9)
Input string filename: zenmodeler_logstash_forwarder_conf
Input string owner : update the file's owner to this provided string
Output bool : returns true if: the owner parameter is not present present OR the file has been chowned to the requested owner successfully
*/
func chownConfFile(f *os.File, id string, filename string, owner string) bool {
if len(owner) != 0 {
parts := strings.Split(owner, ":")
if len(parts) != 2 {
glog.Errorf("Unsupported owner specification: %s, only %%d:%%d supported for now: %s, %s", owner, id, filename)
return false
}
uid, err := strconv.Atoi(parts[0])
if err != nil {
glog.Warningf("Malformed UID: %s %s: %s", id, filename, err)
return false
}
gid, err := strconv.Atoi(parts[0])
if err != nil {
glog.Warningf("Malformed GID: %s %s: %s", id, filename, err)
return false
}
err = f.Chown(uid, gid)
if err != nil {
glog.Warningf("Could not chown config file: %s %s: %s", id, filename, err)
}
}
return true
}
// Start a service instance and update the CP with the state.
func (a *HostAgent) startService(conn *zk.Conn, procFinished chan<- int, ssStats *zk.Stat, service *dao.Service, serviceState *dao.ServiceState) (bool, error) {
glog.V(2).Infof("About to start service %s with name %s", service.Id, service.Name)
client, err := NewControlClient(a.master)
if err != nil {
glog.Errorf("Could not start ControlPlane client %v", err)
return false, err
}
defer client.Close()
//get this service's tenantId for env injection
var tenantId string
err = client.GetTenantId(service.Id, &tenantId)
if err != nil {
glog.Errorf("Failed getting tenantId for service: %s, %s", service.Id, err)
}
portOps := ""
if service.Endpoints != nil {
glog.V(1).Info("Endpoints for service: ", service.Endpoints)
for _, endpoint := range service.Endpoints {
if endpoint.Purpose == "export" { // only expose remote endpoints
portOps += fmt.Sprintf(" -p %d", endpoint.PortNumber)
}
}
}
volumeOpts := ""
if len(tenantId) == 0 && len(service.Volumes) > 0 {
// FIXME: find a better way of handling this error condition
glog.Fatalf("Could not get tenant ID and need to mount a volume, service state: %s, service id: %s", serviceState.Id, service.Id)
}
for _, volume := range service.Volumes {
btrfsVolume, err := getSubvolume(a.varPath, service.PoolId, tenantId)
if err != nil {
glog.Fatal("Could not create subvolume: %s", err)
} else {
resourcePath := path.Join(btrfsVolume.Dir(), volume.ResourcePath)
if err = os.MkdirAll(resourcePath, 0770); err != nil {
glog.Fatal("Could not create resource path: %s, %s", resourcePath, err)
}
if err := createVolumeDir(resourcePath, volume.ContainerPath, service.ImageId, volume.Owner, volume.Permission); err != nil {
glog.Fatalf("Error creating resource path: %v", err)
}
volumeOpts += fmt.Sprintf(" -v %s:%s", resourcePath, volume.ContainerPath)
}
}
dir, binary, err := ExecPath()
if err != nil {
glog.Errorf("Error getting exec path: %v", err)
return false, err
}
volumeBinding := fmt.Sprintf("%s:/serviced", dir)
if err := injectContext(service, client); err != nil {
glog.Errorf("Error injecting context: %s", err)
return false, err
}
// config files
configFiles := ""
for filename, config := range service.ConfigFiles {
prefix := fmt.Sprintf("cp_%s_%s_", service.Id, strings.Replace(filename, "/", "__", -1))
f, err := writeConfFile(prefix, service.Id, filename, config.Content)
if err != nil {
return false, err
}
fileChowned := chownConfFile(f, service.Id, filename, config.Owner)
if fileChowned == false {
continue
}
// everything worked!
configFiles += fmt.Sprintf(" -v %s:%s ", f.Name(), filename)
}
// if this container is going to produce any logs, bind mount the following files:
// logstash-forwarder, sslCertificate, sslKey, logstash-forwarder conf
// FIX ME: consider moving this functionality to its own function...
logstashForwarderMount := ""
if len(service.LogConfigs) > 0 {
logstashForwarderLogConf := `
{
"paths": [ "%s" ],
"fields": { "type": "%s" }
}`
logstashForwarderLogConf = fmt.Sprintf(logstashForwarderLogConf, service.LogConfigs[0].Path, service.LogConfigs[0].Type)
for _, logConfig := range service.LogConfigs[1:] {
logstashForwarderLogConf = logstashForwarderLogConf + `,
{
"paths": [ "%s" ],
"fields": { "type": "%s" }
}`
logstashForwarderLogConf = fmt.Sprintf(logstashForwarderLogConf, logConfig.Path, logConfig.Type)
}
containerDefaultGatewayAndLogstashForwarderPort := "172.17.42.1:5043"
// *********************************************************************************************
// ***** FIX ME the following 3 variables are defined in serviced/proxy.go as well! ************
containerLogstashForwarderDir := "/usr/local/serviced/resources/logstash"
containerLogstashForwarderBinaryPath := containerLogstashForwarderDir + "/logstash-forwarder"
containerLogstashForwarderConfPath := containerLogstashForwarderDir + "/logstash-forwarder.conf"
// *********************************************************************************************
containerSSLCertificatePath := containerLogstashForwarderDir + "/logstash-forwarder.crt"
containerSSLKeyPath := containerLogstashForwarderDir + "/logstash-forwarder.key"
logstashForwarderShipperConf := `
{
"network": {
"servers": [ "%s" ],
"ssl certificate": "%s",
"ssl key": "%s",
"ssl ca": "%s",
"timeout": 15
},
"files": [
%s
]
}`
logstashForwarderShipperConf = fmt.Sprintf(logstashForwarderShipperConf, containerDefaultGatewayAndLogstashForwarderPort, containerSSLCertificatePath, containerSSLKeyPath, containerSSLCertificatePath, logstashForwarderLogConf)
filename := service.Name + "_logstash_forwarder_conf"
prefix := fmt.Sprintf("cp_%s_%s_", service.Id, strings.Replace(filename, "/", "__", -1))
f, err := writeConfFile(prefix, service.Id, filename, logstashForwarderShipperConf)
if err != nil {
return false, err
}
logstashPath := resourcesDir() + "/logstash"
hostLogstashForwarderPath := logstashPath + "/logstash-forwarder"
hostLogstashForwarderConfPath := f.Name()
hostSSLCertificatePath := logstashPath + "/logstash-forwarder.crt"
hostSSLKeyPath := logstashPath + "/logstash-forwarder.key"
logstashForwarderBinaryMount := " -v " + hostLogstashForwarderPath + ":" + containerLogstashForwarderBinaryPath
logstashForwarderConfFileMount := " -v " + hostLogstashForwarderConfPath + ":" + containerLogstashForwarderConfPath
sslCertificateMount := " -v " + hostSSLCertificatePath + ":" + containerSSLCertificatePath
sslKeyMount := " -v " + hostSSLKeyPath + ":" + containerSSLKeyPath
logstashForwarderMount = logstashForwarderBinaryMount + sslCertificateMount + sslKeyMount + logstashForwarderConfFileMount
}
// add arguments to mount requested directory (if requested)
requestedMount := ""
for _, bindMountString := range a.mount {
splitMount := strings.Split(bindMountString, ":")
if len(splitMount) == 3 {
requestedImage := splitMount[0]
hostPath := splitMount[1]
containerPath := splitMount[2]
if requestedImage == service.ImageId {
requestedMount += " -v " + hostPath + ":" + containerPath
}
} else {
glog.Warningf("Could not bind mount the following: %s", bindMountString)
}
}
// add arguments for environment variables
environmentVariables := "-e CONTROLPLANE=1"
environmentVariables = environmentVariables + " -e CONTROLPLANE_SERVICE_ID=" + service.Id
environmentVariables = environmentVariables + " -e CONTROLPLANE_TENANT_ID=" + tenantId
environmentVariables = environmentVariables + " -e CONTROLPLANE_CONSUMER_WS=ws://localhost:8444/ws/metrics/store"
environmentVariables = environmentVariables + " -e CONTROLPLANE_CONSUMER_URL=http://localhost:8444/ws/metrics/store"
proxyCmd := fmt.Sprintf("/serviced/%s proxy %s '%s'", binary, service.Id, service.Startup)
// 01 02 03 04 05 06 07 08 09 10 01 02 03 04 05 06 07 08 09 10
cmdString := fmt.Sprintf("docker run %s -rm -name=%s %s -v %s %s %s %s %s %s %s", portOps, serviceState.Id, environmentVariables, volumeBinding, requestedMount, logstashForwarderMount, volumeOpts, configFiles, service.ImageId, proxyCmd)
glog.V(0).Infof("Starting: %s", cmdString)
a.dockerTerminate(serviceState.Id)
a.dockerRemove(serviceState.Id)
cmd := exec.Command("bash", "-c", cmdString)
go a.waitForProcessToDie(conn, cmd, procFinished, serviceState)
glog.V(2).Info("Process started in goroutine")
return true, nil
}
// main loop of the HostAgent
func (a *HostAgent) start() {
glog.V(1).Info("Starting HostAgent")
for {
// create a wrapping function so that client.Close() can be handled via defer
keepGoing := func() bool {
conn, zkEvt, err := zk.Connect(a.zookeepers, time.Second*10)
if err != nil {
glog.V(0).Info("Unable to connect, retrying.")
return true
}
connectEvent := false
for !connectEvent {
select {
case errc := <-a.closing:
glog.V(0).Info("Received shutdown notice")
errc <- errors.New("Unable to connect to zookeeper")
return false
case evt := <-zkEvt:
glog.V(1).Infof("Got ZK connect event: %v", evt)
if evt.State == zk.StateConnected {
connectEvent = true
}
}
}
defer conn.Close() // Executed after lambda function finishes
zzk.CreateNode(zzk.SCHEDULER_PATH, conn)
node_path := zzk.HostPath(a.hostId)
zzk.CreateNode(node_path, conn)
glog.V(0).Infof("Connected to zookeeper node %s", node_path)
return a.processChildrenAndWait(conn)
}()
if !keepGoing {
break
}
}
}
type stateResult struct {
id string
err error
}
// startMissingChildren accepts a zookeeper connection (conn) and a slice of service instance ids (children),
// a map of channels to signal running children stop, and a stateResult channel for children to signal when
// they shutdown
func (a *HostAgent) startMissingChildren(conn *zk.Conn, children []string, processing map[string]chan int, ssDone chan stateResult) {
glog.V(1).Infof("Agent for %s processing %d children", a.hostId, len(children))
for _, childName := range children {
if processing[childName] == nil {
glog.V(2).Info("Agent starting goroutine to watch ", childName)
childChannel := make(chan int, 1)
processing[childName] = childChannel
go a.processServiceState(conn, childChannel, ssDone, childName)
}
}
return
}
func waitForSsNodes(processing map[string]chan int, ssResultChan chan stateResult) (err error) {
for key, shutdown := range processing {
glog.V(1).Infof("Agent signaling for %s to shutdown.", key)
shutdown <- 1
}
// Wait for goroutines to shutdown
for len(processing) > 0 {
select {
case ssResult := <-ssResultChan:
glog.V(1).Infof("Goroutine finished %s", ssResult.id)
if err == nil && ssResult.err != nil {
err = ssResult.err
}
delete(processing, ssResult.id)
}
}
glog.V(0).Info("All service state nodes are shut down")
return
}
func (a *HostAgent) processChildrenAndWait(conn *zk.Conn) bool {
processing := make(map[string]chan int)
ssDone := make(chan stateResult, 25)
hostPath := zzk.HostPath(a.hostId)
for {
children, _, zkEvent, err := conn.ChildrenW(hostPath)
if err != nil {
glog.V(0).Infoln("Unable to read children, retrying.")
time.Sleep(3 * time.Second)
return true
}
a.startMissingChildren(conn, children, processing, ssDone)
select {
case errc := <-a.closing:
glog.V(1).Info("Agent received interrupt")
err = waitForSsNodes(processing, ssDone)
errc <- err
return false
case ssResult := <-ssDone:
glog.V(1).Infof("Goroutine finished %s", ssResult.id)
delete(processing, ssResult.id)
case evt := <-zkEvent:
glog.V(1).Info("Agent event: ", evt)
}
}
}
func (a *HostAgent) processServiceState(conn *zk.Conn, shutdown <-chan int, done chan<- stateResult, ssId string) {
procFinished := make(chan int, 1)
var attached bool
for {
var hss zzk.HostServiceState
hssStats, zkEvent, err := zzk.LoadHostServiceStateW(conn, a.hostId, ssId, &hss)
if err != nil {
errS := fmt.Sprintf("Unable to load host service state %s: %v", ssId, err)
glog.Error(errS)
done <- stateResult{ssId, errors.New(errS)}
return
}
if len(hss.ServiceStateId) == 0 || len(hss.ServiceId) == 0 {
errS := fmt.Sprintf("Service for %s is invalid", zzk.HostServiceStatePath(a.hostId, ssId))
glog.Error(errS)
done <- stateResult{ssId, errors.New(errS)}
return
}
var ss dao.ServiceState
ssStats, err := zzk.LoadServiceState(conn, hss.ServiceId, hss.ServiceStateId, &ss)
if err != nil {
errS := fmt.Sprintf("Host service state unable to load service state %s", ssId)
glog.Error(errS)
// This goroutine is watching a node for a service state that does not
// exist or could not be loaded. We should *probably* delete this node.
hssPath := zzk.HostServiceStatePath(a.hostId, ssId)
err = conn.Delete(hssPath, hssStats.Version)
if err != nil {
glog.Warningf("Unable to delete host service state %s", hssPath)
}
done <- stateResult{ssId, errors.New(errS)}
return
}
var service dao.Service
_, err = zzk.LoadService(conn, ss.ServiceId, &service)
if err != nil {
errS := fmt.Sprintf("Host service state unable to load service %s", ss.ServiceId)
glog.Errorf(errS)
done <- stateResult{ssId, errors.New(errS)}
return
}
glog.V(1).Infof("Processing %s, desired state: %d", service.Name, hss.DesiredState)
switch {
case hss.DesiredState == dao.SVC_STOP:
// This node is marked for death
glog.V(1).Infof("Service %s was marked for death, quitting", service.Name)
if attached {
err = a.terminateAttached(conn, procFinished, &ss)
} else {
err = a.terminateInstance(conn, &ss)
}
done <- stateResult{ssId, err}
return
case attached:
// Something uninteresting happened. Why are we here?
glog.V(1).Infof("Service %s is attached in a child goroutine", service.Name)
case hss.DesiredState == dao.SVC_RUN &&
ss.Started.Year() <= 1 || ss.Terminated.Year() > 2:
// Should run, and either not started or process died
glog.V(1).Infof("Service %s does not appear to be running; starting", service.Name)
attached, err = a.startService(conn, procFinished, ssStats, &service, &ss)
case ss.Started.Year() > 1 && ss.Terminated.Year() <= 1:
// Service superficially seems to be running. We need to attach
glog.V(1).Infof("Service %s appears to be running; attaching", service.Name)
attached, err = a.attachToService(conn, procFinished, &ss, &hss)
default:
glog.V(0).Infof("Unhandled service %s", service.Name)
}
if !attached || err != nil {
errS := fmt.Sprintf("Service state %s unable to start or attach to process", ssId)
glog.V(1).Info(errS)
a.terminateInstance(conn, &ss)
done <- stateResult{ssId, errors.New(errS)}
return
}
glog.V(3).Infoln("Successfully processed state for %s", service.Name)
select {
case <-shutdown:
glog.V(0).Info("Agent goroutine will stop watching ", ssId)
err = a.terminateAttached(conn, procFinished, &ss)
if err != nil {
glog.Errorf("Error terminating %s: %v", service.Name, err)
}
done <- stateResult{ssId, err}
return
case <-procFinished:
glog.V(1).Infof("Process finished %s", ssId)
attached = false
continue
case evt := <-zkEvent:
if evt.Type == zk.EventNodeDeleted {
glog.V(0).Info("Host service state deleted: ", ssId)
err = a.terminateAttached(conn, procFinished, &ss)
if err != nil {
glog.Errorf("Error terminating %s: %v", service.Name, err)
}
done <- stateResult{ssId, err}
return
}
glog.V(1).Infof("Host service state %s received event %v", ssId, evt)
continue
}
}
}
// GetInfo creates a Host object from the host this function is running on.
func (a *HostAgent) GetInfo(unused int, host *dao.Host) error {
hostInfo, err := CurrentContextAsHost("UNKNOWN")
if err != nil {
return err
}
*host = *hostInfo
return nil
}
//SendHostIPs handles the details of sending HostIPResources
type SendHostIPs func(ips dao.HostIPs, unused *int) error
/**
RegisterResources registers resources on the host such as IP addresses with the control plane master.
The duration parameter is how often to register with the master
*/
func (a *HostAgent) RegisterIPResources(duration time.Duration) {
registerFn := func() {
controlClient, err := NewControlClient(a.master)
if err != nil {
glog.Errorf("Could not start ControlPlane client %v", err)
return
}
defer controlClient.Close()
err = registerIPs(a.hostId, controlClient.RegisterHostIPs)
if err != nil {
glog.Errorf("Error registering resources %v", err)
}
}
//do it the first time
registerFn()
tc := time.Tick(duration)
//run in timed loop
for _ = range tc {
registerFn()
}
}
/*
registerIPs does the actual work of determining the IPs on the host. Parameters are the hostId for this host
and the function used to send the found IPs
*/
func registerIPs(hostId string, sendFn SendHostIPs) error {
interfaces, err := net.Interfaces()
if err != nil {
glog.Error("Problem reading interfaces: ", err)
return err
}
hostIPResources := make([]dao.HostIPResource, 0, len(interfaces))
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
glog.Errorf("Problem reading address for interface %s: %s", iface.Name, err)
return err
}
for _, addr := range addrs {
//send address to Master
hostIp := dao.HostIPResource{}
hostIp.IPAddress = addr.String()
hostIp.InterfaceName = iface.Name
hostIPResources = append(hostIPResources, hostIp)
}
}
var unused int
glog.V(4).Infof("Agent registering IPs %v", hostIPResources)
hostIps := dao.HostIPs{}
hostIps.HostId = hostId
hostIps.IPs = hostIPResources
if err := sendFn(hostIps, &unused); err != nil {
glog.Errorf("Error registering IPs %v", err)
}
return nil
}
// *********************************************************************
// ***** FIXME *********************************************************
// ***** The following three functions are also defined in isvc.go *****
// returns serviced home
func serviceDHome() string {
return os.Getenv("SERVICED_HOME")
}
func localDir(p string) string {
homeDir := ServiceDHome()
if len(homeDir) == 0 {
_, filename, _, _ := runtime.Caller(1)
homeDir = path.Join(path.Dir(filename), "isvcs")
}
return path.Join(homeDir, p)
}
func resourcesDir() string {
return localDir("resources")
}
// *********************************************************************