root / scripts / gnt-backup @ 3a24c527
History | View | Annotate | Download (7.7 kB)
1 | dd4b1106 | Iustin Pop | #!/usr/bin/python |
---|---|---|---|
2 | dd4b1106 | Iustin Pop | # |
3 | dd4b1106 | Iustin Pop | |
4 | dd4b1106 | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc. |
5 | dd4b1106 | Iustin Pop | # |
6 | dd4b1106 | Iustin Pop | # This program is free software; you can redistribute it and/or modify |
7 | dd4b1106 | Iustin Pop | # it under the terms of the GNU General Public License as published by |
8 | dd4b1106 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or |
9 | dd4b1106 | Iustin Pop | # (at your option) any later version. |
10 | dd4b1106 | Iustin Pop | # |
11 | dd4b1106 | Iustin Pop | # This program is distributed in the hope that it will be useful, but |
12 | dd4b1106 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | dd4b1106 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | dd4b1106 | Iustin Pop | # General Public License for more details. |
15 | dd4b1106 | Iustin Pop | # |
16 | dd4b1106 | Iustin Pop | # You should have received a copy of the GNU General Public License |
17 | dd4b1106 | Iustin Pop | # along with this program; if not, write to the Free Software |
18 | dd4b1106 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | dd4b1106 | Iustin Pop | # 02110-1301, USA. |
20 | dd4b1106 | Iustin Pop | |
21 | dd4b1106 | Iustin Pop | |
22 | dd4b1106 | Iustin Pop | import sys |
23 | dd4b1106 | Iustin Pop | from optparse import make_option |
24 | dd4b1106 | Iustin Pop | |
25 | dd4b1106 | Iustin Pop | from ganeti.cli import * |
26 | dd4b1106 | Iustin Pop | from ganeti import cmdlib |
27 | dd4b1106 | Iustin Pop | from ganeti import opcodes |
28 | dd4b1106 | Iustin Pop | from ganeti import constants |
29 | dd4b1106 | Iustin Pop | |
30 | 7c0d6283 | Michael Hanselmann | |
31 | e1d2aa39 | Alexander Schreiber | _VALUE_TRUE = "true" |
32 | e1d2aa39 | Alexander Schreiber | |
33 | dd4b1106 | Iustin Pop | def PrintExportList(opts, args): |
34 | dd4b1106 | Iustin Pop | """Prints a list of all the exported system images. |
35 | dd4b1106 | Iustin Pop | |
36 | dd4b1106 | Iustin Pop | Args: |
37 | dd4b1106 | Iustin Pop | opts - class with options as members (should be empty) |
38 | dd4b1106 | Iustin Pop | args - should be empty |
39 | dd4b1106 | Iustin Pop | |
40 | dd4b1106 | Iustin Pop | Returns: |
41 | dd4b1106 | Iustin Pop | nothing |
42 | dd4b1106 | Iustin Pop | |
43 | dd4b1106 | Iustin Pop | """ |
44 | 8bb66978 | Michael Hanselmann | exports = GetClient().QueryExports(opts.nodes) |
45 | dd4b1106 | Iustin Pop | for node in exports: |
46 | 3a24c527 | Iustin Pop | ToStdout("Node: %s", node) |
47 | 3a24c527 | Iustin Pop | ToStdout("Exports:") |
48 | 461f0538 | Guido Trotter | if isinstance(exports[node], list): |
49 | 461f0538 | Guido Trotter | for instance_name in exports[node]: |
50 | 3a24c527 | Iustin Pop | ToStdout("\t%s", instance_name) |
51 | 461f0538 | Guido Trotter | else: |
52 | 3a24c527 | Iustin Pop | ToStdout(" Could not get exports list") |
53 | dd4b1106 | Iustin Pop | |
54 | dd4b1106 | Iustin Pop | |
55 | dd4b1106 | Iustin Pop | def ExportInstance(opts, args): |
56 | dd4b1106 | Iustin Pop | """Export an instance to an image in the cluster. |
57 | dd4b1106 | Iustin Pop | |
58 | dd4b1106 | Iustin Pop | Args: |
59 | dd4b1106 | Iustin Pop | opts - class with options as members |
60 | dd4b1106 | Iustin Pop | args - list with a single element, the instance name |
61 | dd4b1106 | Iustin Pop | |
62 | dd4b1106 | Iustin Pop | Returns: |
63 | dd4b1106 | Iustin Pop | 1 in case of error, 0 otherwise |
64 | dd4b1106 | Iustin Pop | |
65 | dd4b1106 | Iustin Pop | """ |
66 | dd4b1106 | Iustin Pop | op = opcodes.OpExportInstance(instance_name=args[0], |
67 | dd4b1106 | Iustin Pop | target_node=opts.node, |
68 | dd4b1106 | Iustin Pop | shutdown=opts.shutdown) |
69 | dd4b1106 | Iustin Pop | |
70 | dd4b1106 | Iustin Pop | SubmitOpCode(op) |
71 | dd4b1106 | Iustin Pop | |
72 | 60d49723 | Michael Hanselmann | |
73 | dd4b1106 | Iustin Pop | def ImportInstance(opts, args): |
74 | dd4b1106 | Iustin Pop | """Add an instance to the cluster. |
75 | dd4b1106 | Iustin Pop | |
76 | dd4b1106 | Iustin Pop | Args: |
77 | dd4b1106 | Iustin Pop | opts - class with options as members |
78 | dd4b1106 | Iustin Pop | args - list with a single element, the new instance name |
79 | dd4b1106 | Iustin Pop | Opts used: |
80 | dd4b1106 | Iustin Pop | memory - amount of memory to allocate to instance (MiB) |
81 | dd4b1106 | Iustin Pop | size - amount of disk space to allocate to instance (MiB) |
82 | dd4b1106 | Iustin Pop | os - which OS to run on instance |
83 | dd4b1106 | Iustin Pop | node - node to run new instance on |
84 | dd4b1106 | Iustin Pop | src_node - node containing the export |
85 | dd4b1106 | Iustin Pop | src_dir - directory on the old node with the export in it |
86 | dd4b1106 | Iustin Pop | |
87 | dd4b1106 | Iustin Pop | Returns: |
88 | dd4b1106 | Iustin Pop | 1 in case of error, 0 otherwise |
89 | dd4b1106 | Iustin Pop | |
90 | dd4b1106 | Iustin Pop | """ |
91 | dd4b1106 | Iustin Pop | instance = args[0] |
92 | dd4b1106 | Iustin Pop | |
93 | 60d49723 | Michael Hanselmann | (pnode, snode) = SplitNodeOption(opts.node) |
94 | 60d49723 | Michael Hanselmann | |
95 | b03efa30 | Guido Trotter | hypervisor = None |
96 | b03efa30 | Guido Trotter | hvparams = {} |
97 | b03efa30 | Guido Trotter | if opts.hypervisor: |
98 | b03efa30 | Guido Trotter | hypervisor, hvparams = opts.hypervisor |
99 | b03efa30 | Guido Trotter | |
100 | b03efa30 | Guido Trotter | ValidateBeParams(opts.beparams) |
101 | b03efa30 | Guido Trotter | |
102 | 338e51e8 | Iustin Pop | op = opcodes.OpCreateInstance(instance_name=instance, |
103 | dd4b1106 | Iustin Pop | disk_size=opts.size, swap_size=opts.swap, |
104 | dd4b1106 | Iustin Pop | disk_template=opts.disk_template, |
105 | 098c0958 | Michael Hanselmann | mode=constants.INSTANCE_IMPORT, |
106 | 60d49723 | Michael Hanselmann | pnode=pnode, snode=snode, |
107 | 338e51e8 | Iustin Pop | ip_check=opts.ip_check, |
108 | dd4b1106 | Iustin Pop | ip=opts.ip, bridge=opts.bridge, start=False, |
109 | dd4b1106 | Iustin Pop | src_node=opts.src_node, src_path=opts.src_dir, |
110 | 538475ca | Iustin Pop | wait_for_sync=opts.wait_for_sync, mac="auto", |
111 | 93cb65c5 | Manuel Franceschini | file_storage_dir=opts.file_storage_dir, |
112 | 93cb65c5 | Manuel Franceschini | file_driver=opts.file_driver, |
113 | e1d2aa39 | Alexander Schreiber | iallocator=opts.iallocator, |
114 | b03efa30 | Guido Trotter | hypervisor=hypervisor, |
115 | b03efa30 | Guido Trotter | hvparams=hvparams, |
116 | b03efa30 | Guido Trotter | beparams=opts.beparams) |
117 | e1d2aa39 | Alexander Schreiber | |
118 | dd4b1106 | Iustin Pop | SubmitOpCode(op) |
119 | dd4b1106 | Iustin Pop | return 0 |
120 | dd4b1106 | Iustin Pop | |
121 | dd4b1106 | Iustin Pop | |
122 | 9ac99fda | Guido Trotter | def RemoveExport(opts, args): |
123 | 9ac99fda | Guido Trotter | """Remove an export from the cluster. |
124 | 9ac99fda | Guido Trotter | |
125 | 9ac99fda | Guido Trotter | Args: |
126 | 9ac99fda | Guido Trotter | opts - class with options as members |
127 | 9ac99fda | Guido Trotter | args - list with a single element, the exported instance to remove |
128 | 9ac99fda | Guido Trotter | Opts used: |
129 | 9ac99fda | Guido Trotter | |
130 | 9ac99fda | Guido Trotter | Returns: |
131 | 9ac99fda | Guido Trotter | 1 in case of error, 0 otherwise |
132 | 9ac99fda | Guido Trotter | |
133 | 9ac99fda | Guido Trotter | """ |
134 | 9ac99fda | Guido Trotter | instance = args[0] |
135 | 9ac99fda | Guido Trotter | op = opcodes.OpRemoveExport(instance_name=args[0]) |
136 | 9ac99fda | Guido Trotter | |
137 | 9ac99fda | Guido Trotter | SubmitOpCode(op) |
138 | 9ac99fda | Guido Trotter | return 0 |
139 | 9ac99fda | Guido Trotter | |
140 | 9ac99fda | Guido Trotter | |
141 | dd4b1106 | Iustin Pop | # this is defined separately due to readability only |
142 | dd4b1106 | Iustin Pop | import_opts = [ |
143 | dd4b1106 | Iustin Pop | DEBUG_OPT, |
144 | 60d49723 | Michael Hanselmann | make_option("-n", "--node", dest="node", |
145 | 60d49723 | Michael Hanselmann | help="Target node and optional secondary node", |
146 | 60d49723 | Michael Hanselmann | metavar="<pnode>[:<snode>]"), |
147 | d5687e89 | Guido Trotter | cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless" |
148 | d5687e89 | Guido Trotter | " a suffix is used", |
149 | dd4b1106 | Iustin Pop | default=20 * 1024, type="unit", metavar="<size>"), |
150 | dd4b1106 | Iustin Pop | cli_option("--swap-size", dest="swap", help="Swap size", |
151 | dd4b1106 | Iustin Pop | default=4 * 1024, type="unit", metavar="<size>"), |
152 | b03efa30 | Guido Trotter | keyval_option("-B", "--backend", dest="beparams", |
153 | b03efa30 | Guido Trotter | type="keyval", default={}, |
154 | b03efa30 | Guido Trotter | help="Backend parameters"), |
155 | dd4b1106 | Iustin Pop | make_option("-t", "--disk-template", dest="disk_template", |
156 | 726a7f7f | Iustin Pop | help="Custom disk setup (diskless, file, plain, drbd)", |
157 | abdf0113 | Iustin Pop | default=None, metavar="TEMPL"), |
158 | dd4b1106 | Iustin Pop | make_option("-i", "--ip", dest="ip", |
159 | dd4b1106 | Iustin Pop | help="IP address ('none' [default], 'auto', or specify address)", |
160 | dd4b1106 | Iustin Pop | default='none', type="string", metavar="<ADDRESS>"), |
161 | dd4b1106 | Iustin Pop | make_option("--no-wait-for-sync", dest="wait_for_sync", default=True, |
162 | dd4b1106 | Iustin Pop | action="store_false", help="Don't wait for sync (DANGEROUS!)"), |
163 | dd4b1106 | Iustin Pop | make_option("-b", "--bridge", dest="bridge", |
164 | dd4b1106 | Iustin Pop | help="Bridge to connect this instance to", |
165 | dd4b1106 | Iustin Pop | default=None, metavar="<bridge>"), |
166 | dd4b1106 | Iustin Pop | make_option("--src-node", dest="src_node", help="Source node", |
167 | dd4b1106 | Iustin Pop | metavar="<node>"), |
168 | dd4b1106 | Iustin Pop | make_option("--src-dir", dest="src_dir", help="Source directory", |
169 | dd4b1106 | Iustin Pop | metavar="<dir>"), |
170 | bdd55f71 | Iustin Pop | make_option("--no-ip-check", dest="ip_check", default=True, |
171 | bdd55f71 | Iustin Pop | action="store_false", help="Don't check that the instance's IP" |
172 | bdd55f71 | Iustin Pop | " is alive"), |
173 | 538475ca | Iustin Pop | make_option("--iallocator", metavar="<NAME>", |
174 | 538475ca | Iustin Pop | help="Select nodes for the instance automatically using the" |
175 | 538475ca | Iustin Pop | " <NAME> iallocator plugin", default=None, type="string"), |
176 | 93cb65c5 | Manuel Franceschini | make_option("--file-storage-dir", dest="file_storage_dir", |
177 | 93cb65c5 | Manuel Franceschini | help="Relative path under default cluster-wide file storage dir" |
178 | 93cb65c5 | Manuel Franceschini | " to store file-based disks", default=None, |
179 | 93cb65c5 | Manuel Franceschini | metavar="<DIR>"), |
180 | 93cb65c5 | Manuel Franceschini | make_option("--file-driver", dest="file_driver", help="Driver to use" |
181 | 93cb65c5 | Manuel Franceschini | " for image files", default="loop", metavar="<DRIVER>"), |
182 | b03efa30 | Guido Trotter | ikv_option("-H", "--hypervisor", dest="hypervisor", |
183 | b03efa30 | Guido Trotter | help="Hypervisor and hypervisor options, in the format" |
184 | b03efa30 | Guido Trotter | " hypervisor:option=value,option=value,...", default=None, |
185 | b03efa30 | Guido Trotter | type="identkeyval"), |
186 | dd4b1106 | Iustin Pop | ] |
187 | dd4b1106 | Iustin Pop | |
188 | dd4b1106 | Iustin Pop | commands = { |
189 | dd4b1106 | Iustin Pop | 'list': (PrintExportList, ARGS_NONE, |
190 | dd4b1106 | Iustin Pop | [DEBUG_OPT, |
191 | 1e020d1b | Michael Hanselmann | make_option("--node", dest="nodes", default=[], action="append", |
192 | 59885275 | Guido Trotter | help="List only backups stored on this node" |
193 | 59885275 | Guido Trotter | " (can be used multiple times)"), |
194 | dd4b1106 | Iustin Pop | ], |
195 | 9a033156 | Iustin Pop | "", "Lists instance exports available in the ganeti cluster"), |
196 | dd4b1106 | Iustin Pop | 'export': (ExportInstance, ARGS_ONE, |
197 | 60d49723 | Michael Hanselmann | [DEBUG_OPT, FORCE_OPT, |
198 | 60d49723 | Michael Hanselmann | make_option("-n", "--node", dest="node", help="Target node", |
199 | 60d49723 | Michael Hanselmann | metavar="<node>"), |
200 | dd4b1106 | Iustin Pop | make_option("","--noshutdown", dest="shutdown", |
201 | dd4b1106 | Iustin Pop | action="store_false", default=True, |
202 | dd4b1106 | Iustin Pop | help="Don't shutdown the instance (unsafe)"), ], |
203 | 9a033156 | Iustin Pop | "-n <target_node> [opts...] <name>", |
204 | dd4b1106 | Iustin Pop | "Exports an instance to an image"), |
205 | bdb7d4e8 | Michael Hanselmann | 'import': (ImportInstance, ARGS_ONE, import_opts, |
206 | bdb7d4e8 | Michael Hanselmann | ("[...] -t disk-type -n node[:secondary-node]" |
207 | bdb7d4e8 | Michael Hanselmann | " --src-node node --src-dir dir" |
208 | bdb7d4e8 | Michael Hanselmann | " <name>"), |
209 | dd4b1106 | Iustin Pop | "Imports an instance from an exported image"), |
210 | 9ac99fda | Guido Trotter | 'remove': (RemoveExport, ARGS_ONE, |
211 | 9ac99fda | Guido Trotter | [DEBUG_OPT], |
212 | 9a033156 | Iustin Pop | "<name>", |
213 | 9ac99fda | Guido Trotter | "Remove exports of named instance from the filesystem."), |
214 | dd4b1106 | Iustin Pop | } |
215 | dd4b1106 | Iustin Pop | |
216 | dd4b1106 | Iustin Pop | if __name__ == '__main__': |
217 | 3ecf6786 | Iustin Pop | sys.exit(GenericMain(commands)) |