Add function to format all job log messages
[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   SubmitOpCode(op, opts=opts)
84   return 0
85
86
87 def ImportInstance(opts, args):
88   """Add an instance to the cluster.
89
90   This is just a wrapper over GenericInstanceCreate.
91
92   """
93   return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
94
95
96 def RemoveExport(opts, args):
97   """Remove an export from the cluster.
98
99   @param opts: the command line options selected by the user
100   @type args: list
101   @param args: should contain only one element, the name of the
102       instance whose backup should be removed
103   @rtype: int
104   @return: the desired exit code
105
106   """
107   op = opcodes.OpRemoveExport(instance_name=args[0])
108
109   SubmitOpCode(op, opts=opts)
110   return 0
111
112
113 # this is defined separately due to readability only
114 import_opts = [
115   BACKEND_OPT,
116   DISK_OPT,
117   DISK_TEMPLATE_OPT,
118   FILESTORE_DIR_OPT,
119   FILESTORE_DRIVER_OPT,
120   HYPERVISOR_OPT,
121   IALLOCATOR_OPT,
122   IDENTIFY_DEFAULTS_OPT,
123   NET_OPT,
124   NODE_PLACEMENT_OPT,
125   NOIPCHECK_OPT,
126   NONAMECHECK_OPT,
127   NONICS_OPT,
128   NWSYNC_OPT,
129   OSPARAMS_OPT,
130   OS_SIZE_OPT,
131   SRC_DIR_OPT,
132   SRC_NODE_OPT,
133   SUBMIT_OPT,
134   ]
135
136
137 commands = {
138   'list': (
139     PrintExportList, ARGS_NONE,
140     [NODE_LIST_OPT],
141     "", "Lists instance exports available in the ganeti cluster"),
142   'export': (
143     ExportInstance, ARGS_ONE_INSTANCE,
144     [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT, SHUTDOWN_TIMEOUT_OPT,
145      REMOVE_INSTANCE_OPT, IGNORE_REMOVE_FAILURES_OPT],
146     "-n <target_node> [opts...] <name>",
147     "Exports an instance to an image"),
148   'import': (
149     ImportInstance, ARGS_ONE_INSTANCE, import_opts,
150     "[...] -t disk-type -n node[:secondary-node] <name>",
151     "Imports an instance from an exported image"),
152   'remove': (
153     RemoveExport, [ArgUnknown(min=1, max=1)], [],
154     "<name>", "Remove exports of named instance from the filesystem."),
155   }
156
157
158 if __name__ == '__main__':
159   sys.exit(GenericMain(commands))