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