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, \
28 ISCSI_CMD_OPTION_MODE_NODE, ISCSI_CMD_OPTION_OPERATOR, ISCSI_CMD_OPTION_OPERATOR_DELETE, \
29 ISCSI_CMD_OPTION_TARGETNAME, ISCSI_CMD_OPTION_PORTAL
32 print >>sys.stderr, "[Error] karesansui package was not found."
37 usage = '%prog [options]'
40 optp = OptionParser(usage=usage, version=__version__)
41 optp.add_option('-t', '--target', dest='host', help=_('Target host name'), default=None)
42 optp.add_option('-i', '--iqn', dest='iqn', help=_('Target IQN'), default=None)
43 return optp.parse_args()
46 reg = re.compile("[^a-zA-Z0-9\._-:]")
49 if reg.search(opts.iqn):
50 raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-i or --iqn', opts.iqn))
52 raise KssCommandOptException('ERROR: %s option is required.' % '-i or --iqn')
55 if reg.search(opts.host):
56 raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-t or --target', opts.host))
58 class DeleteIscsi(KssCommand):
61 (opts, args) = getopts()
65 delete_command_args = [
67 ISCSI_CMD_OPTION_MODE,
68 ISCSI_CMD_OPTION_MODE_NODE,
69 ISCSI_CMD_OPTION_OPERATOR,
70 ISCSI_CMD_OPTION_OPERATOR_DELETE,
71 ISCSI_CMD_OPTION_TARGETNAME,
75 delete_command_args.append(ISCSI_CMD_OPTION_PORTAL)
76 delete_command_args.append(opts.host)
78 (delete_rc,delete_res) = execute_command(delete_command_args)
82 raise KssCommandException('Failed to delete iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, delete_res))
84 self.logger.info("Delete iSCSI node successful. - host=%s iqn=%s" % (opts.host, opts.iqn))
85 print >>sys.stdout, _("Delete iSCSI node successful. - host=%s iqn=%s") % (opts.host, opts.iqn)
89 if __name__ == "__main__":
90 target = DeleteIscsi()
91 sys.exit(target.run())