host.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 baidu
  15. import (
  16. "bytes"
  17. "crypto/aes"
  18. "crypto/cipher"
  19. "encoding/hex"
  20. "fmt"
  21. "net/url"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. "yunion.io/x/jsonutils"
  26. "yunion.io/x/pkg/errors"
  27. "yunion.io/x/pkg/utils"
  28. )
  29. type SHost struct {
  30. multicloud.SHostBase
  31. zone *SZone
  32. }
  33. func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
  34. vms, err := host.zone.region.GetInstances("", []string{})
  35. if err != nil {
  36. return nil, err
  37. }
  38. ivms := make([]cloudprovider.ICloudVM, len(vms))
  39. for i := 0; i < len(vms); i += 1 {
  40. vms[i].host = host
  41. ivms[i] = &vms[i]
  42. }
  43. return ivms, nil
  44. }
  45. func (host *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
  46. vm, err := host.zone.region.CreateInstance(host.zone.ZoneName, opts)
  47. if err != nil {
  48. return nil, err
  49. }
  50. vm.host = host
  51. return vm, nil
  52. }
  53. func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
  54. padding := blockSize - len(ciphertext)%blockSize
  55. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  56. return append(ciphertext, padtext...)
  57. }
  58. type ecb struct {
  59. b cipher.Block
  60. blockSize int
  61. }
  62. func newECB(b cipher.Block) *ecb {
  63. return &ecb{
  64. b: b,
  65. blockSize: b.BlockSize(),
  66. }
  67. }
  68. type ecbEncrypter ecb
  69. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  70. return (*ecbEncrypter)(newECB(b))
  71. }
  72. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  73. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  74. if len(src)%x.blockSize != 0 {
  75. panic("crypto/cipher: input not full blocks")
  76. }
  77. if len(dst) < len(src) {
  78. panic("crypto/cipher: output smaller than input")
  79. }
  80. for len(src) > 0 {
  81. x.b.Encrypt(dst, src[:x.blockSize])
  82. src = src[x.blockSize:]
  83. dst = dst[x.blockSize:]
  84. }
  85. }
  86. func AesECBEncryptHex(key, message string) (string, error) {
  87. // ECB is left out intentionally because it's insecure, check https://github.com/golang/go/issues/5597
  88. if len(key) < 16 {
  89. return "", fmt.Errorf("Invalid SecretKey")
  90. }
  91. keyBytes := []byte(key[:16])
  92. msgBytes := []byte(message)
  93. block, err := aes.NewCipher(keyBytes)
  94. if err != nil {
  95. return "", err
  96. }
  97. blockSize := block.BlockSize()
  98. msgBytes = PKCS7Padding(msgBytes, blockSize)
  99. blockMode := NewECBEncrypter(block)
  100. crypted := make([]byte, len(msgBytes))
  101. blockMode.CryptBlocks(crypted, msgBytes)
  102. return hex.EncodeToString(crypted), nil
  103. }
  104. func (region *SRegion) CreateInstance(zoneName string, opts *cloudprovider.SManagedVMCreateConfig) (*SInstance, error) {
  105. params := url.Values{}
  106. params.Set("clientToken", utils.GenRequestId(20))
  107. tags := []BaiduTag{}
  108. for k, v := range opts.Tags {
  109. tags = append(tags, BaiduTag{
  110. TagKey: k,
  111. TagValue: v,
  112. })
  113. }
  114. billing := map[string]interface{}{
  115. "paymentTiming": "Postpaid",
  116. }
  117. if opts.BillingCycle != nil {
  118. billing["paymentTiming"] = "Prepaid"
  119. reservation := map[string]interface{}{}
  120. if opts.BillingCycle.GetYears() > 0 {
  121. reservation["reservationTimeUnit"] = "year"
  122. reservation["reservationLength"] = opts.BillingCycle.GetYears()
  123. } else if opts.BillingCycle.GetMonths() > 0 {
  124. reservation["reservationTimeUnit"] = "month"
  125. reservation["reservationLength"] = opts.BillingCycle.GetMonths()
  126. }
  127. billing["reservation"] = reservation
  128. }
  129. disks := []map[string]interface{}{}
  130. for _, disk := range opts.DataDisks {
  131. disks = append(disks, map[string]interface{}{
  132. "cdsSizeInGB": disk.SizeGB,
  133. "storageType": disk.StorageType,
  134. })
  135. }
  136. body := map[string]interface{}{
  137. "imageId": opts.ExternalImageId,
  138. "spec": opts.InstanceType,
  139. "rootDiskSizeInGb": opts.SysDisk.SizeGB,
  140. "rootDiskStorageType": opts.SysDisk.StorageType,
  141. "networkCapacityInMbps": opts.PublicIpBw,
  142. "name": opts.Name,
  143. "hostname": opts.Hostname,
  144. "adminPass": opts.Password,
  145. "zoneName": zoneName,
  146. "subnetId": opts.ExternalNetworkId,
  147. "tags": tags,
  148. "userData": opts.UserData,
  149. "billing": billing,
  150. "createCdsList": disks,
  151. }
  152. if len(opts.Password) > 0 {
  153. var err error
  154. body["adminPass"], err = AesECBEncryptHex(region.client.accessKeySecret, opts.Password)
  155. if err != nil {
  156. return nil, errors.Wrapf(err, "AesECBEncryptHex")
  157. }
  158. }
  159. if len(opts.IpAddr) > 0 {
  160. body["internalIps"] = []string{opts.IpAddr}
  161. }
  162. if len(opts.PublicKey) > 0 {
  163. keypair, err := region.SyncKeypair(opts.KeypairName, opts.PublicKey)
  164. if err != nil {
  165. return nil, err
  166. }
  167. body["keypairId"] = keypair.KeypairId
  168. }
  169. if len(opts.ExternalSecgroupIds) > 0 {
  170. body["securityGroupId"] = opts.ExternalSecgroupIds[0]
  171. }
  172. resp, err := region.bccPost("v2/instanceBySpec", params, body)
  173. if err != nil {
  174. return nil, err
  175. }
  176. ret := struct {
  177. InstanceIds []string
  178. WarningList []string
  179. }{}
  180. err = resp.Unmarshal(&ret)
  181. if err != nil {
  182. return nil, errors.Wrapf(err, "Unmarshal %s", resp.String())
  183. }
  184. for _, vmId := range ret.InstanceIds {
  185. return region.GetInstance(vmId)
  186. }
  187. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "after create %s", resp.String())
  188. }
  189. func (host *SHost) GetAccessIp() string {
  190. return ""
  191. }
  192. func (host *SHost) GetAccessMac() string {
  193. return ""
  194. }
  195. func (host *SHost) GetName() string {
  196. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Name, host.zone.GetId())
  197. }
  198. func (host *SHost) GetNodeCount() int8 {
  199. return 0
  200. }
  201. func (host *SHost) GetSN() string {
  202. return ""
  203. }
  204. func (host *SHost) GetStatus() string {
  205. return api.HOST_STATUS_RUNNING
  206. }
  207. func (host *SHost) GetCpuCount() int {
  208. return 0
  209. }
  210. func (host *SHost) GetCpuDesc() string {
  211. return ""
  212. }
  213. func (host *SHost) GetCpuMhz() int {
  214. return 0
  215. }
  216. func (host *SHost) GetMemSizeMB() int {
  217. return 0
  218. }
  219. func (host *SHost) GetStorageSizeMB() int64 {
  220. return 0
  221. }
  222. func (host *SHost) GetStorageClass() string {
  223. return ""
  224. }
  225. func (host *SHost) GetStorageType() string {
  226. return api.DISK_TYPE_HYBRID
  227. }
  228. func (host *SHost) GetEnabled() bool {
  229. return true
  230. }
  231. func (host *SHost) GetIsMaintenance() bool {
  232. return false
  233. }
  234. func (host *SHost) IsEmulated() bool {
  235. return true
  236. }
  237. func (host *SHost) GetGlobalId() string {
  238. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Id, host.zone.GetId())
  239. }
  240. func (host *SHost) GetId() string {
  241. return fmt.Sprintf("%s-%s", host.zone.region.client.cpcfg.Id, host.zone.GetId())
  242. }
  243. func (host *SHost) GetHostStatus() string {
  244. return api.HOST_ONLINE
  245. }
  246. func (host *SHost) GetHostType() string {
  247. return api.HOST_TYPE_BAIDU
  248. }
  249. func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
  250. wires, err := host.zone.GetIWires()
  251. if err != nil {
  252. return nil, errors.Wrap(err, "GetIWires")
  253. }
  254. return cloudprovider.GetHostNetifs(host, wires), nil
  255. }
  256. func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  257. return host.zone.GetIStorageById(id)
  258. }
  259. func (host *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  260. return host.zone.GetIStorages()
  261. }
  262. func (host *SHost) GetIVMById(vmId string) (cloudprovider.ICloudVM, error) {
  263. vm, err := host.zone.region.GetInstance(vmId)
  264. if err != nil {
  265. return nil, err
  266. }
  267. vm.host = host
  268. return vm, nil
  269. }
  270. func (host *SHost) GetSysInfo() jsonutils.JSONObject {
  271. info := jsonutils.NewDict()
  272. info.Add(jsonutils.NewString(CLOUD_PROVIDER_BAIDU_CN), "manufacture")
  273. return info
  274. }
  275. func (host *SHost) GetVersion() string {
  276. return ""
  277. }