2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui.
6 # Copyright (C) 2010 HDE, Inc.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
18 from optparse import OptionParser
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
25 from karesansui import __version__
26 from karesansui.lib.utils import load_locale, execute_command
27 from karesansui.lib.const import ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_NODE, \
28 ISCSI_CMD_OPTION_TARGETNAME, ISCSI_CMD_OPTION_PORTAL, ISCSI_CMD_OPTION_LOGOUT
31 print >>sys.stderr, "[Error] karesansui package was not found."
36 usage = '%prog [options]'
39 optp = OptionParser(usage=usage, version=__version__)
40 optp.add_option('-t', '--target', dest='host', help=_('Target host name'), default=None)
41 optp.add_option('-i', '--iqn', dest='iqn', help=_('Target IQN'), default=None)
42 return optp.parse_args()
45 reg = re.compile("[^a-zA-Z0-9\._:-]")
48 if reg.search(opts.iqn):
49 raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-i or --iqn', opts.iqn))
51 raise KssCommandOptException('ERROR: %s option is required.' % '-i or --iqn')
54 if reg.search(opts.host):
55 raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-t or --target', opts.host))
57 class StopIscsi(KssCommand):
60 (opts, args) = getopts()
64 logout_command_args = [ISCSI_CMD,
65 ISCSI_CMD_OPTION_MODE,
66 ISCSI_CMD_OPTION_MODE_NODE,
67 ISCSI_CMD_OPTION_TARGETNAME,
71 logout_command_args.append(ISCSI_CMD_OPTION_PORTAL)
72 logout_command_args.append(opts.host)
74 logout_command_args.append(ISCSI_CMD_OPTION_LOGOUT)
76 (logout_rc,logout_res) = execute_command(logout_command_args)
80 raise KssCommandException('Failed to logout iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, logout_res))
82 for line in logout_res:
86 self.logger.info("%s" % (line))
87 print >>sys.stdout, _("%s") % (line)
91 if __name__ == "__main__":
93 sys.exit(target.run())