OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / delete_iscsi.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 2010 HDE, Inc.
7 #
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.
12 #
13
14 import os
15 import sys
16 import re
17 import logging
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
21 import __cmd__
22
23 try:
24     import karesansui
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
30
31 except ImportError:
32     print >>sys.stderr, "[Error] karesansui package was not found."
33     sys.exit(1)
34
35 _ = load_locale()
36
37 usage = '%prog [options]'
38
39 def getopts():
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()
44
45 def chkopts(opts):
46     reg = re.compile("[^a-zA-Z0-9\._-:]")
47
48     if opts.iqn:
49         if reg.search(opts.iqn):
50             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-i or --iqn', opts.iqn))
51     else:
52         raise KssCommandOptException('ERROR: %s option is required.' % '-i or --iqn')
53
54     if opts.host:
55         if reg.search(opts.host):
56             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-t or --target', opts.host))
57
58 class DeleteIscsi(KssCommand):
59
60     def process(self):
61         (opts, args) = getopts()
62         chkopts(opts)
63         self.up_progress(10)
64
65         delete_command_args = [
66             ISCSI_CMD,
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,
72             opts.iqn,
73             ]
74         if opts.host:
75             delete_command_args.append(ISCSI_CMD_OPTION_PORTAL)
76             delete_command_args.append(opts.host)
77
78         (delete_rc,delete_res) = execute_command(delete_command_args)
79         self.up_progress(50)
80
81         if delete_rc != 0:
82             raise KssCommandException('Failed to delete iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, delete_res))
83
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)
86
87         return True
88
89 if __name__ == "__main__":
90     target = DeleteIscsi()
91     sys.exit(target.run())