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.

144 lines
5.5 KiB

  1. """Example for Cloud Computing Course Master AI / GSD"""
  2. # This script demonstrates how to use libcloud to start an instance in an OpenStack environment.
  3. # The script will start an instance, list all instances, and then destroy the instance again.
  4. #
  5. # The script uses the libcloud library to interact with the OpenStack API.
  6. # Need to install libcloud first: pip install apache-libcloud
  7. #
  8. # libCloud: https://libcloud.apache.org/
  9. # libCloud API documentation: https://libcloud.readthedocs.io/en/latest/
  10. # OpenStack API documentation: https://developer.openstack.org/
  11. # this code was initially based on the former tutorial:
  12. # https://developer.openstack.org/firstapp-libcloud/
  13. # Only needed for the password prompt:
  14. # import getpass
  15. from libcloud.compute.providers import get_driver
  16. from libcloud.compute.types import Provider
  17. # For our new Charmed OpenStack private cloud, we need to specify the path to the
  18. # root CA certificate
  19. import libcloud.security
  20. libcloud.security.CA_CERTS_PATH = ['./root-ca.crt']
  21. # Disable SSL certificate verification (not recommended for production)
  22. # libcloud.security.VERIFY_SSL_CERT = False
  23. # Please use 1-29 for 0 in the following variable to specify your group number.
  24. # (will be used for the username, project etc., as coordinated in the lab sessions)
  25. GROUP_NUMBER = 0
  26. ###############################################################################################
  27. #
  28. # no changes necessary below this line in this example
  29. #
  30. ###############################################################################################
  31. # web service endpoint of the private cloud infrastructure
  32. # auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  33. AUTH_URL = 'https://10.32.4.182:5000'
  34. # auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  35. # your username in OpenStack
  36. AUTH_USERNAME = 'CloudComp' + str(GROUP_NUMBER)
  37. # your project in OpenStack
  38. PROJECT_NAME = 'CloudComp' + str(GROUP_NUMBER)
  39. # A network in the project the started instance will be attached to
  40. PROJECT_NETWORK = 'CloudComp' + str(GROUP_NUMBER) + '-net'
  41. # The image to look for and use for the started instance
  42. # ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  43. UBUNTU_IMAGE_NAME = "auto-sync/ubuntu-jammy-22.04-amd64-server-20240319-disk1.img"
  44. # default region
  45. REGION_NAME = 'RegionOne'
  46. # domain to use, "default" for local accounts, formerly "hsfulda" for LDAP accounts etc.
  47. # domain_name = "default"
  48. def main(): # noqa: C901 pylint: disable=too-many-branches,too-many-statements,too-many-locals,missing-function-docstring
  49. # get the password from user
  50. # auth_password = getpass.getpass("Enter your OpenStack password:")
  51. auth_password = "demo"
  52. # instantiate a connection to the OpenStack private cloud
  53. # make sure to include ex_force_auth_version='3.x_password', as needed in our environment
  54. provider = get_driver(Provider.OPENSTACK)
  55. print(f"Opening connection to {AUTH_URL} as {AUTH_USERNAME}...")
  56. conn = provider(AUTH_USERNAME,
  57. auth_password,
  58. ex_force_auth_url=AUTH_URL,
  59. ex_force_auth_version='3.x_password',
  60. ex_tenant_name=PROJECT_NAME,
  61. ex_force_service_region=REGION_NAME)
  62. # ex_domain_name=domain_name)
  63. print("Getting images and selecting desired one...")
  64. print("=========================================================================")
  65. # get a list of images offered in the cloud context (e.g. Ubuntu 20.04, cirros, ...)
  66. images = conn.list_images()
  67. image = ''
  68. for img in images:
  69. if img.name == UBUNTU_IMAGE_NAME:
  70. image = img
  71. print(img)
  72. print("Getting flavors...")
  73. print("=========================================================================")
  74. # get a list of flavors offered in the cloud context (e.g. m1.small, m1.medium, ...)
  75. flavors = conn.list_sizes()
  76. for flavor in flavors:
  77. print(flavor)
  78. print("Selecting desired flavor...")
  79. print("=========================================================================")
  80. # get the flavor with id 2
  81. flavor_id = '2'
  82. flavor = conn.ex_get_size(flavor_id)
  83. print(flavor)
  84. print("Selecting desired network...")
  85. print("=========================================================================")
  86. # get a list of networks in the cloud context
  87. networks = conn.ex_list_networks()
  88. network = ''
  89. for net in networks:
  90. if net.name == PROJECT_NETWORK:
  91. network = net
  92. print("Create instance 'testing'...")
  93. print("=========================================================================")
  94. # create a new instance with the name "testing"
  95. # make sure to provide networks (networks={network}) the instance should be attached to
  96. instance_name = 'testing'
  97. testing_instance = conn.create_node(name=instance_name, image=image, size=flavor,
  98. networks={network})
  99. print(testing_instance)
  100. print("Showing all running instances...")
  101. print("=========================================================================")
  102. # show all instances (running nodes) in the cloud context
  103. instances = conn.list_nodes()
  104. for instance in instances:
  105. print(instance)
  106. print("Destroying instance...")
  107. print("=========================================================================")
  108. # destroy the instance we have just created
  109. conn.destroy_node(testing_instance)
  110. # method that is called when the script is started from the command line
  111. if __name__ == '__main__':
  112. main()