Rewrite the 'only submit job' handling in scripts
[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, mem_size=opts.mem,
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                                 vcpus=opts.vcpus, 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
116   SubmitOpCode(op)
117   return 0
118
119
120 def RemoveExport(opts, args):
121   """Remove an export from the cluster.
122
123   Args:
124    opts - class with options as members
125    args - list with a single element, the exported instance to remove
126   Opts used:
127
128   Returns:
129     1 in case of error, 0 otherwise
130
131   """
132   instance = args[0]
133   op = opcodes.OpRemoveExport(instance_name=args[0])
134
135   SubmitOpCode(op)
136   return 0
137
138
139 # this is defined separately due to readability only
140 import_opts = [
141   DEBUG_OPT,
142   make_option("-n", "--node", dest="node",
143               help="Target node and optional secondary node",
144               metavar="<pnode>[:<snode>]"),
145   cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
146              " a suffix is used",
147              default=20 * 1024, type="unit", metavar="<size>"),
148   cli_option("--swap-size", dest="swap", help="Swap size",
149              default=4 * 1024, type="unit", metavar="<size>"),
150   cli_option("-m", "--memory", dest="mem", help="Memory size",
151              default=128, type="unit", metavar="<mem>"),
152   make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs",
153               default=1, type="int", metavar="<PROC>"),
154   make_option("-t", "--disk-template", dest="disk_template",
155               help="Custom disk setup (diskless, file, plain, drbd)",
156               default=None, metavar="TEMPL"),
157   make_option("-i", "--ip", dest="ip",
158               help="IP address ('none' [default], 'auto', or specify address)",
159               default='none', type="string", metavar="<ADDRESS>"),
160   make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
161               action="store_false", help="Don't wait for sync (DANGEROUS!)"),
162   make_option("-b", "--bridge", dest="bridge",
163               help="Bridge to connect this instance to",
164               default=None, metavar="<bridge>"),
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   make_option("--hvm-boot-order", dest="hvm_boot_order",
182               help="Boot device order for HVM (one or more of [acdn])",
183               default=None, type="string", metavar="<BOOTORDER>"),
184   make_option("--hvm-acpi", dest="hvm_acpi",
185               help="ACPI support for HVM (true|false)",
186               metavar="<BOOL>", choices=["true", "false"]),
187   make_option("--hvm-nic-type", dest="hvm_nic_type",
188               help="Type of virtual NIC for HVM "
189               "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)",
190               metavar="NICTYPE", choices=[constants.HT_HVM_NIC_RTL8139,
191                                           constants.HT_HVM_NIC_NE2K_PCI,
192                                           constants.HT_HVM_NIC_NE2K_ISA,
193                                           constants.HT_HVM_DEV_PARAVIRTUAL],
194               default=constants.HT_HVM_NIC_RTL8139),
195   make_option("--hvm-disk-type", dest="hvm_disk_type",
196               help="Type of virtual disks for HVM (ioemu,paravirtual)",
197               metavar="DISKTYPE", choices=[constants.HT_HVM_DEV_IOEMU,
198                                            constants.HT_HVM_DEV_PARAVIRTUAL],
199               default=constants.HT_HVM_DEV_IOEMU,),
200   make_option("--hvm-pae", dest="hvm_pae",
201               help="PAE support for HVM (true|false)",
202               metavar="<BOOL>", choices=["true", "false"]),
203   make_option("--hvm-cdrom-image-path", dest="hvm_cdrom_image_path",
204               help="CDROM image path for HVM (absolute path or None)",
205               default=None, type="string", metavar="<CDROMIMAGE>"),
206   make_option("--vnc-bind-address", dest="vnc_bind_address",
207               help="bind address for VNC (IP address)",
208               default=None, type="string", metavar="<VNCADDRESS>"),
209   ]
210
211 commands = {
212   'list': (PrintExportList, ARGS_NONE,
213            [DEBUG_OPT,
214             make_option("--node", dest="nodes", default=[], action="append",
215                         help="List only backups stored on this node"
216                              " (can be used multiple times)"),
217             ],
218            "", "Lists instance exports available in the ganeti cluster"),
219   'export': (ExportInstance, ARGS_ONE,
220              [DEBUG_OPT, FORCE_OPT,
221               make_option("-n", "--node", dest="node", help="Target node",
222                           metavar="<node>"),
223               make_option("","--noshutdown", dest="shutdown",
224                           action="store_false", default=True,
225                           help="Don't shutdown the instance (unsafe)"), ],
226              "-n <target_node> [opts...] <name>",
227              "Exports an instance to an image"),
228   'import': (ImportInstance, ARGS_ONE, import_opts,
229              ("[...] -t disk-type -n node[:secondary-node]"
230               " --src-node node --src-dir dir"
231               " <name>"),
232              "Imports an instance from an exported image"),
233   'remove': (RemoveExport, ARGS_ONE,
234              [DEBUG_OPT],
235              "<name>",
236              "Remove exports of named instance from the filesystem."),
237   }
238
239 if __name__ == '__main__':
240   sys.exit(GenericMain(commands))