Merge lp:~justin-fathomdb/nova/justinsb-hpsan2 into lp:~hudson-openstack/nova/trunk

Proposed by justinsb
Status: Superseded
Proposed branch: lp:~justin-fathomdb/nova/justinsb-hpsan2
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 682 lines (+480/-58)
5 files modified
nova/db/sqlalchemy/migrate_repo/versions/006_add_provider_data_to_volumes.py (+72/-0)
nova/db/sqlalchemy/models.py (+3/-0)
nova/volume/driver.py (+141/-27)
nova/volume/manager.py (+6/-2)
nova/volume/san.py (+258/-29)
To merge this branch: bzr merge lp:~justin-fathomdb/nova/justinsb-hpsan2
Reviewer Review Type Date Requested Status
Jay Pipes (community) Approve
Nova Core security contacts Pending
Review via email: mp+50378@code.launchpad.net

This proposal supersedes a proposal from 2011-02-09.

This proposal has been superseded by a proposal from 2011-02-22.

Description of the change

Support HP/LeftHand SANs. We control the SAN by SSHing and issuing CLIQ commands. Also improved the way iSCSI volumes are mounted: try to store the iSCSI connection info in the volume entity, in preference to doing discovery. Also CHAP authentication support.

CHAP support is necessary to avoid the attach-volume command require an export on the SAN device. If we had to do that, the attach command would have to hit both the volume controller and the compute controller, and that would be complex.

To post a comment you must log in.
Revision history for this message
Jay Pipes (jaypipes) wrote : Posted in a previous version of this proposal

Hi! As I said on IRC, not much I can review as far as the volume stuff goes, so here is some general feedback :)

1 === added file 'nova/db/sqlalchemy/migrate_repo/versions/003_cactus.py'

As you mentioned on IRC, I think it would be best to name this something like 003_add_provider_columns.py instead of 003_cactus.py, to make the actual migration file more descriptive of its contents...

74 + # Create tables
75 + for table in (
76 + #certificates, consoles, console_pools, instance_actions
77 + ):
78 + try:
79 + table.create()
80 + except Exception:
81 + logging.info(repr(table))
82 + logging.exception('Exception while creating table')
83 + raise
84 +
85 + # Alter column types
86 + # auth_tokens.c.user_id.alter(type=String(length=255,
87 + # convert_unicode=False,
88 + # assert_unicode=None,
89 + # unicode_error=None,
90 + # _warn_on_bytestring=False))

I recognize you probably commented that stuff out thinking that we are only using a single 003_cactus.py migration file, but if that migration file is renamed to something more descriptive (and we can have >1 migration file per release), obviously I think you should remove all that commented out code :)

Missed i18n in these locations:

139 + LOG.warn("ISCSI provider_location not stored, using discovery")
175 + LOG.debug("ISCSI Discovery: Found %s" % (location))
552 + raise exception.Error("local_path not supported")

For 175 above, please use the following instead:

LOG.debug(_("ISCSI Discovery: Found %s"), location)

Not sure if you meant to i18n this or not :)

457 + message = ("Unexpected number of virtual ips for cluster %s. "
458 + "Result=%s" %
459 + (cluster_name, ElementTree.tostring(cluster_xml)))

But if so, should be:

message = _("Unexpected number of virtual ips for cluster %(cluster)s. "
            "Result=%(result)s" % {'cluster': cluster_name,
                           'result': ElementTree.tostring(cluster_xml)})

Other than those little nits above, the only other advice I could give would be to maybe sprinkle a few LOG.debug()s around HpSanISCSIDriver methods and maybe add a bit of docstring documentation to HpSanISCSIDriver that gives the rough flow of the commands executed against the volume controller?

Cheers, and thanks for a great contribution!
jay

review: Needs Fixing
Revision history for this message
Todd Willey (xtoddx) wrote :

If we're going to do something special with the return value of driver.create_volume and driver.update_volume, those should be documented as such, probably in nova/volume/driver.py#VolumeDriver (since all the others inherit from that).

I'm not well versed in iscsi, but I'd hope there would be a way to get more specific matches in _do_iscsi_discovery, maybe by making sure a field is surrounded by spaces, etc. The line I'm referencing is "if FLAGS.iscsi_ip_prefix in target and volume_name in target:". I'd hate for a target on 10.1.2.30 to match when we're looking for 10.1.2.3 just because one is a substring of the other.

