root / scripts / gnt-backup @ e16dfb5b
History | View | Annotate | Download (4.4 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 | 2f79bd34 | Iustin Pop | # pylint: disable-msg=W0401,W0614 |
23 | 2f79bd34 | Iustin Pop | # W0401: Wildcard import ganeti.cli |
24 | 2f79bd34 | Iustin Pop | # W0614: Unused import %s from wildcard import (since we need cli) |
25 | 2f79bd34 | Iustin Pop | |
26 | dd4b1106 | Iustin Pop | import sys |
27 | dd4b1106 | Iustin Pop | |
28 | dd4b1106 | Iustin Pop | from ganeti.cli import * |
29 | dd4b1106 | Iustin Pop | from ganeti import opcodes |
30 | dd4b1106 | Iustin Pop | from ganeti import constants |
31 | 50a707fa | Iustin Pop | from ganeti import errors |
32 | 50a707fa | Iustin Pop | from ganeti import utils |
33 | dd4b1106 | Iustin Pop | |
34 | 7c0d6283 | Michael Hanselmann | |
35 | e1d2aa39 | Alexander Schreiber | _VALUE_TRUE = "true" |
36 | e1d2aa39 | Alexander Schreiber | |
37 | a8005e17 | Michael Hanselmann | |
38 | dd4b1106 | Iustin Pop | def PrintExportList(opts, args): |
39 | dd4b1106 | Iustin Pop | """Prints a list of all the exported system images. |
40 | dd4b1106 | Iustin Pop | |
41 | 48de3413 | Iustin Pop | @param opts: the command line options selected by the user |
42 | 48de3413 | Iustin Pop | @type args: list |
43 | 48de3413 | Iustin Pop | @param args: should be an empty list |
44 | 48de3413 | Iustin Pop | @rtype: int |
45 | 48de3413 | Iustin Pop | @return: the desired exit code |
46 | dd4b1106 | Iustin Pop | |
47 | dd4b1106 | Iustin Pop | """ |
48 | 77921a95 | Iustin Pop | exports = GetClient().QueryExports(opts.nodes, False) |
49 | d70b3058 | Iustin Pop | retcode = 0 |
50 | dd4b1106 | Iustin Pop | for node in exports: |
51 | 3a24c527 | Iustin Pop | ToStdout("Node: %s", node) |
52 | 3a24c527 | Iustin Pop | ToStdout("Exports:") |
53 | 461f0538 | Guido Trotter | if isinstance(exports[node], list): |
54 | 461f0538 | Guido Trotter | for instance_name in exports[node]: |
55 | 3a24c527 | Iustin Pop | ToStdout("\t%s", instance_name) |
56 | 461f0538 | Guido Trotter | else: |
57 | 3a24c527 | Iustin Pop | ToStdout(" Could not get exports list") |
58 | d70b3058 | Iustin Pop | retcode = 1 |
59 | d70b3058 | Iustin Pop | return retcode |
60 | dd4b1106 | Iustin Pop | |
61 | dd4b1106 | Iustin Pop | |
62 | dd4b1106 | Iustin Pop | def ExportInstance(opts, args): |
63 | dd4b1106 | Iustin Pop | """Export an instance to an image in the cluster. |
64 | dd4b1106 | Iustin Pop | |
65 | 48de3413 | Iustin Pop | @param opts: the command line options selected by the user |
66 | 48de3413 | Iustin Pop | @type args: list |
67 | 48de3413 | Iustin Pop | @param args: should contain only one element, the name |
68 | 48de3413 | Iustin Pop | of the instance to be exported |
69 | 48de3413 | Iustin Pop | @rtype: int |
70 | 48de3413 | Iustin Pop | @return: the desired exit code |
71 | dd4b1106 | Iustin Pop | |
72 | dd4b1106 | Iustin Pop | """ |
73 | dd4b1106 | Iustin Pop | op = opcodes.OpExportInstance(instance_name=args[0], |
74 | dd4b1106 | Iustin Pop | target_node=opts.node, |
75 | dd4b1106 | Iustin Pop | shutdown=opts.shutdown) |
76 | dd4b1106 | Iustin Pop | |
77 | 084f05a5 | Iustin Pop | fin_resu, dlist = SubmitOpCode(op) |
78 | 084f05a5 | Iustin Pop | if not isinstance(dlist, list): |
79 | 084f05a5 | Iustin Pop | ToStderr("Cannot parse execution results") |
80 | 084f05a5 | Iustin Pop | return 1 |
81 | 084f05a5 | Iustin Pop | tot_dsk = len(dlist) |
82 | 084f05a5 | Iustin Pop | # TODO: handle diskless instances |
83 | 084f05a5 | Iustin Pop | if dlist.count(False) == 0: |
84 | 084f05a5 | Iustin Pop | # all OK |
85 | 084f05a5 | Iustin Pop | rcode = 0 |
86 | 084f05a5 | Iustin Pop | elif dlist.count(True) == 0: |
87 | 084f05a5 | Iustin Pop | ToStderr("Error: No disks were backed up successfully." |
88 | 084f05a5 | Iustin Pop | " The export doesn't have any valid data," |
89 | 084f05a5 | Iustin Pop | " it is recommended to retry the operation.") |
90 | 084f05a5 | Iustin Pop | rcode = 1 |
91 | 084f05a5 | Iustin Pop | else: |
92 | 084f05a5 | Iustin Pop | ToStderr("Partial export failure: %d disks backed up, %d disks failed.", |
93 | 084f05a5 | Iustin Pop | dlist.count(True), dlist.count(False)) |
94 | 084f05a5 | Iustin Pop | rcode = 2 |
95 | 084f05a5 | Iustin Pop | if not fin_resu: |
96 | 084f05a5 | Iustin Pop | rcode = 1 |
97 | 084f05a5 | Iustin Pop | return rcode |
98 | 60d49723 | Michael Hanselmann | |
99 | dd4b1106 | Iustin Pop | def ImportInstance(opts, args): |
100 | dd4b1106 | Iustin Pop | """Add an instance to the cluster. |
101 | dd4b1106 | Iustin Pop | |
102 | d77490c5 | Iustin Pop | This is just a wrapper over GenericInstanceCreate. |
103 | dd4b1106 | Iustin Pop | |
104 | dd4b1106 | Iustin Pop | """ |
105 | d77490c5 | Iustin Pop | return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args) |
106 | dd4b1106 | Iustin Pop | |
107 | dd4b1106 | Iustin Pop | |
108 | 9ac99fda | Guido Trotter | def RemoveExport(opts, args): |
109 | 9ac99fda | Guido Trotter | """Remove an export from the cluster. |
110 | 9ac99fda | Guido Trotter | |
111 | 48de3413 | Iustin Pop | @param opts: the command line options selected by the user |
112 | 48de3413 | Iustin Pop | @type args: list |
113 | 48de3413 | Iustin Pop | @param args: should contain only one element, the name of the |
114 | 48de3413 | Iustin Pop | instance whose backup should be removed |
115 | 48de3413 | Iustin Pop | @rtype: int |
116 | 48de3413 | Iustin Pop | @return: the desired exit code |
117 | 9ac99fda | Guido Trotter | |
118 | 9ac99fda | Guido Trotter | """ |
119 | 9ac99fda | Guido Trotter | instance = args[0] |
120 | 9ac99fda | Guido Trotter | op = opcodes.OpRemoveExport(instance_name=args[0]) |
121 | 9ac99fda | Guido Trotter | |
122 | 9ac99fda | Guido Trotter | SubmitOpCode(op) |
123 | 9ac99fda | Guido Trotter | return 0 |
124 | 9ac99fda | Guido Trotter | |
125 | 9ac99fda | Guido Trotter | |
126 | dd4b1106 | Iustin Pop | # this is defined separately due to readability only |
127 | dd4b1106 | Iustin Pop | import_opts = [ |
128 | 990b7886 | Iustin Pop | NODE_PLACEMENT_OPT, |
129 | 087ed2ed | Iustin Pop | BACKEND_OPT, |
130 | 4f365444 | Iustin Pop | DISK_TEMPLATE_OPT, |
131 | e3876ccb | Iustin Pop | DISK_OPT, |
132 | ff00c1a7 | Iustin Pop | OS_SIZE_OPT, |
133 | 7d3a9fab | Iustin Pop | NET_OPT, |
134 | 26023ecd | Iustin Pop | NONICS_OPT, |
135 | 3f75b4f3 | Iustin Pop | NWSYNC_OPT, |
136 | df62e5db | Iustin Pop | SRC_DIR_OPT, |
137 | df62e5db | Iustin Pop | SRC_NODE_OPT, |
138 | 91e0748c | Iustin Pop | NOIPCHECK_OPT, |
139 | 4eb62659 | Iustin Pop | IALLOCATOR_OPT, |
140 | 4a25828c | Iustin Pop | FILESTORE_DIR_OPT, |
141 | 0f87c43e | Iustin Pop | FILESTORE_DRIVER_OPT, |
142 | 236fd9c4 | Iustin Pop | HYPERVISOR_OPT, |
143 | d77490c5 | Iustin Pop | SUBMIT_OPT, |
144 | dd4b1106 | Iustin Pop | ] |
145 | dd4b1106 | Iustin Pop | |
146 | dd4b1106 | Iustin Pop | commands = { |
147 | 6ea815cf | Iustin Pop | 'list': ( |
148 | 6ea815cf | Iustin Pop | PrintExportList, ARGS_NONE, |
149 | 064c21f8 | Iustin Pop | [NODE_LIST_OPT], |
150 | 6ea815cf | Iustin Pop | "", "Lists instance exports available in the ganeti cluster"), |
151 | 6ea815cf | Iustin Pop | 'export': ( |
152 | 6ea815cf | Iustin Pop | ExportInstance, ARGS_ONE_INSTANCE, |
153 | 064c21f8 | Iustin Pop | [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT], |
154 | 6ea815cf | Iustin Pop | "-n <target_node> [opts...] <name>", |
155 | 6ea815cf | Iustin Pop | "Exports an instance to an image"), |
156 | 6ea815cf | Iustin Pop | 'import': ( |
157 | 6ea815cf | Iustin Pop | ImportInstance, ARGS_ONE_INSTANCE, import_opts, |
158 | 6ea815cf | Iustin Pop | "[...] -t disk-type -n node[:secondary-node] <name>", |
159 | 6ea815cf | Iustin Pop | "Imports an instance from an exported image"), |
160 | 6ea815cf | Iustin Pop | 'remove': ( |
161 | 064c21f8 | Iustin Pop | RemoveExport, [ArgUnknown(min=1, max=1)], [], |
162 | 6ea815cf | Iustin Pop | "<name>", "Remove exports of named instance from the filesystem."), |
163 | dd4b1106 | Iustin Pop | } |
164 | dd4b1106 | Iustin Pop | |
165 | a8005e17 | Michael Hanselmann | |
166 | dd4b1106 | Iustin Pop | if __name__ == '__main__': |
167 | 3ecf6786 | Iustin Pop | sys.exit(GenericMain(commands)) |