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.

124 lines
3.6 KiB

7 years ago
7 years ago
7 years ago
  1. #Python 2 to Python 3 conversion made by "Usama Tahir"
  2. import getpass
  3. import os
  4. import libcloud.security
  5. from libcloud.storage.providers import get_driver
  6. from libcloud.storage.types import Provider
  7. # reqs:
  8. # services: nova, glance, neutron
  9. # resources: 2 instances (m1.small), 2 floating ips (1 keypair, 2 security groups)
  10. # HS-Fulda Private Cloud
  11. auth_url = 'https://192.168.72.40:5000'
  12. region_name = 'RegionOne'
  13. domain_name = "hsfulda"
  14. def main():
  15. ###########################################################################
  16. #
  17. # get credentials
  18. #
  19. ###########################################################################
  20. if "OS_PROJECT_NAME" in os.environ:
  21. project_name = os.environ["OS_PROJECT_NAME"]
  22. else:
  23. project_name = eval(input("Enter your OpenStack project:"))
  24. if "OS_USERNAME" in os.environ:
  25. auth_username = os.environ["OS_USERNAME"]
  26. else:
  27. auth_username = eval(input("Enter your OpenStack username:"))
  28. if "OS_PASSWORD" in os.environ:
  29. auth_password = os.environ["OS_PASSWORD"]
  30. else:
  31. auth_password = getpass.getpass("Enter your OpenStack password:")
  32. ###########################################################################
  33. #
  34. # create connection
  35. #
  36. ###########################################################################
  37. libcloud.security.VERIFY_SSL_CERT = False
  38. provider = get_driver(Provider.OPENSTACK_SWIFT)
  39. swift = provider(auth_username,
  40. auth_password,
  41. ex_force_auth_url=auth_url,
  42. ex_force_auth_version='3.x_password',
  43. ex_tenant_name=project_name,
  44. ex_force_service_region=region_name,
  45. ex_domain_name=domain_name)
  46. ###########################################################################
  47. #
  48. # create container
  49. #
  50. ###########################################################################
  51. container_name = 'fractals'
  52. containers = swift.list_containers()
  53. container = False
  54. for con in containers:
  55. if con.name == container_name:
  56. container = con
  57. if not container:
  58. container = swift.create_container(container_name=container_name)
  59. print(container)
  60. print(swift.list_containers())
  61. ###########################################################################
  62. #
  63. # upload a goat
  64. #
  65. ###########################################################################
  66. object_name = 'an amazing goat'
  67. file_path = 'C:\\Users\\Sebastian\\goat.jpg'
  68. objects = container.list_objects()
  69. object_data = False
  70. for obj in objects:
  71. if obj.name == object_name:
  72. object_data = obj
  73. if not object_data:
  74. # print(os.getcwd())
  75. container = swift.get_container(container_name=container_name)
  76. object_data = container.upload_object(file_path=file_path, object_name=object_name)
  77. objects = container.list_objects()
  78. print(objects)
  79. ###########################################################################
  80. #
  81. # check goat integrity
  82. #
  83. ###########################################################################
  84. import hashlib
  85. print(hashlib.md5(open(file_path, 'rb').read()).hexdigest())
  86. ###########################################################################
  87. #
  88. # delete goat
  89. #
  90. ###########################################################################
  91. swift.delete_object(object_data)
  92. objects = container.list_objects()
  93. print(objects)
  94. if __name__ == '__main__':
  95. main()