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.

114 lines
4.1 KiB

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