It would also be great to document what fields should be in iscsi_properties, since this is a class that is inherited other places. A central point would be better than having to look it up in _run_iscsiadmin, discover_volume, etc, getting just a few of the fields each time.

I also see in HpSanISCSIDriver you're using CHAP, but in ISCSIDriver._do_iscsi_discovery you have a note that discovery doesn't work with CHAP. Can you let me know what I'm missing?

Also, I think it is strange for the volume layer to have knowledge of the database schema, and have to know column names and have a variable it returns called 'db_update'. Maybe just renaming it 'state_changes' or something would assuage my fear, and make me feel that if there was a schema change that would be okay because we don't actually have to pass the return value to database calls.

All in all a very nice contribution. ☺

Revision history for this message
Jay Pipes (jaypipes) wrote :

Thanks for fixing up the few nits I brought up Justin. Approved from me from a style/overview perspective. I trust Todd and some others will more thoroughly review the call sequences to the volume drivers.

Cheers!
jay

review: Approve
Revision history for this message
justinsb (justin-fathomdb) wrote :

I documented the return value in VolumeDriver create_volume & create_export.

I changed the name of the model state change Dictionary from db_update to model_update, based on your suggestion. The volume drivers are tightly coupled to the model definitions, so I don't think we're violating any separation of concerns. However, the naming "DB" was violating the separation, so I renamed it. Thanks for pointing this out.

The two new properties are 'provider_auth' and 'provider_location'. They're intended to be opaque properties for the driver's own use, but the natural assumption is that _location would be data as to the location and _auth info about authentication. I could use a dictionary instead, but I tried to pick two sensible fields that are hopefully fairly generic across drivers. Using a dictionary is problematic in terms of how it gets serialized efficiently. I documented the default iSCSI use of provider_location and provider_auth on ISCSIDriver.

The iSCSI discovery code is inherited, though the indentation level changed (I'm probably not blameless on the original code though!) I agree that it's fragile, so I'd like to move away from it. HP SANs and Solaris 'SANs' no longer use it; I can fix OpenISCSI next but that's a behavioural change on code that is potentially in active use so probably belongs in its own patch. There's a note that discovery is deprecated, but this code path is there until I can fix it for OpeniSCSI, but it is the same code as before. If you think I should clean it up in this patch I can do that, but I think it's best not to.

The CHAP authentication issue with discovery will go away when discovery is removed. HP SANs don't use discovery, and they're also the only ones using CHAP right now. If CHAP is supported with discovery (?), we'd definitely have to pass some extra parameters which aren't being passed right now, and I don't see the point of making this chaneg if I'm going to remove this code anyway :-)

I documented the current iscsi_properties (though I really don't know how to format this so it doesn't look terrible)

702. By justinsb

Merged with head

703. By justinsb

Renamed db_update to model_update, and lots more documentation

704. By justinsb

Fixed my confusion in documenting the syntax of iSCSI discovery

705. By justinsb

Merged with trunk

706. By justinsb

PEP 257 fixes

707. By justinsb

