You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.2 KiB

  1. import configparser
  2. from os.path import expanduser
  3. # import getpass
  4. # import os
  5. # import libcloud.security
  6. import time
  7. from libcloud.compute.base import NodeImage
  8. from libcloud.compute.base import NodeState
  9. from libcloud.compute.providers import get_driver as compute_get_driver
  10. from libcloud.compute.types import Provider as compute_Provider
  11. from libcloud.loadbalancer.base import Member, Algorithm
  12. from libcloud.loadbalancer.types import Provider as loadbalancer_Provider
  13. from libcloud.loadbalancer.providers import get_driver as loadbalancer_get_driver
  14. home = expanduser("~")
  15. # default region
  16. # region_name = 'eu-central-1'
  17. # region_name = 'ap-south-1'
  18. # AWS Academy Labs only allow us-east-1 see our AWS Academy Lab Guide, https://awsacademy.instructure.com/login/
  19. region_name = 'us-east-1'
  20. # keypairs are kept and not deleted, they do not cost anything anyway
  21. def main():
  22. ###########################################################################
  23. #
  24. # get credentials
  25. #
  26. ###########################################################################
  27. # see AWS Academy Lab for Account Details
  28. # read credentials from file
  29. config = configparser.ConfigParser()
  30. config.read_file(open(home + '/.aws/credentials'))
  31. aws_access_key_id = config['default']['aws_access_key_id']
  32. aws_secret_access_key = config['default']['aws_secret_access_key']
  33. aws_session_token = config['default']['aws_session_token']
  34. # aws_access_key_id = "ASIAX..."
  35. # aws_secret_access_key = "eGwE12j..."
  36. # aws_session_token = "FwoGZXIvYXdzEK///////////wEaDE..."
  37. ###########################################################################
  38. #
  39. # delete load balancer (Amazon AWS ELB)
  40. #
  41. ###########################################################################
  42. elb_provider = loadbalancer_get_driver(loadbalancer_Provider.ELB)
  43. elb_conn = elb_provider(aws_access_key_id,
  44. aws_secret_access_key,
  45. token=aws_session_token,
  46. region=region_name)
  47. # print("List of load balancers: " + str(elb_conn.list_balancers()))
  48. for loadbalancer in elb_conn.list_balancers():
  49. if loadbalancer.name == "lb1":
  50. print("Deleting Load Balancer" + str(loadbalancer))
  51. elb_conn.destroy_balancer(loadbalancer)
  52. ###########################################################################
  53. #
  54. # create EC2 connection
  55. #
  56. ###########################################################################
  57. provider = compute_get_driver(compute_Provider.EC2)
  58. conn = provider(key=aws_access_key_id,
  59. secret=aws_secret_access_key,
  60. token=aws_session_token,
  61. region=region_name)
  62. ###########################################################################
  63. #
  64. # clean up resources from previous demos
  65. #
  66. ###########################################################################
  67. # destroy running demo instances
  68. for instance in conn.list_nodes():
  69. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  70. 'app-services', 'app-api-1', 'app-api-2']:
  71. if instance.state is not NodeState.TERMINATED:
  72. print('Destroying Instance: %s' % instance.name)
  73. conn.destroy_node(instance)
  74. # wait until all nodes are destroyed to be able to remove dependent security groups
  75. nodes_still_running = True
  76. while nodes_still_running:
  77. nodes_still_running = False
  78. time.sleep(3)
  79. instances = conn.list_nodes()
  80. for instance in instances:
  81. # if we see any demo instances still running continue to wait for them to stop
  82. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-controller', 'app-services']:
  83. if instance.state is not NodeState.TERMINATED:
  84. nodes_still_running = True
  85. if nodes_still_running is True:
  86. print('There are still instances running, waiting for them to be destroyed...')
  87. else:
  88. print('No instances running')
  89. # delete security groups
  90. for group in conn.ex_list_security_groups():
  91. # services depends on worker and api, so delete services first...
  92. if group in ['services']:
  93. print('Deleting security group: %s' % group)
  94. conn.ex_delete_security_group(group)
  95. for group in conn.ex_list_security_groups():
  96. # control depends on worker, so delete control before worker...
  97. if group in ['control']:
  98. print('Deleting security group: %s' % group)
  99. conn.ex_delete_security_group(group)
  100. for group in conn.ex_list_security_groups():
  101. if group in ['worker', 'api']:
  102. print('Deleting security group: %s' % group)
  103. conn.ex_delete_security_group(group)
  104. # release elastic ips
  105. for elastic_ip in conn.ex_describe_all_addresses():
  106. if elastic_ip.instance_id is None:
  107. print('Releasing unused elastic ip %s' % elastic_ip)
  108. conn.ex_release_address(elastic_ip, domain=elastic_ip.domain)
  109. if __name__ == '__main__':
  110. main()