-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
832 lines (719 loc) · 28.2 KB
/
server.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
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/jaypipes/ghw"
"github.com/mackerelio/go-osstat/cpu"
"github.com/mackerelio/go-osstat/memory"
"github.com/mackerelio/go-osstat/network"
"github.com/mackerelio/go-osstat/uptime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
log "github.com/sirupsen/logrus"
"hurracloud.io/agent/disk"
pb "hurracloud.io/agent/proto"
)
var (
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
certFile = flag.String("cert_file", "", "The TLS cert file")
keyFile = flag.String("key_file", "", "The TLS key file")
jsonDBFile = flag.String("json_db_file", "", "A json file containing a list of features")
listen = flag.String("listen", "127.0.0.1", "Which interface IP to listen on")
tmpDir = flag.String("tmp_dir", "", "Where to store temp files (default: system's tmp dircetory)")
port = flag.Int("port", 10000, "The server port")
uid = flag.Int("uid", -1, "Run commands using this user ID")
jawharUid = flag.Int("jawharUid", 1000, "UID to be given access to mounted drives")
verbose = flag.Bool("verbose", false, "Enable verbose logging")
)
type hurraAgentServer struct {
pb.UnimplementedHurraAgentServer
updateState string
}
// Returns list of drives and their partitions
func (s *hurraAgentServer) GetDrives(ctx context.Context, drive *pb.GetDrivesRequest) (*pb.GetDrivesResponse, error) {
log.Tracef("Request to Get Drives")
block, err := ghw.Block()
if err != nil {
error := fmt.Errorf("Error getting block storage info: %v", err)
log.Error(error)
return nil, error
}
log.Trace("Retrieved Block Storage Info:", block)
response := &pb.GetDrivesResponse{}
for _, disk := range block.Disks {
if disk.SerialNumber == "unknown" {
log.Trace("Warning Found Disk with unknown serial number, will skip it: ", disk)
continue
}
log.Trace("Found Disk: ", disk)
drive := &pb.Drive{
Name: disk.Name,
DeviceFile: "/dev/" + disk.Name,
SizeBytes: disk.SizeBytes,
IsRemovable: disk.IsRemovable,
Type: disk.DriveType.String(),
SerialNumber: disk.SerialNumber,
Vendor: disk.Vendor,
StorageController: disk.StorageController.String(),
}
response.Drives = append(response.Drives, drive)
var index uint32 = 0
for _, partition := range disk.Partitions {
log.Trace("Found Partition: ", partition)
if partition.MountPoint == "" {
// last-resort attempt to find mountpoint if ghw fails to detect it
cmd := exec.Command("lsblk", "/dev/"+partition.Name, "-o", "MOUNTPOINT", "-n")
output, err := cmd.Output()
if err == nil {
partition.MountPoint = strings.Trim(string(output), "\n")
}
}
partition := &pb.Partition{
Index: index,
Name: partition.Name,
DeviceFile: "/dev/" + partition.Name,
SizeBytes: partition.SizeBytes,
Filesystem: partition.Type,
MountPoint: partition.MountPoint,
IsReadOnly: partition.IsReadOnly,
AvailableBytes: 0,
}
index++
// Determine available space (if partition is mounted, no way to know otherwise)
if partition.MountPoint != "" {
log.Tracef("Partition %s is mounted at '%s'. Attempting to find free space.", partition.Name, partition.MountPoint)
cmd := exec.Command("sh", "-c", fmt.Sprintf("df %s | tail -n +2 | awk '{print $4}'", partition.MountPoint))
output, err := cmd.Output()
if err == nil {
freespace, err := strconv.ParseUint(strings.Trim(strings.Trim(string(output), "Gi\n"), "G"), 10, 64)
if err == nil {
partition.AvailableBytes = freespace * uint64(math.Pow(1024, 3))
log.Tracef("Determined Available Bytes for %v to be %v", partition.Name, partition.AvailableBytes)
} else {
log.Errorf("Could not parse df command output: %s: %s", output, err)
}
} else {
log.Errorf("Could not determine mounted partition free space %v", err)
}
}
if partition.Filesystem == "" {
//ghw does not return Filesystem for unmounted partition, attempt to find it on our own
log.Trace("Attempt to find Filesystem for Partition ", partition.Name)
cmd := exec.Command("sh", "-c", fmt.Sprintf("lsblk -o fstype %s | tail -n +2", partition.DeviceFile))
output, err := cmd.Output()
if err == nil {
partition.Filesystem = strings.Trim(string(output), "\n")
log.Tracef("Determined Filesystem of %v is %v", partition.Name, partition.Filesystem)
}
}
// ghw does not return labels, attempt to find it on our own
log.Trace("Attempt to find Label for Partition ", partition.Name)
cmd := exec.Command("sh", "-c", fmt.Sprintf("lsblk -o label %s | tail -n +2", partition.DeviceFile))
output, err := cmd.Output()
if err == nil {
partition.Label = strings.Trim(string(output), "\n")
log.Tracef("Determined Label of %v is %v", partition.Name, partition.Label)
}
drive.Partitions = append(drive.Partitions, partition)
}
}
return response, nil
}
// Get system stats such as uptime, cpu load, memory,..etc.
func (s *hurraAgentServer) GetSystemStats(ctx context.Context, drive *pb.GetSystemStatsRequest) (*pb.GetSystemStatsResponse, error) {
log.Debug("Request to Get System Stats")
response := &pb.GetSystemStatsResponse{}
var networks []network.Stats
// Memory stats
memory, err := memory.Get()
if err == nil {
response.MemoryTotal = memory.Total
response.MemoryCached = memory.Cached
response.MemoryFree = memory.Free
}
// CPU usage %
var total float64
var load float64
var after *cpu.Stats
before, err := cpu.Get()
if err != nil {
log.Errorf("Could not get cpu stats, skipping: %s", err)
goto network_stats
}
time.Sleep(time.Duration(1) * time.Second)
after, err = cpu.Get()
if err != nil {
log.Errorf("Could not get cpu stats, skipping: %s", err)
goto network_stats
}
total = float64(after.Total - before.Total)
load = float64(after.User-before.User) + float64(after.System-before.System)
response.LoadAverage = load / total * 100
network_stats:
// Network stats
networks, err = network.Get()
if err != nil {
log.Errorf("Could not get network stats, skipping: %s", err)
goto disk_stats
}
for _, net := range networks {
response.NetworkReceived += net.RxBytes
response.NetworkSent += net.TxBytes
}
// Disk stats
disk_stats:
disk, err := disk.Get()
if err == nil {
log.Debugf("Library returned %v", disk)
response.DiskReads += disk.ReadsPerSecond
response.DiskWrites += disk.WritesPerSecond
} else {
log.Errorf("Could not get disk stats, skipping it: %s", err)
}
// Uptime
up, err := uptime.Get()
if err == nil {
response.Uptime = up.Seconds()
}
return response, err
}
// Mount specified device on specified mount point.
// Create mount point if not already exists
// Grant read/write access to non-root users (jawharUid)
func (s *hurraAgentServer) MountDrive(ctx context.Context, drive *pb.MountDriveRequest) (*pb.MountDriveResponse, error) {
log.Info("Request to Mount %s at %s", drive.DeviceFile, drive.MountPoint)
response := &pb.MountDriveResponse{IsSuccessful: true}
var error, err error
// Create mount point if it does not exist
_, err = os.Stat(drive.MountPoint)
if os.IsNotExist(err) {
log.Printf("Mount point %s does not exist. Creating it now.", drive.MountPoint)
errDir := os.MkdirAll(drive.MountPoint, 0755)
if errDir != nil {
log.Errorf("Failed to create mount point: %s (%s)", drive.MountPoint, errDir)
return nil, errDir
}
}
// Attempt to mount using default options
log.Infof("Attempting to mount with %s", drive.DeviceFile)
// First try with default options
var options string
if drive.Filesystem == "ntfs" || drive.Filesystem == "ext3" {
options = fmt.Sprintf("umask=0022,uid=%d,gid=%d", *jawharUid, *jawharUid)
}
_, err = exec.Command("mount", "-o", options, drive.DeviceFile, drive.MountPoint).Output()
if err != nil {
// Default options failed
log.Errorf("Failed to mount %s: %s", drive.DeviceFile, err)
return nil, err
}
// Let's add to fstab
// Grab entry from mtab and copy it for fstab
cmd := fmt.Sprintf("cat /etc/mtab | grep %s", drive.MountPoint)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
log.Errorf("Failed to determine mtab entry: %s", cmd)
return nil, fmt.Errorf("Failed to determine mtab entry: %s", cmd)
}
if drive.Filesystem != "ntfs" {
errDir := os.Chmod(drive.MountPoint, 0755)
if errDir != nil {
log.Errorf("Failed to update permissions on mount point %s to uid %d: %v", drive.MountPoint, *jawharUid, errDir)
return nil, errDir
}
}
f, err := os.OpenFile("/etc/fstab", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Errorf("Failed to open fstab: %s", err)
return nil, err
}
defer f.Close()
if _, err := f.WriteString(fmt.Sprintf("%s\n", out)); err != nil {
log.Errorf("Failed to write to fstab: %s", err)
return nil, err
}
return response, error
}
// Mount specified device on specified mount point.
// Create mount point if not already exists
// Grant read/write access to non-root users (jawharUid)
func (s *hurraAgentServer) UnmountDrive(ctx context.Context, drive *pb.UnmountDriveRequest) (*pb.UnmountDriveResponse, error) {
log.Infof("Request to Unmount %s", drive.MountPoint)
response := &pb.UnmountDriveResponse{IsSuccessful: true}
var error, err error
// Attempt to mount using default options
log.Infof("Attempting to unmount %s", drive.MountPoint)
err = syscall.Unmount(drive.MountPoint, 0)
if err != nil {
log.Printf("Failed to unmount %s: %s", drive.MountPoint, err)
return nil, err
}
// Let's remove from fstab
f, err := os.OpenFile("/etc/fstab", os.O_RDONLY, 0644)
if err != nil {
log.Errorf("Failed to open fstab for reading: %s", err)
return nil, err
}
fstabLines, err := ioutil.ReadAll(f)
if err != nil {
log.Errorf("Failed to read fstab: %s", err)
return nil, err
}
re := regexp.MustCompile(fmt.Sprintf("(?m)[\r\n]+^.*%s.*$", strings.ReplaceAll(drive.MountPoint, "/", "\\/")))
newFstab := re.ReplaceAll(fstabLines, []byte(""))
err = f.Close()
if err != nil {
log.Errorf("Failed to close fstab read file descriptor: %s", err)
return nil, err
}
f, err = os.OpenFile("/etc/fstab", os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Errorf("Failed to open fstab for writing: %s", err)
return nil, err
}
defer f.Close()
log.Debugf("Writing to /etc/fstab: %s", fstabLines)
f.Write(newFstab)
if err != nil {
log.Errorf("Failed to write new entry to fstab: %s", err)
return nil, err
}
return response, error
}
// LoadImage load container image.
func (s *hurraAgentServer) LoadImage(ctx context.Context, req *pb.LoadImageRequest) (*pb.LoadImageResponse, error) {
log.Debugf("Downloading image %s", req.URL)
tmpDirectory, err := filepath.Abs(*tmpDir)
if err != nil {
return nil, fmt.Errorf("Could not determine absolute path for temp directory: %s: %v", *tmpDir, err)
}
// Open tmp file for writing image to
if _, err := os.Stat(tmpDirectory); os.IsNotExist(err) {
err := os.MkdirAll(tmpDirectory, 0755)
if err != nil {
return nil, fmt.Errorf("Could not create temp directory: %s: %v", tmpDirectory, err)
}
}
img, err := ioutil.TempFile(tmpDirectory, "image")
if err != nil {
return nil, fmt.Errorf("Could not create temp file: %s", err)
}
log.Debugf("Writing to %s", img.Name())
defer img.Close()
defer os.Remove(img.Name())
// Open image url
httpReq, err := http.NewRequest("GET", req.URL, nil)
if err != nil {
log.Errorf("Error opening HTTP request: %s", err)
return nil, fmt.Errorf("Error opening HTTP request: %s", err)
}
httpReq.SetBasicAuth(req.Username, req.Password)
client := &http.Client{}
resp, err := client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("Could not open URL: %s: %s", req.URL, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Image server returned bad status: %s", resp.Status)
}
_, err = io.Copy(img, resp.Body)
if err != nil {
return nil, fmt.Errorf("Error downloading image: %s: %s", req.URL, err)
}
// Load image in docker daemon
log.Debugf("Loading image in Docker")
cmd := exec.Command("docker", "load", "-i", img.Name())
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to load image. Command Output: %s", strOut)
return nil, fmt.Errorf("Error loading image: %s", err)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.LoadImageResponse{}, nil
}
func (s *hurraAgentServer) UnloadImage(ctx context.Context, req *pb.UnloadImageRequest) (*pb.UnloadImageResponse, error) {
log.Debugf("Unloading image %s", req.Tag)
// Load image in docker daemon
cmd := exec.Command("docker", "rmi", req.Tag)
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
if !strings.Contains(strOut, "No such image") {
log.Errorf("Failed to remove image. Command Output: %s", strOut)
return nil, fmt.Errorf("Error unloading image: %s", err)
} else {
log.Warningf("Image %s already did not exist", req.Tag)
}
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.UnloadImageResponse{}, nil
}
// Run single container.
func (s *hurraAgentServer) RunContainer(ctx context.Context, req *pb.RunContainerRequest) (*pb.RunContainerResponse, error) {
log.Debugf("Running container image %s with name %s and port mapping %d:%d", req.Image, req.Name, req.PortMappingSource, req.PortMappingTarget)
// Start containers
log.Debugf("Run containers")
cmdArgs := []string{"run", "--rm", "-d", "--name", req.Name,
"-p", fmt.Sprintf("%d:%d", req.PortMappingSource, req.PortMappingTarget),
"--add-host", "host.docker.internal:host-gateway"}
for _, env := range strings.Split(req.Env, ",") {
cmdArgs = append(cmdArgs, "-e", env)
}
cmdArgs = append(cmdArgs, req.Image)
cmd := exec.Command("docker", cmdArgs...)
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to start container. Command Output: %s", strOut)
return nil, fmt.Errorf("Error starting containers: %s. Output: %s", err, strOut)
}
log.Debugf("Container Started. Output: '%s'", strOut)
// Connect container to app network
if false {
//TODO: Re-enable if there's value in connecting UI app to app containers network
log.Debugf("Connect container to app network")
cmd = exec.Command("docker", "network", "connect", fmt.Sprintf("%s_%s", req.Name, req.Name), req.Name)
out, err = cmd.CombinedOutput()
strOut = strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to connect container to network. Command Output: %s", strOut)
return nil, fmt.Errorf("Error starting containers: %s. Output: %s", err, strOut)
}
log.Debugf("Container network connected. Output: '%s'", strOut)
}
return &pb.RunContainerResponse{}, nil
}
// Kill single container
func (s *hurraAgentServer) KillContainer(ctx context.Context, req *pb.KillContainerRequest) (*pb.KillContainerResponse, error) {
log.Debugf("Killing container %s", req.Name)
// Kill container
cmd := exec.Command("docker", "kill", req.Name)
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if strings.Contains(strOut, "is not running") {
log.Warningf("Container %s was stoppe. Removing it.", req.Name)
cmd := exec.Command("docker", "rm", req.Name)
out, err = cmd.CombinedOutput()
strOut = strings.Replace(string(out), "\n", " ", -1)
}
if err != nil {
if !strings.Contains(strOut, "No such container") {
log.Errorf("Failed to kill container. Command Output: %s", strOut)
return nil, fmt.Errorf("Error starting containers: %s. Output: %s", err, strOut)
} else {
log.Warningf("Container %s did not exist or was already killed", req.Name)
}
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.KillContainerResponse{}, nil
}
// Run container spec (docker compose file).
func (s *hurraAgentServer) RunContainerSpec(ctx context.Context, req *pb.ContainerSpecRequest) (*pb.ContainerSpecResponse, error) {
log.Debugf("Running containers at root context %s", req.Context)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
// Create file to write componse file contents to
err := ioutil.WriteFile(path.Join(req.Context, composeFilename), []byte(req.Spec), 0644)
if err != nil {
return nil, fmt.Errorf("Could not write to compose file: %s", err)
}
// Start containers
log.Debugf("Start containers")
cmd := exec.Command("docker-compose", "-f", composeFilename, "-p", req.Name, "up", "-d")
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to start containers. Command Output: %s", strOut)
return nil, fmt.Errorf("Error starting containers: %s. Output: %s", err, strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.ContainerSpecResponse{}, nil
}
// Find port binding in container spec
func (s *hurraAgentServer) GetContainerPortBindingInSpec(ctx context.Context, req *pb.ContainerPortBindingInSpecRequest) (*pb.ContainerPortBindingInSpecResponse, error) {
log.Debugf("Determing port binding of container %s/%s/%d", req.Name, req.ContainerName, req.ContainerPort)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
// Open tmp file for writing image to
err := ioutil.WriteFile(path.Join(req.Context, composeFilename), []byte(req.Spec), 0644)
if err != nil {
return nil, fmt.Errorf("Could not write to compose file: %s", err)
}
// Start containers
log.Debugf("Start containers")
cmd := exec.Command("docker-compose", "-f", composeFilename, "-p", req.Name, "port", req.ContainerName, fmt.Sprintf("%d", req.ContainerPort))
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
strOut = strings.TrimSpace(strOut)
log.Debugf("Running %v", cmd)
if err != nil {
log.Errorf("Failed to start containers. Command Output: %s", strOut)
return nil, fmt.Errorf("Error starting containers: %s. Output: %s", err, strOut)
}
portMapping := strings.Split(strOut, ":")
if len(portMapping) != 2 {
log.Errorf("Unexpected output from docker-compose port command (%d). Command Output: %s", len(portMapping), strOut)
return nil, fmt.Errorf("Error determining port: Unexpected output from docker-compose port command. Command Output: %s", strOut)
}
port, err := strconv.Atoi(portMapping[1])
if err != nil {
log.Errorf("Failed to parse docker port output: %s: %s", strOut, err)
return nil, fmt.Errorf("Failed to parse docker port output: %s: %s", strOut, err)
}
log.Debugf("Done. Output: '%s'. Port: %d", strOut, port)
return &pb.ContainerPortBindingInSpecResponse{PortBinding: uint32(port)}, nil
}
// Stop containers
func (s *hurraAgentServer) StopContainerSpec(ctx context.Context, req *pb.ContainerSpecRequest) (*pb.ContainerSpecResponse, error) {
log.Debugf("Running containers at root context %s", req.Context)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
// Start containers
log.Debugf("Stop containers")
cmd := exec.Command("docker-compose", "-f", composeFilename, "-p", req.Name, "stop")
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to stop containers. Command Output: %s", strOut)
return nil, fmt.Errorf("Error stopping containers: %s. Output: %s", err, strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.ContainerSpecResponse{}, nil
}
// Remove containers
func (s *hurraAgentServer) RemoveContainerSpec(ctx context.Context, req *pb.ContainerSpecRequest) (*pb.ContainerSpecResponse, error) {
log.Debugf("Running containers at root context %s", req.Context)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
// Remove containers
log.Debugf("Stop containers")
cmd := exec.Command("docker-compose", "-f", composeFilename, "-p", req.Name, "down", "-v")
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to remove containers. Command Output: %s", strOut)
return nil, fmt.Errorf("Error remove containers: %s. Output: %s", err, strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.ContainerSpecResponse{}, nil
}
// Exec command in a container
func (s *hurraAgentServer) ExecInContainerSpec(ctx context.Context, req *pb.ExecInContainerSpecRequest) (*pb.ExecInContainerSpecResponse, error) {
log.Debugf("Exec command request. Container=%s. Command=%s, Env=%s", req.ContainerName, req.Cmd, req.Env)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
cmdArgs := []string{"-f", composeFilename, "-p", req.Name, "exec"}
// Prepand -e KEY=VAL before exec command arg
envMap := make(map[string]interface{})
err := json.Unmarshal([]byte(req.Env), &envMap)
if err != nil {
log.Errorf("Error parsing env map: %s", err)
return nil, fmt.Errorf("Error parsing env map: %s: %s", req.Env, err)
}
for key, val := range envMap {
cmdArgs = append(cmdArgs, "-e", fmt.Sprintf("%s=%s", key, val))
}
// Append -T containerName COMMAND
cmdArgs = append(cmdArgs, "-T", req.ContainerName, req.Cmd)
// Append command ARGS
var argList []string
err = json.Unmarshal([]byte(req.Args), &argList)
if err != nil {
log.Errorf("Error parsing args list: %s", err)
return nil, fmt.Errorf("Error parsing args list: %s: %s", req.Args, err)
}
for _, val := range argList {
cmdArgs = append(cmdArgs, val)
}
cmd := exec.Command("docker-compose", cmdArgs...)
log.Debugf("Executing in container %s/%s: %v", req.Cmd, req.Name, req.ContainerName, cmd)
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to exec in container. Command Output: %s", strOut)
return nil, fmt.Errorf("Error exec in container: %s. Output: %s", err, strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.ExecInContainerSpecResponse{Output: string(out)}, nil
}
// Stop a specific container in a container spec file
func (s *hurraAgentServer) StopContainerInSpec(ctx context.Context, req *pb.ContainerSpecRequest) (*pb.ContainerSpecResponse, error) {
log.Debugf("Stop container in sepc request. Spec=%s. Container=%s", req.Name, req.ContainerName)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
cmd := exec.Command("docker-compose", "-f", composeFilename, "-p", req.Name, "stop", req.ContainerName)
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to stop container. Command Output: %s", strOut)
return nil, fmt.Errorf("Error stopping container: %s. Output: %s", err, strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.ContainerSpecResponse{}, nil
}
// Start a specific container in a container spec file
func (s *hurraAgentServer) StartContainerInSpec(ctx context.Context, req *pb.ContainerSpecRequest) (*pb.ContainerSpecResponse, error) {
log.Debugf("Start container in sepc request. Spec=%s. Container=%s", req.Name, req.ContainerName)
composeFilename := fmt.Sprintf("%s.yaml", req.Name)
cmd := exec.Command("docker-compose", "-f", composeFilename, "-p", req.Name, "start", req.ContainerName)
cmd.Dir = req.Context
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to start container. Command Output: %s", strOut)
return nil, fmt.Errorf("Error stopping container: %s. Output: %s", err, strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.ContainerSpecResponse{}, nil
}
// ExecCommand returns the feature at the given point.
func (s *hurraAgentServer) ExecCommand(ctx context.Context, command *pb.Command) (*pb.Result, error) {
cmd := exec.Command("bash", "-c", command.Command)
cmd.SysProcAttr = &syscall.SysProcAttr{}
if *uid != -1 {
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(*uid), Gid: uint32(*uid)}
}
out, err := cmd.CombinedOutput()
exitCode := int32(0)
if err != nil {
out = []byte(err.Error())
exitCode = 1
}
log.Printf("Command: %s. Output: %s", command.Command, out)
result := &pb.Result{Message: string(out), ExitCode: exitCode}
return result, nil
}
// UpdateSystem updates system image to specified version using mender
func (s *hurraAgentServer) UpdateSystem(ctx context.Context, req *pb.UpdateSystemRequest) (*pb.UpdateSystemResult, error) {
log.Infof("Update state is: %s", s.updateState)
if s.updateState == "in-progress" {
// update is already in progress
return nil, fmt.Errorf("update in-progress")
}
tmpDirectory, err := filepath.Abs(*tmpDir)
if err != nil {
return nil, fmt.Errorf("Could not determine absolute path for temp directory: %s: %v", *tmpDir, err)
}
log.Infof("Downloading image %s [%s] to %s", req.ImageUrl, req.Hash, tmpDirectory)
// Open tmp file for writing image to
if _, err := os.Stat(tmpDirectory); os.IsNotExist(err) {
err := os.MkdirAll(tmpDirectory, 0755)
if err != nil {
return nil, fmt.Errorf("Could not create temp directory: %s: %v", tmpDirectory, err)
}
}
img, err := ioutil.TempFile(tmpDirectory, "hurraos.mender")
if err != nil {
return nil, fmt.Errorf("Could not create temp file: %s", err)
}
log.Debugf("Writing to %s", img.Name())
// Open image url
httpReq, err := http.NewRequest("GET", req.ImageUrl, nil)
if err != nil {
log.Errorf("Error opening HTTP request: %s", err)
return nil, fmt.Errorf("Error opening HTTP request: %s", err)
}
httpReq.SetBasicAuth(req.Username, req.Password)
client := &http.Client{}
resp, err := client.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("Could not open URL: %s: %s", req.ImageUrl, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Image server returned bad status: %s", resp.Status)
}
_, err = io.Copy(img, resp.Body)
if err != nil {
return nil, fmt.Errorf("Error downloading image: %s: %s", req.ImageUrl, err)
}
// Check sha sum
img.Close()
img, err = os.Open(img.Name())
if err != nil {
return nil, fmt.Errorf("Error opening downloaded img for integrity validation: %s: %v", img.Name(), err)
}
hasher := sha1.New()
_, err = io.Copy(hasher, img)
if err != nil {
return nil, fmt.Errorf("Error computing hash of donwloaded image: %s", err)
}
sha1sum := hex.EncodeToString(hasher.Sum(nil))
if sha1sum != req.Hash {
log.Errorf("Download image's SHA1 sum does not match expected SHA1 sum: %s != %s", sha1sum, req.Hash)
}
log.Infof("SHA1 sum check successful")
// Execute install
go func(s *hurraAgentServer) {
s.updateState = "in-progress"
log.Debugf("Updating system using mender")
cmd := exec.Command("mender", "install", img.Name())
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to install image. Command Output: %s", strOut)
s.updateState = "error"
}
log.Debugf("Done. Output: '%s'", strOut)
s.updateState = "success"
}(s)
return &pb.UpdateSystemResult{}, nil
}
// UpdateSystemStatus updates system image to specified version using mender
func (s *hurraAgentServer) UpdateSystemStatus(ctx context.Context, req *pb.UpdateSystemStatusRequest) (*pb.UpdateSystemResult, error) {
cmd := exec.Command("mender", "show-artifact")
out, err := cmd.CombinedOutput()
strOut := strings.Replace(string(out), "\n", " ", -1)
if err != nil {
log.Errorf("Failed to checking current version. Command Output: %s", strOut)
return nil, fmt.Errorf("Failed to checking current version. Command Output: %s", strOut)
}
log.Debugf("Done. Output: '%s'", strOut)
return &pb.UpdateSystemResult{Status: s.updateState, Version: strOut}, nil
}
func newServer() *hurraAgentServer {
s := &hurraAgentServer{}
return s
}
func main() {
flag.Parse()
if *verbose {
log.SetLevel(log.TraceLevel)
}
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *listen, *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
var opts []grpc.ServerOption
if *tls {
creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
if err != nil {
log.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
log.Infof("Starting server on %s:%d", *listen, *port)
grpcServer := grpc.NewServer(opts...)
pb.RegisterHurraAgentServer(grpcServer, newServer())
grpcServer.Serve(lis)
}