DedicatedVmAddDataDisk.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python
  2. # -*- encoding:utf-8 -*-
  3. #使用方法
  4. '''
  5. 专属宿主机批量挂盘
  6. python Vm DedicatedVmAddDataDisk.py.py 50 filename
  7. 50 代表数据盘大小值
  8. '''
  9. #filename 的文件格式示例
  10. '''
  11. 192.168.1.1
  12. 192.268.1.2
  13. '''
  14. #示例文件
  15. '''
  16. DedicatedVmAddDataDisk
  17. '''
  18. from kscore.session import get_session
  19. import sys
  20. import time
  21. from kscore.exceptions import ClientError
  22. ks_access_key_id ='ak'
  23. ks_secret_access_key = 'sk'
  24. def checkKecStateAndUpdate(ip,instanceId):
  25. check = 0
  26. while check == 0:
  27. time.sleep(20)
  28. try:
  29. kec = kecClient.describe_instances(**{'InstanceId.1': instanceId})
  30. except ClientError, e:
  31. print 'query vm by ip ' + ip + ' error '+str(e)
  32. else:
  33. for _kec in kec['InstancesSet']:
  34. status = _kec['InstanceState']['Name']
  35. if status == 'stopped':
  36. check = 1
  37. cpu = _kec['InstanceConfigure']['VCPU']
  38. mem = _kec['InstanceConfigure']['MemoryGb']
  39. print ip + " is stop "
  40. modifyKec(ip,instanceId,diskSize,cpu,mem)
  41. check = 0
  42. while check == 0:
  43. time.sleep(20)
  44. try:
  45. kec = kecClient.describe_instances(**{'InstanceId.1': instanceId})
  46. except ClientError, e:
  47. print 'query vm by ip ' + ip + ' error '+str(e)
  48. else:
  49. for _kec in kec['InstancesSet']:
  50. status = _kec['InstanceState']['Name']
  51. if status == 'stopped':
  52. check = 1
  53. print ip + " is complete modify "
  54. startKec(ip,instanceId)
  55. def modifyKec(ip,instanceId,diskSize,cpu,mem):
  56. try:
  57. kecClient.modify_instance_type(**{'InstanceId': instanceId ,'InstanceConfigure.VCPU':cpu,'InstanceConfigure.DataDiskGb':diskSize,'InstanceConfigure.MemoryGb':mem})
  58. except ClientError, e:
  59. print 'modify vm by ip ' + ip + ' error '+str(e)
  60. else:
  61. print ip + ' modify !!!!'
  62. def startKec(ip,instanceId):
  63. try:
  64. kecClient.start_instances(**{'InstanceId.1': instanceId})
  65. except ClientError, e:
  66. print 'start vm by ip ' + ip + ' error '+str(e)
  67. else:
  68. print ip + ' start !!!!'
  69. if __name__ == "__main__":
  70. if len(sys.argv) == 3:
  71. diskSize = sys.argv[1]
  72. fileName = sys.argv[2];
  73. s = get_session()
  74. s.set_credentials(ks_access_key_id, ks_secret_access_key)
  75. region = 'cn-shanghai-2'
  76. vifClient = s.create_client("vpc", region, use_ssl=True)
  77. try:
  78. f = open(fileName)
  79. except IOError:
  80. print 'File load Error'
  81. sys.exit(0)
  82. ip = f.readline()
  83. while ip:
  84. ip = ip.replace("\n", "")
  85. try:
  86. vifs = vifClient.describe_network_interfaces(**{'Filter.1.Name':'private-ip-address','Filter.1.Value.1': ip})
  87. size = len(vifs['NetworkInterfaceSet'])
  88. if size > 1:
  89. print 'find ip '+ip+' have multi vif ,please check'
  90. else:
  91. for item in vifs['NetworkInterfaceSet']:
  92. instanceId = item['InstanceId']
  93. kecClient = s.create_client("kec", region, use_ssl=True)
  94. try:
  95. kecClient.stop_instances(**{'InstanceId.1': instanceId})
  96. except ClientError, e:
  97. print 'shutdown vm by ip ' + ip + ' error '+str(e)
  98. else:
  99. print ip + ' shutdown !!!!'
  100. checkKecStateAndUpdate(ip , instanceId)
  101. except ClientError, e:
  102. print ' query vm by ip '+ip+' error '+str(e)
  103. ip = f.readline()
  104. f.close()
  105. else:
  106. print "Parameter Error Must Support action and file"