version.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 misc
  15. import (
  16. "fmt"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/printutils"
  21. "yunion.io/x/pkg/utils"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  25. modules "yunion.io/x/onecloud/pkg/mcclient/modules"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  27. )
  28. func init() {
  29. type VersionOptions struct {
  30. SERVICE string `help:"Service type"`
  31. }
  32. R(&VersionOptions{}, "version-show", "query backend service for its version", func(s *mcclient.ClientSession, args *VersionOptions) error {
  33. body, err := modulebase.GetVersion(s, args.SERVICE)
  34. if err != nil {
  35. return err
  36. }
  37. fmt.Println(body)
  38. return nil
  39. })
  40. type StatsOptions struct {
  41. SERVICE string `help:"Service type"`
  42. }
  43. R(&StatsOptions{}, "api-stats-show", "query backend service for its stats", func(s *mcclient.ClientSession, args *StatsOptions) error {
  44. body, err := modulebase.GetStats(s, "stats", args.SERVICE)
  45. if err != nil {
  46. return err
  47. }
  48. printObject(body)
  49. return nil
  50. })
  51. R(&StatsOptions{}, "db-stats-show", "query backend service for its db stats", func(s *mcclient.ClientSession, args *StatsOptions) error {
  52. body, err := modulebase.GetStats(s, "db_stats", args.SERVICE)
  53. if err != nil {
  54. return err
  55. }
  56. stats, _ := body.Get("db_stats")
  57. printObject(stats)
  58. return nil
  59. })
  60. R(&StatsOptions{}, "worker-stats-show", "query backend service for its worker stats", func(s *mcclient.ClientSession, args *StatsOptions) error {
  61. body, err := modulebase.GetStats(s, "worker_stats", args.SERVICE)
  62. if err != nil {
  63. return err
  64. }
  65. data, _ := body.GetArray("workers")
  66. printList(&printutils.ListResult{Data: data}, nil)
  67. return nil
  68. })
  69. type VersionListOptions struct {
  70. }
  71. R(&VersionListOptions{}, "version-list", "query all backend service version", func(s *mcclient.ClientSession, args *VersionListOptions) error {
  72. services := []jsonutils.JSONObject{}
  73. params := map[string]interface{}{}
  74. for {
  75. params["offset"] = len(services)
  76. resp, err := identity.ServicesV3.List(s, jsonutils.Marshal(params))
  77. if err != nil {
  78. return err
  79. }
  80. services = append(services, resp.Data...)
  81. if len(services) >= resp.Total {
  82. break
  83. }
  84. }
  85. vers := map[string]string{}
  86. for _, service := range services {
  87. serviceType, _ := service.GetString("type")
  88. if utils.IsInStringArray(serviceType, []string{
  89. apis.SERVICE_TYPE_OFFLINE_CLOUDMETA,
  90. apis.SERVICE_TYPE_CLOUDMETA,
  91. apis.SERVICE_TYPE_INFLUXDB,
  92. apis.SERVICE_TYPE_VICTORIA_METRICS,
  93. apis.SERVICE_TYPE_ETCD,
  94. "torrent-tracker",
  95. }) {
  96. continue
  97. }
  98. ver, err := modules.GetVersion(s, serviceType)
  99. if err != nil {
  100. if errors.Cause(err) == cloudprovider.ErrNotFound || errors.Cause(err) == errors.ErrConnectRefused {
  101. continue
  102. }
  103. vers[serviceType] = err.Error()
  104. } else {
  105. vers[serviceType] = ver
  106. }
  107. }
  108. printObject(jsonutils.Marshal(vers))
  109. return nil
  110. })
  111. }