webconsole.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "encoding/base64"
  17. "fmt"
  18. "net/url"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/utils"
  21. webconsole_api "yunion.io/x/onecloud/pkg/apis/webconsole"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/webconsole"
  25. o "yunion.io/x/onecloud/pkg/mcclient/options"
  26. "yunion.io/x/onecloud/pkg/webconsole/command"
  27. )
  28. func init() {
  29. handleResult := func(s *mcclient.ClientSession, opt o.WebConsoleOptions, obj jsonutils.JSONObject) error {
  30. if obj.Contains("access_url") {
  31. accessUrl, _ := obj.GetString("access_url")
  32. fmt.Println("AccessURL:", accessUrl)
  33. return nil
  34. }
  35. if opt.WebconsoleUrl == "" {
  36. resp, err := identity.ServicesV3.GetSpecific(s, "common", "config", nil)
  37. if err != nil {
  38. return err
  39. }
  40. apiServer, _ := resp.GetString("config", "default", "api_server")
  41. if len(apiServer) > 0 {
  42. opt.WebconsoleUrl = fmt.Sprintf("%s/web-console", apiServer)
  43. }
  44. }
  45. u, err := url.Parse(opt.WebconsoleUrl)
  46. if err != nil {
  47. return err
  48. }
  49. resp := &webconsole_api.ServerRemoteConsoleResponse{}
  50. if err := obj.Unmarshal(resp); err != nil {
  51. return err
  52. }
  53. connectParams := resp.GetConnectParams()
  54. protocol, err := resp.GetConnectProtocol()
  55. if err != nil {
  56. return err
  57. }
  58. if !utils.IsInStringArray(protocol, []string{
  59. command.PROTOCOL_TTY, webconsole_api.VNC,
  60. webconsole_api.SPICE, webconsole_api.WMKS, webconsole_api.WS,
  61. }) {
  62. fmt.Println(connectParams)
  63. return nil
  64. }
  65. if protocol == webconsole_api.WS {
  66. u, err = url.Parse(fmt.Sprintf("%s/ws", opt.WebconsoleUrl))
  67. if err != nil {
  68. return err
  69. }
  70. }
  71. newQuery := url.Values{}
  72. newQuery.Set("data", base64.StdEncoding.EncodeToString([]byte(connectParams)))
  73. u.RawQuery = newQuery.Encode()
  74. fmt.Println(u.String())
  75. return nil
  76. }
  77. R(&o.PodShellOptions{}, "webconsole-k8s-pod", "Show TTY console of given pod", func(s *mcclient.ClientSession, args *o.PodShellOptions) error {
  78. params, err := args.Params()
  79. if err != nil {
  80. return err
  81. }
  82. ret, err := webconsole.WebConsole.DoK8sShellConnect(s, args.NAME, params)
  83. if err != nil {
  84. return err
  85. }
  86. handleResult(s, args.WebConsoleOptions, ret)
  87. return nil
  88. })
  89. R(&o.PodLogOptoins{}, "webconsole-k8s-pod-log", "Get logs of given pod", func(s *mcclient.ClientSession, args *o.PodLogOptoins) error {
  90. params, err := args.Params()
  91. if err != nil {
  92. return err
  93. }
  94. ret, err := webconsole.WebConsole.DoK8sLogConnect(s, args.NAME, params)
  95. if err != nil {
  96. return err
  97. }
  98. handleResult(s, args.WebConsoleOptions, ret)
  99. return nil
  100. })
  101. R(&o.WebConsoleBaremetalOptions{}, "webconsole-baremetal", "Connect baremetal host webconsole", func(s *mcclient.ClientSession, args *o.WebConsoleBaremetalOptions) error {
  102. ret, err := webconsole.WebConsole.DoBaremetalConnect(s, args.ID, nil)
  103. if err != nil {
  104. return err
  105. }
  106. handleResult(s, args.WebConsoleOptions, ret)
  107. return nil
  108. })
  109. R(&o.WebConsoleSshOptions{}, "webconsole-ssh", "Connect ssh webconsole", func(s *mcclient.ClientSession, args *o.WebConsoleSshOptions) error {
  110. params, err := args.Params()
  111. if err != nil {
  112. return err
  113. }
  114. ret, err := webconsole.WebConsole.DoSshConnect(s, args.ID, params)
  115. if err != nil {
  116. return err
  117. }
  118. handleResult(s, args.WebConsoleOptions, ret)
  119. return nil
  120. })
  121. R(&o.WebConsoleServerOptions{}, "webconsole-server", "Connect server remote graphic console", func(s *mcclient.ClientSession, args *o.WebConsoleServerOptions) error {
  122. ret, err := webconsole.WebConsole.DoServerConnect(s, args.ID, nil)
  123. if err != nil {
  124. return err
  125. }
  126. handleResult(s, args.WebConsoleOptions, ret)
  127. return nil
  128. })
  129. R(&o.WebConsoleServerRdpOptions{}, "webconsole-server-rdp", "Connect server remote graphic console by rdp", func(s *mcclient.ClientSession, args *o.WebConsoleServerRdpOptions) error {
  130. ret, err := webconsole.WebConsole.DoServerRDPConnect(s, args.ID, jsonutils.Marshal(map[string]interface{}{"webconsole": args}))
  131. if err != nil {
  132. return err
  133. }
  134. handleResult(s, args.WebConsoleOptions, ret)
  135. return nil
  136. })
  137. R(&o.WebConsoleContainerExecOptions{}, "webconsole-container-exec", "Container exec", func(s *mcclient.ClientSession, args *o.WebConsoleContainerExecOptions) error {
  138. ret, err := webconsole.WebConsole.DoContainerExec(s, jsonutils.Marshal(map[string]interface{}{"container_id": args.ID}))
  139. if err != nil {
  140. return err
  141. }
  142. return handleResult(s, args.WebConsoleOptions, ret)
  143. })
  144. }