OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / start_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.iscsi import iscsi_parse_session
28     from karesansui.lib.const import ISCSI_CMD, \
29         ISCSI_CMD_OPTION_MODE,       ISCSI_CMD_OPTION_MODE_SESSION, \
30         ISCSI_CMD_OPTION_MODE_NODE,  ISCSI_CMD_OPTION_TARGETNAME, \
31         ISCSI_CMD_OPTION_PORTAL,     ISCSI_CMD_OPTION_LOGIN, \
32         ISCSI_CMD_RES_NO_ACTIVE_SESSION
33
34 except ImportError:
35     print >>sys.stderr, "[Error] karesansui package was not found."
36     sys.exit(1)
37
38 _ = load_locale()
39
40 usage = '%prog [options]'
41
42 def getopts():
43     optp = OptionParser(usage=usage, version=__version__)
44     optp.add_option('-t', '--target', dest='host', help=_('Target host name'), default=None)
45     optp.add_option('-i', '--iqn', dest='iqn', help=_('Target IQN'), default=None)
46     return optp.parse_args()
47
48 def chkopts(opts):
49     reg = re.compile("[^a-zA-Z0-9\._:-]")
50
51     if opts.iqn:
52         if reg.search(opts.iqn):
53             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-i or --iqn', opts.iqn))
54     else:
55         raise KssCommandOptException('ERROR: %s option is required.' % '-i or --iqn')
56
57     if opts.host:
58         if reg.search(opts.host):
59             raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-t or --target', opts.host))
60
61 class StartIscsi(KssCommand):
62
63     def process(self):
64         (opts, args) = getopts()
65         chkopts(opts)
66         self.up_progress(10)
67
68         already_exist = False
69
70         session_command_args = (ISCSI_CMD,
71                                 ISCSI_CMD_OPTION_MODE,
72                                 ISCSI_CMD_OPTION_MODE_SESSION
73                                 )
74
75         (session_rc, session_res) = execute_command(session_command_args)
76         if session_rc != 0:
77             raise KssCommandException('Failed to get iSCSI session. message=%s' % (session_res))
78
79         for session_line in session_res:
80             if not session_line:
81                 continue
82
83             if session_line.find(ISCSI_CMD_RES_NO_ACTIVE_SESSION) != -1:
84                 break
85
86             session = iscsi_parse_session(session_line)
87             if session['iqn'] == opts.iqn:
88                 if opts.host:
89                     if opts.host != session['hostname']:
90                         continue
91                 already_exist = True
92                 break
93
94         if already_exist:
95             self.logger.info("[target: %s]: already exists" % (opts.iqn))
96             print >>sys.stdout, _("[target: %s]: already exists") % (opts.iqn)
97         else:
98             login_command_args = [ISCSI_CMD,
99                                   ISCSI_CMD_OPTION_MODE,
100                                   ISCSI_CMD_OPTION_MODE_NODE,
101                                   ISCSI_CMD_OPTION_TARGETNAME,
102                                   opts.iqn,
103                                   ]
104             if opts.host:
105                 login_command_args.append(ISCSI_CMD_OPTION_PORTAL)
106                 login_command_args.append(opts.host)
107
108             login_command_args.append(ISCSI_CMD_OPTION_LOGIN)
109
110             (login_rc,login_res) = execute_command(login_command_args)
111             self.up_progress(50)
112
113             if login_rc != 0:
114                 raise KssCommandException('Failed to login to iSCSI. - host=%s iqn=%s message=%s' % (opts.host, opts.iqn, login_res))
115
116             for line in login_res:
117                 if not line:
118                     continue
119
120                 self.logger.info("%s" % (line))
121                 print >>sys.stdout, _("%s") % (line)
122
123         return True
124
125 if __name__ == "__main__":
126     target = StartIscsi()
127     sys.exit(target.run())