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.

109 lines
4.0 KiB

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