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)
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")
58 def ExportInstance(opts, args):
59 """Export an instance to an image in the cluster.
62 opts - class with options as members
63 args - list with a single element, the instance name
66 1 in case of error, 0 otherwise
69 op = opcodes.OpExportInstance(instance_name=args[0],
70 target_node=opts.node,
71 shutdown=opts.shutdown)
76 def ImportInstance(opts, args):
77 """Add an instance to the cluster.
80 opts - class with options as members
81 args - list with a single element, the new instance name
83 memory - amount of memory to allocate to instance (MiB)
84 size - amount of disk space to allocate to instance (MiB)
85 os - which OS to run on instance
86 node - node to run new instance on
87 src_node - node containing the export
88 src_dir - directory on the old node with the export in it
91 1 in case of error, 0 otherwise
96 (pnode, snode) = SplitNodeOption(opts.node)
101 hypervisor, hvparams = opts.hypervisor
103 ValidateBeParams(opts.beparams)
105 op = opcodes.OpCreateInstance(instance_name=instance,
106 disk_size=opts.size, swap_size=opts.swap,
107 disk_template=opts.disk_template,
108 mode=constants.INSTANCE_IMPORT,
109 pnode=pnode, snode=snode,
110 ip_check=opts.ip_check,
111 ip=opts.ip, bridge=opts.bridge, start=False,
112 src_node=opts.src_node, src_path=opts.src_dir,
113 wait_for_sync=opts.wait_for_sync, mac=opts.mac,
114 file_storage_dir=opts.file_storage_dir,
115 file_driver=opts.file_driver,
116 iallocator=opts.iallocator,
117 hypervisor=hypervisor,
119 beparams=opts.beparams)
125 def RemoveExport(opts, args):
126 """Remove an export from the cluster.
129 opts - class with options as members
130 args - list with a single element, the exported instance to remove
134 1 in case of error, 0 otherwise
138 op = opcodes.OpRemoveExport(instance_name=args[0])
144 # this is defined separately due to readability only
147 make_option("-n", "--node", dest="node",
148 help="Target node and optional secondary node",
149 metavar="<pnode>[:<snode>]"),
150 cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
152 default=20 * 1024, type="unit", metavar="<size>"),
153 cli_option("--swap-size", dest="swap", help="Swap size",
154 default=4 * 1024, type="unit", metavar="<size>"),
155 keyval_option("-B", "--backend", dest="beparams",
156 type="keyval", default={},
157 help="Backend parameters"),
158 make_option("-t", "--disk-template", dest="disk_template",
159 help="Custom disk setup (diskless, file, plain, drbd)",
160 default=None, metavar="TEMPL"),
161 make_option("-i", "--ip", dest="ip",
162 help="IP address ('none' [default], 'auto', or specify address)",
163 default='none', type="string", metavar="<ADDRESS>"),
164 make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
165 action="store_false", help="Don't wait for sync (DANGEROUS!)"),
166 make_option("-b", "--bridge", dest="bridge",
167 help="Bridge to connect this instance to",
168 default=None, metavar="<bridge>"),
169 make_option("--mac", dest="mac",
170 help="MAC address ('auto' [default], or specify address)",
171 default='auto', type="string", metavar="<MACADDRESS>"),
172 make_option("--src-node", dest="src_node", help="Source node",
174 make_option("--src-dir", dest="src_dir", help="Source directory",
176 make_option("--no-ip-check", dest="ip_check", default=True,
177 action="store_false", help="Don't check that the instance's IP"
179 make_option("--iallocator", metavar="<NAME>",
180 help="Select nodes for the instance automatically using the"
181 " <NAME> iallocator plugin", default=None, type="string"),
182 make_option("--file-storage-dir", dest="file_storage_dir",
183 help="Relative path under default cluster-wide file storage dir"
184 " to store file-based disks", default=None,
186 make_option("--file-driver", dest="file_driver", help="Driver to use"
187 " for image files", default="loop", metavar="<DRIVER>"),
188 ikv_option("-H", "--hypervisor", dest="hypervisor",
189 help="Hypervisor and hypervisor options, in the format"
190 " hypervisor:option=value,option=value,...", default=None,
195 'list': (PrintExportList, ARGS_NONE,
197 make_option("--node", dest="nodes", default=[], action="append",
198 help="List only backups stored on this node"
199 " (can be used multiple times)"),
201 "", "Lists instance exports available in the ganeti cluster"),
202 'export': (ExportInstance, ARGS_ONE,
203 [DEBUG_OPT, FORCE_OPT,
204 make_option("-n", "--node", dest="node", help="Target node",
206 make_option("","--noshutdown", dest="shutdown",
207 action="store_false", default=True,
208 help="Don't shutdown the instance (unsafe)"), ],
209 "-n <target_node> [opts...] <name>",
210 "Exports an instance to an image"),
211 'import': (ImportInstance, ARGS_ONE, import_opts,
212 ("[...] -t disk-type -n node[:secondary-node]"
213 " --src-node node --src-dir dir"
215 "Imports an instance from an exported image"),
216 'remove': (RemoveExport, ARGS_ONE,
219 "Remove exports of named instance from the filesystem."),
222 if __name__ == '__main__':
223 sys.exit(GenericMain(commands))