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.

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