mod_webconsole.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 webconsole
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/httputils"
  21. compute_api "yunion.io/x/onecloud/pkg/apis/compute"
  22. webconsole_api "yunion.io/x/onecloud/pkg/apis/webconsole"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  28. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  29. compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  30. )
  31. var (
  32. WebConsole WebConsoleManager
  33. )
  34. func init() {
  35. WebConsole = WebConsoleManager{NewWebConsoleManager()}
  36. modulebase.Register(&WebConsole)
  37. }
  38. type WebConsoleManager struct {
  39. modulebase.ResourceManager
  40. }
  41. func NewWebConsoleManager() modulebase.ResourceManager {
  42. return modulebase.ResourceManager{BaseManager: *modulebase.NewBaseManager("webconsole", "", "", nil, nil),
  43. Keyword: "webconsole", KeywordPlural: "webconsole"}
  44. }
  45. func (m WebConsoleManager) DoConnect(s *mcclient.ClientSession, connType, id, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  46. if len(connType) == 0 {
  47. return nil, fmt.Errorf("Empty connection resource type")
  48. }
  49. url := fmt.Sprintf("/webconsole/%s", connType)
  50. if id != "" {
  51. url = fmt.Sprintf("%s/%s", url, id)
  52. }
  53. if action != "" {
  54. url = fmt.Sprintf("%s/%s", url, action)
  55. }
  56. return modulebase.Post(m.ResourceManager, s, url, params, "webconsole")
  57. }
  58. func (m WebConsoleManager) DoK8sConnect(s *mcclient.ClientSession, id, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  59. return m.DoConnect(s, "k8s", id, action, params)
  60. }
  61. func (m WebConsoleManager) DoK8sShellConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  62. return m.DoK8sConnect(s, id, "shell", params)
  63. }
  64. func (m WebConsoleManager) DoAdbShell(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  65. return m.DoConnect(s, "adb", id, "shell", params)
  66. }
  67. func (m WebConsoleManager) doContainerAction(s *mcclient.ClientSession, data jsonutils.JSONObject, getArgs func(containerId string) []string) (jsonutils.JSONObject, error) {
  68. containerId, err := data.GetString("container_id")
  69. if err != nil {
  70. return nil, errors.Wrap(err, "get container_id")
  71. }
  72. if containerId == "" {
  73. return nil, httperrors.NewNotEmptyError("container_id")
  74. }
  75. obj, err := compute.Containers.Get(s, containerId, nil)
  76. if err != nil {
  77. return nil, errors.Wrapf(err, "get container by %s", containerId)
  78. }
  79. containerName, _ := obj.GetString("name")
  80. serverId, _ := obj.GetString("guest_id")
  81. if serverId == "" {
  82. return nil, httperrors.NewNotFoundError("not found guest_id from container %s", obj)
  83. }
  84. serverObj, err := compute.Servers.Get(s, serverId, nil)
  85. if err != nil {
  86. return nil, errors.Wrapf(err, "get server by %s", serverId)
  87. }
  88. serverDetails := &compute_api.ServerDetails{}
  89. if err := serverObj.Unmarshal(serverDetails); err != nil {
  90. return nil, errors.Wrapf(err, "unmarshal server details: %s", serverObj)
  91. }
  92. info := &webconsole_api.SK8sShellDisplayInfo{
  93. InstanceName: fmt.Sprintf("%s/%s", serverDetails.Name, containerName),
  94. IPs: strings.Split(serverDetails.IPs, ","),
  95. }
  96. args := getArgs(containerId)
  97. return m.doCloudShell(s, info, "/opt/yunion/bin/climc", args...)
  98. }
  99. func (m WebConsoleManager) DoContainerExec(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  100. return m.doContainerAction(s, data, func(containerId string) []string {
  101. return []string{"container-exec", containerId, "sh"}
  102. })
  103. }
  104. func (m WebConsoleManager) DoContainerLog(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  105. opt := new(compute_options.ContainerLogOptions)
  106. data.Unmarshal(opt)
  107. return m.doContainerAction(s, data, func(containerId string) []string {
  108. args := []string{"container-log"}
  109. if opt.Tail > 0 {
  110. args = append(args, "--tail", fmt.Sprintf("%d", opt.Tail))
  111. }
  112. if opt.LimitBytes > 0 {
  113. args = append(args, "--limit-bytes", fmt.Sprintf("%d", opt.LimitBytes))
  114. }
  115. if opt.Since != "" {
  116. args = append(args, "--since", opt.Since)
  117. }
  118. if opt.Follow {
  119. args = append(args, "-f")
  120. }
  121. args = append(args, containerId)
  122. return args
  123. })
  124. }
  125. type CloudShellRequest struct {
  126. InstanceName string `json:"instance_name"`
  127. IPs []string `json:"ips"`
  128. }
  129. func (m WebConsoleManager) DoCloudShell(s *mcclient.ClientSession, _ jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  130. return m.doCloudSshShell(s, nil, "", nil, nil)
  131. }
  132. func (m WebConsoleManager) climcSshConnect(s *mcclient.ClientSession, hostname string, command string, args []string, env map[string]string, info *webconsole_api.SK8sShellDisplayInfo) (jsonutils.JSONObject, error) {
  133. if hostname == "" {
  134. hostname = "climc"
  135. }
  136. // maybe running in docker compose environment, so try to use ssh way
  137. data, err := m.DoClimcSshConnect(s, hostname, 22, command, args, env, info)
  138. if err != nil {
  139. return nil, errors.Wrap(err, "DoClimcSshConnect")
  140. }
  141. return data, nil
  142. }
  143. func (m WebConsoleManager) doActionWithClimcPod(
  144. s *mcclient.ClientSession,
  145. af func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error),
  146. ) (jsonutils.JSONObject, error) {
  147. adminSession := s
  148. if auth.IsAuthed() {
  149. adminSession = auth.GetAdminSession(s.GetContext(), s.GetRegion())
  150. }
  151. query := jsonutils.NewDict()
  152. query.Add(jsonutils.JSONTrue, "system")
  153. query.Add(jsonutils.NewString("system"), "scope")
  154. query.Add(jsonutils.NewString("system-default"), "name")
  155. clusters, err := k8s.KubeClusters.List(adminSession, query)
  156. if err != nil {
  157. return nil, errors.Wrap(err, "list k8s cluster")
  158. }
  159. clusterId, _ := clusters.Data[0].GetString("id")
  160. if len(clusterId) == 0 {
  161. return nil, httperrors.NewNotFoundError("cluster system-default no id")
  162. }
  163. query = jsonutils.NewDict()
  164. query.Add(jsonutils.NewString(clusterId), "cluster")
  165. query.Add(jsonutils.NewString("onecloud"), "namespace")
  166. query.Add(jsonutils.NewString("climc"), "search")
  167. query.Add(jsonutils.JSONTrue, "details")
  168. pods, err := k8s.Pods.List(adminSession, query)
  169. if err != nil {
  170. return nil, errors.Wrap(err, "Pods")
  171. }
  172. if len(pods.Data) == 0 {
  173. return nil, httperrors.NewNotFoundError("pod climc not found")
  174. }
  175. pod := pods.Data[0]
  176. return af(s, clusterId, pod)
  177. }
  178. func (m WebConsoleManager) doCloudShell(s *mcclient.ClientSession, info *webconsole_api.SK8sShellDisplayInfo, cmd string, args ...string) (jsonutils.JSONObject, error) {
  179. endpointType := "internal"
  180. authUrl, err := s.GetServiceURL("identity", endpointType, httputils.POST)
  181. if err != nil {
  182. return nil, httperrors.NewNotFoundError("auth_url not found")
  183. }
  184. env := map[string]string{
  185. "OS_AUTH_TOKEN": s.GetToken().GetTokenString(),
  186. "OS_PROJECT_NAME": s.GetProjectName(),
  187. "OS_PROJECT_DOMAIN": s.GetProjectDomain(),
  188. "OS_AUTH_URL": authUrl,
  189. "OS_ENDPOINT_TYPE": endpointType,
  190. "YUNION_USE_CACHED_TOKEN": "false",
  191. "YUNION_INSECURE": "true",
  192. "OS_USERNAME": "",
  193. "OS_PASSWORD": "",
  194. "OS_DOMAIN_NAME": "",
  195. "OS_ACCESS_KEY": "",
  196. "OS_SECRET_KEY": "",
  197. "OS_TRY_TERM_WIDTH": "false",
  198. "GOMAXPROCS": "2",
  199. }
  200. return m.doCloudSshShell(s, info, cmd, args, env)
  201. /*return m.doActionWithClimcPod(s, func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  202. req := webconsole_api.SK8sShellRequest{}
  203. req.Cluster = clusterId
  204. req.Namespace = "onecloud"
  205. req.Container = "climc"
  206. req.Command = cmd
  207. req.Args = args
  208. endpointType := "internal"
  209. authUrl, err := s.GetServiceURL("identity", endpointType)
  210. if err != nil {
  211. return nil, httperrors.NewNotFoundError("auth_url not found")
  212. }
  213. req.Env = map[string]string{
  214. "OS_AUTH_TOKEN": s.GetToken().GetTokenString(),
  215. "OS_PROJECT_NAME": s.GetProjectName(),
  216. "OS_PROJECT_DOMAIN": s.GetProjectDomain(),
  217. "OS_AUTH_URL": authUrl,
  218. "OS_ENDPOINT_TYPE": endpointType,
  219. "YUNION_USE_CACHED_TOKEN": "false",
  220. "YUNION_INSECURE": "true",
  221. "OS_USERNAME": "",
  222. "OS_PASSWORD": "",
  223. "OS_DOMAIN_NAME": "",
  224. "OS_ACCESS_KEY": "",
  225. "OS_SECRET_KEY": "",
  226. "OS_TRY_TERM_WIDTH": "false",
  227. }
  228. req.DisplayInfo = info
  229. podName, err := pod.GetString("name")
  230. if err != nil {
  231. return nil, errors.Wrapf(err, "get pod name from: %s", pod.String())
  232. }
  233. return m.DoK8sConnect(s, podName, "shell", jsonutils.Marshal(req))
  234. })*/
  235. }
  236. func (m WebConsoleManager) doCloudSshShell(s *mcclient.ClientSession, info *webconsole_api.SK8sShellDisplayInfo, command string, args []string, env map[string]string) (jsonutils.JSONObject, error) {
  237. data, err := m.doActionWithClimcPod(s, func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  238. podIP, err := pod.GetString("podIP")
  239. if err != nil {
  240. return nil, errors.Wrap(err, "get podIP")
  241. }
  242. return m.climcSshConnect(s, podIP, command, args, env, info)
  243. })
  244. errs := []error{}
  245. if err != nil {
  246. errs = append(errs, err)
  247. // try climc ssh
  248. data, err := m.climcSshConnect(s, "", command, args, env, info)
  249. if err != nil {
  250. errs = append(errs, err)
  251. return nil, errors.NewAggregate(errs)
  252. }
  253. return data, nil
  254. }
  255. return data, nil
  256. }
  257. func (m WebConsoleManager) DoK8sLogConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  258. return m.DoK8sConnect(s, id, "log", params)
  259. }
  260. func (m WebConsoleManager) DoBaremetalConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  261. return m.DoConnect(s, "baremetal", id, "", params)
  262. }
  263. func (m WebConsoleManager) DoSshConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  264. return m.DoConnect(s, "ssh", id, "", params)
  265. }
  266. func (m WebConsoleManager) DoServerConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  267. return m.DoConnect(s, "server", id, "", params)
  268. }
  269. func (m WebConsoleManager) DoServerRDPConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  270. return m.DoConnect(s, "server-rdp", id, "", params)
  271. }
  272. func (m WebConsoleManager) DoClimcSshConnect(s *mcclient.ClientSession, ip string, port int, command string, args []string, env map[string]string, info *webconsole_api.SK8sShellDisplayInfo) (jsonutils.JSONObject, error) {
  273. data := jsonutils.Marshal(map[string]interface{}{
  274. "username": "root",
  275. "keep_username": true,
  276. "ip_addr": ip,
  277. "port": port,
  278. "name": "climc",
  279. "env": env,
  280. "command": command,
  281. "args": args,
  282. "display_info": info,
  283. })
  284. body := jsonutils.NewDict()
  285. body.Set("webconsole", data)
  286. return m.DoConnect(s, "climc", "shell", "", body)
  287. }