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
32 from ganeti import errors
33 from ganeti import utils
38 def PrintExportList(opts, args):
39 """Prints a list of all the exported system images.
41 @param opts: the command line options selected by the user
43 @param args: should be an empty list
45 @return: the desired exit code
48 exports = GetClient().QueryExports(opts.nodes, False)
51 ToStdout("Node: %s", node)
53 if isinstance(exports[node], list):
54 for instance_name in exports[node]:
55 ToStdout("\t%s", instance_name)
57 ToStdout(" Could not get exports list")
62 def ExportInstance(opts, args):
63 """Export an instance to an image in the cluster.
65 @param opts: the command line options selected by the user
67 @param args: should contain only one element, the name
68 of the instance to be exported
70 @return: the desired exit code
73 op = opcodes.OpExportInstance(instance_name=args[0],
74 target_node=opts.node,
75 shutdown=opts.shutdown)
80 def ImportInstance(opts, args):
81 """Add an instance to the cluster.
83 @param opts: the command line options selected by the user
85 @param args: should contain only one element, the new instance name
87 @return: the desired exit code
92 (pnode, snode) = SplitNodeOption(opts.node)
97 hypervisor, hvparams = opts.hypervisor
101 nic_max = max(int(nidx[0])+1 for nidx in opts.nics)
102 except ValueError, err:
103 raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
104 nics = [{}] * nic_max
105 for nidx, ndict in opts.nics.items():
112 # default of one nic, all auto
115 if opts.disk_template == constants.DT_DISKLESS:
116 if opts.disks or opts.sd_size is not None:
117 raise errors.OpPrereqError("Diskless instance but disk"
118 " information passed")
121 if not opts.disks and not opts.sd_size:
122 raise errors.OpPrereqError("No disk information specified")
123 if opts.disks and opts.sd_size is not None:
124 raise errors.OpPrereqError("Please use either the '--disk' or"
126 if opts.sd_size is not None:
127 opts.disks = [(0, {"size": opts.sd_size})]
129 disk_max = max(int(didx[0])+1 for didx in opts.disks)
130 except ValueError, err:
131 raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
132 disks = [{}] * disk_max
133 for didx, ddict in opts.disks:
135 if "size" not in ddict:
136 raise errors.OpPrereqError("Missing size for disk %d" % didx)
138 ddict["size"] = utils.ParseUnit(ddict["size"])
139 except ValueError, err:
140 raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
144 utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES)
145 utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
147 op = opcodes.OpCreateInstance(instance_name=instance,
148 disk_template=opts.disk_template,
151 mode=constants.INSTANCE_IMPORT,
152 pnode=pnode, snode=snode,
153 ip_check=opts.ip_check,
155 src_node=opts.src_node, src_path=opts.src_dir,
156 wait_for_sync=opts.wait_for_sync,
157 file_storage_dir=opts.file_storage_dir,
158 file_driver=opts.file_driver,
159 iallocator=opts.iallocator,
160 hypervisor=hypervisor,
162 beparams=opts.beparams)
168 def RemoveExport(opts, args):
169 """Remove an export from the cluster.
171 @param opts: the command line options selected by the user
173 @param args: should contain only one element, the name of the
174 instance whose backup should be removed
176 @return: the desired exit code
180 op = opcodes.OpRemoveExport(instance_name=args[0])
186 # this is defined separately due to readability only
189 make_option("-n", "--node", dest="node",
190 help="Target node and optional secondary node",
191 metavar="<pnode>[:<snode>]"),
192 keyval_option("-B", "--backend", dest="beparams",
193 type="keyval", default={},
194 help="Backend parameters"),
195 make_option("-t", "--disk-template", dest="disk_template",
196 help="Custom disk setup (diskless, file, plain, drbd)",
197 default=None, metavar="TEMPL"),
198 ikv_option("--disk", help="Disk information",
199 default=[], dest="disks",
202 cli_option("-s", "--os-size", dest="sd_size", help="Disk size for a"
203 " single-disk configuration, when not using the --disk option,"
204 " in MiB unless a suffix is used",
205 default=None, type="unit", metavar="<size>"),
206 ikv_option("--net", help="NIC information",
207 default=[], dest="nics",
210 make_option("--no-nics", default=False, action="store_true",
211 help="Do not create any network cards for the instance"),
212 make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
213 action="store_false", help="Don't wait for sync (DANGEROUS!)"),
214 make_option("--src-node", dest="src_node", help="Source node",
216 make_option("--src-dir", dest="src_dir", help="Source directory",
218 make_option("--no-ip-check", dest="ip_check", default=True,
219 action="store_false", help="Don't check that the instance's IP"
221 make_option("-I", "--iallocator", metavar="<NAME>",
222 help="Select nodes for the instance automatically using the"
223 " <NAME> iallocator plugin", default=None, type="string"),
224 make_option("--file-storage-dir", dest="file_storage_dir",
225 help="Relative path under default cluster-wide file storage dir"
226 " to store file-based disks", default=None,
228 make_option("--file-driver", dest="file_driver", help="Driver to use"
229 " for image files", default="loop", metavar="<DRIVER>"),
230 ikv_option("-H", "--hypervisor", dest="hypervisor",
231 help="Hypervisor and hypervisor options, in the format"
232 " hypervisor:option=value,option=value,...", default=None,
237 'list': (PrintExportList, ARGS_NONE,
239 make_option("--node", dest="nodes", default=[], action="append",
240 help="List only backups stored on this node"
241 " (can be used multiple times)"),
243 "", "Lists instance exports available in the ganeti cluster"),
244 'export': (ExportInstance, ARGS_ONE,
245 [DEBUG_OPT, FORCE_OPT,
246 make_option("-n", "--node", dest="node", help="Target node",
248 make_option("","--noshutdown", dest="shutdown",
249 action="store_false", default=True,
250 help="Don't shutdown the instance (unsafe)"), ],
251 "-n <target_node> [opts...] <name>",
252 "Exports an instance to an image"),
253 'import': (ImportInstance, ARGS_ONE, import_opts,
254 ("[...] -t disk-type -n node[:secondary-node]"
256 "Imports an instance from an exported image"),
257 'remove': (RemoveExport, ARGS_ONE,
260 "Remove exports of named instance from the filesystem."),
263 if __name__ == '__main__':
264 sys.exit(GenericMain(commands))