2
0

DedicatedVmCreate.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. #coding=utf-8
  3. from kscore.session import get_session
  4. from kscore.exceptions import ClientError
  5. import sys
  6. #使用方法
  7. '''
  8. 专属宿主机批量创建
  9. python python DedicatedVmCreate.py inputfile.csv
  10. '''
  11. #输入文件格式(csv文件)示例
  12. '''
  13. InstanceName,Vcpus,Memory,DataDiskSize,InstancePassword,PrivateIpAddress,SubnetId,SecurityGroupId,DedicatedHostId,ImageId
  14. Vm-1,4,8,100,123@123,10.0.0.1,9c29fe3e-6a16-41d0-85e3-94c52ffb1038,2d0ab207-6fa5-451c-b0f5-34d7244fd424,ac5a9f97-ab38-40ce-b284-df14282e0916,3dc0a83e-2dbf-4fd4-99c4-e620fcf4d849
  15. '''
  16. #可以参考 DedicatedVmCreate.csv
  17. #属性解释
  18. '''
  19. InstanceName 主机名称
  20. Vcpus cpu数量
  21. Memory 内存大小
  22. DataDiskSize 数据盘大小
  23. InstancePassword 主机密码
  24. PrivateIpAddress 内网IP
  25. SubnetId 子网ID
  26. SecurityGroupId 安全组ID
  27. DedicatedHostId 专属宿主机ID
  28. ImageId 镜像ID
  29. '''
  30. ak ='ak'
  31. sk = 'sk'
  32. region = 'cn-shanghai-2'
  33. def createSdkClinet(service,region):
  34. s = get_session()
  35. s.set_credentials(ak,sk)
  36. client = s.create_client(service, region, use_ssl=True)
  37. return client
  38. def createDedicatedVm(contents,content):
  39. try:
  40. client = createSdkClinet("kec", region)
  41. param = {
  42. "MaxCount": "1",
  43. "MinCount": "1",
  44. "ImageId": contents[9],
  45. "SubnetId": contents[6],
  46. "InstanceName": contents[0],
  47. "InstancePassword": contents[4],
  48. "SecurityGroupId": contents[7],
  49. "DataDiskGb": contents[3],
  50. "ChargeType": "Daily",
  51. "InstanceType": "DVM1.NONE",
  52. "PrivateIpAddress": contents[5],
  53. "PurchaseTime": "0",
  54. "DedicatedHostId": contents[8],
  55. "InstanceConfigure.VCPU": contents[1],
  56. "InstanceConfigure.MemoryGb": contents[2],
  57. "InstanceConfigure.DataDiskGb": contents[3]
  58. }
  59. client.run_instances(**param)
  60. print param["InstanceName"]+" create success "
  61. except ClientError, e:
  62. print content+" process error,please check error is "+str(e)
  63. def readConfigFileAndProcess():
  64. try:
  65. f = open(sys.argv[1])
  66. except IOError:
  67. print 'File load Error'
  68. sys.exit(0)
  69. content = f.readline()
  70. content = content.replace("\n", "")
  71. while content:
  72. content = f.readline()
  73. content = content.replace("\n", "")
  74. if content != '':
  75. createDedicatedVm(content.split(','),content)
  76. if __name__ == '__main__':
  77. readConfigFileAndProcess()