OSDN Git Service

ignore inactive pools. (ticket#94)
[karesansui/karesansui.git] / bin / replicate_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 """
15 <comment-ja>
16 すべてのディスクがfile形式のみ実行可能です。
17 </comment-ja>
18 <comment-en>
19 The only executable file format all disks.
20 </comment-en>
21 """
22
23 import os
24 import os.path
25 import sys
26 import signal
27 import logging
28 from optparse import OptionParser
29
30 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
31
32 import __cmd__
33
34 try:
35     import karesansui
36     from karesansui import __version__
37     from karesansui.lib.virt.virt import KaresansuiVirtConnection
38     from karesansui.lib.utils import load_locale
39 except ImportError:
40     print >>sys.stderr, "[Error] karesansui package was not found."
41     sys.exit(1)
42
43 _ = load_locale()
44
45 usage = '%prog [options]'
46
47 def getopts():
48     optp = OptionParser(usage=usage, version=__version__)
49     optp.add_option('-s', '--src-name', dest='src_name', help=_('Source domain name'))
50     optp.add_option('-d', '--dest-name', dest='name', help=_('Destination domain name'))
51     optp.add_option('-v', '--vnc-port', dest='vnc_port', help=_('VNC port number'), default=None)
52     optp.add_option('-u', '--uuid', dest='uuid', help=_('UUID'), default=None)
53     optp.add_option('-a', '--mac', dest='mac', help=_('MAC address'), default=None)
54     optp.add_option('-p', '--pool', dest='pool', help=_('Destination storage pool'))
55     return optp.parse_args()
56
57 def chkopts(opts):
58     if not opts.name:
59         raise KssCommandOptException('ERROR: -d or --dest-name option is required.')
60     if not opts.src_name:
61         raise KssCommandOptException('ERROR: -s or --src-name option is required.')
62     if not opts.pool:
63         raise KssCommandOptException('ERROR: -p or --pool option is required.')
64
65 class ReplicateGuest(KssCommand):
66
67     def process(self):
68         (opts, args) = getopts()
69         chkopts(opts)
70         self.up_progress(10)
71
72         conn = KaresansuiVirtConnection(readonly=False)
73         try:
74             # dest storage pool
75             dest_target_path = conn.get_storage_pool_targetpath(opts.pool)
76             if not dest_target_path:
77                 raise KssCommandException(
78                     "Could not get the target path of the storage pool. - path=%s" % dest_target_path)
79
80             self.dest_disk = "%s/%s/images/%s.img" \
81                              % (dest_target_path, opts.name,opts.name,)
82
83             # source storage pool
84             src_pool = conn.get_storage_pool_name_bydomain(opts.src_name, "os")
85             if not src_pool:
86                 raise KssCommandException("Source storage pool is not found.")
87             src_pool_type = conn.get_storage_pool_type(src_pool)
88             if src_pool_type == 'dir':
89                 raise KssCommandException(
90                     "Storage pool type 'dir' is not. - type=%s" % src_pool_type)
91
92             src_target_path = conn.get_storage_pool_targetpath(src_pool[0])
93             self.src_disk  = "%s/%s/images/%s.img" \
94                              % (src_target_path, opts.src_name,opts.src_name,)
95
96             if os.path.isfile(self.src_disk) is False:
97                 raise KssCommandException(
98                     'source disk image is not found. - src=%s' % (self.src_disk))
99
100             if os.path.isfile(self.dest_disk) is True:
101                 raise KssCommandException(
102                     'destination disk image already exists. - dest=%s' % (self.dest_disk))
103
104             self.up_progress(10)
105
106             active_storage_pools = conn.list_active_storage_pool()
107             self.up_progress(10)
108             if not (opts.pool in active_storage_pools):
109                 raise KssCommandException('Storage pool does not exist. - pool=%s'
110                                           % (opts.pool))
111
112             try:
113                 active_guests = conn.list_active_guest()
114                 inactive_guests = conn.list_inactive_guest()
115                 # source guestos
116                 if not (opts.src_name in active_guests or opts.src_name in inactive_guests):
117                     raise KssCommandException(
118                         "Unable to get the source guest OS. - src_name=%s" % opts.src_name)
119
120                 if (opts.name in active_guests or opts.name in inactive_guests):
121                     raise KssCommandException(
122                         "Destination Guest OS is already there. - dest_name=%s" % opts.name)
123
124                 self.up_progress(10)
125
126                 conn.replicate_guest(opts.name,
127                                      opts.src_name,
128                                      opts.pool,
129                                      opts.mac,
130                                      opts.uuid,
131                                      opts.vnc_port)
132                 self.up_progress(40)
133             except:
134                 self.logger.error('Failed to replicate guest. - src=%s dom=%s' % (opts.src_name,opts.name))
135                 raise
136         finally:
137             conn.close()
138
139         conn1 = KaresansuiVirtConnection(readonly=False)
140         try:
141             self.up_progress(10)
142             active_guests = conn1.list_active_guest()
143             inactive_guests = conn1.list_inactive_guest()
144             if opts.name in active_guests or opts.name in inactive_guests:
145                 self.logger.info('Replicated guest. - src=%s dom=%s' % (opts.src_name,opts.name))
146                 print >>sys.stderr, _('Replicated guest. - src=%s dom=%s') % (opts.src_name,opts.name)
147                 return True
148             else:
149                 raise KssCommandException(
150                     'Replicate guest not found. - src=%s dom=%s' % (opts.src_name,opts.name))
151         finally:
152             conn1.close()
153
154         return True
155
156     """
157     def sigusr1_handler(self, signum, frame):
158         if os.path.exists(self.src_disk) and os.path.exists(self.dest_disk):
159             s_size = os.path.getsize(self.src_disk)
160             d_size = os.path.getsize(self.dest_disk)
161             print int(d_size*100/s_size)
162     """
163     """
164     def sigint_handler(self, signum, frame):
165         if os.path.exists(self.dest_disk):
166             os.unlink(self.dest_disk)
167         self.logger.error('Aborted by user request.')
168         print >> sys.stderr, _("Aborted by user request.")
169         raise ""
170     """
171
172 if __name__ == "__main__":
173     target = ReplicateGuest()
174     #signal.signal(signal.SIGUSR1, target.sigusr1_handler)
175     #signal.signal(signal.SIGINT,  target.sigint_handler)
176     sys.exit(target.run())