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