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.

126 lines
4.9 KiB

  1. """Example for Cloud Computing Course Master AI / GSD"""
  2. # This script demonstrates how to use libcloud to cleanup all instances used in the demos
  3. # for our OpenStack private cloud environment.
  4. # import getpass
  5. import os
  6. import sys
  7. import time
  8. from libcloud.compute.providers import get_driver
  9. from libcloud.compute.types import Provider
  10. # For our new Charmed OpenStack private cloud, we need to specify the path to the
  11. # root CA certificate
  12. import libcloud.security
  13. libcloud.security.CA_CERTS_PATH = ['./root-ca.crt']
  14. # Disable SSL certificate verification (not recommended for production)
  15. # libcloud.security.VERIFY_SSL_CERT = False
  16. # Please use 1-29 for X in the following variable to specify your group number.
  17. # (will be used for the username, project etc., as coordinated in the lab sessions)
  18. group_number = os.environ.get('GROUP_NUMBER')
  19. if group_number is None:
  20. sys.exit('Please set the GROUP_NUMBER environment variable to your group number,\n'
  21. 'e.g., on Windows:\n'
  22. ' "$env:GROUP_NUMBER=0" or "set GROUP_NUMBER=0"\n'
  23. 'or on Linux/MacOS:\n'
  24. ' "export GROUP_NUMBER=0" or "set GROUP_NUMBER=0"')
  25. ###############################################################################################
  26. #
  27. # no changes necessary below this line in this example
  28. #
  29. ###############################################################################################
  30. # web service endpoint of the private cloud infrastructure
  31. # auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  32. AUTH_URL = 'https://10.32.4.182:5000'
  33. # auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  34. # your username in OpenStack
  35. AUTH_USERNAME = 'CloudComp' + str(group_number)
  36. # your project in OpenStack
  37. PROJECT_NAME = 'CloudComp' + str(group_number)
  38. # A network in the project the started instance will be attached to
  39. PROJET_NETWORK = 'CloudComp' + str(group_number) + '-net'
  40. # The image to look for and use for the started instance
  41. # ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  42. UBUNTU_IMAGE_NAME = "auto-sync/ubuntu-jammy-22.04-amd64-server-20240319-disk1.img"
  43. # default region
  44. REGION_NAME = 'RegionOne'
  45. # domain to use, "default" for local accounts, formerly "hsfulda" for LDAP accounts etc.
  46. # domain_name = "default"
  47. def main(): # noqa: C901 pylint: disable=too-many-branches,too-many-statements,too-many-locals,missing-function-docstring
  48. ###########################################################################
  49. #
  50. # get credentials
  51. #
  52. ###########################################################################
  53. # if "OS_PASSWORD" in os.environ:
  54. # auth_password = os.environ["OS_PASSWORD"]
  55. # else:
  56. # auth_password = getpass.getpass("Enter your OpenStack password:")
  57. auth_password = "demo"
  58. ###########################################################################
  59. #
  60. # create connection
  61. #
  62. ###########################################################################
  63. # libcloud.security.VERIFY_SSL_CERT = False
  64. provider = get_driver(Provider.OPENSTACK)
  65. conn = provider(AUTH_USERNAME,
  66. auth_password,
  67. ex_force_auth_url=AUTH_URL,
  68. ex_force_auth_version='3.x_password',
  69. ex_tenant_name=PROJECT_NAME,
  70. ex_force_service_region=REGION_NAME)
  71. # ex_domain_name=domain_name)
  72. ###########################################################################
  73. #
  74. # clean up resources from previous demos
  75. #
  76. ###########################################################################
  77. # destroy running demo instances
  78. for instance in conn.list_nodes():
  79. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3',
  80. 'app-controller',
  81. 'app-services', 'app-api-1', 'app-api-2']:
  82. print(f'Destroying Instance: {instance.name}')
  83. conn.destroy_node(instance)
  84. # wait until all nodes are destroyed to be able to remove depended security groups
  85. nodes_still_running = True
  86. while nodes_still_running:
  87. nodes_still_running = False
  88. time.sleep(3)
  89. instances = conn.list_nodes()
  90. for instance in instances:
  91. # if we see any demo instances still running continue to wait for them to stop
  92. if instance.name in ['all-in-one', 'app-worker-1', 'app-worker-2', 'app-worker-3',
  93. 'app-controller', 'app-services', 'app-api-1', 'app-api-2']:
  94. nodes_still_running = True
  95. print('There are still instances running, waiting for them to be destroyed...')
  96. # delete security groups
  97. for group in conn.ex_list_security_groups():
  98. if group.name in ['control', 'worker', 'api', 'services']:
  99. print(f'Deleting security group: {group.name}')
  100. conn.ex_delete_security_group(group)
  101. if __name__ == '__main__':
  102. main()