2
0

VmOperateStatus.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python
  2. #coding=utf-8
  3. #使用方法
  4. '''
  5. 虚机批量重启 关机 删除
  6. python Vm VmOperateStatus.py action filename
  7. action 可选值
  8. restart 重启
  9. shutdown 关机
  10. remove 删除
  11. '''
  12. #filename 的文件格式示例
  13. '''
  14. 192.168.1.1
  15. 192.268.1.2
  16. '''
  17. #示例文件
  18. '''
  19. VmOperateStatus
  20. '''
  21. from kscore.exceptions import ClientError
  22. from kscore.session import get_session
  23. import sys
  24. ks_access_key_id ='ak'
  25. ks_secret_access_key = 'sk'
  26. region = 'cn-shanghai-2'
  27. if __name__ == "__main__":
  28. if len(sys.argv) == 3:
  29. action = sys.argv[1]
  30. fileName = sys.argv[2];
  31. s = get_session()
  32. s.set_credentials(ks_access_key_id, ks_secret_access_key)
  33. vifClient = s.create_client("vpc", region, use_ssl=True)
  34. kecClient = s.create_client("kec", region, use_ssl=True)
  35. eipClient = s.create_client("eip", region, use_ssl=True)
  36. slbClient = s.create_client("slb", region, use_ssl=True)
  37. try:
  38. f = open(fileName)
  39. except IOError:
  40. print 'File load Error'
  41. sys.exit(0)
  42. ip = f.readline()
  43. while ip:
  44. ip = ip.replace("\n", "")
  45. try:
  46. vifs = vifClient.describe_network_interfaces(**{'Filter.1.Name':'private-ip-address','Filter.1.Value.1': ip})
  47. size = len(vifs['NetworkInterfaceSet'])
  48. if size == 0:
  49. print 'not find ip ' + ip + ' have multi vif ,please check'
  50. if size > 1:
  51. print 'find ip '+ip+' have multi vif ,please check'
  52. else:
  53. for item in vifs['NetworkInterfaceSet']:
  54. instanceId = item['InstanceId']
  55. deviceId = item['NetworkInterfaceId']
  56. if action == 'restart':
  57. try:
  58. kecClient.reboot_instances(**{'InstanceId.1': instanceId})
  59. except ClientError, e:
  60. print 'restart vm by ip ' + ip + ' error '+str(e)
  61. else:
  62. print ip + ' ' + instanceId +' restart!!!!'
  63. if action == 'shutdown':
  64. try:
  65. kecClient.stop_instances(**{'InstanceId.1': instanceId})
  66. except ClientError, e:
  67. print 'shutdown vm by ip ' + ip + ' error '+str(e)
  68. else:
  69. print ip + ' shutdown !!!!'
  70. if action == 'remove':
  71. try:
  72. eipList = eipClient.describe_addresses(**{'Filter.1.Name': 'network-interface-id', 'Filter.1.Value.1': deviceId})
  73. for eip in eipList['AddressesSet']:
  74. eipId = eip['AllocationId']
  75. eipClient.disassociate_address(AllocationId=eipId)
  76. try:
  77. rsList = slbClient.describe_instances_with_listener(**{'Filter.1.Name':'instance-id','Filter.1.Value.1':instanceId})
  78. for rs in rsList['RealServerSet']:
  79. rsId = rs['RegisterId']
  80. slbClient.deregister_instances_from_listener(RegisterId=rsId)
  81. try:
  82. kecClient.terminate_instances(**{'InstanceId.1':instanceId})
  83. print 'remove vm by ip ' + ip + ' success '
  84. except ClientError, e:
  85. print 'remove vm by ip ' + ip + ' error '+str(e)
  86. except ClientError, e:
  87. print 'DeregisterInstancesFromListener vm by ip ' + ip + ' error '+str(e)
  88. except ClientError, e:
  89. print 'DisassociateAddress vm by ip ' + ip + ' error '+str(e)
  90. except ClientError, e:
  91. print ' query vm by ip '+ip+' error '
  92. ip = f.readline()
  93. f.close()
  94. else:
  95. print "Parameter Error Must Support action and file"