| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package webconsole
- import (
- "fmt"
- "strings"
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/util/httputils"
- compute_api "yunion.io/x/onecloud/pkg/apis/compute"
- webconsole_api "yunion.io/x/onecloud/pkg/apis/webconsole"
- "yunion.io/x/onecloud/pkg/httperrors"
- "yunion.io/x/onecloud/pkg/mcclient"
- "yunion.io/x/onecloud/pkg/mcclient/auth"
- "yunion.io/x/onecloud/pkg/mcclient/modulebase"
- "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
- "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
- compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
- )
- var (
- WebConsole WebConsoleManager
- )
- func init() {
- WebConsole = WebConsoleManager{NewWebConsoleManager()}
- modulebase.Register(&WebConsole)
- }
- type WebConsoleManager struct {
- modulebase.ResourceManager
- }
- func NewWebConsoleManager() modulebase.ResourceManager {
- return modulebase.ResourceManager{BaseManager: *modulebase.NewBaseManager("webconsole", "", "", nil, nil),
- Keyword: "webconsole", KeywordPlural: "webconsole"}
- }
- func (m WebConsoleManager) DoConnect(s *mcclient.ClientSession, connType, id, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- if len(connType) == 0 {
- return nil, fmt.Errorf("Empty connection resource type")
- }
- url := fmt.Sprintf("/webconsole/%s", connType)
- if id != "" {
- url = fmt.Sprintf("%s/%s", url, id)
- }
- if action != "" {
- url = fmt.Sprintf("%s/%s", url, action)
- }
- return modulebase.Post(m.ResourceManager, s, url, params, "webconsole")
- }
- func (m WebConsoleManager) DoK8sConnect(s *mcclient.ClientSession, id, action string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoConnect(s, "k8s", id, action, params)
- }
- func (m WebConsoleManager) DoK8sShellConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoK8sConnect(s, id, "shell", params)
- }
- func (m WebConsoleManager) DoAdbShell(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoConnect(s, "adb", id, "shell", params)
- }
- func (m WebConsoleManager) doContainerAction(s *mcclient.ClientSession, data jsonutils.JSONObject, getArgs func(containerId string) []string) (jsonutils.JSONObject, error) {
- containerId, err := data.GetString("container_id")
- if err != nil {
- return nil, errors.Wrap(err, "get container_id")
- }
- if containerId == "" {
- return nil, httperrors.NewNotEmptyError("container_id")
- }
- obj, err := compute.Containers.Get(s, containerId, nil)
- if err != nil {
- return nil, errors.Wrapf(err, "get container by %s", containerId)
- }
- containerName, _ := obj.GetString("name")
- serverId, _ := obj.GetString("guest_id")
- if serverId == "" {
- return nil, httperrors.NewNotFoundError("not found guest_id from container %s", obj)
- }
- serverObj, err := compute.Servers.Get(s, serverId, nil)
- if err != nil {
- return nil, errors.Wrapf(err, "get server by %s", serverId)
- }
- serverDetails := &compute_api.ServerDetails{}
- if err := serverObj.Unmarshal(serverDetails); err != nil {
- return nil, errors.Wrapf(err, "unmarshal server details: %s", serverObj)
- }
- info := &webconsole_api.SK8sShellDisplayInfo{
- InstanceName: fmt.Sprintf("%s/%s", serverDetails.Name, containerName),
- IPs: strings.Split(serverDetails.IPs, ","),
- }
- args := getArgs(containerId)
- return m.doCloudShell(s, info, "/opt/yunion/bin/climc", args...)
- }
- func (m WebConsoleManager) DoContainerExec(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.doContainerAction(s, data, func(containerId string) []string {
- return []string{"container-exec", containerId, "sh"}
- })
- }
- func (m WebConsoleManager) DoContainerLog(s *mcclient.ClientSession, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- opt := new(compute_options.ContainerLogOptions)
- data.Unmarshal(opt)
- return m.doContainerAction(s, data, func(containerId string) []string {
- args := []string{"container-log"}
- if opt.Tail > 0 {
- args = append(args, "--tail", fmt.Sprintf("%d", opt.Tail))
- }
- if opt.LimitBytes > 0 {
- args = append(args, "--limit-bytes", fmt.Sprintf("%d", opt.LimitBytes))
- }
- if opt.Since != "" {
- args = append(args, "--since", opt.Since)
- }
- if opt.Follow {
- args = append(args, "-f")
- }
- args = append(args, containerId)
- return args
- })
- }
- type CloudShellRequest struct {
- InstanceName string `json:"instance_name"`
- IPs []string `json:"ips"`
- }
- func (m WebConsoleManager) DoCloudShell(s *mcclient.ClientSession, _ jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.doCloudSshShell(s, nil, "", nil, nil)
- }
- func (m WebConsoleManager) climcSshConnect(s *mcclient.ClientSession, hostname string, command string, args []string, env map[string]string, info *webconsole_api.SK8sShellDisplayInfo) (jsonutils.JSONObject, error) {
- if hostname == "" {
- hostname = "climc"
- }
- // maybe running in docker compose environment, so try to use ssh way
- data, err := m.DoClimcSshConnect(s, hostname, 22, command, args, env, info)
- if err != nil {
- return nil, errors.Wrap(err, "DoClimcSshConnect")
- }
- return data, nil
- }
- func (m WebConsoleManager) doActionWithClimcPod(
- s *mcclient.ClientSession,
- af func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error),
- ) (jsonutils.JSONObject, error) {
- adminSession := s
- if auth.IsAuthed() {
- adminSession = auth.GetAdminSession(s.GetContext(), s.GetRegion())
- }
- query := jsonutils.NewDict()
- query.Add(jsonutils.JSONTrue, "system")
- query.Add(jsonutils.NewString("system"), "scope")
- query.Add(jsonutils.NewString("system-default"), "name")
- clusters, err := k8s.KubeClusters.List(adminSession, query)
- if err != nil {
- return nil, errors.Wrap(err, "list k8s cluster")
- }
- clusterId, _ := clusters.Data[0].GetString("id")
- if len(clusterId) == 0 {
- return nil, httperrors.NewNotFoundError("cluster system-default no id")
- }
- query = jsonutils.NewDict()
- query.Add(jsonutils.NewString(clusterId), "cluster")
- query.Add(jsonutils.NewString("onecloud"), "namespace")
- query.Add(jsonutils.NewString("climc"), "search")
- query.Add(jsonutils.JSONTrue, "details")
- pods, err := k8s.Pods.List(adminSession, query)
- if err != nil {
- return nil, errors.Wrap(err, "Pods")
- }
- if len(pods.Data) == 0 {
- return nil, httperrors.NewNotFoundError("pod climc not found")
- }
- pod := pods.Data[0]
- return af(s, clusterId, pod)
- }
- func (m WebConsoleManager) doCloudShell(s *mcclient.ClientSession, info *webconsole_api.SK8sShellDisplayInfo, cmd string, args ...string) (jsonutils.JSONObject, error) {
- endpointType := "internal"
- authUrl, err := s.GetServiceURL("identity", endpointType, httputils.POST)
- if err != nil {
- return nil, httperrors.NewNotFoundError("auth_url not found")
- }
- env := map[string]string{
- "OS_AUTH_TOKEN": s.GetToken().GetTokenString(),
- "OS_PROJECT_NAME": s.GetProjectName(),
- "OS_PROJECT_DOMAIN": s.GetProjectDomain(),
- "OS_AUTH_URL": authUrl,
- "OS_ENDPOINT_TYPE": endpointType,
- "YUNION_USE_CACHED_TOKEN": "false",
- "YUNION_INSECURE": "true",
- "OS_USERNAME": "",
- "OS_PASSWORD": "",
- "OS_DOMAIN_NAME": "",
- "OS_ACCESS_KEY": "",
- "OS_SECRET_KEY": "",
- "OS_TRY_TERM_WIDTH": "false",
- "GOMAXPROCS": "2",
- }
- return m.doCloudSshShell(s, info, cmd, args, env)
- /*return m.doActionWithClimcPod(s, func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- req := webconsole_api.SK8sShellRequest{}
- req.Cluster = clusterId
- req.Namespace = "onecloud"
- req.Container = "climc"
- req.Command = cmd
- req.Args = args
- endpointType := "internal"
- authUrl, err := s.GetServiceURL("identity", endpointType)
- if err != nil {
- return nil, httperrors.NewNotFoundError("auth_url not found")
- }
- req.Env = map[string]string{
- "OS_AUTH_TOKEN": s.GetToken().GetTokenString(),
- "OS_PROJECT_NAME": s.GetProjectName(),
- "OS_PROJECT_DOMAIN": s.GetProjectDomain(),
- "OS_AUTH_URL": authUrl,
- "OS_ENDPOINT_TYPE": endpointType,
- "YUNION_USE_CACHED_TOKEN": "false",
- "YUNION_INSECURE": "true",
- "OS_USERNAME": "",
- "OS_PASSWORD": "",
- "OS_DOMAIN_NAME": "",
- "OS_ACCESS_KEY": "",
- "OS_SECRET_KEY": "",
- "OS_TRY_TERM_WIDTH": "false",
- }
- req.DisplayInfo = info
- podName, err := pod.GetString("name")
- if err != nil {
- return nil, errors.Wrapf(err, "get pod name from: %s", pod.String())
- }
- return m.DoK8sConnect(s, podName, "shell", jsonutils.Marshal(req))
- })*/
- }
- func (m WebConsoleManager) doCloudSshShell(s *mcclient.ClientSession, info *webconsole_api.SK8sShellDisplayInfo, command string, args []string, env map[string]string) (jsonutils.JSONObject, error) {
- data, err := m.doActionWithClimcPod(s, func(s *mcclient.ClientSession, clusterId string, pod jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- podIP, err := pod.GetString("podIP")
- if err != nil {
- return nil, errors.Wrap(err, "get podIP")
- }
- return m.climcSshConnect(s, podIP, command, args, env, info)
- })
- errs := []error{}
- if err != nil {
- errs = append(errs, err)
- // try climc ssh
- data, err := m.climcSshConnect(s, "", command, args, env, info)
- if err != nil {
- errs = append(errs, err)
- return nil, errors.NewAggregate(errs)
- }
- return data, nil
- }
- return data, nil
- }
- func (m WebConsoleManager) DoK8sLogConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoK8sConnect(s, id, "log", params)
- }
- func (m WebConsoleManager) DoBaremetalConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoConnect(s, "baremetal", id, "", params)
- }
- func (m WebConsoleManager) DoSshConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoConnect(s, "ssh", id, "", params)
- }
- func (m WebConsoleManager) DoServerConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoConnect(s, "server", id, "", params)
- }
- func (m WebConsoleManager) DoServerRDPConnect(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
- return m.DoConnect(s, "server-rdp", id, "", params)
- }
- 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) {
- data := jsonutils.Marshal(map[string]interface{}{
- "username": "root",
- "keep_username": true,
- "ip_addr": ip,
- "port": port,
- "name": "climc",
- "env": env,
- "command": command,
- "args": args,
- "display_info": info,
- })
- body := jsonutils.NewDict()
- body.Set("webconsole", data)
- return m.DoConnect(s, "climc", "shell", "", body)
- }
|