OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / stop_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, ISCSI_CMD_OPTION_MODE_NODE, \
28         ISCSI_CMD_OPTION_TARGETNAME, ISCSI_CMD_OPTION_PORTAL, ISCSI_CMD_OPTION_LOGOUT
29
30 except ImportError:
31     print >>sys.stderr, "[Error] karesansui package was not found."
32     sys.exit(1)
33
34 _ = load_locale()
35
36 usage = '%prog [options]'
37
38 def getopts():
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()
43
44 def chkopts(opts):
45     reg = re.compile("[^a-zA-Z0-9\._:-]")
46
47     if opts.iqn:
48         if reg.search(opts.iqn):
49             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-i or --iqn', opts.iqn))
50     else:
51         raise KssCommandOptException('ERROR: %s option is required.' % '-i or --iqn')
52
53     if opts.host:
54         if reg.search(opts.host):
55             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-t or --target', opts.host))
56
57 class StopIscsi(KssCommand):
58
59     def process(self):
60         (opts, args) = getopts()
61         chkopts(opts)
62         self.up_progress(10)
63
64         logout_command_args = [ISCSI_CMD,
65                                ISCSI_CMD_OPTION_MODE,
66                                ISCSI_CMD_OPTION_MODE_NODE,
67                                ISCSI_CMD_OPTION_TARGETNAME,
68                                opts.iqn,
69                                ]
70         if opts.host:
71             logout_command_args.append(ISCSI_CMD_OPTION_PORTAL)
72             logout_command_args.append(opts.host)
73
74         logout_command_args.append(ISCSI_CMD_OPTION_LOGOUT)
75
76         (logout_rc,logout_res) = execute_command(logout_command_args)
77         self.up_progress(50)
78
79         if logout_rc != 0:
80             raise KssCommandException('Failed to logout iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, logout_res))
81
82         for line in logout_res:
83             if not line:
84                 continue
85
86             self.logger.info("%s" % (line))
87             print >>sys.stdout, _("%s") % (line)
88
89         return True
90
91 if __name__ == "__main__":
92     target = StopIscsi()
93     sys.exit(target.run())