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.
39 @param opts: the command line options selected by the user
41 @param args: should be an empty list
43 @return: the desired exit code
46 exports = GetClient().QueryExports(opts.nodes)
49 ToStdout("Node: %s", node)
51 if isinstance(exports[node], list):
52 for instance_name in exports[node]:
53 ToStdout("\t%s", instance_name)
55 ToStdout(" Could not get exports list")
60 def ExportInstance(opts, args):
61 """Export an instance to an image in the cluster.
63 @param opts: the command line options selected by the user
65 @param args: should contain only one element, the name
66 of the instance to be exported
68 @return: the desired exit code
71 op = opcodes.OpExportInstance(instance_name=args[0],
72 target_node=opts.node,
73 shutdown=opts.shutdown)
78 def ImportInstance(opts, args):
79 """Add an instance to the cluster.
81 @param opts: the command line options selected by the user
83 @param args: should contain only one element, the new instance name
85 @return: the desired exit code
90 (pnode, snode) = SplitNodeOption(opts.node)
95 hypervisor, hvparams = opts.hypervisor
97 ValidateBeParams(opts.beparams)
99 op = opcodes.OpCreateInstance(instance_name=instance,
100 disk_size=opts.size, swap_size=opts.swap,
101 disk_template=opts.disk_template,
102 mode=constants.INSTANCE_IMPORT,
103 pnode=pnode, snode=snode,
104 ip_check=opts.ip_check,
105 ip=opts.ip, bridge=opts.bridge, start=False,
106 src_node=opts.src_node, src_path=opts.src_dir,
107 wait_for_sync=opts.wait_for_sync, mac=opts.mac,
108 file_storage_dir=opts.file_storage_dir,
109 file_driver=opts.file_driver,
110 iallocator=opts.iallocator,
111 hypervisor=hypervisor,
113 beparams=opts.beparams)
119 def RemoveExport(opts, args):
120 """Remove an export from the cluster.
122 @param opts: the command line options selected by the user
124 @param args: should contain only one element, the name of the
125 instance whose backup should be removed
127 @return: the desired exit code
131 op = opcodes.OpRemoveExport(instance_name=args[0])
137 # this is defined separately due to readability only
140 make_option("-n", "--node", dest="node",
141 help="Target node and optional secondary node",
142 metavar="<pnode>[:<snode>]"),
143 cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
145 default=20 * 1024, type="unit", metavar="<size>"),
146 cli_option("--swap-size", dest="swap", help="Swap size",
147 default=4 * 1024, type="unit", metavar="<size>"),
148 keyval_option("-B", "--backend", dest="beparams",
149 type="keyval", default={},
150 help="Backend parameters"),
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("--mac", dest="mac",
163 help="MAC address ('auto' [default], or specify address)",
164 default='auto', type="string", metavar="<MACADDRESS>"),
165 make_option("--src-node", dest="src_node", help="Source node",
167 make_option("--src-dir", dest="src_dir", help="Source directory",
169 make_option("--no-ip-check", dest="ip_check", default=True,
170 action="store_false", help="Don't check that the instance's IP"
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,
179 make_option("--file-driver", dest="file_driver", help="Driver to use"
180 " for image files", default="loop", metavar="<DRIVER>"),
181 ikv_option("-H", "--hypervisor", dest="hypervisor",
182 help="Hypervisor and hypervisor options, in the format"
183 " hypervisor:option=value,option=value,...", default=None,
188 'list': (PrintExportList, ARGS_NONE,
190 make_option("--node", dest="nodes", default=[], action="append",
191 help="List only backups stored on this node"
192 " (can be used multiple times)"),
194 "", "Lists instance exports available in the ganeti cluster"),
195 'export': (ExportInstance, ARGS_ONE,
196 [DEBUG_OPT, FORCE_OPT,
197 make_option("-n", "--node", dest="node", help="Target node",
199 make_option("","--noshutdown", dest="shutdown",
200 action="store_false", default=True,
201 help="Don't shutdown the instance (unsafe)"), ],
202 "-n <target_node> [opts...] <name>",
203 "Exports an instance to an image"),
204 'import': (ImportInstance, ARGS_ONE, import_opts,
205 ("[...] -t disk-type -n node[:secondary-node]"
206 " --src-node node --src-dir dir"
208 "Imports an instance from an exported image"),
209 'remove': (RemoveExport, ARGS_ONE,
212 "Remove exports of named instance from the filesystem."),
215 if __name__ == '__main__':
216 sys.exit(GenericMain(commands))