4 # Copyright (C) 2006, 2007, 2010, 2011 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=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
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import constants
32 from ganeti import errors
33 from ganeti import qlang
36 _LIST_DEF_FIELDS = ["node", "export"]
39 def PrintExportList(opts, args):
40 """Prints a list of all the exported system images.
42 @param opts: the command line options selected by the user
44 @param args: should be an empty list
46 @return: the desired exit code
49 selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
51 qfilter = qlang.MakeSimpleFilter("node", opts.nodes)
53 return GenericList(constants.QR_EXPORT, selected_fields, None, opts.units,
54 opts.separator, not opts.no_headers,
55 verbose=opts.verbose, qfilter=qfilter)
58 def ListExportFields(opts, args):
59 """List export fields.
61 @param opts: the command line options selected by the user
63 @param args: fields to list, or empty for all
65 @return: the desired exit code
68 return GenericListFields(constants.QR_EXPORT, args, opts.separator,
72 def ExportInstance(opts, args):
73 """Export an instance to an image in the cluster.
75 @param opts: the command line options selected by the user
77 @param args: should contain only one element, the name
78 of the instance to be exported
80 @return: the desired exit code
83 ignore_remove_failures = opts.ignore_remove_failures
86 raise errors.OpPrereqError("Target node must be specified",
89 op = opcodes.OpBackupExport(instance_name=args[0],
90 target_node=opts.node,
91 shutdown=opts.shutdown,
92 shutdown_timeout=opts.shutdown_timeout,
93 remove_instance=opts.remove_instance,
94 ignore_remove_failures=ignore_remove_failures)
96 SubmitOrSend(op, opts)
100 def ImportInstance(opts, args):
101 """Add an instance to the cluster.
103 This is just a wrapper over GenericInstanceCreate.
106 return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
109 def RemoveExport(opts, args):
110 """Remove an export from the cluster.
112 @param opts: the command line options selected by the user
114 @param args: should contain only one element, the name of the
115 instance whose backup should be removed
117 @return: the desired exit code
120 op = opcodes.OpBackupRemove(instance_name=args[0])
122 SubmitOrSend(op, opts)
126 # this is defined separately due to readability only
128 IDENTIFY_DEFAULTS_OPT,
137 PrintExportList, ARGS_NONE,
138 [NODE_LIST_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, VERBOSE_OPT],
139 "", "Lists instance exports available in the ganeti cluster"),
141 ListExportFields, [ArgUnknown()],
142 [NOHDR_OPT, SEP_OPT],
144 "Lists all available fields for exports"),
146 ExportInstance, ARGS_ONE_INSTANCE,
147 [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT, SHUTDOWN_TIMEOUT_OPT,
148 REMOVE_INSTANCE_OPT, IGNORE_REMOVE_FAILURES_OPT, DRY_RUN_OPT,
149 PRIORITY_OPT, SUBMIT_OPT],
150 "-n <target_node> [opts...] <name>",
151 "Exports an instance to an image"),
153 ImportInstance, ARGS_ONE_INSTANCE, COMMON_CREATE_OPTS + import_opts,
154 "[...] -t disk-type -n node[:secondary-node] <name>",
155 "Imports an instance from an exported image"),
157 RemoveExport, [ArgUnknown(min=1, max=1)],
158 [DRY_RUN_OPT, PRIORITY_OPT, SUBMIT_OPT],
159 "<name>", "Remove exports of named instance from the filesystem."),
164 return GenericMain(commands)