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.

133 lines
5.2 KiB

  1. # import getpass
  2. # import os
  3. # import libcloud.security
  4. import time
  5. from libcloud.compute.base import NodeImage
  6. from libcloud.compute.base import NodeState
  7. from libcloud.compute.providers import get_driver as compute_get_driver
  8. from libcloud.compute.types import Provider as compute_Provider
  9. from libcloud.loadbalancer.base import Member, Algorithm
  10. from libcloud.loadbalancer.types import Provider as loadbalancer_Provider
  11. from libcloud.loadbalancer.providers import get_driver as loadbalancer_get_driver
  12. # reqs:
  13. # services: EC2 (nova, glance, neutron)
  14. # resources: 2 instances, 2 elastic ips (1 keypair, 2 security groups)
  15. # The image to look for and use for the started instance
  16. # ubuntu_image_name = 'ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20200408'
  17. ubuntu_image_id = "ami-085925f297f89fce1" # local ami id for resent ubuntu 18.04 20200408 in us-west-1
  18. # The public key to be used for SSH connection, please make sure, that you have the corresponding private key
  19. #
  20. # id_rsa.pub should look like this (standard sshd pubkey format):
  21. # ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw+J...F3w2mleybgT1w== user@HOSTNAME
  22. keypair_name = 'srieger-pub'
  23. pub_key_file = '~/.ssh/id_rsa.pub'
  24. flavor_name = 't2.nano'
  25. # default region
  26. # region_name = 'eu-central-1'
  27. # region_name = 'ap-south-1'
  28. # AWS Educate only allows us-east-1 see our AWS classroom at https://www.awseducate.com
  29. # e.g., https://www.awseducate.com/student/s/launch-classroom?classroomId=a1v3m000005mNm6AAE
  30. region_name = 'us-east-1'
  31. def main():
  32. ###########################################################################
  33. #
  34. # get credentials
  35. #
  36. ###########################################################################
  37. # see AWS Educate classroom, Account Details
  38. # access_id = getpass.win_getpass("Enter your access_id:")
  39. # secret_key = getpass.win_getpass("Enter your secret_key:")
  40. # session_token = getpass.win_getpass("Enter your session_token:")
  41. # access_id = "ASIAU..."
  42. # secret_key = "7lafW..."
  43. # session_token = "IQoJb3JpZ...EMb//..."
  44. access_id = "ASIA5ML7..."
  45. secret_key = "76lAjn..."
  46. session_token = "IQoJb3JpZ2luX2VjEBc..."
  47. ###########################################################################
  48. #
  49. # delete load balancer (Amazon AWS ELB)
  50. #
  51. ###########################################################################
  52. elb_provider = loadbalancer_get_driver(loadbalancer_Provider.ELB)
  53. elb_conn = elb_provider(access_id,
  54. secret_key,
  55. token=session_token,
  56. region=region_name)
  57. print("Deleting previously created load balancers in: " + str(elb_conn.list_balancers()))
  58. for loadbalancer in elb_conn.list_balancers():
  59. if loadbalancer.name == "lb1":
  60. elb_conn.destroy_balancer(loadbalancer)
  61. ###########################################################################
  62. #
  63. # create EC2 connection
  64. #
  65. ###########################################################################
  66. provider = compute_get_driver(compute_Provider.EC2)
  67. conn = provider(key=access_id,
  68. secret=secret_key,
  69. token=session_token,
  70. region=region_name)
  71. ###########################################################################
  72. #
  73. # clean up resources from previous demos
  74. #
  75. ###########################################################################
  76. # destroy running demo instances
  77. for instance in conn.list_nodes():
  78. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  79. 'app-services', 'app-api-1', 'app-api-2']:
  80. if instance.state is not NodeState.TERMINATED:
  81. print('Destroying Instance: %s' % instance.name)
  82. conn.destroy_node(instance)
  83. # wait until all nodes are destroyed to be able to remove depended security groups
  84. nodes_still_running = True
  85. while nodes_still_running:
  86. nodes_still_running = False
  87. time.sleep(3)
  88. instances = conn.list_nodes()
  89. for instance in instances:
  90. # if we see any demo instances still running continue to wait for them to stop
  91. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-controller', 'app-services']:
  92. if instance.state is not NodeState.TERMINATED:
  93. nodes_still_running = True
  94. print('There are still instances running, waiting for them to be destroyed...')
  95. # delete security groups, respecting dependencies (hence deleting 'control' and 'services' first)
  96. for group in conn.ex_list_security_groups():
  97. if group in ['control', 'services']:
  98. print('Deleting security group: %s' % group)
  99. conn.ex_delete_security_group(group)
  100. # now we can delete security groups 'api' and 'worker', as 'control' and 'api' depended on them, otherwise AWS will
  101. # throw DependencyViolation: resource has a dependent object
  102. for group in conn.ex_list_security_groups():
  103. if group in ['api', 'worker']:
  104. print('Deleting security group: %s' % group)
  105. conn.ex_delete_security_group(group)
  106. if __name__ == '__main__':
  107. main()