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.

84 lines
3.0 KiB

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 = 'us-east-2'
  13. #region_name = 'ap-south-1'
  14. def main():
  15. ###########################################################################
  16. #
  17. # get credentials
  18. #
  19. ###########################################################################
  20. access_id = getpass.win_getpass("Enter your access_id:")
  21. secret_key = getpass.win_getpass("Enter your secret_key:")
  22. # access_id = "AXY..."
  23. # secret_key = "j1zomy61..."
  24. ###########################################################################
  25. #
  26. # create connection
  27. #
  28. ###########################################################################
  29. provider = get_driver(Provider.EC2)
  30. conn = provider(access_id,
  31. secret_key,
  32. region=region_name)
  33. ###########################################################################
  34. #
  35. # clean up resources from previous demos
  36. #
  37. ###########################################################################
  38. # destroy running demo instances
  39. for instance in conn.list_nodes():
  40. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  41. 'app-services', 'app-api-1', 'app-api-2']:
  42. print('Destroying Instance: %s' % instance.name)
  43. conn.destroy_node(instance)
  44. # wait until all nodes are destroyed to be able to remove depended security groups
  45. nodes_still_running = True
  46. while nodes_still_running:
  47. nodes_still_running = False
  48. time.sleep(3)
  49. instances = conn.list_nodes()
  50. for instance in instances:
  51. # if we see any demo instances still running continue to wait for them to stop
  52. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  53. 'app-services', 'app-api-1', 'app-api-2']:
  54. if instance.state.value is not 'terminated':
  55. nodes_still_running = True
  56. print('There are still instances running, waiting for them to be destroyed...')
  57. # delete security groups
  58. for group in conn.ex_list_security_groups():
  59. if group in ['control', 'worker', 'api', 'services']:
  60. print('Deleting security group: %s' % group)
  61. conn.ex_delete_security_group(group)
  62. # release elastic ips
  63. for elastic_ip in conn.ex_describe_all_addresses():
  64. if elastic_ip.instance_id is None:
  65. print('Releasing unused elastic ip %s' % elastic_ip)
  66. conn.ex_release_address(elastic_ip, domain=elastic_ip.domain)
  67. if __name__ == '__main__':
  68. main()