4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
27 from optparse import make_option
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import constants
36 def PrintExportList(opts, args):
37 """Prints a list of all the exported system images.
40 opts - class with options as members (should be empty)
41 args - should be empty
47 exports = GetClient().QueryExports(opts.nodes)
50 ToStdout("Node: %s", node)
52 if isinstance(exports[node], list):
53 for instance_name in exports[node]:
54 ToStdout("\t%s", instance_name)
56 ToStdout(" Could not get exports list")
61 def ExportInstance(opts, args):
62 """Export an instance to an image in the cluster.
65 opts - class with options as members
66 args - list with a single element, the instance name
69 1 in case of error, 0 otherwise
72 op = opcodes.OpExportInstance(instance_name=args[0],
73 target_node=opts.node,
74 shutdown=opts.shutdown)
79 def ImportInstance(opts, args):
80 """Add an instance to the cluster.
83 opts - class with options as members
84 args - list with a single element, the new instance name
86 memory - amount of memory to allocate to instance (MiB)
87 size - amount of disk space to allocate to instance (MiB)
88 os - which OS to run on instance
89 node - node to run new instance on
90 src_node - node containing the export
91 src_dir - directory on the old node with the export in it
94 1 in case of error, 0 otherwise
99 (pnode, snode) = SplitNodeOption(opts.node)
104 hypervisor, hvparams = opts.hypervisor
106 ValidateBeParams(opts.beparams)
108 op = opcodes.OpCreateInstance(instance_name=instance,
109 disk_size=opts.size, swap_size=opts.swap,
110 disk_template=opts.disk_template,
111 mode=constants.INSTANCE_IMPORT,
112 pnode=pnode, snode=snode,
113 ip_check=opts.ip_check,
114 ip=opts.ip, bridge=opts.bridge, start=False,
115 src_node=opts.src_node, src_path=opts.src_dir,
116 wait_for_sync=opts.wait_for_sync, mac=opts.mac,
117 file_storage_dir=opts.file_storage_dir,
118 file_driver=opts.file_driver,
119 iallocator=opts.iallocator,
120 hypervisor=hypervisor,
122 beparams=opts.beparams)
128 def RemoveExport(opts, args):
129 """Remove an export from the cluster.
132 opts - class with options as members
133 args - list with a single element, the exported instance to remove
137 1 in case of error, 0 otherwise
141 op = opcodes.OpRemoveExport(instance_name=args[0])
147 # this is defined separately due to readability only
150 make_option("-n", "--node", dest="node",
151 help="Target node and optional secondary node",
152 metavar="<pnode>[:<snode>]"),
153 cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
155 default=20 * 1024, type="unit", metavar="<size>"),
156 cli_option("--swap-size", dest="swap", help="Swap size",
157 default=4 * 1024, type="unit", metavar="<size>"),
158 keyval_option("-B", "--backend", dest="beparams",
159 type="keyval", default={},
160 help="Backend parameters"),
161 make_option("-t", "--disk-template", dest="disk_template",
162 help="Custom disk setup (diskless, file, plain, drbd)",
163 default=None, metavar="TEMPL"),
164 make_option("-i", "--ip", dest="ip",
165 help="IP address ('none' [default], 'auto', or specify address)",
166 default='none', type="string", metavar="<ADDRESS>"),
167 make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
168 action="store_false", help="Don't wait for sync (DANGEROUS!)"),
169 make_option("-b", "--bridge", dest="bridge",
170 help="Bridge to connect this instance to",
171 default=None, metavar="<bridge>"),
172 make_option("--mac", dest="mac",
173 help="MAC address ('auto' [default], or specify address)",
174 default='auto', type="string", metavar="<MACADDRESS>"),
175 make_option("--src-node", dest="src_node", help="Source node",
177 make_option("--src-dir", dest="src_dir", help="Source directory",
179 make_option("--no-ip-check", dest="ip_check", default=True,
180 action="store_false", help="Don't check that the instance's IP"
182 make_option("--iallocator", metavar="<NAME>",
183 help="Select nodes for the instance automatically using the"
184 " <NAME> iallocator plugin", default=None, type="string"),
185 make_option("--file-storage-dir", dest="file_storage_dir",
186 help="Relative path under default cluster-wide file storage dir"
187 " to store file-based disks", default=None,
189 make_option("--file-driver", dest="file_driver", help="Driver to use"
190 " for image files", default="loop", metavar="<DRIVER>"),
191 ikv_option("-H", "--hypervisor", dest="hypervisor",
192 help="Hypervisor and hypervisor options, in the format"
193 " hypervisor:option=value,option=value,...", default=None,
198 'list': (PrintExportList, ARGS_NONE,
200 make_option("--node", dest="nodes", default=[], action="append",
201 help="List only backups stored on this node"
202 " (can be used multiple times)"),
204 "", "Lists instance exports available in the ganeti cluster"),
205 'export': (ExportInstance, ARGS_ONE,
206 [DEBUG_OPT, FORCE_OPT,
207 make_option("-n", "--node", dest="node", help="Target node",
209 make_option("","--noshutdown", dest="shutdown",
210 action="store_false", default=True,
211 help="Don't shutdown the instance (unsafe)"), ],
212 "-n <target_node> [opts...] <name>",
213 "Exports an instance to an image"),
214 'import': (ImportInstance, ARGS_ONE, import_opts,
215 ("[...] -t disk-type -n node[:secondary-node]"
216 " --src-node node --src-dir dir"
218 "Imports an instance from an exported image"),
219 'remove': (RemoveExport, ARGS_ONE,
222 "Remove exports of named instance from the filesystem."),
225 if __name__ == '__main__':
226 sys.exit(GenericMain(commands))