-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNOMAD.patch
2849 lines (2662 loc) · 78.4 KB
/
NOMAD.patch
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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 1c7b7bcdfc1e2ff88084b2a5ff32d15abe568487 Mon Sep 17 00:00:00 2001
From: Pier Luigi Ventre <[email protected]>
Date: Wed, 24 Feb 2016 19:24:30 +0100
Subject: [PATCH 01/15] Changes: - Add Leonardo's code
Warning:
- Make fail, some APIs are changed
---
client/driver/driver.go | 1 +
client/driver/xen.go | 160 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 161 insertions(+)
create mode 100644 client/driver/xen.go
diff --git a/client/driver/driver.go b/client/driver/driver.go
index db808b1..a8d5443 100644
--- a/client/driver/driver.go
+++ b/client/driver/driver.go
@@ -25,6 +25,7 @@ var BuiltinDrivers = map[string]Factory{
"java": NewJavaDriver,
"qemu": NewQemuDriver,
"rkt": NewRktDriver,
+ "xen": NewXenDriver,
}
// NewDriver is used to instantiate and return a new driver
diff --git a/client/driver/xen.go b/client/driver/xen.go
new file mode 100644
index 0000000..be6a4c5
--- /dev/null
+++ b/client/driver/xen.go
@@ -0,0 +1,160 @@
+package driver
+
+import (
+ "fmt"
+ "os/exec"
+ "regexp"
+ "strings"
+ "time"
+
+ "github.com/hashicorp/nomad/client/config"
+ "github.com/hashicorp/nomad/client/driver/executor"
+ cstructs "github.com/hashicorp/nomad/client/driver/structs"
+ "github.com/hashicorp/nomad/client/fingerprint"
+ "github.com/hashicorp/nomad/nomad/structs"
+)
+
+var (
+ reMajVersion = regexp.MustCompile(`xen_major:'d'`)
+ reMinVersion = regexp.MustCompile(`xen_minor:'d'`)
+ reExtVersion = regexp.MustCompile(`xen_extra:.'d'`)
+)
+
+// XenDriver is a driver for running Xen.
+type XenDriver struct {
+ DriverContext
+ fingerprint.StaticFingerprinter
+}
+
+// Configuration for XenDriver
+type XenDriverConfig struct {
+ ArtifactSource string `mapstructure:"artifact_source"`
+ Checksum string `mapstructure:"checksum"`
+ Accelerator string `mapstructure:"accelerator"`
+ PortMap []map[string]int `mapstructure:"port_map"` // A map of host port labels and to guest ports.
+}
+
+// xenHandle is returned from Start/Open as a handle to the PID (identical to qemu and java).
+type xenHandle struct {
+ cmd executor.Executor
+ waitCh chan *cstructs.WaitResult
+ doneCh chan struct{}
+}
+
+// NewXenDriver is used to create a new exec driver (identical to qemu and java).
+func NewXenDriver(ctx *DriverContext) Driver {
+ return &XenDriver{DriverContext: *ctx}
+}
+
+// Return the driver to be used
+func (d *XenDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
+bin := "xl"
+
+ outBytes, err := exec.Command(bin, "info").Output()
+ if err != nil {
+ return false, nil
+ }
+ out := strings.TrimSpace(string(outBytes))
+
+ matches1 := reMajVersion.FindStringSubmatch(out)
+ if len(matches1) != 2 {
+ return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches1)
+ }
+ matches2 := reMinVersion.FindStringSubmatch(out)
+ if len(matches2) != 2 {
+ return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches2)
+ }
+ matches3 := reExtVersion.FindStringSubmatch(out)
+ if len(matches3) != 2 {
+ return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches3)
+ }
+
+ matches := matches1[1]+"."+matches2[1]+"."+matches3[1]
+
+ node.Attributes["driver.xen-hyp"] = "1"
+ node.Attributes["driver.xen-hyp.version"] = matches
+
+ return true, nil
+}
+
+
+// Run an existing Xen image. Start() will pull down an existing, valid Xen
+// image and save it to the Drivers Allocation Dir
+func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
+ /*
+ // Xen defaults to 256M of RAM for a given VM. Instead, we force users to
+ // supply a memory size in the tasks resources
+ if task.Resources == nil || task.Resources.MemoryMB == 0 {
+ return nil, fmt.Errorf("Missing required Task Resource: Memory")
+ }*/
+
+ args := []string{
+ "xl",
+ "create",
+ "/root/clickos.cfg",
+ }
+
+ // Setup the command
+ cmd := executor.Command(args[0], args[1:]...)
+ d.logger.Printf("[DEBUG] Starting XenVM command: %q", strings.Join(args, " "))
+
+ // Create and Return Handle
+ h := &execHandle{
+ cmd: cmd,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
+
+ go h.run()
+ return h, nil
+}
+
+func (d *XenDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
+ // Find the process
+ cmd, err := executor.OpenId(handleID)
+ if err != nil {
+ return nil, fmt.Errorf("failed to open ID %v: %v", handleID, err)
+ }
+
+ // Return a driver handle
+ h := &execHandle{
+ cmd: cmd,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
+ go h.run()
+ return h, nil
+}
+
+func (h *xenHandle) ID() string {
+ id, _ := h.cmd.ID()
+ return id
+}
+
+func (h *xenHandle) WaitCh() chan *cstructs.WaitResult {
+ return h.waitCh
+}
+
+func (h *xenHandle) Update(task *structs.Task) error {
+ // Update is not possible
+ return nil
+}
+
+// Shut-down command
+func (h *xenHandle) Kill() error {
+ h.cmd.Shutdown()
+ select {
+ case <-h.doneCh:
+ return nil
+ case <-time.After(5 * time.Second):
+ return h.cmd.ForceStop()
+ }
+}
+
+// Run command
+func (h *xenHandle) run() {
+ res := h.cmd.Wait()
+ close(h.doneCh)
+ h.waitCh <- res
+ close(h.waitCh)
+}
\ No newline at end of file
--
1.8.1.2
From 945d25eb8805d165ca59a98b5fde03876e857132 Mon Sep 17 00:00:00 2001
From: Pier Luigi Ventre <[email protected]>
Date: Thu, 25 Feb 2016 12:30:39 +0100
Subject: [PATCH 02/15] Changes: - Add configuration files
---
dist/canary_client.hcl | 17 +++++++++++++++++
dist/canary_dev.hcl | 13 +++++++++++++
dist/canary_server.hcl | 16 ++++++++++++++++
3 files changed, 46 insertions(+)
create mode 100644 dist/canary_client.hcl
create mode 100644 dist/canary_dev.hcl
create mode 100644 dist/canary_server.hcl
diff --git a/dist/canary_client.hcl b/dist/canary_client.hcl
new file mode 100644
index 0000000..7631013
--- /dev/null
+++ b/dist/canary_client.hcl
@@ -0,0 +1,17 @@
+# Client has to run on canary
+
+# Increase log verbosity
+log_level = "DEBUG"
+
+# Setup data dir
+data_dir = "/tmp/canary_client"
+
+bind_addr = "160.80.105.4"
+
+# Enable the client
+client {
+ enabled = true
+
+ # This is the endpoint where run the Server
+ servers = ["160.80.103.45:4647"]
+}
\ No newline at end of file
diff --git a/dist/canary_dev.hcl b/dist/canary_dev.hcl
new file mode 100644
index 0000000..b7fe551
--- /dev/null
+++ b/dist/canary_dev.hcl
@@ -0,0 +1,13 @@
+# This deployment is only for dev, we have client and server in the same machine
+
+bind_addr = "160.80.105.4"
+data_dir = "/var/lib/canary_dev"
+
+server {
+ enabled = true
+ bootstrap_expect = 1
+}
+
+client {
+ enabled = true
+}
diff --git a/dist/canary_server.hcl b/dist/canary_server.hcl
new file mode 100644
index 0000000..8c64e2b
--- /dev/null
+++ b/dist/canary_server.hcl
@@ -0,0 +1,16 @@
+# Server has to run in a different endpoint, for example in your laptop
+
+# Increase log verbosity
+log_level = "DEBUG"
+
+# Setup data dir
+data_dir = "/tmp/canary_server"
+
+bind_addr = "160.80.103.45"
+
+server {
+ enabled = true
+
+ # This is necessary for master election. In this case we have auto-proclamation
+ bootstrap_expect = 1
+}
\ No newline at end of file
--
1.8.1.2
From 39ee345cdc37664af6289a51bdfdb0363cb95e72 Mon Sep 17 00:00:00 2001
From: Pier Luigi Ventre <[email protected]>
Date: Thu, 25 Feb 2016 18:32:44 +0100
Subject: [PATCH 03/15] Changes: - Modified build
---
client/driver/driver.go | 2 +-
client/driver/xen.go | 62 ++++++++++++++++++++++++-------------------------
scripts/build.sh | 6 +++--
3 files changed, 36 insertions(+), 34 deletions(-)
diff --git a/client/driver/driver.go b/client/driver/driver.go
index a8d5443..88e54da 100644
--- a/client/driver/driver.go
+++ b/client/driver/driver.go
@@ -25,7 +25,7 @@ var BuiltinDrivers = map[string]Factory{
"java": NewJavaDriver,
"qemu": NewQemuDriver,
"rkt": NewRktDriver,
- "xen": NewXenDriver,
+ "xen": NewXenDriver,
}
// NewDriver is used to instantiate and return a new driver
diff --git a/client/driver/xen.go b/client/driver/xen.go
index be6a4c5..2cd9313 100644
--- a/client/driver/xen.go
+++ b/client/driver/xen.go
@@ -16,8 +16,8 @@ import (
var (
reMajVersion = regexp.MustCompile(`xen_major:'d'`)
- reMinVersion = regexp.MustCompile(`xen_minor:'d'`)
- reExtVersion = regexp.MustCompile(`xen_extra:.'d'`)
+ reMinVersion = regexp.MustCompile(`xen_minor:'d'`)
+ reExtVersion = regexp.MustCompile(`xen_extra:.'d'`)
)
// XenDriver is a driver for running Xen.
@@ -48,8 +48,8 @@ func NewXenDriver(ctx *DriverContext) Driver {
// Return the driver to be used
func (d *XenDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
-bin := "xl"
-
+ bin := "xl"
+
outBytes, err := exec.Command(bin, "info").Output()
if err != nil {
return false, nil
@@ -60,34 +60,34 @@ bin := "xl"
if len(matches1) != 2 {
return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches1)
}
- matches2 := reMinVersion.FindStringSubmatch(out)
- if len(matches2) != 2 {
- return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches2)
- }
- matches3 := reExtVersion.FindStringSubmatch(out)
- if len(matches3) != 2 {
- return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches3)
- }
+ matches2 := reMinVersion.FindStringSubmatch(out)
+ if len(matches2) != 2 {
+ return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches2)
+ }
+ matches3 := reExtVersion.FindStringSubmatch(out)
+ if len(matches3) != 2 {
+ return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches3)
+ }
- matches := matches1[1]+"."+matches2[1]+"."+matches3[1]
+ matches := matches1[1] + "." + matches2[1] + "." + matches3[1]
- node.Attributes["driver.xen-hyp"] = "1"
- node.Attributes["driver.xen-hyp.version"] = matches
+ node.Attributes["driver.xen"] = "1"
+ node.Attributes["driver.xen.version"] = matches
return true, nil
}
-
// Run an existing Xen image. Start() will pull down an existing, valid Xen
// image and save it to the Drivers Allocation Dir
func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
/*
- // Xen defaults to 256M of RAM for a given VM. Instead, we force users to
- // supply a memory size in the tasks resources
- if task.Resources == nil || task.Resources.MemoryMB == 0 {
- return nil, fmt.Errorf("Missing required Task Resource: Memory")
- }*/
-
+ // Xen defaults to 256M of RAM for a given VM. Instead, we force users to
+ // supply a memory size in the tasks resources
+ if task.Resources == nil || task.Resources.MemoryMB == 0 {
+ return nil, fmt.Errorf("Missing required Task Resource: Memory")
+ }*/
+
+
args := []string{
"xl",
"create",
@@ -99,14 +99,14 @@ func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e
d.logger.Printf("[DEBUG] Starting XenVM command: %q", strings.Join(args, " "))
// Create and Return Handle
- h := &execHandle{
- cmd: cmd,
- doneCh: make(chan struct{}),
- waitCh: make(chan *cstructs.WaitResult, 1),
- }
-
- go h.run()
- return h, nil
+ h := &execHandle{
+ cmd: cmd,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
+
+ go h.run()
+ return h, nil
}
func (d *XenDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
@@ -157,4 +157,4 @@ func (h *xenHandle) run() {
close(h.doneCh)
h.waitCh <- res
close(h.waitCh)
-}
\ No newline at end of file
+}
diff --git a/scripts/build.sh b/scripts/build.sh
index 90e6b08..3d07441 100755
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -16,8 +16,10 @@ GIT_COMMIT=$(git rev-parse HEAD)
GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
# Determine the arch/os combos we're building for
-XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
-XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
+#XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
+#XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
+XC_ARCH="386"
+XC_OS=linux
# Delete the old dir
echo "==> Removing old directory..."
--
1.8.1.2
From a37fdf998a8b5da062648a56434670e8cbb12186 Mon Sep 17 00:00:00 2001
From: Pier Luigi Ventre <[email protected]>
Date: Thu, 25 Feb 2016 22:09:19 +0100
Subject: [PATCH 04/15] Changes: - Refactoring of the xen code
---
client/driver/xen.go | 344 ++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 311 insertions(+), 33 deletions(-)
diff --git a/client/driver/xen.go b/client/driver/xen.go
index 2cd9313..b9b5870 100644
--- a/client/driver/xen.go
+++ b/client/driver/xen.go
@@ -1,16 +1,22 @@
package driver
import (
+ "encoding/json"
"fmt"
+ "log"
"os/exec"
+ "path/filepath"
"regexp"
"strings"
"time"
+ "github.com/hashicorp/go-plugin"
+ "github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/executor"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/client/fingerprint"
+ "github.com/hashicorp/nomad/helper/discover"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -34,12 +40,28 @@ type XenDriverConfig struct {
PortMap []map[string]int `mapstructure:"port_map"` // A map of host port labels and to guest ports.
}
-// xenHandle is returned from Start/Open as a handle to the PID (identical to qemu and java).
+// old xenHandle is returned from Start/Open as a handle to the PID (identical to qemu and java).
+
+/*
type xenHandle struct {
cmd executor.Executor
waitCh chan *cstructs.WaitResult
doneCh chan struct{}
}
+*/
+
+// xenHandle is returned from Start/Open as a handle to the PID (identical to qemu)
+// TODO verify if it is ok
+type xenHandle struct {
+ pluginClient *plugin.Client
+ userPid int
+ executor executor.Executor
+ allocDir *allocdir.AllocDir
+ killTimeout time.Duration
+ logger *log.Logger
+ waitCh chan *cstructs.WaitResult
+ doneCh chan struct{}
+}
// NewXenDriver is used to create a new exec driver (identical to qemu and java).
func NewXenDriver(ctx *DriverContext) Driver {
@@ -80,55 +102,263 @@ func (d *XenDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, e
// Run an existing Xen image. Start() will pull down an existing, valid Xen
// image and save it to the Drivers Allocation Dir
func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
+
/*
+ Old code
// Xen defaults to 256M of RAM for a given VM. Instead, we force users to
// supply a memory size in the tasks resources
if task.Resources == nil || task.Resources.MemoryMB == 0 {
return nil, fmt.Errorf("Missing required Task Resource: Memory")
- }*/
+ }
+
+ args := []string{
+ "xl",
+ "create",
+ "/root/clickos.cfg",
+ }
+
+ // Setup the command
+ cmd := executor.Command(args[0], args[1:]...)
+ d.logger.Printf("[DEBUG] Starting XenVM command: %q", strings.Join(args, " "))
+
+ // Create and Return Handle
+ h := &execHandle{
+ cmd: cmd,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
+
+ go h.run()
+ return h, nil
+ */
+ /*var driverConfig QemuDriverConfig
+ if err := mapstructure.WeakDecode(task.Config, &driverConfig); err != nil {
+ return nil, err
+ }
+
+ if len(driverConfig.PortMap) > 1 {
+ return nil, fmt.Errorf("Only one port_map block is allowed in the qemu driver config")
+ }
+
+ // Get the image source
+ source, ok := task.Config["artifact_source"]
+ if !ok || source == "" {
+ return nil, fmt.Errorf("Missing source image Qemu driver")
+ }
+
+ // Qemu defaults to 128M of RAM for a given VM. Instead, we force users to
+ // supply a memory size in the tasks resources
+ if task.Resources == nil || task.Resources.MemoryMB == 0 {
+ return nil, fmt.Errorf("Missing required Task Resource: Memory")
+ }
+ */
+
+ // Get the tasks local directory.
+ taskDir, ok := ctx.AllocDir.TaskDirs[d.DriverContext.taskName]
+ if !ok {
+ return nil, fmt.Errorf("Could not find task directory for task: %v", d.DriverContext.taskName)
+ }
+
+ // Proceed to download an artifact to be executed.
+ /*
+ vmPath, err := getter.GetArtifact(
+ taskDir,
+ driverConfig.ArtifactSource,
+ driverConfig.Checksum,
+ d.logger,
+ )
+ if err != nil {
+ return nil, err
+ }
+
+ vmID := filepath.Base(vmPath)
+
+ // Parse configuration arguments
+ // Create the base arguments
+ accelerator := "tcg"
+ if driverConfig.Accelerator != "" {
+ accelerator = driverConfig.Accelerator
+ }
+ // TODO: Check a lower bounds, e.g. the default 128 of Qemu
+ mem := fmt.Sprintf("%dM", task.Resources.MemoryMB)
+ */
args := []string{
- "xl",
- "create",
- "/root/clickos.cfg",
- }
-
- // Setup the command
- cmd := executor.Command(args[0], args[1:]...)
- d.logger.Printf("[DEBUG] Starting XenVM command: %q", strings.Join(args, " "))
-
+ "xl",
+ "create",
+ "/root/clickos.cfg",
+ }
+
+ /*
+ // Check the Resources required Networks to add port mappings. If no resources
+ // are required, we assume the VM is a purely compute job and does not require
+ // the outside world to be able to reach it. VMs ran without port mappings can
+ // still reach out to the world, but without port mappings it is effectively
+ // firewalled
+ protocols := []string{"udp", "tcp"}
+ if len(task.Resources.Networks) > 0 && len(driverConfig.PortMap) == 1 {
+ // Loop through the port map and construct the hostfwd string, to map
+ // reserved ports to the ports listenting in the VM
+ // Ex: hostfwd=tcp::22000-:22,hostfwd=tcp::80-:8080
+ var forwarding []string
+ taskPorts := task.Resources.Networks[0].MapLabelToValues(nil)
+ for label, guest := range driverConfig.PortMap[0] {
+ host, ok := taskPorts[label]
+ if !ok {
+ return nil, fmt.Errorf("Unknown port label %q", label)
+ }
+
+ for _, p := range protocols {
+ forwarding = append(forwarding, fmt.Sprintf("hostfwd=%s::%d-:%d", p, host, guest))
+ }
+ }
+
+ if len(forwarding) != 0 {
+ args = append(args,
+ "-netdev",
+ fmt.Sprintf("user,id=user.0,%s", strings.Join(forwarding, ",")),
+ "-device", "virtio-net,netdev=user.0",
+ )
+ }
+ }
+
+ // If using KVM, add optimization args
+ if accelerator == "kvm" {
+ args = append(args,
+ "-enable-kvm",
+ "-cpu", "host",
+ // Do we have cores information available to the Driver?
+ // "-smp", fmt.Sprintf("%d", cores),
+ )
+ }*/
+
+ d.logger.Printf("[DEBUG] Starting xenVM command: %q", strings.Join(args, " "))
+ bin, err := discover.NomadExecutable()
+ if err != nil {
+ return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
+ }
+
+ pluginLogFile := filepath.Join(taskDir, fmt.Sprintf("%s-executor.out", task.Name))
+ pluginConfig := &plugin.ClientConfig{
+ Cmd: exec.Command(bin, "executor", pluginLogFile),
+ }
+
+ exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
+ if err != nil {
+ return nil, err
+ }
+ executorCtx := &executor.ExecutorContext{
+ TaskEnv: d.taskEnv,
+ AllocDir: ctx.AllocDir,
+ TaskName: task.Name,
+ TaskResources: task.Resources,
+ LogConfig: task.LogConfig,
+ }
+ t1 := time.Now()
+ ps, err := exec.LaunchCmd(&executor.ExecCommand{Cmd: args[0], Args: args[1:]}, executorCtx)
+ if err != nil {
+ pluginClient.Kill()
+ return nil, fmt.Errorf("error starting process via the plugin: %v", err)
+ }
+ t2 := time.Now()
+ duration := t2.Sub(t1)
+ d.logger.Printf("[INFO] Started new xenVM in:", duration)
+
// Create and Return Handle
- h := &execHandle{
- cmd: cmd,
- doneCh: make(chan struct{}),
- waitCh: make(chan *cstructs.WaitResult, 1),
- }
-
- go h.run()
- return h, nil
+ h := &xenHandle{
+ pluginClient: pluginClient,
+ executor: exec,
+ userPid: ps.Pid,
+ allocDir: ctx.AllocDir,
+ killTimeout: d.DriverContext.KillTimeout(task),
+ logger: d.logger,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
+
+ go h.run()
+ return h, nil
+}
+
+type xenId struct {
+ KillTimeout time.Duration
+ UserPid int
+ PluginConfig *PluginReattachConfig
+ AllocDir *allocdir.AllocDir
}
func (d *XenDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
- // Find the process
- cmd, err := executor.OpenId(handleID)
+ /*
+ Old Process
+ // Find the process
+ cmd, err := executor.OpenId(handleID)
+ if err != nil {
+ return nil, fmt.Errorf("failed to open ID %v: %v", handleID, err)
+ }
+
+ // Return a driver handle
+ h := &execHandle{
+ cmd: cmd,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
+ go h.run()
+ return h, nil
+ */
+
+ id := &xenId{}
+ if err := json.Unmarshal([]byte(handleID), id); err != nil {
+ return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err)
+ }
+
+ pluginConfig := &plugin.ClientConfig{
+ Reattach: id.PluginConfig.PluginConfig(),
+ }
+
+ executor, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
if err != nil {
- return nil, fmt.Errorf("failed to open ID %v: %v", handleID, err)
+ d.logger.Println("[ERROR] driver.xen: error connecting to plugin so destroying plugin pid and user pid")
+ if e := destroyPlugin(id.PluginConfig.Pid, id.UserPid); e != nil {
+ d.logger.Printf("[ERROR] driver.xen: error destroying plugin and userpid: %v", e)
+ }
+ return nil, fmt.Errorf("error connecting to plugin: %v", err)
}
// Return a driver handle
- h := &execHandle{
- cmd: cmd,
- doneCh: make(chan struct{}),
- waitCh: make(chan *cstructs.WaitResult, 1),
+ h := &xenHandle{
+ pluginClient: pluginClient,
+ executor: executor,
+ userPid: id.UserPid,
+ allocDir: id.AllocDir,
+ logger: d.logger,
+ killTimeout: id.KillTimeout,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
}
go h.run()
return h, nil
+
}
func (h *xenHandle) ID() string {
- id, _ := h.cmd.ID()
- return id
+ /*
+ Old code
+ id, _ := h.cmd.ID()
+ return id
+ */
+ id := xenId{
+ KillTimeout: h.killTimeout,
+ PluginConfig: NewPluginReattachConfig(h.pluginClient.ReattachConfig()),
+ UserPid: h.userPid,
+ AllocDir: h.allocDir,
+ }
+
+ data, err := json.Marshal(id)
+ if err != nil {
+ h.logger.Printf("[ERR] driver.xen: failed to marshal ID to JSON: %s", err)
+ }
+ return string(data)
}
func (h *xenHandle) WaitCh() chan *cstructs.WaitResult {
@@ -136,25 +366,73 @@ func (h *xenHandle) WaitCh() chan *cstructs.WaitResult {
}
func (h *xenHandle) Update(task *structs.Task) error {
+ /*
+ Old code
+ // Update is not possible
+ return nil
+ */
+ // Store the updated kill timeout.
+ h.killTimeout = task.KillTimeout
+ h.executor.UpdateLogConfig(task.LogConfig)
+
// Update is not possible
return nil
}
// Shut-down command
func (h *xenHandle) Kill() error {
- h.cmd.Shutdown()
+ /*
+ Old code
+ h.cmd.Shutdown()
+ select {
+ case <-h.doneCh:
+ return nil
+ case <-time.After(5 * time.Second):
+ return h.cmd.ForceStop()
+ }*/
+ if err := h.executor.ShutDown(); err != nil {
+ if h.pluginClient.Exited() {
+ return nil
+ }
+ return fmt.Errorf("executor Shutdown failed: %v", err)
+ }
+
select {
case <-h.doneCh:
return nil
- case <-time.After(5 * time.Second):
- return h.cmd.ForceStop()
+ case <-time.After(h.killTimeout):
+ if h.pluginClient.Exited() {
+ return nil
+ }
+ if err := h.executor.Exit(); err != nil {
+ return fmt.Errorf("executor Exit failed: %v", err)
+ }
+
+ return nil
}
}
// Run command
func (h *xenHandle) run() {
- res := h.cmd.Wait()
+ /*
+ Old code
+ res := h.cmd.Wait()
+ close(h.doneCh)
+ h.waitCh <- res
+ close(h.waitCh)
+ */
+ ps, err := h.executor.Wait()
+ if ps.ExitCode == 0 && err != nil {
+ if e := killProcess(h.userPid); e != nil {
+ h.logger.Printf("[ERROR] driver.xen: error killing user process: %v", e)
+ }
+ if e := h.allocDir.UnmountAll(); e != nil {
+ h.logger.Printf("[ERROR] driver.xen: unmounting dev,proc and alloc dirs failed: %v", e)
+ }
+ }
close(h.doneCh)
- h.waitCh <- res
+ h.waitCh <- &cstructs.WaitResult{ExitCode: ps.ExitCode, Signal: 0, Err: err}
close(h.waitCh)
+ h.pluginClient.Kill()
+
}
--
1.8.1.2
From 7ee1bbb5525fbffa5f89b5712112c795dbf43198 Mon Sep 17 00:00:00 2001
From: canary <canary@debian>
Date: Sat, 27 Feb 2016 21:18:17 +0100
Subject: [PATCH 05/15] Changes: - fix driver fingerprint
---
client/driver/xen.go | 65 ++++++++++++++++++++++++++--------------------------
dist/canary_dev.hcl | 4 +++-
2 files changed, 35 insertions(+), 34 deletions(-)
diff --git a/client/driver/xen.go b/client/driver/xen.go
index b9b5870..d50cacb 100644
--- a/client/driver/xen.go
+++ b/client/driver/xen.go
@@ -21,9 +21,9 @@ import (
)
var (
- reMajVersion = regexp.MustCompile(`xen_major:'d'`)
- reMinVersion = regexp.MustCompile(`xen_minor:'d'`)
- reExtVersion = regexp.MustCompile(`xen_extra:.'d'`)
+ reMajVersion = regexp.MustCompile(`xen_major\s+:\s([1-9])`)
+ reMinVersion = regexp.MustCompile(`xen_minor\s+:\s([1-9])`)
+ reExtVersion = regexp.MustCompile(`xen_extra\s+:\s.([1-9])`)
)
// XenDriver is a driver for running Xen.
@@ -77,18 +77,17 @@ func (d *XenDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, e
return false, nil
}
out := strings.TrimSpace(string(outBytes))
-
matches1 := reMajVersion.FindStringSubmatch(out)
if len(matches1) != 2 {
- return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches1)
+ return false, fmt.Errorf("Unable to parse Xen major version string: %#v", matches1)
}
matches2 := reMinVersion.FindStringSubmatch(out)
if len(matches2) != 2 {
- return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches2)
+ return false, fmt.Errorf("Unable to parse Xen minor version string: %#v", matches2)
}
matches3 := reExtVersion.FindStringSubmatch(out)
if len(matches3) != 2 {
- return false, fmt.Errorf("Unable to parse Xen version string: %#v", matches3)
+ return false, fmt.Errorf("Unable to parse Xen extra version string: %#v", matches3)
}
matches := matches1[1] + "." + matches2[1] + "." + matches3[1]
@@ -104,32 +103,32 @@ func (d *XenDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, e
func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
/*
- Old code
- // Xen defaults to 256M of RAM for a given VM. Instead, we force users to
- // supply a memory size in the tasks resources
- if task.Resources == nil || task.Resources.MemoryMB == 0 {
- return nil, fmt.Errorf("Missing required Task Resource: Memory")
- }
+ Old code
+ // Xen defaults to 256M of RAM for a given VM. Instead, we force users to
+ // supply a memory size in the tasks resources
+ if task.Resources == nil || task.Resources.MemoryMB == 0 {
+ return nil, fmt.Errorf("Missing required Task Resource: Memory")
+ }
- args := []string{
- "xl",
- "create",
- "/root/clickos.cfg",
- }
+ args := []string{
+ "xl",
+ "create",
+ "/root/clickos.cfg",
+ }
- // Setup the command
- cmd := executor.Command(args[0], args[1:]...)
- d.logger.Printf("[DEBUG] Starting XenVM command: %q", strings.Join(args, " "))
+ // Setup the command
+ cmd := executor.Command(args[0], args[1:]...)
+ d.logger.Printf("[DEBUG] Starting XenVM command: %q", strings.Join(args, " "))
- // Create and Return Handle
- h := &execHandle{
- cmd: cmd,
- doneCh: make(chan struct{}),
- waitCh: make(chan *cstructs.WaitResult, 1),
- }
+ // Create and Return Handle
+ h := &execHandle{
+ cmd: cmd,
+ doneCh: make(chan struct{}),
+ waitCh: make(chan *cstructs.WaitResult, 1),
+ }
- go h.run()
- return h, nil
+ go h.run()
+ return h, nil
*/
/*var driverConfig QemuDriverConfig
@@ -185,9 +184,9 @@ func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e
*/
args := []string{
- "xl",
- "create",
- "/root/clickos.cfg",
+ "pwd",
+ " ",
+ //"/root/clickos.cfg",
}
/*
@@ -263,7 +262,7 @@ func (d *XenDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e
}
t2 := time.Now()
duration := t2.Sub(t1)
- d.logger.Printf("[INFO] Started new xenVM in:", duration)
+ d.logger.Printf("[INFO] Started new xenVM in:%v\n", duration)
// Create and Return Handle
h := &xenHandle{