server_pod.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "os"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/sets"
  20. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  23. options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  24. )
  25. func init() {
  26. R(&options.PodCreateOptions{}, "pod-create", "Create a container pod", func(s *mcclient.ClientSession, opts *options.PodCreateOptions) error {
  27. params, err := opts.Params()
  28. if err != nil {
  29. return err
  30. }
  31. if opts.Count > 1 {
  32. results := modules.Servers.BatchCreate(s, params.JSON(params), opts.Count)
  33. printBatchResults(results, modules.Servers.GetColumns(s))
  34. } else {
  35. server, err := modules.Servers.Create(s, params.JSON(params))
  36. if err != nil {
  37. return err
  38. }
  39. printObject(server)
  40. }
  41. return nil
  42. })
  43. getContainerId := func(s *mcclient.ClientSession, scope string, podId string, container string) (string, error) {
  44. listOpt := map[string]string{
  45. "guest_id": podId,
  46. }
  47. if len(scope) != 0 {
  48. listOpt["scope"] = scope
  49. }
  50. ctrs, err := modules.Containers.List(s, jsonutils.Marshal(listOpt))
  51. if err != nil {
  52. return "", errors.Wrapf(err, "list containers by guest_id %s", podId)
  53. }
  54. if len(ctrs.Data) == 0 {
  55. return "", errors.Errorf("count of container is 0")
  56. }
  57. var ctrId string
  58. if container == "" {
  59. ctrId, _ = ctrs.Data[0].GetString("id")
  60. } else {
  61. for _, ctr := range ctrs.Data {
  62. id, _ := ctr.GetString("id")
  63. name, _ := ctr.GetString("name")
  64. if container == id || container == name {
  65. ctrId, _ = ctr.GetString("id")
  66. break
  67. }
  68. }
  69. }
  70. return ctrId, nil
  71. }
  72. R(&options.PodExecOptions{}, "pod-exec", "Execute a command in a container", func(s *mcclient.ClientSession, opt *options.PodExecOptions) error {
  73. ctrId, err := getContainerId(s, opt.Scope, opt.ID, opt.Container)
  74. if err != nil {
  75. return err
  76. }
  77. return modules.Containers.Exec(s, ctrId, opt.ToAPIInput())
  78. })
  79. R(&options.PodLogOptions{}, "pod-log", "Get container log of a pod", func(s *mcclient.ClientSession, opt *options.PodLogOptions) error {
  80. ctrId, err := getContainerId(s, opt.Scope, opt.ID, opt.Container)
  81. if err != nil {
  82. return err
  83. }
  84. input, err := opt.ToAPIInput()
  85. if err != nil {
  86. return err
  87. }
  88. if err := modules.Containers.LogToWriter(s, ctrId, input, os.Stdout); err != nil {
  89. return errors.Wrap(err, "get container log")
  90. }
  91. return nil
  92. })
  93. type MigratePortMappingsOptions struct {
  94. options.ServerIdOptions
  95. RemovePort []int `help:"remove port"`
  96. RemoteIp []string `help:"remote ips"`
  97. }
  98. R(&MigratePortMappingsOptions{}, "pod-migrate-port-mappings", "Migrate port mappings to nic", func(s *mcclient.ClientSession, opt *MigratePortMappingsOptions) error {
  99. sObj, err := modules.Servers.Get(s, opt.ID, nil)
  100. if err != nil {
  101. return err
  102. }
  103. id, err := sObj.GetString("id")
  104. if err != nil {
  105. return errors.Wrapf(err, "get server id from %s", sObj)
  106. }
  107. metadata, err := modules.Servers.GetMetadata(s, id, nil)
  108. if err != nil {
  109. return err
  110. }
  111. pmStr, err := metadata.GetString(computeapi.POD_METADATA_PORT_MAPPINGS)
  112. if err != nil {
  113. return err
  114. }
  115. pms, err := jsonutils.ParseString(pmStr)
  116. if err != nil {
  117. return err
  118. }
  119. pmObjs := []computeapi.PodPortMapping{}
  120. if err := pms.Unmarshal(&pmObjs); err != nil {
  121. return errors.Wrapf(err, "unmarshal %s to port_mappings", pms)
  122. }
  123. if len(opt.RemovePort) > 0 {
  124. pp := sets.NewInt(opt.RemovePort...)
  125. newPmObjs := []computeapi.PodPortMapping{}
  126. for _, pm := range pmObjs {
  127. if pp.Has(pm.ContainerPort) {
  128. continue
  129. }
  130. tmpPm := pm
  131. newPmObjs = append(newPmObjs, tmpPm)
  132. }
  133. pmObjs = newPmObjs
  134. }
  135. nicPm := make([]*computeapi.GuestPortMapping, len(pmObjs))
  136. for i, pm := range pmObjs {
  137. nicPm[i] = &computeapi.GuestPortMapping{
  138. Protocol: computeapi.GuestPortMappingProtocol(pm.Protocol),
  139. Port: pm.ContainerPort,
  140. HostPort: pm.HostPort,
  141. HostIp: pm.HostIp,
  142. RemoteIps: opt.RemoteIp,
  143. }
  144. if pm.HostPortRange != nil {
  145. nicPm[i].HostPortRange = &computeapi.GuestPortMappingPortRange{
  146. Start: pm.HostPortRange.Start,
  147. End: pm.HostPortRange.End,
  148. }
  149. }
  150. }
  151. params := jsonutils.Marshal(map[string]interface{}{
  152. "scope": "system",
  153. "details": true,
  154. })
  155. sNets, err := modules.Servernetworks.ListDescendent(s, id, params)
  156. if err != nil {
  157. return errors.Wrap(err, "list server networks")
  158. }
  159. if len(sNets.Data) == 0 {
  160. return errors.Errorf("no server networks found")
  161. }
  162. firstNet := sNets.Data[0]
  163. sNet := new(computeapi.GuestnetworkDetails)
  164. if err := firstNet.Unmarshal(sNet); err != nil {
  165. return errors.Wrap(err, "unmarshal to guestnetwork details")
  166. }
  167. updateData := jsonutils.Marshal(map[string]interface{}{
  168. "port_mappings": nicPm,
  169. })
  170. obj, err := modules.Servernetworks.Update(s, sNet.GuestId, sNet.NetworkId, nil, updateData)
  171. if err != nil {
  172. return errors.Wrap(err, "update server networks")
  173. }
  174. printObject(obj)
  175. return nil
  176. })
  177. }