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.

97 lines
2.9 KiB

6 years ago
  1. from __future__ import print_function
  2. import getpass
  3. import json
  4. import os
  5. import libcloud
  6. import libcloud.security
  7. import requests
  8. from libcloud.storage.providers import get_driver
  9. from libcloud.storage.types import Provider
  10. # HS-Fulda Private Cloud
  11. auth_url = 'https://192.168.72.40:5000'
  12. region_name = 'RegionOne'
  13. domain_name = "hsfulda"
  14. api_ip = '192.168.72.102'
  15. def main():
  16. ###########################################################################
  17. #
  18. # get credentials
  19. #
  20. ###########################################################################
  21. if "OS_PROJECT_NAME" in os.environ:
  22. project_name = os.environ["OS_PROJECT_NAME"]
  23. else:
  24. project_name = input("Enter your OpenStack project:")
  25. if "OS_USERNAME" in os.environ:
  26. auth_username = os.environ["OS_USERNAME"]
  27. else:
  28. auth_username = input("Enter your OpenStack username:")
  29. if "OS_PASSWORD" in os.environ:
  30. auth_password = os.environ["OS_PASSWORD"]
  31. else:
  32. auth_password = getpass.getpass("Enter your OpenStack password:")
  33. ###########################################################################
  34. #
  35. # create connection
  36. #
  37. ###########################################################################
  38. libcloud.security.VERIFY_SSL_CERT = False
  39. provider = get_driver(Provider.OPENSTACK_SWIFT)
  40. swift = provider(auth_username,
  41. auth_password,
  42. ex_force_auth_url=auth_url,
  43. ex_force_auth_version='3.x_password',
  44. ex_tenant_name=project_name,
  45. ex_force_service_region=region_name,
  46. ex_domain_name=domain_name)
  47. ###########################################################################
  48. #
  49. # create container
  50. #
  51. ###########################################################################
  52. container_name = 'fractals'
  53. containers = swift.list_containers()
  54. container = False
  55. for con in containers:
  56. if con.name == container_name:
  57. container = con
  58. if not container:
  59. container = swift.create_container(container_name=container_name)
  60. print(container)
  61. ###########################################################################
  62. #
  63. # backup existing fractals to container
  64. #
  65. ###########################################################################
  66. endpoint = 'http://' + api_ip
  67. params = { 'results_per_page': '-1' }
  68. response = requests.get('%s/v1/fractal' % endpoint, params=params)
  69. data = json.loads(response.text)
  70. for fractal in data['objects']:
  71. response = requests.get('%s/fractal/%s' % (endpoint, fractal['uuid']), stream=True)
  72. container.upload_object_via_stream(response.iter_content(), object_name=fractal['uuid'])
  73. for object_data in container.list_objects():
  74. print(object_data)
  75. if __name__ == '__main__':
  76. main()