Fix a broken commandline switch option
[ganeti-local] / scripts / gnt-backup
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 """Backup related commands"""
22
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
28
29 import sys
30
31 from ganeti.cli import *
32 from ganeti import opcodes
33 from ganeti import constants
34
35
36 _VALUE_TRUE = "true"
37
38
39 def PrintExportList(opts, args):
40   """Prints a list of all the exported system images.
41
42   @param opts: the command line options selected by the user
43   @type args: list
44   @param args: should be an empty list
45   @rtype: int
46   @return: the desired exit code
47
48   """
49   exports = GetClient().QueryExports(opts.nodes, False)
50   retcode = 0
51   for node in exports:
52     ToStdout("Node: %s", node)
53     ToStdout("Exports:")
54     if isinstance(exports[node], list):
55       for instance_name in exports[node]:
56         ToStdout("\t%s", instance_name)
57     else:
58       ToStdout("  Could not get exports list")
59       retcode = 1
60   return retcode
61
62
63 def ExportInstance(opts, args):
64   """Export an instance to an image in the cluster.
65
66   @param opts: the command line options selected by the user
67   @type args: list
68   @param args: should contain only one element, the name
69       of the instance to be exported
70   @rtype: int
71   @return: the desired exit code
72
73   """
74   ignore_remove_failures = opts.ignore_remove_failures
75
76   op = opcodes.OpExportInstance(instance_name=args[0],
77                                 target_node=opts.node,
78                                 shutdown=opts.shutdown,
79                                 shutdown_timeout=opts.shutdown_timeout,
80                                 remove_instance=opts.remove_instance,
81                                 ignore_remove_failures=ignore_remove_failures)
82
83   fin_resu, dlist = SubmitOpCode(op, opts=opts)
84   if not isinstance(dlist, list):
85     ToStderr("Cannot parse execution results")
86     return 1
87   # TODO: handle diskless instances
88   if dlist.count(False) == 0:
89     # all OK
90     rcode = 0
91   elif dlist.count(True) == 0:
92     ToStderr("Error: No disks were backed up successfully."
93              " The export doesn't have any valid data,"
94              " it is recommended to retry the operation.")
95     rcode = 1
96   else:
97     ToStderr("Partial export failure: %d disks backed up, %d disks failed.",
98              dlist.count(True), dlist.count(False))
99     rcode = 2
100   if not fin_resu:
101     rcode = 1
102   return rcode
103
104
105 def ImportInstance(opts, args):
106   """Add an instance to the cluster.
107
108   This is just a wrapper over GenericInstanceCreate.
109
110   """
111   return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
112
113
114 def RemoveExport(opts, args):
115   """Remove an export from the cluster.
116
117   @param opts: the command line options selected by the user
118   @type args: list
119   @param args: should contain only one element, the name of the
120       instance whose backup should be removed
121   @rtype: int
122   @return: the desired exit code
123
124   """
125   op = opcodes.OpRemoveExport(instance_name=args[0])
126
127   SubmitOpCode(op, opts=opts)
128   return 0
129
130
131 # this is defined separately due to readability only
132 import_opts = [
133   BACKEND_OPT,
134   DISK_OPT,
135   DISK_TEMPLATE_OPT,
136   FILESTORE_DIR_OPT,
137   FILESTORE_DRIVER_OPT,
138   HYPERVISOR_OPT,
139   IALLOCATOR_OPT,
140   IDENTIFY_DEFAULTS_OPT,
141   NET_OPT,
142   NODE_PLACEMENT_OPT,
143   NOIPCHECK_OPT,
144   NONAMECHECK_OPT,
145   NONICS_OPT,
146   NWSYNC_OPT,
147   OSPARAMS_OPT,
148   OS_SIZE_OPT,
149   SRC_DIR_OPT,
150   SRC_NODE_OPT,
151   SUBMIT_OPT,
152   ]
153
154
155 commands = {
156   'list': (
157     PrintExportList, ARGS_NONE,
158     [NODE_LIST_OPT],
159     "", "Lists instance exports available in the ganeti cluster"),
160   'export': (
161     ExportInstance, ARGS_ONE_INSTANCE,
162     [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT, SHUTDOWN_TIMEOUT_OPT,
163      REMOVE_INSTANCE_OPT, IGNORE_REMOVE_FAILURES_OPT],
164     "-n <target_node> [opts...] <name>",
165     "Exports an instance to an image"),
166   'import': (
167     ImportInstance, ARGS_ONE_INSTANCE, import_opts,
168     "[...] -t disk-type -n node[:secondary-node] <name>",
169     "Imports an instance from an exported image"),
170   'remove': (
171     RemoveExport, [ArgUnknown(min=1, max=1)], [],
172     "<name>", "Remove exports of named instance from the filesystem."),
173   }
174
175
176 if __name__ == '__main__':
177   sys.exit(GenericMain(commands))