container.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright (c) 2018 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package simulator
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "log"
  18. "os/exec"
  19. "strings"
  20. "github.com/vmware/govmomi/vim25/types"
  21. )
  22. // container provides methods to manage a container within a simulator VM lifecycle.
  23. type container struct {
  24. id string
  25. }
  26. // inspect applies container network settings to vm.Guest properties.
  27. func (c *container) inspect(vm *VirtualMachine) error {
  28. if c.id == "" {
  29. return nil
  30. }
  31. var objects []struct {
  32. NetworkSettings struct {
  33. Gateway string
  34. IPAddress string
  35. IPPrefixLen int
  36. MacAddress string
  37. }
  38. }
  39. cmd := exec.Command("docker", "inspect", c.id)
  40. out, err := cmd.Output()
  41. if err != nil {
  42. return err
  43. }
  44. if err = json.NewDecoder(bytes.NewReader(out)).Decode(&objects); err != nil {
  45. return err
  46. }
  47. vm.Config.Annotation = strings.Join(cmd.Args, " ")
  48. vm.logPrintf("%s: %s", vm.Config.Annotation, string(out))
  49. for _, o := range objects {
  50. s := o.NetworkSettings
  51. if s.IPAddress == "" {
  52. continue
  53. }
  54. vm.Guest.IpAddress = s.IPAddress
  55. vm.Summary.Guest.IpAddress = s.IPAddress
  56. if len(vm.Guest.Net) != 0 {
  57. net := &vm.Guest.Net[0]
  58. net.IpAddress = []string{s.IPAddress}
  59. net.MacAddress = s.MacAddress
  60. }
  61. }
  62. return nil
  63. }
  64. // start runs the container if specified by the RUN.container extraConfig property.
  65. func (c *container) start(vm *VirtualMachine) {
  66. if c.id != "" {
  67. start := "start"
  68. if vm.Runtime.PowerState == types.VirtualMachinePowerStateSuspended {
  69. start = "unpause"
  70. }
  71. cmd := exec.Command("docker", start, c.id)
  72. err := cmd.Run()
  73. if err != nil {
  74. log.Printf("%s %s: %s", vm.Name, cmd.Args, err)
  75. }
  76. return
  77. }
  78. var args []string
  79. for _, opt := range vm.Config.ExtraConfig {
  80. val := opt.GetOptionValue()
  81. if val.Key == "RUN.container" {
  82. run := val.Value.(string)
  83. err := json.Unmarshal([]byte(run), &args)
  84. if err != nil {
  85. args = []string{run}
  86. }
  87. break
  88. }
  89. }
  90. if len(args) == 0 {
  91. return
  92. }
  93. args = append([]string{"run", "-d", "--name", vm.Name}, args...)
  94. cmd := exec.Command("docker", args...)
  95. out, err := cmd.Output()
  96. if err != nil {
  97. log.Printf("%s %s: %s", vm.Name, cmd.Args, err)
  98. return
  99. }
  100. c.id = strings.TrimSpace(string(out))
  101. vm.logPrintf("%s %s: %s", cmd.Path, cmd.Args, c.id)
  102. if err = c.inspect(vm); err != nil {
  103. log.Printf("%s inspect %s: %s", vm.Name, c.id, err)
  104. }
  105. }
  106. // stop the container (if any) for the given vm.
  107. func (c *container) stop(vm *VirtualMachine) {
  108. if c.id == "" {
  109. return
  110. }
  111. cmd := exec.Command("docker", "stop", c.id)
  112. err := cmd.Run()
  113. if err != nil {
  114. log.Printf("%s %s: %s", vm.Name, cmd.Args, err)
  115. }
  116. }
  117. // pause the container (if any) for the given vm.
  118. func (c *container) pause(vm *VirtualMachine) {
  119. if c.id == "" {
  120. return
  121. }
  122. cmd := exec.Command("docker", "pause", c.id)
  123. err := cmd.Run()
  124. if err != nil {
  125. log.Printf("%s %s: %s", vm.Name, cmd.Args, err)
  126. }
  127. }
  128. // remove the container (if any) for the given vm.
  129. func (c *container) remove(vm *VirtualMachine) {
  130. if c.id == "" {
  131. return
  132. }
  133. cmd := exec.Command("docker", "rm", "-f", c.id)
  134. err := cmd.Run()
  135. if err != nil {
  136. log.Printf("%s %s: %s", vm.Name, cmd.Args, err)
  137. }
  138. }