2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui.
6 # Copyright (C) 2009-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
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
31 print >>sys.stderr, "[Error] karesansui package was not found."
36 usage = '%prog [options]'
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()
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')
57 class SetVnc(KssCommand):
60 (opts, args) = getopts()
64 conn = KaresansuiVirtConnection(readonly=False)
66 conn.set_domain_name(opts.name)
69 if opts.passwd is not None:
71 elif opts.passwd_file is not None and os.path.exists(opts.passwd_file):
73 fp = open(opts.passwd_file, "r")
76 fcntl.lockf(fp.fileno(), fcntl.LOCK_SH)
78 passwd = fp.readline().strip("\n")
80 fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
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)
93 os.remove(opts.passwd_file)
96 elif opts.random_passwd and opts.random_passwd is not None:
97 passwd = generate_phrase(8,'23456789abcdefghijkmnpqrstuvwxyz')
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:
105 conn.guest.set_vnc(port=opts.port,
110 info = conn.guest.get_graphics_info()
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'])
119 self.logger.error('Failed to set vnc. - dom=%s' % (opts.name))
120 print >>sys.stderr, _('Failed to set vnc. - dom=%s') % (opts.name)
124 raise KssCommandException('guest not found. - dom=%s' % (opts.name))
130 if __name__ == "__main__":
132 sys.exit(target.run())