Documentation fixes so that output looks better

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'nova/db/sqlalchemy/migrate_repo/versions/006_add_provider_data_to_volumes.py'
2--- nova/db/sqlalchemy/migrate_repo/versions/006_add_provider_data_to_volumes.py 1970-01-01 00:00:00 +0000
3+++ nova/db/sqlalchemy/migrate_repo/versions/006_add_provider_data_to_volumes.py 2011-02-21 23:58:13 +0000
4@@ -0,0 +1,72 @@
5+# vim: tabstop=4 shiftwidth=4 softtabstop=4
6+
7+# Copyright 2011 Justin Santa Barbara.
8+# All Rights Reserved.
9+#
10+# Licensed under the Apache License, Version 2.0 (the "License"); you may
11+# not use this file except in compliance with the License. You may obtain
12+# a copy of the License at
13+#
14+# http://www.apache.org/licenses/LICENSE-2.0
15+#
16+# Unless required by applicable law or agreed to in writing, software
17+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19+# License for the specific language governing permissions and limitations
20+# under the License.
21+
22+from sqlalchemy import *
23+from migrate import *
24+
25+from nova import log as logging
26+
27+
28+meta = MetaData()
29+
30+
31+# Table stub-definitions
32+# Just for the ForeignKey and column creation to succeed, these are not the
33+# actual definitions of instances or services.
34+#
35+volumes = Table('volumes', meta,
36+ Column('id', Integer(), primary_key=True, nullable=False),
37+ )
38+
39+
40+#
41+# New Tables
42+#
43+# None
44+
45+#
46+# Tables to alter
47+#
48+# None
49+
50+#
51+# Columns to add to existing tables
52+#
53+
54+volumes_provider_location = Column('provider_location',
55+ String(length=256,
56+ convert_unicode=False,
57+ assert_unicode=None,
58+ unicode_error=None,
59+ _warn_on_bytestring=False))
60+
61+volumes_provider_auth = Column('provider_auth',
62+ String(length=256,
63+ convert_unicode=False,
64+ assert_unicode=None,
65+ unicode_error=None,
66+ _warn_on_bytestring=False))
67+
68+
69+def upgrade(migrate_engine):
70+ # Upgrade operations go here. Don't create your own engine;
71+ # bind migrate_engine to your metadata
72+ meta.bind = migrate_engine
73+
74+ # Add columns to existing tables
75+ volumes.create_column(volumes_provider_location)
76+ volumes.create_column(volumes_provider_auth)
77
78=== modified file 'nova/db/sqlalchemy/models.py'
79--- nova/db/sqlalchemy/models.py 2011-02-17 21:39:03 +0000
80+++ nova/db/sqlalchemy/models.py 2011-02-21 23:58:13 +0000
81@@ -243,6 +243,9 @@
82 display_name = Column(String(255))
83 display_description = Column(String(255))
84
85+ provider_location = Column(String(255))
86+ provider_auth = Column(String(255))
87+
88
89 class Quota(BASE, NovaBase):
90 """Represents quota overrides for a project."""
91
92=== modified file 'nova/volume/driver.py'
93--- nova/volume/driver.py 2011-02-04 17:04:55 +0000
94+++ nova/volume/driver.py 2011-02-21 23:58:13 +0000
95@@ -21,6 +21,7 @@
96 """
97
98 import time
99+import os
100
101 from nova import exception
102 from nova import flags
103@@ -36,6 +37,8 @@
104 'Which device to export the volumes on')
105 flags.DEFINE_string('num_shell_tries', 3,
106 'number of times to attempt to run flakey shell commands')
107+flags.DEFINE_string('num_iscsi_scan_tries', 3,
108+ 'number of times to rescan iSCSI target to find volume')
109 flags.DEFINE_integer('num_shelves',
110 100,
111 'Number of vblade shelves')
112@@ -88,7 +91,8 @@
113 % FLAGS.volume_group)
114
115 def create_volume(self, volume):
116- """Creates a logical volume."""
117+ """Creates a logical volume. Can optionally return a Dictionary of
118+ changes to the volume object to be persisted."""
119 if int(volume['size']) == 0:
120 sizestr = '100M'
121 else:
122@@ -123,7 +127,8 @@
123 raise NotImplementedError()
124
125 def create_export(self, context, volume):
126- """Exports the volume."""
127+ """Exports the volume. Can optionally return a Dictionary of changes
128+ to the volume object to be persisted."""
129 raise NotImplementedError()
130
131 def remove_export(self, context, volume):
132@@ -222,7 +227,14 @@
133
134
135 class ISCSIDriver(VolumeDriver):
136- """Executes commands relating to ISCSI volumes."""
137+ """Executes commands relating to ISCSI volumes. We make use of model
138+ provider properties as follows:
139+ provider_location - if present, contains the iSCSI target information
140+ in the same format as an ietadm discovery
141+ i.e. '<target iqn>,<target portal> <target name>'
142+ provider_auth - if present, contains a space-separated triple:
143+ '<auth method> <auth username> <auth password>'. CHAP is the only
144+ auth_method in use at the moment."""
145
146 def ensure_export(self, context, volume):
147 """Synchronously recreates an export for a logical volume."""
148@@ -294,40 +306,142 @@
149 self._execute("sudo ietadm --op delete --tid=%s" %
150 iscsi_target)
151
152- def _get_name_and_portal(self, volume):
153- """Gets iscsi name and portal from volume name and host."""
154+ def _do_iscsi_discovery(self, volume):
155+ #TODO(justinsb): Deprecate discovery and use stored info
156+ #NOTE(justinsb): Discovery won't work with CHAP-secured targets (?)
157+ LOG.warn(_("ISCSI provider_location not stored, using discovery"))
158+
159 volume_name = volume['name']
160- host = volume['host']
161+
162 (out, _err) = self._execute("sudo iscsiadm -m discovery -t "
163- "sendtargets -p %s" % host)
164+ "sendtargets -p %s" % (volume['host']))
165 for target in out.splitlines():
166 if FLAGS.iscsi_ip_prefix in target and volume_name in target:
167- (location, _sep, iscsi_name) = target.partition(" ")
168- break
169- iscsi_portal = location.split(",")[0]
170- return (iscsi_name, iscsi_portal)
171+ return target
172+ return None
173+
174+ def _get_iscsi_properties(self, volume):
175+ """Gets iscsi configuration, ideally from saved information in the
176+ volume entity, but falling back to discovery if need be. The
177+ properties are:
178+ target_discovered - boolean indicating whether discovery was used,
179+ target_iqn - the IQN of the iSCSI target,
180+ target_portal - the portal of the iSCSI target,
181+ and auth_method, auth_username and auth_password
182+ - the authentication details. Right now, either
183+ auth_method is not present meaning no authentication, or
184+ auth_method == 'CHAP' meaning use CHAP with the specified
185+ credentials."""
186+
187+ properties = {}
188+
189+ location = volume['provider_location']
190+
191+ if location:
192+ # provider_location is the same format as iSCSI discovery output
193+ properties['target_discovered'] = False
194+ else:
195+ location = self._do_iscsi_discovery(volume)
196+
197+ if not location:
198+ raise exception.Error(_("Could not find iSCSI export "
199+ " for volume %s") %
200+ (volume['name']))
201+
202+ LOG.debug(_("ISCSI Discovery: Found %s") % (location))
203+ properties['target_discovered'] = True
204+
205+ (iscsi_target, _sep, iscsi_name) = location.partition(" ")
206+
207+ iscsi_portal = iscsi_target.split(",")[0]
208+
209+ properties['target_iqn'] = iscsi_name
210+ properties['target_portal'] = iscsi_portal
211+
212+ auth = volume['provider_auth']
213+
214+ if auth:
215+ (auth_method, auth_username, auth_secret) = auth.split()
216+
217+ properties['auth_method'] = auth_method
218+ properties['auth_username'] = auth_username
219+ properties['auth_password'] = auth_secret
220+
221+ return properties
222+
223+ def _run_iscsiadm(self, iscsi_properties, iscsi_command):
224+ command = ("sudo iscsiadm -m node -T %s -p %s %s" %
225+ (iscsi_properties['target_iqn'],
226+ iscsi_properties['target_portal'],
227+ iscsi_command))
228+ (out, err) = self._execute(command)
229+ LOG.debug("iscsiadm %s: stdout=%s stderr=%s" %
230+ (iscsi_command, out, err))
231+ return (out, err)
232+
233+ def _iscsiadm_update(self, iscsi_properties, property_key, property_value):
234+ iscsi_command = ("--op update -n %s -v %s" %
235+ (property_key, property_value))
236+ return self._run_iscsiadm(iscsi_properties, iscsi_command)
237
238 def discover_volume(self, volume):
239 """Discover volume on a remote host."""
240- iscsi_name, iscsi_portal = self._get_name_and_portal(volume)
241- self._execute("sudo iscsiadm -m node -T %s -p %s --login" %
242- (iscsi_name, iscsi_portal))
243- self._execute("sudo iscsiadm -m node -T %s -p %s --op update "
244- "-n node.startup -v automatic" %
245- (iscsi_name, iscsi_portal))
246- return "/dev/disk/by-path/ip-%s-iscsi-%s-lun-0" % (iscsi_portal,
247- iscsi_name)
248+ iscsi_properties = self._get_iscsi_properties(volume)
249+
250+ if not iscsi_properties['target_discovered']:
251+ self._run_iscsiadm(iscsi_properties, "--op new")
252+
253+ if iscsi_properties.get('auth_method'):
254+ self._iscsiadm_update(iscsi_properties,
255+ "node.session.auth.authmethod",
256+ iscsi_properties['auth_method'])
257+ self._iscsiadm_update(iscsi_properties,
258+ "node.session.auth.username",
259+ iscsi_properties['auth_username'])
260+ self._iscsiadm_update(iscsi_properties,
261+ "node.session.auth.password",
262+ iscsi_properties['auth_password'])
263+
264+ self._run_iscsiadm(iscsi_properties, "--login")
265+
266+ self._iscsiadm_update(iscsi_properties, "node.startup", "automatic")
267+
268+ mount_device = ("/dev/disk/by-path/ip-%s-iscsi-%s-lun-0" %
269+ (iscsi_properties['target_portal'],
270+ iscsi_properties['target_iqn']))
271+
272+ # The /dev/disk/by-path/... node is not always present immediately
273+ # TODO(justinsb): This retry-with-delay is a pattern, move to utils?
274+ tries = 0
275+ while not os.path.exists(mount_device):
276+ if tries >= FLAGS.num_iscsi_scan_tries:
277+ raise exception.Error(_("iSCSI device not found at %s") %
278+ (mount_device))
279+
280+ LOG.warn(_("ISCSI volume not yet found at: %(mount_device)s. "
281+ "Will rescan & retry. Try number: %(tries)s") %
282+ locals())
283+
284+ # The rescan isn't documented as being necessary(?), but it helps
285+ self._run_iscsiadm(iscsi_properties, "--rescan")
286+
287+ tries = tries + 1
288+ if not os.path.exists(mount_device):
289+ time.sleep(tries ** 2)
290+
291+ if tries != 0:
292+ LOG.debug(_("Found iSCSI node %(mount_device)s "
293+ "(after %(tries)s rescans)") %
294+ locals())
295+
296+ return mount_device
297
298 def undiscover_volume(self, volume):
299 """Undiscover volume on a remote host."""
300- iscsi_name, iscsi_portal = self._get_name_and_portal(volume)
301- self._execute("sudo iscsiadm -m node -T %s -p %s --op update "
302- "-n node.startup -v manual" %
303- (iscsi_name, iscsi_portal))
304- self._execute("sudo iscsiadm -m node -T %s -p %s --logout " %
305- (iscsi_name, iscsi_portal))
306- self._execute("sudo iscsiadm -m node --op delete "
307- "--targetname %s" % iscsi_name)
308+ iscsi_properties = self._get_iscsi_properties(volume)
309+ self._iscsiadm_update(iscsi_properties, "node.startup", "manual")
310+ self._run_iscsiadm(iscsi_properties, "--logout")
311+ self._run_iscsiadm(iscsi_properties, "--op delete")
312
313
314 class FakeISCSIDriver(ISCSIDriver):
315
316=== modified file 'nova/volume/manager.py'
317--- nova/volume/manager.py 2011-02-15 18:19:52 +0000
318+++ nova/volume/manager.py 2011-02-21 23:58:13 +0000
319@@ -107,10 +107,14 @@
320 vol_size = volume_ref['size']
321 LOG.debug(_("volume %(vol_name)s: creating lv of"
322 " size %(vol_size)sG") % locals())
323- self.driver.create_volume(volume_ref)
324+ model_update = self.driver.create_volume(volume_ref)
325+ if model_update:
326+ self.db.volume_update(context, volume_ref['id'], model_update)
327
328 LOG.debug(_("volume %s: creating export"), volume_ref['name'])
329- self.driver.create_export(context, volume_ref)
330+ model_update = self.driver.create_export(context, volume_ref)
331+ if model_update:
332+ self.db.volume_update(context, volume_ref['id'], model_update)
333 except Exception:
334 self.db.volume_update(context,
335 volume_ref['id'], {'status': 'error'})
336
337=== modified file 'nova/volume/san.py'
338--- nova/volume/san.py 2011-02-09 19:25:18 +0000
339+++ nova/volume/san.py 2011-02-21 23:58:13 +0000
340@@ -23,6 +23,8 @@
341 import os
342 import paramiko
343
344+from xml.etree import ElementTree
345+
346 from nova import exception
347 from nova import flags
348 from nova import log as logging
349@@ -41,37 +43,15 @@
350 'Password for SAN controller')
351 flags.DEFINE_string('san_privatekey', '',
352 'Filename of private key to use for SSH authentication')
353+flags.DEFINE_string('san_clustername', '',
354+ 'Cluster name to use for creating volumes')
355+flags.DEFINE_integer('san_ssh_port', 22,
356+ 'SSH port to use with SAN')
357
358
359 class SanISCSIDriver(ISCSIDriver):
360 """ Base class for SAN-style storage volumes
361 (storage providers we access over SSH)"""
362- #Override because SAN ip != host ip
363- def _get_name_and_portal(self, volume):
364- """Gets iscsi name and portal from volume name and host."""
365- volume_name = volume['name']
366-
367- # TODO(justinsb): store in volume, remerge with generic iSCSI code
368- host = FLAGS.san_ip
369-
370- (out, _err) = self._execute("sudo iscsiadm -m discovery -t "
371- "sendtargets -p %s" % host)
372-
373- location = None
374- find_iscsi_name = self._build_iscsi_target_name(volume)
375- for target in out.splitlines():
376- if find_iscsi_name in target:
377- (location, _sep, iscsi_name) = target.partition(" ")
378- break
379- if not location:
380- raise exception.Error(_("Could not find iSCSI export "
381- " for volume %s") %
382- volume_name)
383-
384- iscsi_portal = location.split(",")[0]
385- LOG.debug("iscsi_name=%s, iscsi_portal=%s" %
386- (iscsi_name, iscsi_portal))
387- return (iscsi_name, iscsi_portal)
388
389 def _build_iscsi_target_name(self, volume):
390 return "%s%s" % (FLAGS.iscsi_target_prefix, volume['name'])
391@@ -85,6 +65,7 @@
392 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
393 if FLAGS.san_password:
394 ssh.connect(FLAGS.san_ip,
395+ port=FLAGS.san_ssh_port,
396 username=FLAGS.san_login,
397 password=FLAGS.san_password)
398 elif FLAGS.san_privatekey:
399@@ -92,10 +73,11 @@
400 # It sucks that paramiko doesn't support DSA keys
401 privatekey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
402 ssh.connect(FLAGS.san_ip,
403+ port=FLAGS.san_ssh_port,
404 username=FLAGS.san_login,
405 pkey=privatekey)
406 else:
407- raise exception.Error("Specify san_password or san_privatekey")
408+ raise exception.Error(_("Specify san_password or san_privatekey"))
409 return ssh
410
411 def _run_ssh(self, command, check_exit_code=True):
412@@ -124,10 +106,10 @@
413 def check_for_setup_error(self):
414 """Returns an error if prerequisites aren't met"""
415 if not (FLAGS.san_password or FLAGS.san_privatekey):
416- raise exception.Error("Specify san_password or san_privatekey")
417+ raise exception.Error(_("Specify san_password or san_privatekey"))
418
419 if not (FLAGS.san_ip):
420- raise exception.Error("san_ip must be set")
421+ raise exception.Error(_("san_ip must be set"))
422
423
424 def _collect_lines(data):
425@@ -306,6 +288,17 @@
426 self._run_ssh("pfexec /usr/sbin/stmfadm add-view -t %s %s" %
427 (target_group_name, luid))
428
429+ #TODO(justinsb): Is this always 1? Does it matter?
430+ iscsi_portal_interface = '1'
431+ iscsi_portal = FLAGS.san_ip + ":3260," + iscsi_portal_interface
432+
433+ db_update = {}
434+ db_update['provider_location'] = ("%s %s" %
435+ (iscsi_portal,
436+ iscsi_name))
437+
438+ return db_update
439+
440 def remove_export(self, context, volume):
441 """Removes an export for a logical volume."""
442
443@@ -333,3 +326,239 @@
444 if self._is_lu_created(volume):
445 self._run_ssh("pfexec /usr/sbin/sbdadm delete-lu %s" %
446 (luid))
447+
448+
449+class HpSanISCSIDriver(SanISCSIDriver):
450+ """Executes commands relating to HP/Lefthand SAN ISCSI volumes.
451+ We use the CLIQ interface, over SSH.
452+
453+ Rough overview of CLIQ commands used:
454+ CLIQ createVolume (creates the volume)
455+ CLIQ getVolumeInfo (to discover the IQN etc)
456+ CLIQ getClusterInfo (to discover the iSCSI target IP address)
457+ CLIQ assignVolumeChap (exports it with CHAP security)
458+
459+ The 'trick' here is that the HP SAN enforces security by default, so
460+ normally a volume mount would need both to configure the SAN in the volume
461+ layer and do the mount on the compute layer. Multi-layer operations are
462+ not catered for at the moment in the nova architecture, so instead we
463+ share the volume using CHAP at volume creation time. Then the mount need
464+ only use those CHAP credentials, so can take place exclusively in the
465+ compute layer"""
466+
467+ def _cliq_run(self, verb, cliq_args):
468+ """Runs a CLIQ command over SSH, without doing any result parsing"""
469+ cliq_arg_strings = []
470+ for k, v in cliq_args.items():
471+ cliq_arg_strings.append(" %s=%s" % (k, v))
472+ cmd = verb + ''.join(cliq_arg_strings)
473+
474+ return self._run_ssh(cmd)
475+
476+ def _cliq_run_xml(self, verb, cliq_args, check_cliq_result=True):
477+ """Runs a CLIQ command over SSH, parsing and checking the output"""
478+ cliq_args['output'] = 'XML'
479+ (out, _err) = self._cliq_run(verb, cliq_args)
480+
481+ LOG.debug(_("CLIQ command returned %s"), out)
482+
483+ result_xml = ElementTree.fromstring(out)
484+ if check_cliq_result:
485+ response_node = result_xml.find("response")
486+ if response_node is None:
487+ msg = (_("Malformed response to CLIQ command "
488+ "%(verb)s %(cliq_args)s. Result=%(out)s") %
489+ locals())
490+ raise exception.Error(msg)
491+
492+ result_code = response_node.attrib.get("result")
493+
494+ if result_code != "0":
495+ msg = (_("Error running CLIQ command %(verb)s %(cliq_args)s. "
496+ " Result=%(out)s") %
497+ locals())
498+ raise exception.Error(msg)
499+
500+ return result_xml
501+
502+ def _cliq_get_cluster_info(self, cluster_name):
503+ """Queries for info about the cluster (including IP)"""
504+ cliq_args = {}
505+ cliq_args['clusterName'] = cluster_name
506+ cliq_args['searchDepth'] = '1'
507+ cliq_args['verbose'] = '0'
508+
509+ result_xml = self._cliq_run_xml("getClusterInfo", cliq_args)
510+
511+ return result_xml
512+
513+ def _cliq_get_cluster_vip(self, cluster_name):
514+ """Gets the IP on which a cluster shares iSCSI volumes"""
515+ cluster_xml = self._cliq_get_cluster_info(cluster_name)
516+
517+ vips = []
518+ for vip in cluster_xml.findall("response/cluster/vip"):
519+ vips.append(vip.attrib.get('ipAddress'))
520+
521+ if len(vips) == 1:
522+ return vips[0]
523+
524+ _xml = ElementTree.tostring(cluster_xml)
525+ msg = (_("Unexpected number of virtual ips for cluster "
526+ " %(cluster_name)s. Result=%(_xml)s") %
527+ locals())
528+ raise exception.Error(msg)
529+
530+ def _cliq_get_volume_info(self, volume_name):
531+ """Gets the volume info, including IQN"""
532+ cliq_args = {}
533+ cliq_args['volumeName'] = volume_name
534+ result_xml = self._cliq_run_xml("getVolumeInfo", cliq_args)
535+
536+ # Result looks like this:
537+ #<gauche version="1.0">
538+ # <response description="Operation succeeded." name="CliqSuccess"
539+ # processingTime="87" result="0">
540+ # <volume autogrowPages="4" availability="online" blockSize="1024"
541+ # bytesWritten="0" checkSum="false" clusterName="Cluster01"
542+ # created="2011-02-08T19:56:53Z" deleting="false" description=""
543+ # groupName="Group01" initialQuota="536870912" isPrimary="true"
544+ # iscsiIqn="iqn.2003-10.com.lefthandnetworks:group01:25366:vol-b"
545+ # maxSize="6865387257856" md5="9fa5c8b2cca54b2948a63d833097e1ca"
546+ # minReplication="1" name="vol-b" parity="0" replication="2"
547+ # reserveQuota="536870912" scratchQuota="4194304"
548+ # serialNumber="9fa5c8b2cca54b2948a63d833097e1ca0000000000006316"
549+ # size="1073741824" stridePages="32" thinProvision="true">
550+ # <status description="OK" value="2"/>
551+ # <permission access="rw"
552+ # authGroup="api-34281B815713B78-(trimmed)51ADD4B7030853AA7"
553+ # chapName="chapusername" chapRequired="true" id="25369"
554+ # initiatorSecret="" iqn="" iscsiEnabled="true"
555+ # loadBalance="true" targetSecret="supersecret"/>
556+ # </volume>
557+ # </response>
558+ #</gauche>
559+
560+ # Flatten the nodes into a dictionary; use prefixes to avoid collisions
561+ volume_attributes = {}
562+
563+ volume_node = result_xml.find("response/volume")
564+ for k, v in volume_node.attrib.items():
565+ volume_attributes["volume." + k] = v
566+
567+ status_node = volume_node.find("status")
568+ if not status_node is None:
569+ for k, v in status_node.attrib.items():
570+ volume_attributes["status." + k] = v
571+
572+ # We only consider the first permission node
573+ permission_node = volume_node.find("permission")
574+ if not permission_node is None:
575+ for k, v in status_node.attrib.items():
576+ volume_attributes["permission." + k] = v
577+
578+ LOG.debug(_("Volume info: %(volume_name)s => %(volume_attributes)s") %
579+ locals())
580+ return volume_attributes
581+
582+ def create_volume(self, volume):
583+ """Creates a volume."""
584+ cliq_args = {}
585+ cliq_args['clusterName'] = FLAGS.san_clustername
586+ #TODO(justinsb): Should we default to inheriting thinProvision?
587+ cliq_args['thinProvision'] = '1' if FLAGS.san_thin_provision else '0'
588+ cliq_args['volumeName'] = volume['name']
589+ if int(volume['size']) == 0:
590+ cliq_args['size'] = '100MB'
591+ else:
592+ cliq_args['size'] = '%sGB' % volume['size']
593+
594+ self._cliq_run_xml("createVolume", cliq_args)
595+
596+ volume_info = self._cliq_get_volume_info(volume['name'])
597+ cluster_name = volume_info['volume.clusterName']
598+ iscsi_iqn = volume_info['volume.iscsiIqn']
599+
600+ #TODO(justinsb): Is this always 1? Does it matter?
601+ cluster_interface = '1'
602+
603+ cluster_vip = self._cliq_get_cluster_vip(cluster_name)
604+ iscsi_portal = cluster_vip + ":3260," + cluster_interface
605+
606+ model_update = {}
607+ model_update['provider_location'] = ("%s %s" %
608+ (iscsi_portal,
609+ iscsi_iqn))
610+
611+ return model_update
612+
613+ def delete_volume(self, volume):
614+ """Deletes a volume."""
615+ cliq_args = {}
616+ cliq_args['volumeName'] = volume['name']
617+ cliq_args['prompt'] = 'false' # Don't confirm
618+
619+ self._cliq_run_xml("deleteVolume", cliq_args)
620+
621+ def local_path(self, volume):
622+ # TODO(justinsb): Is this needed here?
623+ raise exception.Error(_("local_path not supported"))
624+
625+ def ensure_export(self, context, volume):
626+ """Synchronously recreates an export for a logical volume."""
627+ return self._do_export(context, volume, force_create=False)
628+
629+ def create_export(self, context, volume):
630+ return self._do_export(context, volume, force_create=True)
631+
632+ def _do_export(self, context, volume, force_create):
633+ """Supports ensure_export and create_export"""
634+ volume_info = self._cliq_get_volume_info(volume['name'])
635+
636+ is_shared = 'permission.authGroup' in volume_info
637+
638+ model_update = {}
639+
640+ should_export = False
641+
642+ if force_create or not is_shared:
643+ should_export = True
644+ # Check that we have a project_id
645+ project_id = volume['project_id']
646+ if not project_id:
647+ project_id = context.project_id
648+
649+ if project_id:
650+ #TODO(justinsb): Use a real per-project password here
651+ chap_username = 'proj_' + project_id
652+ # HP/Lefthand requires that the password be >= 12 characters
653+ chap_password = 'project_secret_' + project_id
654+ else:
655+ msg = (_("Could not determine project for volume %s, "
656+ "can't export") %
657+ (volume['name']))
658+ if force_create:
659+ raise exception.Error(msg)
660+ else:
661+ LOG.warn(msg)
662+ should_export = False
663+
664+ if should_export:
665+ cliq_args = {}
666+ cliq_args['volumeName'] = volume['name']
667+ cliq_args['chapName'] = chap_username
668+ cliq_args['targetSecret'] = chap_password
669+
670+ self._cliq_run_xml("assignVolumeChap", cliq_args)
671+
672+ model_update['provider_auth'] = ("CHAP %s %s" %
673+ (chap_username, chap_password))
674+
675+ return model_update
676+
677+ def remove_export(self, context, volume):
678+ """Removes an export for a logical volume."""
679+ cliq_args = {}
680+ cliq_args['volumeName'] = volume['name']
681+
682+ self._cliq_run_xml("unassignVolume", cliq_args)