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.

93 lines
3.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import getpass
  2. # import os
  3. # import libcloud.security
  4. import time
  5. from libcloud.compute.providers import get_driver
  6. from libcloud.compute.types import Provider, NodeState
  7. # reqs:
  8. # services: EC2 (nova, glance, neutron)
  9. # resources: 2 instances (m1.small), 2 elastic ips (1 keypair, 2 security groups)
  10. # default region
  11. # region_name = 'eu-central-1'
  12. # region_name = 'ap-south-1'
  13. # AWS Educate only allows us-east-1 see our AWS classroom at https://www.awseducate.com
  14. # e.g., https://www.awseducate.com/student/s/launch-classroom?classroomId=a1v3m000005mNm6AAE
  15. region_name = 'us-east-1'
  16. def main():
  17. ###########################################################################
  18. #
  19. # get credentials
  20. #
  21. ###########################################################################
  22. # see AWS Educate classroom, Account Details
  23. # access_id = getpass.win_getpass("Enter your access_id:")
  24. # secret_key = getpass.win_getpass("Enter your secret_key:")
  25. # session_token = getpass.win_getpass("Enter your session_token:")
  26. access_id = "ASIAX..."
  27. secret_key = "eGwE12j..."
  28. session_token = "FwoGZXIvYXdzEK///////////wEaDE..."
  29. ###########################################################################
  30. #
  31. # create connection
  32. #
  33. ###########################################################################
  34. provider = get_driver(Provider.EC2)
  35. conn = provider(access_id,
  36. secret_key,
  37. token=session_token,
  38. region=region_name)
  39. ###########################################################################
  40. #
  41. # clean up resources from previous demos
  42. #
  43. ###########################################################################
  44. # destroy running demo instances
  45. for instance in conn.list_nodes():
  46. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  47. 'app-services', 'app-api-1', 'app-api-2']:
  48. print('Destroying Instance: %s' % instance.name)
  49. conn.destroy_node(instance)
  50. # wait until all nodes are destroyed to be able to remove depended security groups
  51. nodes_still_running = True
  52. while nodes_still_running:
  53. nodes_still_running = False
  54. time.sleep(3)
  55. instances = conn.list_nodes()
  56. for instance in instances:
  57. # if we see any demo instances still running continue to wait for them to stop
  58. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  59. 'app-services', 'app-api-1', 'app-api-2']:
  60. if instance.state.value != 'terminated':
  61. nodes_still_running = True
  62. print('There are still instances running, waiting for them to be destroyed...')
  63. # delete security groups
  64. for group in conn.ex_list_security_groups():
  65. if group in ['control', 'worker', 'api', 'services']:
  66. print('Deleting security group: %s' % group)
  67. conn.ex_delete_security_group(group)
  68. # release elastic ips
  69. for elastic_ip in conn.ex_describe_all_addresses():
  70. if elastic_ip.instance_id is None:
  71. print('Releasing unused elastic ip %s' % elastic_ip)
  72. conn.ex_release_address(elastic_ip, domain=elastic_ip.domain)
  73. if __name__ == '__main__':
  74. main()