EIPChanger.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #coding=utf-8
  2. import json
  3. from config import Config
  4. from word_generater import decodestr
  5. from aliyunsdkcore.client import AcsClient
  6. from aliyunsdkcore.acs_exception.exceptions import ClientException
  7. from aliyunsdkcore.acs_exception.exceptions import ServerException
  8. from aliyunsdkvpc.request.v20160428.AllocateEipAddressRequest import AllocateEipAddressRequest
  9. from aliyunsdkvpc.request.v20160428.DescribeEipAddressesRequest import DescribeEipAddressesRequest
  10. from aliyunsdkvpc.request.v20160428.UnassociateEipAddressRequest import UnassociateEipAddressRequest
  11. from aliyunsdkvpc.request.v20160428.AssociateEipAddressRequest import AssociateEipAddressRequest
  12. from aliyunsdkvpc.request.v20160428.ReleaseEipAddressRequest import ReleaseEipAddressRequest
  13. from aliyunsdkvpc.request.v20160428.ModifyEipAddressAttributeRequest import ModifyEipAddressAttributeRequest
  14. import aliyunsdkcore
  15. import time
  16. class AliyunEip(object):
  17. def __init__(self, instanceid, region, *eipid):
  18. self.client = AcsClient(decodestr(Config.accessKeyId), decodestr(Config.accessSecret), region)
  19. self.instanceid = instanceid
  20. if eipid:
  21. self.eipid = eipid[0]
  22. else:
  23. self.eipid = 0
  24. self.bandwidth = "1"
  25. # create EIP
  26. def allocateEip(self):
  27. self.request = AllocateEipAddressRequest()
  28. self.request.set_Bandwidth(self.bandwidth)
  29. self.request.set_InternetChargeType("PayByTraffic")
  30. return self.doAaction
  31. # display EIP
  32. def describeEip(self):
  33. self.request = DescribeEipAddressesRequest()
  34. if self.eipid:
  35. self.request.set_AllocationId(self.eipid)
  36. return self.doAaction
  37. return
  38. # release EIP from vps
  39. def unassociateEip(self):
  40. self.request = UnassociateEipAddressRequest()
  41. self.request.set_AllocationId(self.eipid)
  42. self.request.set_InstanceId(self.instanceid)
  43. return self.doAaction
  44. # bonding EIP to vps
  45. def associateEip(self):
  46. self.request = AssociateEipAddressRequest()
  47. self.request.set_AllocationId(self.eipid)
  48. self.request.set_InstanceId(self.instanceid)
  49. return self.doAaction
  50. # release EIP
  51. def releaseEip(self):
  52. self.request = ReleaseEipAddressRequest()
  53. self.request.set_AllocationId(self.eipid)
  54. return self.doAaction
  55. # set EIP info
  56. # test EIP name GTGJEIP
  57. def modifyEip(self):
  58. self.request = ModifyEipAddressAttributeRequest()
  59. self.request.set_Name(self.eipname)
  60. self.request.set_AllocationId(self.eipid)
  61. return self.doAaction
  62. @property
  63. def doAaction(self):
  64. self.request.set_accept_format('json')
  65. response = self.client.do_action_with_exception(self.request)
  66. return json.loads(str(response, encoding='utf-8'))
  67. def eipInfo(self):
  68. eipinfo = self.describeEip()
  69. d = {}
  70. if eipinfo:
  71. for eip in eipinfo['EipAddresses']['EipAddress']:
  72. self.eipid = eip['AllocationId']
  73. ipaddr = eip['IpAddress']
  74. d['IpAddress'] = ipaddr
  75. if eip['InstanceType'] == 'EcsInstance':
  76. d['isbonding'] = 1
  77. else:
  78. d['isbonding'] = 0
  79. return d
  80. def run(self):
  81. try:
  82. eipinfo = self.eipInfo()
  83. sourceip = eipinfo.get('IpAddress')
  84. if eipinfo.get('IpAddress'):
  85. if eipinfo['isbonding']:
  86. self.unassociateEip()
  87. isbond = self.eipInfo().get('isbonding')
  88. while isbond:
  89. isbond = self.eipInfo().get('isbonding')
  90. time.sleep(0.5)
  91. self.releaseEip()
  92. iseipexists = self.eipInfo().get(self.eipid)
  93. while iseipexists:
  94. iseipexists = self.eipInfo().get(self.eipid)
  95. time.sleep(0.5)
  96. neweip = self.allocateEip()
  97. self.eipid = neweip["AllocationId"]
  98. #self.modifyEip()
  99. self.associateEip()
  100. eipinfo = self.eipInfo()
  101. _neweip = eipinfo['IpAddress']
  102. print("replace ip success.")
  103. return {"status":"success", "sourceip": sourceip, "neweip": _neweip}
  104. except aliyunsdkcore.acs_exception.exceptions.ServerException as e:
  105. return {"ERROR": str(e)}
  106. # except:
  107. # return {"status":"false"}
  108. #print(AliyunEip('cn-shenzhen').unassociateEip())
  109. if __name__ == "__main__":
  110. #instance = (AliyunEip('i-wz9dzuvasloqyryn6xod', "cn-hangzhou", 'eip-wz94nj3izkmakzuf3r254'))
  111. instance = (AliyunEip('i-bp11ah3f4dtxjezay2oo', "cn-hangzhou", ''))
  112. print(instance.run())
  113. # print(instance.allocateEip())