root / scripts / gnt-backup @ c38c44ad
History | View | Annotate | Download (10.3 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2006, 2007 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
|
22 |
# pylint: disable-msg=W0401,W0614 |
23 |
# W0401: Wildcard import ganeti.cli |
24 |
# W0614: Unused import %s from wildcard import (since we need cli) |
25 |
|
26 |
import sys |
27 |
|
28 |
from ganeti.cli import * |
29 |
from ganeti import opcodes |
30 |
from ganeti import constants |
31 |
from ganeti import errors |
32 |
from ganeti import utils |
33 |
|
34 |
|
35 |
_VALUE_TRUE = "true" |
36 |
|
37 |
|
38 |
def PrintExportList(opts, args): |
39 |
"""Prints a list of all the exported system images. |
40 |
|
41 |
@param opts: the command line options selected by the user |
42 |
@type args: list |
43 |
@param args: should be an empty list |
44 |
@rtype: int |
45 |
@return: the desired exit code |
46 |
|
47 |
""" |
48 |
exports = GetClient().QueryExports(opts.nodes, False) |
49 |
retcode = 0 |
50 |
for node in exports: |
51 |
ToStdout("Node: %s", node) |
52 |
ToStdout("Exports:") |
53 |
if isinstance(exports[node], list): |
54 |
for instance_name in exports[node]: |
55 |
ToStdout("\t%s", instance_name) |
56 |
else: |
57 |
ToStdout(" Could not get exports list") |
58 |
retcode = 1 |
59 |
return retcode |
60 |
|
61 |
|
62 |
def ExportInstance(opts, args): |
63 |
"""Export an instance to an image in the cluster. |
64 |
|
65 |
@param opts: the command line options selected by the user |
66 |
@type args: list |
67 |
@param args: should contain only one element, the name |
68 |
of the instance to be exported |
69 |
@rtype: int |
70 |
@return: the desired exit code |
71 |
|
72 |
""" |
73 |
op = opcodes.OpExportInstance(instance_name=args[0], |
74 |
target_node=opts.node, |
75 |
shutdown=opts.shutdown) |
76 |
|
77 |
fin_resu, dlist = SubmitOpCode(op) |
78 |
if not isinstance(dlist, list): |
79 |
ToStderr("Cannot parse execution results") |
80 |
return 1 |
81 |
tot_dsk = len(dlist) |
82 |
# TODO: handle diskless instances |
83 |
if dlist.count(False) == 0: |
84 |
# all OK |
85 |
rcode = 0 |
86 |
elif dlist.count(True) == 0: |
87 |
ToStderr("Error: No disks were backed up successfully." |
88 |
" The export doesn't have any valid data," |
89 |
" it is recommended to retry the operation.") |
90 |
rcode = 1 |
91 |
else: |
92 |
ToStderr("Partial export failure: %d disks backed up, %d disks failed.", |
93 |
dlist.count(True), dlist.count(False)) |
94 |
rcode = 2 |
95 |
if not fin_resu: |
96 |
rcode = 1 |
97 |
return rcode |
98 |
|
99 |
def ImportInstance(opts, args): |
100 |
"""Add an instance to the cluster. |
101 |
|
102 |
@param opts: the command line options selected by the user |
103 |
@type args: list |
104 |
@param args: should contain only one element, the new instance name |
105 |
@rtype: int |
106 |
@return: the desired exit code |
107 |
|
108 |
""" |
109 |
instance = args[0] |
110 |
|
111 |
(pnode, snode) = SplitNodeOption(opts.node) |
112 |
|
113 |
hypervisor = None |
114 |
hvparams = {} |
115 |
if opts.hypervisor: |
116 |
hypervisor, hvparams = opts.hypervisor |
117 |
|
118 |
if opts.nics: |
119 |
try: |
120 |
nic_max = max(int(nidx[0])+1 for nidx in opts.nics) |
121 |
except ValueError, err: |
122 |
raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err)) |
123 |
nics = [{}] * nic_max |
124 |
for nidx, ndict in opts.nics: |
125 |
nidx = int(nidx) |
126 |
if not isinstance(ndict, dict): |
127 |
msg = "Invalid nic/%d value: expected dict, got %s" % (nidx, ndict) |
128 |
raise errors.OpPrereqError(msg) |
129 |
nics[nidx] = ndict |
130 |
elif opts.no_nics: |
131 |
# no nics |
132 |
nics = [] |
133 |
else: |
134 |
# default of one nic, all auto |
135 |
nics = [{}] |
136 |
|
137 |
if opts.disk_template == constants.DT_DISKLESS: |
138 |
if opts.disks or opts.sd_size is not None: |
139 |
raise errors.OpPrereqError("Diskless instance but disk" |
140 |
" information passed") |
141 |
disks = [] |
142 |
else: |
143 |
if not opts.disks and not opts.sd_size: |
144 |
raise errors.OpPrereqError("No disk information specified") |
145 |
if opts.disks and opts.sd_size is not None: |
146 |
raise errors.OpPrereqError("Please use either the '--disk' or" |
147 |
" '-s' option") |
148 |
if opts.sd_size is not None: |
149 |
opts.disks = [(0, {"size": opts.sd_size})] |
150 |
try: |
151 |
disk_max = max(int(didx[0])+1 for didx in opts.disks) |
152 |
except ValueError, err: |
153 |
raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err)) |
154 |
disks = [{}] * disk_max |
155 |
for didx, ddict in opts.disks: |
156 |
didx = int(didx) |
157 |
if not isinstance(ddict, dict): |
158 |
msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict) |
159 |
raise errors.OpPrereqError(msg) |
160 |
elif "size" not in ddict: |
161 |
raise errors.OpPrereqError("Missing size for disk %d" % didx) |
162 |
try: |
163 |
ddict["size"] = utils.ParseUnit(ddict["size"]) |
164 |
except ValueError, err: |
165 |
raise errors.OpPrereqError("Invalid disk size for disk %d: %s" % |
166 |
(didx, err)) |
167 |
disks[didx] = ddict |
168 |
|
169 |
utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES) |
170 |
utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES) |
171 |
|
172 |
op = opcodes.OpCreateInstance(instance_name=instance, |
173 |
disk_template=opts.disk_template, |
174 |
disks=disks, |
175 |
nics=nics, |
176 |
mode=constants.INSTANCE_IMPORT, |
177 |
pnode=pnode, snode=snode, |
178 |
ip_check=opts.ip_check, |
179 |
start=False, |
180 |
src_node=opts.src_node, src_path=opts.src_dir, |
181 |
wait_for_sync=opts.wait_for_sync, |
182 |
file_storage_dir=opts.file_storage_dir, |
183 |
file_driver=opts.file_driver, |
184 |
iallocator=opts.iallocator, |
185 |
hypervisor=hypervisor, |
186 |
hvparams=hvparams, |
187 |
beparams=opts.beparams) |
188 |
|
189 |
SubmitOpCode(op) |
190 |
return 0 |
191 |
|
192 |
|
193 |
def RemoveExport(opts, args): |
194 |
"""Remove an export from the cluster. |
195 |
|
196 |
@param opts: the command line options selected by the user |
197 |
@type args: list |
198 |
@param args: should contain only one element, the name of the |
199 |
instance whose backup should be removed |
200 |
@rtype: int |
201 |
@return: the desired exit code |
202 |
|
203 |
""" |
204 |
instance = args[0] |
205 |
op = opcodes.OpRemoveExport(instance_name=args[0]) |
206 |
|
207 |
SubmitOpCode(op) |
208 |
return 0 |
209 |
|
210 |
|
211 |
# this is defined separately due to readability only |
212 |
import_opts = [ |
213 |
DEBUG_OPT, |
214 |
cli_option("-n", "--node", dest="node", |
215 |
help="Target node and optional secondary node", |
216 |
metavar="<pnode>[:<snode>]"), |
217 |
cli_option("-B", "--backend", dest="beparams", |
218 |
type="keyval", default={}, |
219 |
help="Backend parameters"), |
220 |
cli_option("-t", "--disk-template", dest="disk_template", |
221 |
help="Custom disk setup (diskless, file, plain, drbd)", |
222 |
default=None, metavar="TEMPL"), |
223 |
cli_option("--disk", help="Disk information", |
224 |
default=[], dest="disks", |
225 |
action="append", |
226 |
type="identkeyval"), |
227 |
cli_option("-s", "--os-size", dest="sd_size", help="Disk size for a" |
228 |
" single-disk configuration, when not using the --disk option," |
229 |
" in MiB unless a suffix is used", |
230 |
default=None, type="unit", metavar="<size>"), |
231 |
cli_option("--net", help="NIC information", |
232 |
default=[], dest="nics", |
233 |
action="append", |
234 |
type="identkeyval"), |
235 |
cli_option("--no-nics", default=False, action="store_true", |
236 |
help="Do not create any network cards for the instance"), |
237 |
cli_option("--no-wait-for-sync", dest="wait_for_sync", default=True, |
238 |
action="store_false", help="Don't wait for sync (DANGEROUS!)"), |
239 |
cli_option("--src-node", dest="src_node", help="Source node", |
240 |
metavar="<node>"), |
241 |
cli_option("--src-dir", dest="src_dir", help="Source directory", |
242 |
metavar="<dir>"), |
243 |
cli_option("--no-ip-check", dest="ip_check", default=True, |
244 |
action="store_false", help="Don't check that the instance's IP" |
245 |
" is alive"), |
246 |
cli_option("-I", "--iallocator", metavar="<NAME>", |
247 |
help="Select nodes for the instance automatically using the" |
248 |
" <NAME> iallocator plugin", default=None, type="string"), |
249 |
cli_option("--file-storage-dir", dest="file_storage_dir", |
250 |
help="Relative path under default cluster-wide file storage dir" |
251 |
" to store file-based disks", default=None, |
252 |
metavar="<DIR>"), |
253 |
cli_option("--file-driver", dest="file_driver", help="Driver to use" |
254 |
" for image files", default="loop", metavar="<DRIVER>"), |
255 |
cli_option("-H", "--hypervisor", dest="hypervisor", |
256 |
help="Hypervisor and hypervisor options, in the format" |
257 |
" hypervisor:option=value,option=value,...", default=None, |
258 |
type="identkeyval"), |
259 |
] |
260 |
|
261 |
commands = { |
262 |
'list': (PrintExportList, [], |
263 |
[DEBUG_OPT, |
264 |
cli_option("--node", dest="nodes", default=[], action="append", |
265 |
help="List only backups stored on this node" |
266 |
" (can be used multiple times)"), |
267 |
], |
268 |
"", "Lists instance exports available in the ganeti cluster"), |
269 |
'export': (ExportInstance, [ArgInstance(min=1, max=1)], |
270 |
[DEBUG_OPT, FORCE_OPT, |
271 |
cli_option("-n", "--node", dest="node", help="Target node", |
272 |
metavar="<node>"), |
273 |
cli_option("","--noshutdown", dest="shutdown", |
274 |
action="store_false", default=True, |
275 |
help="Don't shutdown the instance (unsafe)"), ], |
276 |
"-n <target_node> [opts...] <name>", |
277 |
"Exports an instance to an image"), |
278 |
'import': (ImportInstance, [ArgInstance(min=1, max=1)], import_opts, |
279 |
("[...] -t disk-type -n node[:secondary-node]" |
280 |
" <name>"), |
281 |
"Imports an instance from an exported image"), |
282 |
'remove': (RemoveExport, [ArgUnknown(min=1, max=1)], |
283 |
[DEBUG_OPT], |
284 |
"<name>", |
285 |
"Remove exports of named instance from the filesystem."), |
286 |
} |
287 |
|
288 |
|
289 |
if __name__ == '__main__': |
290 |
sys.exit(GenericMain(commands)) |