Merge branch 'devel-2.2'
[ganeti-local] / scripts / gnt-backup
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007, 2010 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 from ganeti import errors
35
36
37 _VALUE_TRUE = "true"
38
39
40 def PrintExportList(opts, args):
41   """Prints a list of all the exported system images.
42
43   @param opts: the command line options selected by the user
44   @type args: list
45   @param args: should be an empty list
46   @rtype: int
47   @return: the desired exit code
48
49   """
50   exports = GetClient().QueryExports(opts.nodes, False)
51   retcode = 0
52   for node in exports:
53     ToStdout("Node: %s", node)
54     ToStdout("Exports:")
55     if isinstance(exports[node], list):
56       for instance_name in exports[node]:
57         ToStdout("\t%s", instance_name)
58     else:
59       ToStdout("  Could not get exports list")
60       retcode = 1
61   return retcode
62
63
64 def ExportInstance(opts, args):
65   """Export an instance to an image in the cluster.
66
67   @param opts: the command line options selected by the user
68   @type args: list
69   @param args: should contain only one element, the name
70       of the instance to be exported
71   @rtype: int
72   @return: the desired exit code
73
74   """
75   ignore_remove_failures = opts.ignore_remove_failures
76
77   if not opts.node:
78     raise errors.OpPrereqError("Target node must be specified",
79                                errors.ECODE_INVAL)
80
81   op = opcodes.OpExportInstance(instance_name=args[0],
82                                 target_node=opts.node,
83                                 shutdown=opts.shutdown,
84                                 shutdown_timeout=opts.shutdown_timeout,
85                                 remove_instance=opts.remove_instance,
86                                 ignore_remove_failures=ignore_remove_failures)
87
88   SubmitOpCode(op, opts=opts)
89   return 0
90
91
92 def ImportInstance(opts, args):
93   """Add an instance to the cluster.
94
95   This is just a wrapper over GenericInstanceCreate.
96
97   """
98   return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
99
100
101 def RemoveExport(opts, args):
102   """Remove an export from the cluster.
103
104   @param opts: the command line options selected by the user
105   @type args: list
106   @param args: should contain only one element, the name of the
107       instance whose backup should be removed
108   @rtype: int
109   @return: the desired exit code
110
111   """
112   op = opcodes.OpRemoveExport(instance_name=args[0])
113
114   SubmitOpCode(op, opts=opts)
115   return 0
116
117
118 # this is defined separately due to readability only
119 import_opts = [
120   BACKEND_OPT,
121   DISK_OPT,
122   DISK_TEMPLATE_OPT,
123   FILESTORE_DIR_OPT,
124   FILESTORE_DRIVER_OPT,
125   HYPERVISOR_OPT,
126   IALLOCATOR_OPT,
127   IDENTIFY_DEFAULTS_OPT,
128   NET_OPT,
129   NODE_PLACEMENT_OPT,
130   NOIPCHECK_OPT,
131   NONAMECHECK_OPT,
132   NONICS_OPT,
133   NWSYNC_OPT,
134   OSPARAMS_OPT,
135   OS_SIZE_OPT,
136   SRC_DIR_OPT,
137   SRC_NODE_OPT,
138   SUBMIT_OPT,
139   DRY_RUN_OPT,
140   PRIORITY_OPT,
141   ]
142
143
144 commands = {
145   'list': (
146     PrintExportList, ARGS_NONE,
147     [NODE_LIST_OPT],
148     "", "Lists instance exports available in the ganeti cluster"),
149   'export': (
150     ExportInstance, ARGS_ONE_INSTANCE,
151     [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT, SHUTDOWN_TIMEOUT_OPT,
152      REMOVE_INSTANCE_OPT, IGNORE_REMOVE_FAILURES_OPT, DRY_RUN_OPT,
153      PRIORITY_OPT],
154     "-n <target_node> [opts...] <name>",
155     "Exports an instance to an image"),
156   'import': (
157     ImportInstance, ARGS_ONE_INSTANCE, import_opts,
158     "[...] -t disk-type -n node[:secondary-node] <name>",
159     "Imports an instance from an exported image"),
160   'remove': (
161     RemoveExport, [ArgUnknown(min=1, max=1)], [DRY_RUN_OPT, PRIORITY_OPT],
162     "<name>", "Remove exports of named instance from the filesystem."),
163   }
164
165
166 if __name__ == '__main__':
167   sys.exit(GenericMain(commands))