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.

125 lines
4.6 KiB

5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
  1. # Example for Cloud Computing Course Master AI / GSD
  2. #
  3. # uses libCloud: https://libcloud.apache.org/
  4. # libCloud API documentation: https://libcloud.readthedocs.io/en/latest/
  5. # OpenStack API documentation: https://developer.openstack.org/
  6. # this code was initially based on the former tutorial: https://developer.openstack.org/firstapp-libcloud/
  7. import getpass
  8. from libcloud.compute.providers import get_driver
  9. from libcloud.compute.types import Provider
  10. # Please use 1-29 for X in the following variable to specify your group number. (will be used for the username,
  11. # project etc., as coordinated in the lab sessions)
  12. group_number = 30
  13. ########################################################################################################################
  14. #
  15. # no changes necessary below this line in this example
  16. #
  17. ########################################################################################################################
  18. # web service endpoint of the private cloud infrastructure
  19. auth_url = 'https://private-cloud.informatik.hs-fulda.de:5000'
  20. # your username in OpenStack
  21. auth_username = 'CloudComp' + str(group_number)
  22. # your project in OpenStack
  23. project_name = 'CloudComp' + str(group_number)
  24. # A network in the project the started instance will be attached to
  25. project_network = 'CloudComp' + str(group_number) + '-net'
  26. # The image to look for and use for the started instance
  27. ubuntu_image_name = "Ubuntu 18.04 - Bionic Beaver - 64-bit - Cloud Based Image"
  28. # default region
  29. region_name = 'RegionOne'
  30. # domain to use, "default" for local accounts, "hsfulda" for RZ LDAP, e.g., using fdaiXXXX as auth_username
  31. # domain_name = "default"
  32. def main():
  33. # get the password from user
  34. # auth_password = getpass.getpass("Enter your OpenStack password:")
  35. auth_password = "demo"
  36. # instantiate a connection to the OpenStack private cloud
  37. # make sure to include ex_force_auth_version='3.x_password', as needed in our environment
  38. provider = get_driver(Provider.OPENSTACK)
  39. print("Opening connection to %s as %s..." % (auth_url, auth_username))
  40. conn = provider(auth_username,
  41. auth_password,
  42. ex_force_auth_url=auth_url,
  43. ex_force_auth_version='3.x_password',
  44. ex_tenant_name=project_name,
  45. ex_force_service_region=region_name)
  46. # ex_domain_name=domain_name)
  47. print("Getting images and selecting desired one...")
  48. print("=========================================================================")
  49. # get a list of images offered in the cloud context (e.g. Ubuntu 20.04, cirros, ...)
  50. images = conn.list_images()
  51. image = ''
  52. for img in images:
  53. if img.name == ubuntu_image_name:
  54. image = img
  55. print(img)
  56. print("Getting flavors...")
  57. print("=========================================================================")
  58. # get a list of flavors offered in the cloud context (e.g. m1.small, m1.medium, ...)
  59. flavors = conn.list_sizes()
  60. for flavor in flavors:
  61. print(flavor)
  62. print("Selecting desired flavor...")
  63. print("=========================================================================")
  64. # get the flavor with id 2
  65. flavor_id = '2'
  66. flavor = conn.ex_get_size(flavor_id)
  67. print(flavor)
  68. print("Selecting desired network...")
  69. print("=========================================================================")
  70. # get a list of networks in the cloud context
  71. networks = conn.ex_list_networks()
  72. network = ''
  73. for net in networks:
  74. if net.name == project_network:
  75. network = net
  76. print("Create instance 'testing'...")
  77. print("=========================================================================")
  78. # create a new instance with the name "testing"
  79. # make sure to provide networks (networks={network}) the instance should be attached to
  80. instance_name = 'testing'
  81. testing_instance = conn.create_node(name=instance_name, image=image, size=flavor, networks={network})
  82. print(testing_instance)
  83. print("Showing all running instances...")
  84. print("=========================================================================")
  85. # show all instances (running nodes) in the cloud context
  86. instances = conn.list_nodes()
  87. for instance in instances:
  88. print(instance)
  89. print("Destroying instance...")
  90. print("=========================================================================")
  91. # destroy the instance we have just created
  92. conn.destroy_node(testing_instance)
  93. # method that is called when the script is started from the command line
  94. if __name__ == '__main__':
  95. main()