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