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.

145 lines
5.6 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. UBUNTU_IMAGE_NAME = "ubuntu-22.04-jammy-x86_64"
  45. # default region
  46. REGION_NAME = 'RegionOne'
  47. # domain to use, "default" for local accounts, formerly "hsfulda" for LDAP accounts etc.
  48. # domain_name = "default"
  49. def main(): # noqa: C901 pylint: disable=too-many-branches,too-many-statements,too-many-locals,missing-function-docstring
  50. # get the password from user
  51. # auth_password = getpass.getpass("Enter your OpenStack password:")
  52. auth_password = "demo"
  53. # instantiate a connection to the OpenStack private cloud
  54. # make sure to include ex_force_auth_version='3.x_password', as needed in our environment
  55. provider = get_driver(Provider.OPENSTACK)
  56. print(f"Opening connection to {AUTH_URL} as {AUTH_USERNAME}...")
  57. conn = provider(AUTH_USERNAME,
  58. auth_password,
  59. ex_force_auth_url=AUTH_URL,
  60. ex_force_auth_version='3.x_password',
  61. ex_tenant_name=PROJECT_NAME,
  62. ex_force_service_region=REGION_NAME)
  63. # ex_domain_name=domain_name)
  64. print("Getting images and selecting desired one...")
  65. print("=========================================================================")
  66. # get a list of images offered in the cloud context (e.g. Ubuntu 20.04, cirros, ...)
  67. images = conn.list_images()
  68. image = ''
  69. for img in images:
  70. if img.name == UBUNTU_IMAGE_NAME:
  71. image = img
  72. print(img)
  73. print("Getting flavors...")
  74. print("=========================================================================")
  75. # get a list of flavors offered in the cloud context (e.g. m1.small, m1.medium, ...)
  76. flavors = conn.list_sizes()
  77. for flavor in flavors:
  78. print(flavor)
  79. print("Selecting desired flavor...")
  80. print("=========================================================================")
  81. # get the flavor with id 2
  82. flavor_id = '2'
  83. flavor = conn.ex_get_size(flavor_id)
  84. print(flavor)
  85. print("Selecting desired network...")
  86. print("=========================================================================")
  87. # get a list of networks in the cloud context
  88. networks = conn.ex_list_networks()
  89. network = ''
  90. for net in networks:
  91. if net.name == PROJECT_NETWORK:
  92. network = net
  93. print("Create instance 'testing'...")
  94. print("=========================================================================")
  95. # create a new instance with the name "testing"
  96. # make sure to provide networks (networks={network}) the instance should be attached to
  97. instance_name = 'testing'
  98. testing_instance = conn.create_node(name=instance_name, image=image, size=flavor,
  99. networks={network})
  100. print(testing_instance)
  101. print("Showing all running instances...")
  102. print("=========================================================================")
  103. # show all instances (running nodes) in the cloud context
  104. instances = conn.list_nodes()
  105. for instance in instances:
  106. print(instance)
  107. print("Destroying instance...")
  108. print("=========================================================================")
  109. # destroy the instance we have just created
  110. conn.destroy_node(testing_instance)
  111. # method that is called when the script is started from the command line
  112. if __name__ == '__main__':
  113. main()