-
Notifications
You must be signed in to change notification settings - Fork 2
/
container.go
846 lines (685 loc) · 21.6 KB
/
container.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
package container
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/hpcloud/garden-hcs/tar_utils"
"github.com/hpcloud/garden-hcs/windows_client"
"github.com/hpcloud/garden-hcs/windows_containers"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/lager"
"github.com/Microsoft/hcsshim"
)
type WindowsContainerSpec struct {
garden.ContainerSpec
}
type UndefinedPropertyError struct {
Key string
}
func (err UndefinedPropertyError) Error() string {
return fmt.Sprintf("property does not exist: %s", err.Key)
}
type WindowsContainer struct {
WindowsContainerSpec
id string
handle string
logger lager.Logger
hostIP string
hcsContainer hcsshim.Container
natEndpointId string
pids map[int]*windows_client.ProcessTracker
hostPort int
containerPort int
containerIp string
portMappings []garden.PortMapping
bindMounts []garden.BindMount
runMutex sync.Mutex
startMutex sync.Mutex
propertiesMutex sync.RWMutex
driverInfo hcsshim.DriverInfo
active int
layerChain []string
volumeName string
layerFolderPath string
}
func NewContainer(id, handle string, containerSpec garden.ContainerSpec, logger lager.Logger, hostIP string, driverInfo hcsshim.DriverInfo, baseImagePath string) (*WindowsContainer, error) {
logger.Debug("WC: NewContainer")
result := &WindowsContainer{
id: id,
handle: handle,
hostIP: hostIP,
logger: logger,
pids: make(map[int]*windows_client.ProcessTracker),
driverInfo: driverInfo,
active: 0,
bindMounts: containerSpec.BindMounts,
}
result.Env = containerSpec.Env
// The rootfs we need to use is the scheme from the
result.RootFSPath = containerSpec.RootFSPath
result.WindowsContainerSpec.Properties = containerSpec.Properties
// We need to get an available port and map it now.
// This is because there is currently no way to change network settings
// for a compute system that's already been created.
result.hostPort = freeTcp4Port()
layerChain, err := windows_containers.GetLayerChain(baseImagePath)
result.layerChain = layerChain
if err != nil {
return nil, err
}
layerFolderPath, volumePath, err := windows_containers.CreateAndActivateContainerLayer(driverInfo, id, layerChain)
if err != nil {
return nil, err
}
result.volumeName = volumePath
result.layerFolderPath = layerFolderPath
return result, nil
}
func (container *WindowsContainer) Handle() string {
container.logger.Debug("WC: Handle")
return container.handle
}
// Stop stops a container.
//
// If kill is false, garden stops a container by sending the processes running inside it the SIGTERM signal.
// It then waits for the processes to terminate before returning a response.
// If one or more processes do not terminate within 10 seconds,
// garden sends these processes the SIGKILL signal, killing them ungracefully.
//
// If kill is true, garden stops a container by sending the processing running inside it a SIGKILL signal.
//
// Once a container is stopped, garden does not allow spawning new processes inside the container.
// It is possible to copy files in to and out of a stopped container.
// It is only when a container is destroyed that its filesystem is cleaned up.
//
// Errors:
// * None.
func (container *WindowsContainer) Stop(kill bool) error {
const shutdownTimeout = time.Minute * 5
const terminateTimeout = time.Minute * 5
var err error
container.logger.Debug("WC: Stop")
container.runMutex.Lock()
defer container.runMutex.Unlock()
// https://github.com/docker/docker/blob/cf58eb437c4229e876f2d952a228b603a074e584/libcontainerd/container_windows.go#L281-L318
if container.hcsContainer != nil {
if kill == true {
// Terminate the compute system
err = container.hcsContainer.Terminate()
if hcsshim.IsPending(err) {
err = container.hcsContainer.WaitTimeout(terminateTimeout)
if err != nil {
container.logger.Error("hcsContainer.WaitTimeout error", err)
}
} else if !hcsshim.IsAlreadyStopped(err) {
container.logger.Error("hcsContainer.Terminate error", err)
return err
}
} else {
err = container.hcsContainer.Shutdown()
if hcsshim.IsPending(err) {
err = container.hcsContainer.WaitTimeout(shutdownTimeout)
if err != nil {
container.logger.Error("hcsContainer.WaitTimeout error", err)
}
} else if !hcsshim.IsAlreadyStopped(err) {
container.logger.Error("hcsContainer.Shutdown error", err)
// Call terminate if shutdown fails
err = container.hcsContainer.Terminate()
if hcsshim.IsPending(err) {
err = container.hcsContainer.WaitTimeout(terminateTimeout)
if err != nil {
container.logger.Error("hcsContainer.WaitTimeout error", err)
}
} else if !hcsshim.IsAlreadyStopped(err) {
container.logger.Error("hcsContainer.Terminate error", err)
}
return err
}
}
err = container.hcsContainer.Close()
if err != nil {
container.logger.Error("hcsContainer.Close error", err)
return err
}
container.hcsContainer = nil
}
return nil
}
func (container *WindowsContainer) Destroy() error {
var err error
container.logger.Debug("WC: Destroy")
container.Stop(true)
container.runMutex.Lock()
defer container.runMutex.Unlock()
if container.natEndpointId != "" {
_, err = hcsshim.HNSEndpointRequest("DELETE", container.natEndpointId, "")
if err != nil {
container.logger.Error("hcsshim.HNSEndpointRequest error", err)
} else {
container.natEndpointId = ""
}
}
// Deactivate our layer
err = hcsshim.DeactivateLayer(container.driverInfo, container.id)
if err != nil {
container.logger.Error("hcsshim.DeactivateLayer error", err)
}
// Destroy the sandbox layer
err = hcsshim.DestroyLayer(container.driverInfo, container.id)
if err != nil {
container.logger.Error("hcsshim.DestroyLayer error", err)
}
return err
}
// Returns information about a container.
func (container *WindowsContainer) Info() (garden.ContainerInfo, error) {
container.logger.Debug("WC: Info")
properties, _ := container.Properties()
processIDs := []string{}
for _, pt := range container.pids {
processIDs = append(processIDs, pt.ID())
}
result := garden.ContainerInfo{
State: "active",
ExternalIP: container.hostIP,
HostIP: container.hostIP,
ContainerIP: container.containerIp,
Events: []string{},
ProcessIDs: processIDs,
Properties: properties,
MappedPorts: container.portMappings,
}
return result, nil
}
// StreamIn streams data into a file in a container.
//
// Errors:
// * TODO.
func (container *WindowsContainer) StreamIn(spec garden.StreamInSpec) error {
container.logger.Debug("WC: StreamIn")
container.runMutex.Lock()
defer container.runMutex.Unlock()
// Write the tar stream to the directory
outDir := filepath.Join(container.volumeName, spec.Path)
err := tar_utils.MkdirAll(container.volumeName, spec.Path, os.FileMode(0755))
if err != nil {
return err
}
return tar_utils.Untar(spec.TarStream, outDir)
}
// StreamOut streams a file out of a container.
//
// Errors:
// * TODO.
func (container *WindowsContainer) StreamOut(spec garden.StreamOutSpec) (io.ReadCloser, error) {
container.logger.Debug("TODO: StreamOut")
// TODO: investigate a proper implementation
// It is unclear if it's ok to keep the container mounted until the reader
// is closed.
tarReaderPipe, tarWriterPipe := io.Pipe()
// Write the tar stream to the directory
sourceDir := filepath.Join(container.volumeName, spec.Path)
go func() {
tar_utils.Tarit(sourceDir, tarWriterPipe)
}()
return tarReaderPipe, nil
}
func (container *WindowsContainer) CurrentBandwidthLimits() (garden.BandwidthLimits, error) {
container.logger.Debug("WC: CurrentBandwidthLimits")
return garden.BandwidthLimits{}, nil
}
func (container *WindowsContainer) CurrentCPULimits() (garden.CPULimits, error) {
container.logger.Debug("WC: CurrentCPULimits")
return garden.CPULimits{}, nil
}
func (container *WindowsContainer) CurrentDiskLimits() (garden.DiskLimits, error) {
container.logger.Debug("WC: CurrentDiskLimits")
return garden.DiskLimits{}, nil
}
func (container *WindowsContainer) CurrentMemoryLimits() (garden.MemoryLimits, error) {
container.logger.Debug("WC: CurrentMemoryLimits")
return garden.MemoryLimits{}, nil
}
// Map a port on the host to a port in the container so that traffic to the
// host port is forwarded to the container port.
//
// If a host port is not given, a port will be acquired from the server's port
// pool.
//
// If a container port is not given, the port will be the same as the
// container port.
//
// The two resulting ports are returned in the response.
//
// Errors:
// * When no port can be acquired from the server's port pool.
func (container *WindowsContainer) NetIn(hostPort, containerPort uint32) (uint32, uint32, error) {
container.logger.Debug("TODO: NetIn")
if container.hcsContainer != nil {
return 0, 0, errors.New("NetIn calls are not supported after the first Run in a Container")
}
if hostPort == 0 {
freePort := freeTcp4Port()
hostPort = uint32(freePort)
}
if containerPort == 0 {
containerPort = hostPort
}
container.portMappings = append(container.portMappings,
garden.PortMapping{
HostPort: hostPort,
ContainerPort: containerPort,
})
return hostPort, containerPort, nil
}
// Whitelist outbound network traffic.
//
// If the configuration directive deny_networks is not used,
// all networks are already whitelisted and this command is effectively a no-op.
//
// Later NetOut calls take precedence over earlier calls, which is
// significant only in relation to logging.
//
// Errors:
// * An error is returned if the NetOut call fails.
func (container *WindowsContainer) NetOut(netOutRule garden.NetOutRule) error {
container.logger.Debug("TODO: NetOut")
return nil
}
// Run a script inside a container.
//
// The root user will be mapped to a non-root UID in the host unless the container (not this process) was created with 'privileged' true.
//
// Errors:
// * TODO.
func (container *WindowsContainer) Run(spec garden.ProcessSpec, pio garden.ProcessIO) (garden.Process, error) {
if container.hcsContainer == nil {
err := container.startContainer()
if err != nil {
return nil, err
}
}
container.runMutex.Lock()
defer container.runMutex.Unlock()
//https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
// Combine all arguments using ' ' as a separator
// TODO: EscapeArg from https://golang.org/src/syscall/exec_windows.go
concatArgs := ""
for _, v := range spec.Args {
vp := v
vp = strings.Replace(vp, "\n", "\r\n", -1)
if strings.Count(vp, " ") != 0 {
concatArgs = concatArgs + " " + "\"" + vp + "\""
} else {
concatArgs = concatArgs + " " + vp
}
}
executablePath := spec.Path
if filepath.IsAbs(executablePath) && filepath.VolumeName(executablePath) == "" {
executablePath = "C:" + filepath.FromSlash(executablePath)
}
// Create the command line that we're going to run
commandLine := fmt.Sprintf("%s %s", executablePath, concatArgs)
// Create an env var map
envs := map[string]string{}
for _, env := range spec.Env {
splitEnv := strings.SplitN(env, "=", 2)
envs[splitEnv[0]] = splitEnv[1]
}
// Also add container's env vars
for _, env := range container.Env {
splitEnv := strings.SplitN(env, "=", 2)
envs[splitEnv[0]] = splitEnv[1]
}
emulateConsole := true
consoleSize := [2]uint{0, 0}
if spec.TTY != nil && spec.TTY.WindowSize != nil {
// https://github.com/docker/docker/blob/25587906d122c4fce0eb1fbd3dfb805914455f59/api/client/container/run.go#L145
// https: //github.com/docker/docker/blob/3d42bf5f120b6cbd38b54f71dff4343c316939a0/api/client/utils.go#L24
consoleSize = [2]uint{uint(spec.TTY.WindowSize.Rows), uint(spec.TTY.WindowSize.Columns)}
} else {
emulateConsole = false
}
createProcessConfig := &hcsshim.ProcessConfig{
CommandLine: commandLine,
WorkingDirectory: spec.Dir,
CreateStdErrPipe: pio.Stderr != nil,
CreateStdInPipe: pio.Stdin != nil,
CreateStdOutPipe: pio.Stdout != nil,
EmulateConsole: emulateConsole,
ConsoleSize: consoleSize,
Environment: envs,
}
// Create the process
hcsProcess, err := container.hcsContainer.CreateProcess(createProcessConfig)
if err != nil {
return nil, err
}
pid := hcsProcess.Pid()
stdin, stdout, stderr, err := hcsProcess.Stdio()
if err != nil {
return nil, err
}
if emulateConsole {
hcsProcess.ResizeConsole(uint16(spec.TTY.WindowSize.Columns), uint16(spec.TTY.WindowSize.Rows))
}
if err != nil {
return nil, err
}
// Hook our stdin/stdout/stderr pipes
go func() {
if pio.Stdout != nil {
io.Copy(pio.Stdout, stdout)
stdout.Close()
}
}()
go func() {
if pio.Stderr != nil {
io.Copy(pio.Stderr, stderr)
stderr.Close()
}
}()
go func() {
if pio.Stdin != nil {
io.Copy(stdin, pio.Stdin)
stdin.Close()
}
}()
// Create a new process tracker for the process we've just created
pt := windows_client.NewProcessTracker(container.id, uint32(pid), hcsProcess, container.driverInfo, container.logger)
container.logger.Debug("Container run created new process.", lager.Data{
"PID": pt.ID(),
})
container.pids[pid] = pt
return pt, nil
}
// Attach starts streaming the output back to the client from a specified process.
//
// Errors:
// * processID does not refer to a running process.
func (container *WindowsContainer) Attach(processID string, io garden.ProcessIO) (garden.Process, error) {
pid, err := strconv.Atoi(processID)
if err != nil {
return nil, err
}
pt := container.pids[pid]
return pt, nil
}
// Metrics returns the current set of metrics for a container
func (container *WindowsContainer) Metrics() (garden.Metrics, error) {
container.logger.Debug("TODO: Metrics")
return garden.Metrics{
MemoryStat: garden.ContainerMemoryStat{},
CPUStat: garden.ContainerCPUStat{},
DiskStat: garden.ContainerDiskStat{},
NetworkStat: garden.ContainerNetworkStat{},
}, nil
}
// Properties returns the current set of properties
func (container *WindowsContainer) Properties() (garden.Properties, error) {
container.propertiesMutex.RLock()
defer container.propertiesMutex.RUnlock()
return container.WindowsContainerSpec.Properties, nil
}
// Property returns the value of the property with the specified name.
//
// Errors:
// * When the property does not exist on the container.
func (container *WindowsContainer) Property(name string) (string, error) {
container.propertiesMutex.RLock()
defer container.propertiesMutex.RUnlock()
value, found := container.WindowsContainerSpec.Properties[name]
if !found {
return "", UndefinedPropertyError{name}
}
return value, nil
}
// Set a named property on a container to a specified value.
//
// Errors:
// * None.
func (container *WindowsContainer) SetProperty(name string, value string) error {
container.propertiesMutex.Lock()
defer container.propertiesMutex.Unlock()
props := garden.Properties{}
for k, v := range container.WindowsContainerSpec.Properties {
props[k] = v
}
props[name] = value
container.WindowsContainerSpec.Properties = props
return nil
}
// Remove a property with the specified name from a container.
//
// Errors:
// * None.
func (container *WindowsContainer) RemoveProperty(name string) error {
container.propertiesMutex.Lock()
defer container.propertiesMutex.Unlock()
if _, found := container.WindowsContainerSpec.Properties[name]; !found {
return UndefinedPropertyError{name}
}
delete(container.WindowsContainerSpec.Properties, name)
return nil
}
func (container *WindowsContainer) SetGraceTime(graceTime time.Duration) error {
// TODO: not implemented
return nil
}
func freeTcp4Port() int {
l, _ := net.Listen("tcp4", ":0")
defer l.Close()
freePort := strings.Split(l.Addr().String(), ":")[1]
ret, _ := strconv.ParseUint(freePort, 10, 32)
return int(ret)
}
func (c *WindowsContainer) dir(id string) string {
return filepath.Join(c.driverInfo.HomeDir, filepath.Base(id))
}
// Get returns the rootfs path for the id. This will mount the dir at it's given path
func (c *WindowsContainer) getMountPathForLayer(rId string, layerChain []string) (string, error) {
var dir string
if c.active == 0 {
if err := hcsshim.ActivateLayer(c.driverInfo, rId); err != nil {
return "", err
}
if err := hcsshim.PrepareLayer(c.driverInfo, rId, layerChain); err != nil {
if err2 := hcsshim.DeactivateLayer(c.driverInfo, rId); err2 != nil {
c.logger.Info(fmt.Sprintf("Failed to Deactivate %s: %s", rId, err))
}
return "", err
}
}
mountPath, err := hcsshim.GetLayerMountPath(c.driverInfo, rId)
if err != nil {
if err2 := hcsshim.DeactivateLayer(c.driverInfo, rId); err2 != nil {
c.logger.Info(fmt.Sprintf("Failed to Deactivate %s: %s", rId, err))
}
return "", err
}
c.active++
// If the layer has a mount path, use that. Otherwise, use the
// folder path.
if mountPath != "" {
dir = mountPath
} else {
dir = c.dir(rId)
}
return dir, nil
}
func (c *WindowsContainer) getComputeSystemConfiguration() (*hcsshim.ContainerConfig, error) {
layerFolderPath := c.dir(c.id)
// TODO: investigate further when IgnoreFlushesDuringBoot should be false
cu := &hcsshim.ContainerConfig{
SystemType: "Container",
IgnoreFlushesDuringBoot: true,
Name: c.id,
Owner: windows_containers.DefaultOwner,
IsDummy: false,
VolumePath: c.volumeName,
LayerFolderPath: layerFolderPath,
Layers: []hcsshim.Layer{},
EndpointList: []string{c.natEndpointId},
MappedDirectories: []hcsshim.MappedDir{},
}
for _, layerPath := range c.layerChain {
layerId := filepath.Base(layerPath)
layerGuid, err := hcsshim.NameToGuid(layerId)
if err != nil {
return nil, err
}
cu.Layers = append(cu.Layers, hcsshim.Layer{
ID: layerGuid.ToString(),
Path: layerPath,
})
}
// https://github.com/cloudfoundry/garden/blob/f90a312c91dc1d586c15a42ca29959445bdd25a1/client.go#L161
for _, bindMount := range c.bindMounts {
srcPath := bindMount.SrcPath
dstPath := bindMount.DstPath
if dstPath[1] != ':' {
dstPath = "C:" + filepath.FromSlash(dstPath)
}
if bindMount.Origin == garden.BindMountOriginHost {
readOnly := bindMount.Mode == garden.BindMountModeRO
mappedDir := hcsshim.MappedDir{
HostPath: srcPath,
ContainerPath: dstPath,
ReadOnly: readOnly,
}
cu.MappedDirectories = append(cu.MappedDirectories, mappedDir)
fmt.Println(srcPath, dstPath)
}
if bindMount.Origin == garden.BindMountOriginContainer {
return nil, errors.New("'Container' bind mount mode not supported")
}
}
return cu, nil
}
func (c *WindowsContainer) createNatNetworkEndpoint() error {
hcsNets, err := hcsshim.HNSListNetworkRequest("GET", "", "")
if err != nil {
return err
}
natNetworkId := ""
for _, n := range hcsNets {
if n.Name == "nat" {
natNetworkId = n.Id
}
}
if natNetworkId == "" {
return errors.New("Nat network not found")
}
// https://github.com/docker/libnetwork/blob/f9a1590164b878e668eabf889dd79fb6af8eaced/drivers/windows/windows.go#L284
netInPolicies := []json.RawMessage{}
for _, pm := range c.portMappings {
natPolicy, err := json.Marshal(hcsshim.NatPolicy{
Type: "NAT",
ExternalPort: uint16(pm.HostPort),
InternalPort: uint16(pm.ContainerPort),
Protocol: "TCP",
})
if err != nil {
return err
}
netInPolicies = append(netInPolicies, natPolicy)
}
endpointRequest := hcsshim.HNSEndpoint{
// Name: "endpoint 1",
VirtualNetwork: natNetworkId,
}
endpointRequest.Policies = netInPolicies
endpointRequestJson, err := json.Marshal(endpointRequest)
if err != nil {
return err
}
endpoint, err := hcsshim.HNSEndpointRequest("POST", "", string(endpointRequestJson))
if err != nil {
return err
}
c.containerIp = endpoint.IPAddress.String()
c.natEndpointId = endpoint.Id
c.logger.Info(fmt.Sprintf("Nat endpoint created %v IP Addrss %v", endpoint.Id, endpoint.IPAddress.String()))
return nil
}
func (c *WindowsContainer) startContainer() error {
c.startMutex.Lock()
defer c.startMutex.Unlock()
if c.hcsContainer == nil {
c.logger.Info(fmt.Sprintf("Starting container %v", c.id))
err := c.createNatNetworkEndpoint()
if err != nil {
return err
}
// Build a configuration json for creating a compute system
configuration, err := c.getComputeSystemConfiguration()
if err != nil {
return err
}
// Create a compute system.
// This is our container.
c.logger.Info(fmt.Sprintf("HCS create container", lager.Data{"HCSContainerConfig": configuration}))
hcsContainer, err := hcsshim.CreateContainer(c.id, configuration)
if err != nil {
return err
}
c.hcsContainer = hcsContainer
// Start the container
c.logger.Info(fmt.Sprintf("HCS start container"))
err = c.hcsContainer.Start()
if err != nil {
return err
}
}
return nil
}
func (c *WindowsContainer) getContainerIp() (string, error) {
// TODO: this is a very inefficient and bad workaround to get the IP
// of the container.
processSpec := garden.ProcessSpec{
Path: "ipconfig",
Args: []string{},
Env: []string{},
Dir: "c:\\",
}
stdout := bytes.NewBufferString("")
pio := garden.ProcessIO{
Stdin: nil,
Stdout: stdout,
Stderr: nil,
}
pt, err := c.Run(processSpec, pio)
if err != nil {
return "", err
}
_, err = pt.Wait()
if err != nil {
return "", err
}
output, err := ioutil.ReadAll(stdout)
re, err := regexp.Compile(`IPv4 Address. . . . . . . . . . . : (?P<ip>\d+\.\d+\.\d+\.\d+)`)
if err != nil {
return "", err
}
match := re.FindStringSubmatch(string(output))
for i, name := range re.SubexpNames() {
if name == "ip" {
if i < len(match) {
return match[i], nil
}
}
}
return "", fmt.Errorf("Could not detect container IP address from ipconfig: %s", output)
}