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.

123 lines
3.5 KiB

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