gnt-instance fix ValidateBeParams call
[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 import sys
23 from optparse import make_option
24
25 from ganeti.cli import *
26 from ganeti import cmdlib
27 from ganeti import opcodes
28 from ganeti import constants
29
30
31 _VALUE_TRUE = "true"
32
33 def PrintExportList(opts, args):
34   """Prints a list of all the exported system images.
35
36   Args:
37    opts - class with options as members (should be empty)
38    args - should be empty
39
40   Returns:
41     nothing
42
43   """
44   exports = GetClient().QueryExports(opts.nodes)
45   for node in exports:
46     print ("Node: %s" % node)
47     print ("Exports:")
48     if isinstance(exports[node], list):
49       for instance_name in exports[node]:
50         print ("\t%s" % instance_name)
51     else:
52       print ("  Could not get exports list")
53
54
55 def ExportInstance(opts, args):
56   """Export an instance to an image in the cluster.
57
58   Args:
59    opts - class with options as members
60    args - list with a single element, the instance name
61
62   Returns:
63     1 in case of error, 0 otherwise
64
65   """
66   op = opcodes.OpExportInstance(instance_name=args[0],
67                                 target_node=opts.node,
68                                 shutdown=opts.shutdown)
69
70   SubmitOpCode(op)
71
72
73 def ImportInstance(opts, args):
74   """Add an instance to the cluster.
75
76   Args:
77    opts - class with options as members
78    args - list with a single element, the new instance name
79   Opts used:
80    memory - amount of memory to allocate to instance (MiB)
81    size - amount of disk space to allocate to instance (MiB)
82    os - which OS to run on instance
83    node - node to run new instance on
84    src_node - node containing the export
85    src_dir - directory on the old node with the export in it
86
87   Returns:
88     1 in case of error, 0 otherwise
89
90   """
91   instance = args[0]
92
93   (pnode, snode) = SplitNodeOption(opts.node)
94
95   op = opcodes.OpCreateInstance(instance_name=instance,
96                                 disk_size=opts.size, swap_size=opts.swap,
97                                 disk_template=opts.disk_template,
98                                 mode=constants.INSTANCE_IMPORT,
99                                 pnode=pnode, snode=snode,
100                                 ip_check=opts.ip_check,
101                                 ip=opts.ip, bridge=opts.bridge, start=False,
102                                 src_node=opts.src_node, src_path=opts.src_dir,
103                                 wait_for_sync=opts.wait_for_sync, mac="auto",
104                                 file_storage_dir=opts.file_storage_dir,
105                                 file_driver=opts.file_driver,
106                                 iallocator=opts.iallocator,
107                                 auto_balance=auto_balance,
108                                 hvm_boot_order=opts.hvm_boot_order,
109                                 hvm_acpi=opts.hvm_acpi,
110                                 hvm_nic_type=opts.hvm_nic_type,
111                                 hvm_disk_type=opts.hvm_disk_type,
112                                 hvm_pae=opts.hvm_pae,
113                                 hvm_cdrom_image_path=opts.hvm_cdrom_image_path,
114                                 vnc_bind_address=opts.vnc_bind_address,
115                                 beparams=ValidateBeParams(opts.beparams))
116
117   SubmitOpCode(op)
118   return 0
119
120
121 def RemoveExport(opts, args):
122   """Remove an export from the cluster.
123
124   Args:
125    opts - class with options as members
126    args - list with a single element, the exported instance to remove
127   Opts used:
128
129   Returns:
130     1 in case of error, 0 otherwise
131
132   """
133   instance = args[0]
134   op = opcodes.OpRemoveExport(instance_name=args[0])
135
136   SubmitOpCode(op)
137   return 0
138
139
140 # this is defined separately due to readability only
141 import_opts = [
142   DEBUG_OPT,
143   make_option("-n", "--node", dest="node",
144               help="Target node and optional secondary node",
145               metavar="<pnode>[:<snode>]"),
146   cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
147              " a suffix is used",
148              default=20 * 1024, type="unit", metavar="<size>"),
149   cli_option("--swap-size", dest="swap", help="Swap size",
150              default=4 * 1024, type="unit", metavar="<size>"),
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("--src-node", dest="src_node", help="Source node",
163               metavar="<node>"),
164   make_option("--src-dir", dest="src_dir", help="Source directory",
165               metavar="<dir>"),
166   make_option("--no-ip-check", dest="ip_check", default=True,
167               action="store_false", help="Don't check that the instance's IP"
168               " is alive"),
169   make_option("--iallocator", metavar="<NAME>",
170               help="Select nodes for the instance automatically using the"
171               " <NAME> iallocator plugin", default=None, type="string"),
172   make_option("--file-storage-dir", dest="file_storage_dir",
173               help="Relative path under default cluster-wide file storage dir"
174               " to store file-based disks", default=None,
175               metavar="<DIR>"),
176   make_option("--file-driver", dest="file_driver", help="Driver to use"
177               " for image files", default="loop", metavar="<DRIVER>"),
178   make_option("--hvm-boot-order", dest="hvm_boot_order",
179               help="Boot device order for HVM (one or more of [acdn])",
180               default=None, type="string", metavar="<BOOTORDER>"),
181   make_option("--hvm-acpi", dest="hvm_acpi",
182               help="ACPI support for HVM (true|false)",
183               metavar="<BOOL>", choices=["true", "false"]),
184   make_option("--hvm-nic-type", dest="hvm_nic_type",
185               help="Type of virtual NIC for HVM "
186               "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)",
187               metavar="NICTYPE", choices=[constants.HT_HVM_NIC_RTL8139,
188                                           constants.HT_HVM_NIC_NE2K_PCI,
189                                           constants.HT_HVM_NIC_NE2K_ISA,
190                                           constants.HT_HVM_DEV_PARAVIRTUAL],
191               default=constants.HT_HVM_NIC_RTL8139),
192   make_option("--hvm-disk-type", dest="hvm_disk_type",
193               help="Type of virtual disks for HVM (ioemu,paravirtual)",
194               metavar="DISKTYPE", choices=[constants.HT_HVM_DEV_IOEMU,
195                                            constants.HT_HVM_DEV_PARAVIRTUAL],
196               default=constants.HT_HVM_DEV_IOEMU,),
197   make_option("--hvm-pae", dest="hvm_pae",
198               help="PAE support for HVM (true|false)",
199               metavar="<BOOL>", choices=["true", "false"]),
200   make_option("--hvm-cdrom-image-path", dest="hvm_cdrom_image_path",
201               help="CDROM image path for HVM (absolute path or None)",
202               default=None, type="string", metavar="<CDROMIMAGE>"),
203   make_option("--vnc-bind-address", dest="vnc_bind_address",
204               help="bind address for VNC (IP address)",
205               default=None, type="string", metavar="<VNCADDRESS>"),
206   ]
207
208 commands = {
209   'list': (PrintExportList, ARGS_NONE,
210            [DEBUG_OPT,
211             make_option("--node", dest="nodes", default=[], action="append",
212                         help="List only backups stored on this node"
213                              " (can be used multiple times)"),
214             ],
215            "", "Lists instance exports available in the ganeti cluster"),
216   'export': (ExportInstance, ARGS_ONE,
217              [DEBUG_OPT, FORCE_OPT,
218               make_option("-n", "--node", dest="node", help="Target node",
219                           metavar="<node>"),
220               make_option("","--noshutdown", dest="shutdown",
221                           action="store_false", default=True,
222                           help="Don't shutdown the instance (unsafe)"), ],
223              "-n <target_node> [opts...] <name>",
224              "Exports an instance to an image"),
225   'import': (ImportInstance, ARGS_ONE, import_opts,
226              ("[...] -t disk-type -n node[:secondary-node]"
227               " --src-node node --src-dir dir"
228               " <name>"),
229              "Imports an instance from an exported image"),
230   'remove': (RemoveExport, ARGS_ONE,
231              [DEBUG_OPT],
232              "<name>",
233              "Remove exports of named instance from the filesystem."),
234   }
235
236 if __name__ == '__main__':
237   sys.exit(GenericMain(commands))