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.

100 lines
3.5 KiB

  1. #Python 2 to Python 3 conversion made by "Usama Tahir"
  2. # import getpass
  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
  8. # reqs:
  9. # services: nova, glance, neutron
  10. # resources: 2 instances (m1.small), 2 floating ips (1 keypair, 2 security groups)
  11. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  12. # project etc., as coordinated in the lab sessions)
  13. group_number = 30
  14. # web service endpoint of the private cloud infrastructure
  15. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  16. # your username in OpenStack
  17. auth_username = 'CloudComp' + str(group_number)
  18. # your project in OpenStack
  19. project_name = 'CloudComp' + str(group_number)
  20. # default region
  21. region_name = 'RegionOne'
  22. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  23. domain_name = "default"
  24. def main():
  25. ###########################################################################
  26. #
  27. # get credentials
  28. #
  29. ###########################################################################
  30. # if "OS_PASSWORD" in os.environ:
  31. # auth_password = os.environ["OS_PASSWORD"]
  32. # else:
  33. # auth_password = getpass.getpass("Enter your OpenStack password:")
  34. auth_password = "demo"
  35. ###########################################################################
  36. #
  37. # create connection
  38. #
  39. ###########################################################################
  40. # libcloud.security.VERIFY_SSL_CERT = False
  41. provider = get_driver(Provider.OPENSTACK)
  42. conn = provider(auth_username,
  43. auth_password,
  44. ex_force_auth_url=auth_url,
  45. ex_force_auth_version='3.x_password',
  46. ex_tenant_name=project_name,
  47. ex_force_service_region=region_name,
  48. ex_domain_name=domain_name)
  49. ###########################################################################
  50. #
  51. # clean up resources from previous demos
  52. #
  53. ###########################################################################
  54. # destroy running demo instances
  55. for instance in conn.list_nodes():
  56. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  57. 'app-services', 'app-api-1', 'app-api-2']:
  58. print(('Destroying Instance: %s' % instance.name))
  59. conn.destroy_node(instance)
  60. # wait until all nodes are destroyed to be able to remove depended security groups
  61. nodes_still_running = True
  62. while nodes_still_running:
  63. nodes_still_running = False
  64. time.sleep(3)
  65. instances = conn.list_nodes()
  66. for instance in instances:
  67. # if we see any demo instances still running continue to wait for them to stop
  68. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3', 'app-controller',
  69. 'app-services', 'app-api-1', 'app-api-2']:
  70. nodes_still_running = True
  71. print('There are still instances running, waiting for them to be destroyed...')
  72. # delete security groups
  73. for group in conn.ex_list_security_groups():
  74. if group.name in ['control', 'worker', 'api', 'services']:
  75. print(('Deleting security group: %s' % group.name))
  76. conn.ex_delete_security_group(group)
  77. if __name__ == '__main__':
  78. main()