Last active
August 29, 2015 14:22
-
-
Save tsekiyama/ee56cc0a953368a179f9 to your computer and use it in GitHub Desktop.
Attach a Cinder volume to the host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import socket | |
import sys | |
import logging | |
from cinderclient import client as cinder_client | |
from os_brick.initiator import connector | |
# Get volume id to attach from the command argument | |
volume_id = sys.argv[1] | |
use_multipath = False | |
# Set up logger | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
sh = logging.StreamHandler() | |
sh.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
logger.addHandler(sh) | |
sh.setFormatter(formatter) | |
# Initialize Cinderclient and connector | |
client = cinder_client.Client('2', | |
os.environ['OS_USERNAME'], | |
os.environ['OS_PASSWORD'], | |
os.environ['OS_TENANT_NAME'], | |
os.environ['OS_AUTH_URL'], | |
False, | |
region_name=os.environ['OS_REGION_NAME']) | |
root_helper = 'sudo' | |
# root_helper = 'sudo cinder-rootwrap /etc/cinder/rootwrap.conf' | |
properties = connector.get_connector_properties(root_helper, | |
socket.gethostname(), | |
multipath=use_multipath, | |
enforce_multipath=False) | |
# Attach the volume to the host | |
connection_info = client.volumes.initialize_connection(volume_id, properties) | |
conn = connector.InitiatorConnector.factory( | |
connection_info['driver_volume_type'], | |
root_helper, | |
use_multipath=use_multipath) | |
device = conn.connect_volume(connection_info['data']) | |
host_device = device['path'] | |
print("\n**************************") | |
print("Connected! Host deivce = %s" % host_device) | |
# Wait for any input | |
print("Hit any key to disconnect.") | |
raw_input() | |
# Detach the volume | |
device = conn.disconnect_volume(connection_info['data'], device) | |
client.volumes.terminate_connection(volume_id, properties) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment