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.

92 lines
3.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # Example for Cloud Computing Course Master AI / GSD
  2. #
  3. # based on the tutorial: https://developer.openstack.org/firstapp-libcloud/
  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. import getpass
  8. from libcloud.compute.providers import get_driver
  9. from libcloud.compute.types import Provider
  10. # Please use 1-25 for X, as coordinated in the lab sessions
  11. # web service endpoint of the private cloud infrastructure
  12. auth_url = 'https://private-cloud2.informatik.hs-fulda.de:5000'
  13. # your username in OpenStack
  14. auth_username = 'CloudCompX'
  15. # your project in OpenStack
  16. project_name = 'CloudCompGrpX'
  17. # default region
  18. region_name = 'RegionOne'
  19. # domain to use, "default" for local accounts, "hsfulda" for LDAP of DVZ, e.g., using fdaiXXXX as auth_username
  20. domain_name = "default"
  21. # The image to look for and use for the started instance
  22. ubuntu_image_name = "Ubuntu 14.04 - Trusty Tahr - 64-bit - Cloud Based Image"
  23. # A network in the project the started instance will be attached to
  24. project_network = "CloudCompGrpX-net"
  25. def main():
  26. # get the password from user
  27. auth_password = getpass.getpass("Enter your OpenStack password:")
  28. # auth_password = "demo"
  29. # instantiate a connection to the OpenStack private cloud
  30. # make sure to include ex_domain_name and ex_force_auth_version='3.x_password', as they are needed in our
  31. # environment
  32. provider = get_driver(Provider.OPENSTACK)
  33. conn = provider(auth_username,
  34. auth_password,
  35. ex_force_auth_url=auth_url,
  36. ex_force_auth_version='3.x_password',
  37. ex_tenant_name=project_name,
  38. ex_force_service_region=region_name,
  39. ex_domain_name=domain_name)
  40. # get a list of images offered in the cloud context (e.g. Ubuntu 14.04, Ubuntu 16.04, cirros, ...)
  41. images = conn.list_images()
  42. image = ''
  43. for img in images:
  44. if img.name == ubuntu_image_name:
  45. image = img
  46. print(img)
  47. # get a list of flavors offered in the cloud context (e.g. m1.small, m1.medium, ...)
  48. flavors = conn.list_sizes()
  49. for flavor in flavors:
  50. print(flavor)
  51. # get the flavor with id 2
  52. flavor_id = '2'
  53. flavor = conn.ex_get_size(flavor_id)
  54. print(flavor)
  55. # get a list of networks in the cloud context
  56. networks = conn.ex_list_networks()
  57. network = ''
  58. for net in networks:
  59. if net.name == project_network:
  60. network = net
  61. # create a new instance with the name "testing"
  62. # make sure to provide networks (networks={network}) the instance should be attached to
  63. instance_name = 'testing'
  64. testing_instance = conn.create_node(name=instance_name, image=image, size=flavor, networks={network})
  65. print(testing_instance)
  66. # show all instances (running nodes) in the cloud context
  67. instances = conn.list_nodes()
  68. for instance in instances:
  69. print(instance)
  70. # destroy the instance we have just created
  71. conn.destroy_node(testing_instance)
  72. # method that is called when the script is started from the command line
  73. if __name__ == '__main__':
  74. main()