OSDN Git Service

ignore inactive pools. (ticket#94)
[karesansui/karesansui.git] / bin / set_vnc.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 2009-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 logging
17 import fcntl
18 from optparse import OptionParser
19
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
21
22 import __cmd__
23
24 try:
25     import karesansui
26     from karesansui import __version__
27     from karesansui.lib.virt.virt import KaresansuiVirtConnection
28     from karesansui.lib.utils import load_locale, generate_phrase
29     from karesansui.lib.const import DEFAULT_KEYMAP
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('-n', '--name', dest='name', help=_('Domain Name'))
41     optp.add_option('-P', '--passwd', dest='passwd', help=_('VNC password'), default=None)
42     optp.add_option('-w', '--passwd-file', dest='passwd_file', help=_('VNC password file'), default=None)
43     optp.add_option('-W', '--random-passwd', dest='random_passwd', action="store_true", help=_('Set random VNC password'))
44     optp.add_option('-p', '--port', dest='port', help=_('VNC port number'), default=None)
45     optp.add_option('-l', '--listen', dest='listen', help=_('VNC listen address'), default='0.0.0.0')
46     optp.add_option('-k', '--keymap', dest='keymap', help=_('VNC keyboard map'), default=DEFAULT_KEYMAP)
47     return optp.parse_args()
48
49 def chkopts(opts):
50     if not opts.name:
51         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
52     if opts.passwd_file is not None and not os.path.exists(opts.passwd_file):
53         raise KssCommandOptException('ERROR: %s is not found.' % opts.passwd_file)
54     if opts.passwd != None and opts.passwd_file != None and opts.random_passwd != None:
55         raise KssCommandOptException('ERROR: %s options are conflicted.' % '--passwd, --passwd-file and --random-passwd')
56
57 class SetVnc(KssCommand):
58
59     def process(self):
60         (opts, args) = getopts()
61         chkopts(opts)
62         self.up_progress(10)
63
64         conn = KaresansuiVirtConnection(readonly=False)
65         try:
66             conn.set_domain_name(opts.name)
67
68             passwd = None
69             if opts.passwd is not None:
70                 passwd = opts.passwd
71             elif opts.passwd_file is not None and os.path.exists(opts.passwd_file):
72                 try:
73                     fp = open(opts.passwd_file, "r")
74                     try:
75                         self.up_progress(10)
76                         fcntl.lockf(fp.fileno(), fcntl.LOCK_SH)
77                         try:
78                             passwd = fp.readline().strip("\n")
79                         finally:
80                             fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
81
82                         self.up_progress(10)
83                     finally:
84                         fp.close()
85
86                 except Exception, e:
87                     self.logger.error('Failed to read file. - dom=%s passwd_file=%s' \
88                                       % (opts.name,opts.passwd_file))
89                     print >>sys.stderr, _('Failed to read file. - dom=%s passwd_file=%s') \
90                           % (opts.name,opts.passwd_file)
91                     raise
92
93                 os.remove(opts.passwd_file)
94                 self.up_progress(10)
95
96             elif opts.random_passwd and opts.random_passwd is not None:
97                 passwd = generate_phrase(8,'23456789abcdefghijkmnpqrstuvwxyz')
98
99             active_guests = conn.list_active_guest()
100             inactive_guests = conn.list_inactive_guest()
101             if opts.name in active_guests or opts.name in inactive_guests:
102
103                 try:
104                     self.up_progress(10)
105                     conn.guest.set_vnc(port=opts.port,
106                                        listen=opts.listen,
107                                        passwd=passwd,
108                                        keymap=opts.keymap)
109                     self.up_progress(20)
110                     info = conn.guest.get_graphics_info()
111                     self.up_progress(10)
112
113                     self.logger.info('Set vnc. - dom=%s port=%s listen=%s passwd=%s keymap=%s' \
114                                      % (opts.name, info['setting']['port'], info['setting']['listen'],"xxxxxx", info['setting']['keymap']))
115                     print >>sys.stderr, _('Set vnc. - dom=%s port=%s listen=%s passwd=%s keymap=%s') \
116                           % (opts.name, info['setting']['port'], info['setting']['listen'],"xxxxxx", info['setting']['keymap'])
117
118                 except:
119                     self.logger.error('Failed to set vnc. - dom=%s' % (opts.name))
120                     print >>sys.stderr, _('Failed to set vnc. - dom=%s') % (opts.name)
121                     raise
122
123             else:
124                 raise KssCommandException('guest not found. - dom=%s' % (opts.name))
125
126             return True
127         finally:
128             conn.close()
129
130 if __name__ == "__main__":
131     target = SetVnc()
132     sys.exit(target.run())