servers.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 models
  15. import (
  16. "fmt"
  17. "net"
  18. "strconv"
  19. "strings"
  20. "github.com/pkg/errors"
  21. "yunion.io/x/pkg/tristate"
  22. "yunion.io/x/pkg/util/netutils"
  23. )
  24. type Server struct {
  25. VirtualResource
  26. VcpuCount int
  27. VmemSize int
  28. BootOrder string
  29. DisableDelete tristate.TriState
  30. ShutdownBehavior string
  31. KeypairId string
  32. HostId string
  33. BackupHostId string
  34. Vga string
  35. Vdi string
  36. Machine string
  37. Bios string
  38. OsType string
  39. FlavorId string
  40. SecgrpId string
  41. AdminSecgrpId string
  42. Hypervisor string
  43. InstanceType string
  44. // Derived attributes
  45. Networks string
  46. Eip string
  47. Nics ServerNics
  48. }
  49. type ServerNics []ServerNic
  50. type ServerNic struct {
  51. IpAddr string `json:"ip_addr"`
  52. Mac string `json:"mac"`
  53. NetworkId string `json:"network_id"`
  54. VpcId string `json:"VpcId"`
  55. }
  56. type ServerNetworks []ServerNetwork
  57. type ServerNetwork struct {
  58. Index int
  59. Ip net.IP
  60. IpMask int
  61. MacAddr net.HardwareAddr
  62. VlanId int
  63. Name string
  64. Driver string
  65. Bw int
  66. }
  67. func (sns ServerNetworks) GetPrivateIPs() []net.IP {
  68. ips := []net.IP{}
  69. for _, sn := range sns {
  70. ipStr := sn.Ip.String()
  71. ipAddr, err := netutils.NewIPV4Addr(ipStr)
  72. if err != nil {
  73. continue
  74. }
  75. if netutils.IsPrivate(ipAddr) {
  76. ips = append(ips, sn.Ip)
  77. }
  78. }
  79. return ips
  80. }
  81. func ParseServerNetworkDetailedString(s string) (ServerNetworks, error) {
  82. sns := ServerNetworks{}
  83. lines := strings.Split(s, "\n")
  84. for _, line := range lines {
  85. line = strings.TrimSpace(line)
  86. if line == "" {
  87. continue
  88. }
  89. if !strings.HasPrefix(line, "eth") {
  90. return nil, errors.New("should be prefixed with 'eth'")
  91. }
  92. parts := strings.Split(line, "/")
  93. if len(parts) < 7 {
  94. return nil, errors.New("less than 7 parts separated by '/'")
  95. }
  96. sn := ServerNetwork{
  97. Name: parts[4],
  98. Driver: parts[5],
  99. }
  100. { // index, ipaddr
  101. idip := parts[0][3:]
  102. i := strings.IndexByte(idip, ':')
  103. if i <= 0 {
  104. return nil, errors.New("no sep ':'")
  105. }
  106. idStr, ipStr := idip[:i], idip[i+1:]
  107. {
  108. id, err := strconv.ParseUint(idStr, 10, 8)
  109. if err != nil {
  110. return nil, errors.WithMessagef(err, "bad index %s", idStr)
  111. }
  112. sn.Index = int(id)
  113. }
  114. {
  115. ip := net.ParseIP(ipStr)
  116. if ip == nil {
  117. return nil, fmt.Errorf("bad ip %s", ipStr)
  118. }
  119. ip = ip.To4()
  120. if ip == nil {
  121. return nil, fmt.Errorf("not ipv4 addr: %s", ipStr)
  122. }
  123. sn.Ip = ip
  124. }
  125. }
  126. { // ip mask
  127. masklenStr := parts[1]
  128. masklen, err := strconv.ParseUint(masklenStr, 10, 8)
  129. if err != nil {
  130. return nil, errors.WithMessagef(err, "bad network mask len: %s", masklenStr)
  131. }
  132. sn.IpMask = int(masklen)
  133. }
  134. { // macaddr
  135. macaddrStr := parts[2]
  136. macaddr, err := net.ParseMAC(macaddrStr)
  137. if err != nil {
  138. return nil, errors.WithMessagef(err, "bad macaddr: %s", macaddrStr)
  139. }
  140. sn.MacAddr = macaddr
  141. }
  142. { // vlan
  143. vlanStr := parts[3]
  144. vlan, err := strconv.ParseUint(vlanStr, 10, 16)
  145. if err != nil {
  146. return nil, errors.WithMessagef(err, "bad vlan id: %s", vlanStr)
  147. }
  148. sn.VlanId = int(vlan)
  149. }
  150. { // bw
  151. bwStr := parts[6]
  152. bw, err := strconv.ParseUint(bwStr, 10, 32)
  153. if err != nil {
  154. return nil, errors.WithMessagef(err, "bad bw: %s", bwStr)
  155. }
  156. sn.Bw = int(bw)
  157. }
  158. sns = append(sns, sn)
  159. }
  160. return sns, nil
  161. }