mod_hosts.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/util/httputils"
  21. "yunion.io/x/pkg/util/printutils"
  22. "yunion.io/x/pkg/utils"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules"
  27. )
  28. const MACAddressPattern = `(([a-fA-F0-9]{2}[:-]){5}([a-fA-F0-9]{2}))`
  29. type HostManager struct {
  30. modulebase.ResourceManager
  31. }
  32. func (this *HostManager) GetLoginInfo(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  33. data, e := this.GetMetadata(s, id, nil)
  34. if e != nil {
  35. return nil, e
  36. }
  37. ret := jsonutils.NewDict()
  38. login_key, e := data.GetString("password")
  39. if e != nil {
  40. return nil, httperrors.NewNotFoundError("No ssh password: %s", e)
  41. }
  42. passwd, e := utils.DescryptAESBase64(id, login_key)
  43. if e != nil {
  44. return nil, e
  45. }
  46. passwd, e = utils.DescryptAESBase64(id, passwd)
  47. if e != nil {
  48. return nil, e
  49. }
  50. ret.Add(jsonutils.NewString(passwd), "password")
  51. v, e := data.Get("username")
  52. if e == nil {
  53. ret.Add(v, "username")
  54. }
  55. v, e = data.Get("ip")
  56. if e == nil {
  57. ret.Add(v, "ip")
  58. }
  59. return ret, nil
  60. }
  61. func (this *HostManager) GetIpmiInfo(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  62. data, e := this.GetSpecific(s, id, "ipmi", nil)
  63. if e != nil {
  64. return nil, e
  65. }
  66. ret := jsonutils.NewDict()
  67. passwd, e := data.GetString("password")
  68. if e != nil {
  69. return nil, fmt.Errorf("No IPMI password: %s", e)
  70. }
  71. ret.Add(jsonutils.NewString(passwd), "password")
  72. v, e := data.Get("username")
  73. if e == nil {
  74. ret.Add(v, "username")
  75. }
  76. v, e = data.Get("ip_addr")
  77. if e == nil {
  78. ret.Add(v, "ip")
  79. }
  80. return ret, nil
  81. }
  82. func parseHosts(titles []string, data string) []*jsonutils.JSONDict {
  83. hosts := strings.Split(data, "\n")
  84. ret := []*jsonutils.JSONDict{}
  85. for i, host := range hosts {
  86. host = strings.TrimSpace(host)
  87. if len(host) == 0 {
  88. log.Warningf("DoBatchRegister 第%d行: 空白行(已忽略)\n", i)
  89. continue
  90. }
  91. fields := strings.Split(host, ",")
  92. params := jsonutils.NewDict()
  93. params.Add(jsonutils.NewString("baremetal"), "host_type")
  94. for i := range fields {
  95. field := fields[i]
  96. if len(field) > 0 {
  97. params.Add(jsonutils.NewString(field), titles[i])
  98. }
  99. }
  100. ret = append(ret, params)
  101. }
  102. return ret
  103. }
  104. func (this *HostManager) BatchRegister(s *mcclient.ClientSession, titles []string, params jsonutils.JSONObject) ([]printutils.SubmitResult, error) {
  105. data, err := params.GetString("hosts")
  106. if err != nil {
  107. return nil, err
  108. }
  109. input := params.(*jsonutils.JSONDict)
  110. input.Remove("hosts")
  111. hosts := parseHosts(titles, data)
  112. results := make(chan printutils.SubmitResult, len(hosts))
  113. for _, host := range hosts {
  114. host.Update(input)
  115. go func(data jsonutils.JSONObject) {
  116. ret, e := this.Create(s, data)
  117. id, _ := data.GetString("access_mac")
  118. if e != nil {
  119. ecls, ok := e.(*httputils.JSONClientError)
  120. if ok {
  121. results <- printutils.SubmitResult{Status: ecls.Code, Id: id, Data: jsonutils.Marshal(ecls)}
  122. } else {
  123. results <- printutils.SubmitResult{Status: 400, Id: id, Data: jsonutils.NewString(e.Error())}
  124. }
  125. } else {
  126. results <- printutils.SubmitResult{Status: 200, Id: id, Data: ret}
  127. }
  128. }(host)
  129. }
  130. ret := make([]printutils.SubmitResult, len(hosts))
  131. for i := 0; i < len(hosts); i++ {
  132. ret[i] = <-results
  133. }
  134. close(results)
  135. return ret, nil
  136. }
  137. func (this *HostManager) DoBatchRegister(s *mcclient.ClientSession, params jsonutils.JSONObject) ([]printutils.SubmitResult, error) {
  138. titles := []string{"access_mac", "name", "ipmi_ip_addr", "ipmi_username", "ipmi_password"}
  139. return this.BatchRegister(s, titles, params)
  140. }
  141. var (
  142. Hosts HostManager
  143. )
  144. func init() {
  145. Hosts = HostManager{modules.NewComputeManager("host", "hosts",
  146. []string{"ID", "Name", "Access_mac", "Access_ip", "Ipmi_Ip",
  147. "Ovn_Mapped_Ip_Addr", "Ovn_Mapped_Ip6_Addr",
  148. "Manager_URI",
  149. "Status", "enabled", "host_status",
  150. "Guests", "Running_guests",
  151. "storage", "storage_used",
  152. "storage_virtual", "disk_used",
  153. "storage_free", "storage_commit_rate",
  154. "mem_size", "mem_used", "mem_free",
  155. "mem_commit", "cpu_count", "cpu_used",
  156. "cpu_commit", "cpu_commit_rate",
  157. "mem_commit_rate", "cpu_commit_bound",
  158. "mem_commit_bound", "node_count", "sn", "storage_type",
  159. "host_type", "version", "schedtags",
  160. "storage_size",
  161. "expired_at",
  162. "domain_id", "project_domain",
  163. "public_scope",
  164. },
  165. []string{})}
  166. modules.RegisterCompute(&Hosts)
  167. }