Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import socket
  4. import sys
  5.  
  6. import logging
  7.  
  8. from cinderclient import client as cinder_client
  9. from os_brick.initiator import connector
  10.  
  11. # Get volume id to attach from the command argument
  12. volume_id = sys.argv[1]
  13. use_multipath = False
  14.  
  15. # Set up logger
  16. logger = logging.getLogger()
  17. logger.setLevel(logging.DEBUG)
  18. sh = logging.StreamHandler()
  19. sh.setLevel(logging.DEBUG)
  20. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  21. logger.addHandler(sh)
  22. sh.setFormatter(formatter)
  23.  
  24. # Initialize Cinderclient and connector
  25. client = cinder_client.Client('2',
  26. os.environ['OS_USERNAME'],
  27. os.environ['OS_PASSWORD'],
  28. os.environ['OS_TENANT_NAME'],
  29. os.environ['OS_AUTH_URL'],
  30. False,
  31. region_name=os.environ['OS_REGION_NAME'])
  32.  
  33. root_helper = 'sudo'
  34. # root_helper = 'sudo cinder-rootwrap /etc/cinder/rootwrap.conf'
  35. properties = connector.get_connector_properties(root_helper,
  36. socket.gethostname(),
  37. multipath=use_multipath,
  38. enforce_multipath=False)
  39.  
  40. # Attach the volume to the host
  41. connection_info = client.volumes.initialize_connection(volume_id, properties)
  42. conn = connector.InitiatorConnector.factory(
  43. connection_info['driver_volume_type'],
  44. root_helper,
  45. use_multipath=use_multipath)
  46. device = conn.connect_volume(connection_info['data'])
  47. host_device = device['path']
  48.  
  49. print("\n**************************")
  50. print("Connected! Host deivce = %s" % host_device)
  51.  
  52. # Wait for any input
  53. print("Hit any key to disconnect.")
  54. raw_input()
  55.  
  56. # Detach the volume
  57. device = conn.disconnect_volume(connection_info['data'], device)
  58. client.volumes.terminate_connection(volume_id, properties)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement