Change exit code of gnt-backup list
[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
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
25
26 import sys
27 from optparse import make_option
28
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import constants
32
33
34 _VALUE_TRUE = "true"
35
36 def PrintExportList(opts, args):
37   """Prints a list of all the exported system images.
38
39   Args:
40    opts - class with options as members (should be empty)
41    args - should be empty
42
43   Returns:
44     nothing
45
46   """
47   exports = GetClient().QueryExports(opts.nodes)
48   retcode = 0
49   for node in exports:
50     ToStdout("Node: %s", node)
51     ToStdout("Exports:")
52     if isinstance(exports[node], list):
53       for instance_name in exports[node]:
54         ToStdout("\t%s", instance_name)
55     else:
56       ToStdout("  Could not get exports list")
57       retcode = 1
58   return retcode
59
60
61 def ExportInstance(opts, args):
62   """Export an instance to an image in the cluster.
63
64   Args:
65    opts - class with options as members
66    args - list with a single element, the instance name
67
68   Returns:
69     1 in case of error, 0 otherwise
70
71   """
72   op = opcodes.OpExportInstance(instance_name=args[0],
73                                 target_node=opts.node,
74                                 shutdown=opts.shutdown)
75
76   SubmitOpCode(op)
77
78
79 def ImportInstance(opts, args):
80   """Add an instance to the cluster.
81
82   Args:
83    opts - class with options as members
84    args - list with a single element, the new instance name
85   Opts used:
86    memory - amount of memory to allocate to instance (MiB)
87    size - amount of disk space to allocate to instance (MiB)
88    os - which OS to run on instance
89    node - node to run new instance on
90    src_node - node containing the export
91    src_dir - directory on the old node with the export in it
92
93   Returns:
94     1 in case of error, 0 otherwise
95
96   """
97   instance = args[0]
98
99   (pnode, snode) = SplitNodeOption(opts.node)
100
101   hypervisor = None
102   hvparams = {}
103   if opts.hypervisor:
104     hypervisor, hvparams = opts.hypervisor
105
106   ValidateBeParams(opts.beparams)
107
108   op = opcodes.OpCreateInstance(instance_name=instance,
109                                 disk_size=opts.size, swap_size=opts.swap,
110                                 disk_template=opts.disk_template,
111                                 mode=constants.INSTANCE_IMPORT,
112                                 pnode=pnode, snode=snode,
113                                 ip_check=opts.ip_check,
114                                 ip=opts.ip, bridge=opts.bridge, start=False,
115                                 src_node=opts.src_node, src_path=opts.src_dir,
116                                 wait_for_sync=opts.wait_for_sync, mac=opts.mac,
117                                 file_storage_dir=opts.file_storage_dir,
118                                 file_driver=opts.file_driver,
119                                 iallocator=opts.iallocator,
120                                 hypervisor=hypervisor,
121                                 hvparams=hvparams,
122                                 beparams=opts.beparams)
123
124   SubmitOpCode(op)
125   return 0
126
127
128 def RemoveExport(opts, args):
129   """Remove an export from the cluster.
130
131   Args:
132    opts - class with options as members
133    args - list with a single element, the exported instance to remove
134   Opts used:
135
136   Returns:
137     1 in case of error, 0 otherwise
138
139   """
140   instance = args[0]
141   op = opcodes.OpRemoveExport(instance_name=args[0])
142
143   SubmitOpCode(op)
144   return 0
145
146
147 # this is defined separately due to readability only
148 import_opts = [
149   DEBUG_OPT,
150   make_option("-n", "--node", dest="node",
151               help="Target node and optional secondary node",
152               metavar="<pnode>[:<snode>]"),
153   cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
154              " a suffix is used",
155              default=20 * 1024, type="unit", metavar="<size>"),
156   cli_option("--swap-size", dest="swap", help="Swap size",
157              default=4 * 1024, type="unit", metavar="<size>"),
158   keyval_option("-B", "--backend", dest="beparams",
159                 type="keyval", default={},
160                 help="Backend parameters"),
161   make_option("-t", "--disk-template", dest="disk_template",
162               help="Custom disk setup (diskless, file, plain, drbd)",
163               default=None, metavar="TEMPL"),
164   make_option("-i", "--ip", dest="ip",
165               help="IP address ('none' [default], 'auto', or specify address)",
166               default='none', type="string", metavar="<ADDRESS>"),
167   make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
168               action="store_false", help="Don't wait for sync (DANGEROUS!)"),
169   make_option("-b", "--bridge", dest="bridge",
170               help="Bridge to connect this instance to",
171               default=None, metavar="<bridge>"),
172   make_option("--mac", dest="mac",
173               help="MAC address ('auto' [default], or specify address)",
174               default='auto', type="string", metavar="<MACADDRESS>"),
175   make_option("--src-node", dest="src_node", help="Source node",
176               metavar="<node>"),
177   make_option("--src-dir", dest="src_dir", help="Source directory",
178               metavar="<dir>"),
179   make_option("--no-ip-check", dest="ip_check", default=True,
180               action="store_false", help="Don't check that the instance's IP"
181               " is alive"),
182   make_option("--iallocator", metavar="<NAME>",
183               help="Select nodes for the instance automatically using the"
184               " <NAME> iallocator plugin", default=None, type="string"),
185   make_option("--file-storage-dir", dest="file_storage_dir",
186               help="Relative path under default cluster-wide file storage dir"
187               " to store file-based disks", default=None,
188               metavar="<DIR>"),
189   make_option("--file-driver", dest="file_driver", help="Driver to use"
190               " for image files", default="loop", metavar="<DRIVER>"),
191   ikv_option("-H", "--hypervisor", dest="hypervisor",
192               help="Hypervisor and hypervisor options, in the format"
193               " hypervisor:option=value,option=value,...", default=None,
194               type="identkeyval"),
195   ]
196
197 commands = {
198   'list': (PrintExportList, ARGS_NONE,
199            [DEBUG_OPT,
200             make_option("--node", dest="nodes", default=[], action="append",
201                         help="List only backups stored on this node"
202                              " (can be used multiple times)"),
203             ],
204            "", "Lists instance exports available in the ganeti cluster"),
205   'export': (ExportInstance, ARGS_ONE,
206              [DEBUG_OPT, FORCE_OPT,
207               make_option("-n", "--node", dest="node", help="Target node",
208                           metavar="<node>"),
209               make_option("","--noshutdown", dest="shutdown",
210                           action="store_false", default=True,
211                           help="Don't shutdown the instance (unsafe)"), ],
212              "-n <target_node> [opts...] <name>",
213              "Exports an instance to an image"),
214   'import': (ImportInstance, ARGS_ONE, import_opts,
215              ("[...] -t disk-type -n node[:secondary-node]"
216               " --src-node node --src-dir dir"
217               " <name>"),
218              "Imports an instance from an exported image"),
219   'remove': (RemoveExport, ARGS_ONE,
220              [DEBUG_OPT],
221              "<name>",
222              "Remove exports of named instance from the filesystem."),
223   }
224
225 if __name__ == '__main__':
226   sys.exit(GenericMain(commands))