4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
21 """Backup related commands"""
23 # pylint: disable-msg=W0401,W0613,W0614,C0103
24 # W0401: Wildcard import ganeti.cli
25 # W0613: Unused argument, since all functions follow the same API
26 # W0614: Unused import %s from wildcard import (since we need cli)
27 # C0103: Invalid name gnt-backup
31 from ganeti.cli import *
32 from ganeti import opcodes
33 from ganeti import constants
34 from ganeti import errors
35 from ganeti import utils
41 def PrintExportList(opts, args):
42 """Prints a list of all the exported system images.
44 @param opts: the command line options selected by the user
46 @param args: should be an empty list
48 @return: the desired exit code
51 exports = GetClient().QueryExports(opts.nodes, False)
54 ToStdout("Node: %s", node)
56 if isinstance(exports[node], list):
57 for instance_name in exports[node]:
58 ToStdout("\t%s", instance_name)
60 ToStdout(" Could not get exports list")
65 def ExportInstance(opts, args):
66 """Export an instance to an image in the cluster.
68 @param opts: the command line options selected by the user
70 @param args: should contain only one element, the name
71 of the instance to be exported
73 @return: the desired exit code
76 op = opcodes.OpExportInstance(instance_name=args[0],
77 target_node=opts.node,
78 shutdown=opts.shutdown,
79 shutdown_timeout=opts.shutdown_timeout)
81 fin_resu, dlist = SubmitOpCode(op)
82 if not isinstance(dlist, list):
83 ToStderr("Cannot parse execution results")
85 # TODO: handle diskless instances
86 if dlist.count(False) == 0:
89 elif dlist.count(True) == 0:
90 ToStderr("Error: No disks were backed up successfully."
91 " The export doesn't have any valid data,"
92 " it is recommended to retry the operation.")
95 ToStderr("Partial export failure: %d disks backed up, %d disks failed.",
96 dlist.count(True), dlist.count(False))
102 def ImportInstance(opts, args):
103 """Add an instance to the cluster.
105 This is just a wrapper over GenericInstanceCreate.
108 return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
111 def RemoveExport(opts, args):
112 """Remove an export from the cluster.
114 @param opts: the command line options selected by the user
116 @param args: should contain only one element, the name of the
117 instance whose backup should be removed
119 @return: the desired exit code
122 op = opcodes.OpRemoveExport(instance_name=args[0])
128 # this is defined separately due to readability only
144 FILESTORE_DRIVER_OPT,
151 PrintExportList, ARGS_NONE,
153 "", "Lists instance exports available in the ganeti cluster"),
155 ExportInstance, ARGS_ONE_INSTANCE,
156 [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT, SHUTDOWN_TIMEOUT_OPT],
157 "-n <target_node> [opts...] <name>",
158 "Exports an instance to an image"),
160 ImportInstance, ARGS_ONE_INSTANCE, import_opts,
161 "[...] -t disk-type -n node[:secondary-node] <name>",
162 "Imports an instance from an exported image"),
164 RemoveExport, [ArgUnknown(min=1, max=1)], [],
165 "<name>", "Remove exports of named instance from the filesystem."),
169 if __name__ == '__main__':
170 sys.exit(GenericMain(commands))