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
23 from optparse import make_option
25 from ganeti.cli import *
26 from ganeti import cmdlib
27 from ganeti import opcodes
28 from ganeti import constants
30 def PrintExportList(opts, args):
31 """Prints a list of all the exported system images.
34 opts - class with options as members (should be empty)
35 args - should be empty
41 op = opcodes.OpQueryExports(nodes=opts.nodes)
42 exports = SubmitOpCode(op)
44 print ("Node: %s" % node)
46 if isinstance(exports[node], list):
47 for instance_name in exports[node]:
48 print ("\t%s" % instance_name)
50 print (" Could not get exports list")
53 def ExportInstance(opts, args):
54 """Export an instance to an image in the cluster.
57 opts - class with options as members
58 args - list with a single element, the instance name
61 1 in case of error, 0 otherwise
64 op = opcodes.OpExportInstance(instance_name=args[0],
65 target_node=opts.node,
66 shutdown=opts.shutdown)
71 def ImportInstance(opts, args):
72 """Add an instance to the cluster.
75 opts - class with options as members
76 args - list with a single element, the new instance name
78 memory - amount of memory to allocate to instance (MiB)
79 size - amount of disk space to allocate to instance (MiB)
80 os - which OS to run on instance
81 node - node to run new instance on
82 src_node - node containing the export
83 src_dir - directory on the old node with the export in it
86 1 in case of error, 0 otherwise
91 (pnode, snode) = SplitNodeOption(opts.node)
93 op = opcodes.OpCreateInstance(instance_name=instance, mem_size=opts.mem,
94 disk_size=opts.size, swap_size=opts.swap,
95 disk_template=opts.disk_template,
96 mode=constants.INSTANCE_IMPORT,
97 pnode=pnode, snode=snode,
98 vcpus=opts.vcpus, ip_check=opts.ip_check,
99 ip=opts.ip, bridge=opts.bridge, start=False,
100 src_node=opts.src_node, src_path=opts.src_dir,
101 wait_for_sync=opts.wait_for_sync, mac="auto",
102 iallocator=opts.iallocator)
107 def RemoveExport(opts, args):
108 """Remove an export from the cluster.
111 opts - class with options as members
112 args - list with a single element, the exported instance to remove
116 1 in case of error, 0 otherwise
120 op = opcodes.OpRemoveExport(instance_name=args[0])
126 # this is defined separately due to readability only
129 make_option("-n", "--node", dest="node",
130 help="Target node and optional secondary node",
131 metavar="<pnode>[:<snode>]"),
132 cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
134 default=20 * 1024, type="unit", metavar="<size>"),
135 cli_option("--swap-size", dest="swap", help="Swap size",
136 default=4 * 1024, type="unit", metavar="<size>"),
137 cli_option("-m", "--memory", dest="mem", help="Memory size",
138 default=128, type="unit", metavar="<mem>"),
139 make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs",
140 default=1, type="int", metavar="<PROC>"),
141 make_option("-t", "--disk-template", dest="disk_template",
142 help="Custom disk setup (diskless, plain, local_raid1,"
143 " remote_raid1 or drbd)", default=None, metavar="TEMPL"),
144 make_option("-i", "--ip", dest="ip",
145 help="IP address ('none' [default], 'auto', or specify address)",
146 default='none', type="string", metavar="<ADDRESS>"),
147 make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
148 action="store_false", help="Don't wait for sync (DANGEROUS!)"),
149 make_option("-b", "--bridge", dest="bridge",
150 help="Bridge to connect this instance to",
151 default=None, metavar="<bridge>"),
152 make_option("--src-node", dest="src_node", help="Source node",
154 make_option("--src-dir", dest="src_dir", help="Source directory",
156 make_option("--no-ip-check", dest="ip_check", default=True,
157 action="store_false", help="Don't check that the instance's IP"
159 make_option("--iallocator", metavar="<NAME>",
160 help="Select nodes for the instance automatically using the"
161 " <NAME> iallocator plugin", default=None, type="string"),
165 'list': (PrintExportList, ARGS_NONE,
167 make_option("--node", dest="nodes", default=[], action="append",
168 help="List only backups stored on this node"
169 " (can be used multiple times)"),
171 "", "Lists instance exports available in the ganeti cluster"),
172 'export': (ExportInstance, ARGS_ONE,
173 [DEBUG_OPT, FORCE_OPT,
174 make_option("-n", "--node", dest="node", help="Target node",
176 make_option("","--noshutdown", dest="shutdown",
177 action="store_false", default=True,
178 help="Don't shutdown the instance (unsafe)"), ],
179 "-n <target_node> [opts...] <name>",
180 "Exports an instance to an image"),
181 'import': (ImportInstance, ARGS_ONE, import_opts, "[opts...] <name>",
182 "Imports an instance from an exported image"),
183 'remove': (RemoveExport, ARGS_ONE,
186 "Remove exports of named instance from the filesystem."),
189 if __name__ == '__main__':
190 sys.exit(GenericMain(commands))