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.7 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
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_domain_name and ex_force_auth_version='3.x_password', as they are needed in our
  38. # environment
  39. provider = get_driver(Provider.OPENSTACK)
  40. print("Opening connection to %s as %s..." % (auth_url, auth_username))
  41. conn = provider(auth_username,
  42. auth_password,
  43. ex_force_auth_url=auth_url,
  44. ex_force_auth_version='3.x_password',
  45. ex_tenant_name=project_name,
  46. ex_force_service_region=region_name,
  47. ex_domain_name=domain_name)
  48. print("Getting images and selecting desired one...")
  49. print("=========================================================================")
  50. # get a list of images offered in the cloud context (e.g. Ubuntu 14.04, Ubuntu 16.04, cirros, ...)
  51. images = conn.list_images()
  52. image = ''
  53. for img in images:
  54. if img.name == ubuntu_image_name:
  55. image = img
  56. print(img)
  57. print("Getting flavors...")
  58. print("=========================================================================")
  59. # get a list of flavors offered in the cloud context (e.g. m1.small, m1.medium, ...)
  60. flavors = conn.list_sizes()
  61. for flavor in flavors:
  62. print(flavor)
  63. print("Selecting desired flavor...")
  64. print("=========================================================================")
  65. # get the flavor with id 2
  66. flavor_id = '2'
  67. flavor = conn.ex_get_size(flavor_id)
  68. print(flavor)
  69. print("Selecting desired network...")
  70. print("=========================================================================")
  71. # get a list of networks in the cloud context
  72. networks = conn.ex_list_networks()
  73. network = ''
  74. for net in networks:
  75. if net.name == project_network:
  76. network = net
  77. print("Create instance 'testing'...")
  78. print("=========================================================================")
  79. # create a new instance with the name "testing"
  80. # make sure to provide networks (networks={network}) the instance should be attached to
  81. instance_name = 'testing'
  82. testing_instance = conn.create_node(name=instance_name, image=image, size=flavor, networks={network})
  83. print(testing_instance)
  84. print("Showing all running instances...")
  85. print("=========================================================================")
  86. # show all instances (running nodes) in the cloud context
  87. instances = conn.list_nodes()
  88. for instance in instances:
  89. print(instance)
  90. print("Destroying instance...")
  91. print("=========================================================================")
  92. # destroy the instance we have just created
  93. conn.destroy_node(testing_instance)
  94. # method that is called when the script is started from the command line
  95. if __name__ == '__main__':
  96. main()