OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / create_guest.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 re
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
29     from karesansui.lib.const import DEFAULT_KEYMAP
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     # basic
42     optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
43     optp.add_option('-t', '--type', dest='type', help=_('VM Type'), default="xen")
44     optp.add_option('-m', '--mem-size', dest='mem_size', help=_('Memory size (MB)'), default=256)
45     optp.add_option('-d', '--disk', dest='disk', help=_('Disk image file'), default=None)
46     optp.add_option('-v', '--vnc-port', dest='vnc_port', help=_('VNC port number'), default=None)
47     optp.add_option('-u', '--uuid', dest='uuid', help=_('UUID'), default=None)
48     optp.add_option('-a', '--mac', dest='mac', help=_('MAC address'), default=None)
49     optp.add_option('-c', '--vcpus', dest='vcpus', help=_('Number of virtual CPUs to allocate'), default=1)
50     optp.add_option('-f', '--interface-format', dest='interface_format', help=_('Interface format'), default='b:xenbr0')
51     optp.add_option('-b', '--keymap', dest='keymap', help=_('VNC Keyboard Map'), default=DEFAULT_KEYMAP)
52     optp.add_option('-e', '--extra', dest='extra', help=_('Extra kernel options'), default=None)
53     # Storage pool only
54     optp.add_option('-P', '--storage-pool', dest='storage_pool', help=_('Storage pool name'), default=None)
55     optp.add_option('-V', '--storage-volume', dest='storage_volume', help=_('Storage volume name'), default=None)
56     # make disk only
57     optp.add_option('-D', '--disk-format', dest='disk_format', help=_('Disk format'), default=None)
58     optp.add_option('-s', '--disk-size', dest='disk_size', help=_('Disk size (MB)'), default=1024*8)
59     # IDE or SCSI or Virtio
60     optp.add_option('-B', '--bus', dest='bus', help=_('Device bus type'), default=None)
61     # ISO only
62     optp.add_option('-o', '--iso', dest='iso', help=_('ISO image'), default=None)
63     # Individual designation
64     optp.add_option('-k', '--kernel', dest='kernel', help=_('Kernel image'), default=None)
65     optp.add_option('-i', '--initrd', dest='initrd', help=_('initrd image'), default=None)
66
67     return optp.parse_args()
68
69 def chkopts(opts):
70     if not opts.name:
71         raise KssCommandOptException('ERROR: -n or --name option is required.')
72
73     if opts.disk and os.path.isfile(opts.disk) is False:
74         raise KssCommandOptException('ERROR: %s not found.' % opts.disk)
75
76
77     if opts.iso is not None:
78         if opts.kernel is not None or opts.initrd is not None:
79             raise KssCommandOptException('ERROR: %s option cannot be specified with %s options.' % ('--iso', '--kernel and --initrd',))
80
81         if os.path.isfile(opts.iso) is False:
82             raise KssCommandOptException('ERROR: The specified ISO image path does not exist. - %s' % opts.iso)
83
84         from karesansui.lib.utils import is_iso9660_filesystem_format
85         if is_iso9660_filesystem_format(opts.iso) is False:
86             raise KssCommandOptException('ERROR: The specified ISO image is not valid ISO 9660 CD-ROM filesystem data. - %s' % opts.iso)
87
88     else:
89         _r_get_net = re.compile("^(ftp|http)://")
90
91         if _r_get_net.match(opts.kernel) is None and os.path.isfile(opts.kernel) is False:
92             raise KssCommandOptException('ERROR: The specified kernel image path does not exist. - %s' % opts.kernel)
93
94         if _r_get_net.match(opts.initrd) is None and os.path.isfile(opts.initrd) is False:
95             raise KssCommandOptException('ERROR: The specified initrd image path does not exist. - %s' % opts.initrd)
96
97     """
98     TODO
99     実装お疲れ様です。
100     valid のチェックをお願いします。
101     メモ storage-pool and storage-volume is, virt.py#create_guest() is checked
102     """
103
104 class CreateGuest(KssCommand):
105
106     def process(self):
107         (opts, args) = getopts()
108         chkopts(opts)
109         self.up_progress(10)
110
111         conn = KaresansuiVirtConnection(readonly=False)
112         try:
113             conn.set_interface_format(opts.interface_format)
114
115             active_guests = conn.list_active_guest()
116             inactive_guests = conn.list_inactive_guest()
117
118             self.up_progress(10)
119
120             if opts.name in active_guests or opts.name in inactive_guests:
121                 raise KssCommandException('guest already exists. - dom=%s' % (opts.name))
122             else:
123                 try:
124                     self.up_progress(10)
125                     conn.create_guest(opts.name,
126                                       opts.type.lower(),
127                                       opts.mem_size,
128                                       opts.disk,
129                                       opts.disk_size,
130                                       opts.mac,
131                                       opts.uuid,
132                                       opts.kernel,
133                                       opts.initrd,
134                                       opts.iso,
135                                       opts.vnc_port,
136                                       opts.vcpus,
137                                       opts.extra,
138                                       opts.keymap,
139                                       opts.bus,
140                                       opts.disk_format,
141                                       opts.storage_pool,
142                                       opts.storage_volume,
143                                       )
144
145                     self.up_progress(40)
146                 except Exception, e:
147                     self.logger.error('Failed to create guest. - dom=%s - detail %s' % (opts.name, str(e.args)))
148                     print >>sys.stderr, _('Failed to create guest. - dom=%s - detail %s') % (opts.name, str(e.args))
149                     #raise KssCommandException(
150                     #    'Failed to create guest. - dom=%s' % (opts.name))
151                     raise
152
153                 self.up_progress(10)
154                 active_guests = conn.list_active_guest()
155                 inactive_guests = conn.list_inactive_guest()
156                 if not (opts.name in active_guests or opts.name in inactive_guests):
157                     raise KssCommandException('Guest OS is not recognized. - dom=%s' % (opts.name))
158
159                 self.logger.info('Created guest. - dom=%s' % (opts.name))
160                 print >>sys.stdout, 'Created guest. - dom=%s' % opts.name
161                 return True
162
163         finally:
164             conn.close()
165
166 if __name__ == "__main__":
167     target = CreateGuest()
168     sys.exit(target.run())