root / scripts / gnt-cluster @ 12c3449a
History | View | Annotate | Download (9.6 kB)
1 | a8083063 | Iustin Pop | #!/usr/bin/python |
---|---|---|---|
2 | a8083063 | Iustin Pop | # |
3 | a8083063 | Iustin Pop | |
4 | a8083063 | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc. |
5 | a8083063 | Iustin Pop | # |
6 | a8083063 | Iustin Pop | # This program is free software; you can redistribute it and/or modify |
7 | a8083063 | Iustin Pop | # it under the terms of the GNU General Public License as published by |
8 | a8083063 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or |
9 | a8083063 | Iustin Pop | # (at your option) any later version. |
10 | a8083063 | Iustin Pop | # |
11 | a8083063 | Iustin Pop | # This program is distributed in the hope that it will be useful, but |
12 | a8083063 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | a8083063 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | a8083063 | Iustin Pop | # General Public License for more details. |
15 | a8083063 | Iustin Pop | # |
16 | a8083063 | Iustin Pop | # You should have received a copy of the GNU General Public License |
17 | a8083063 | Iustin Pop | # along with this program; if not, write to the Free Software |
18 | a8083063 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | a8083063 | Iustin Pop | # 02110-1301, USA. |
20 | a8083063 | Iustin Pop | |
21 | a8083063 | Iustin Pop | |
22 | a8083063 | Iustin Pop | import sys |
23 | a8083063 | Iustin Pop | from optparse import make_option |
24 | a8083063 | Iustin Pop | import pprint |
25 | a8083063 | Iustin Pop | |
26 | a8083063 | Iustin Pop | from ganeti.cli import * |
27 | a8083063 | Iustin Pop | from ganeti import opcodes |
28 | c2a62a33 | Michael Hanselmann | from ganeti import constants |
29 | a8083063 | Iustin Pop | |
30 | a8083063 | Iustin Pop | |
31 | a8083063 | Iustin Pop | def InitCluster(opts, args): |
32 | a8083063 | Iustin Pop | """Initialize the cluster. |
33 | a8083063 | Iustin Pop | |
34 | a8083063 | Iustin Pop | Args: |
35 | a8083063 | Iustin Pop | opts - class with options as members |
36 | a8083063 | Iustin Pop | args - list of arguments, expected to be [clustername] |
37 | a8083063 | Iustin Pop | |
38 | a8083063 | Iustin Pop | """ |
39 | a8083063 | Iustin Pop | op = opcodes.OpInitCluster(cluster_name=args[0], |
40 | a8083063 | Iustin Pop | secondary_ip=opts.secondary_ip, |
41 | a8083063 | Iustin Pop | hypervisor_type=opts.hypervisor_type, |
42 | a8083063 | Iustin Pop | vg_name=opts.vg_name, |
43 | a8083063 | Iustin Pop | mac_prefix=opts.mac_prefix, |
44 | 880478f8 | Iustin Pop | def_bridge=opts.def_bridge, |
45 | 880478f8 | Iustin Pop | master_netdev=opts.master_netdev) |
46 | a8083063 | Iustin Pop | SubmitOpCode(op) |
47 | a8083063 | Iustin Pop | return 0 |
48 | a8083063 | Iustin Pop | |
49 | a8083063 | Iustin Pop | |
50 | a8083063 | Iustin Pop | def DestroyCluster(opts, args): |
51 | a8083063 | Iustin Pop | """Destroy the cluster. |
52 | a8083063 | Iustin Pop | |
53 | a8083063 | Iustin Pop | Args: |
54 | a8083063 | Iustin Pop | opts - class with options as members |
55 | 098c0958 | Michael Hanselmann | |
56 | a8083063 | Iustin Pop | """ |
57 | a8083063 | Iustin Pop | if not opts.yes_do_it: |
58 | a8083063 | Iustin Pop | print ("Destroying a cluster is irreversibly. If you really want destroy" |
59 | 79f7be7b | Iustin Pop | " this cluster, supply the --yes-do-it option.") |
60 | a8083063 | Iustin Pop | return 1 |
61 | a8083063 | Iustin Pop | |
62 | a8083063 | Iustin Pop | op = opcodes.OpDestroyCluster() |
63 | a8083063 | Iustin Pop | SubmitOpCode(op) |
64 | a8083063 | Iustin Pop | return 0 |
65 | a8083063 | Iustin Pop | |
66 | a8083063 | Iustin Pop | |
67 | 07bd8a51 | Iustin Pop | def RenameCluster(opts, args): |
68 | 07bd8a51 | Iustin Pop | """Rename the cluster. |
69 | 07bd8a51 | Iustin Pop | |
70 | 07bd8a51 | Iustin Pop | Args: |
71 | 07bd8a51 | Iustin Pop | opts - class with options as members, we use force only |
72 | 07bd8a51 | Iustin Pop | args - list of arguments, expected to be [new_name] |
73 | 07bd8a51 | Iustin Pop | |
74 | 07bd8a51 | Iustin Pop | """ |
75 | 07bd8a51 | Iustin Pop | name = args[0] |
76 | 07bd8a51 | Iustin Pop | if not opts.force: |
77 | 07bd8a51 | Iustin Pop | usertext = ("This will rename the cluster to '%s'. If you are connected" |
78 | 07bd8a51 | Iustin Pop | " over the network to the cluster name, the operation is very" |
79 | 07bd8a51 | Iustin Pop | " dangerous as the IP address will be removed from the node" |
80 | 07bd8a51 | Iustin Pop | " and the change may not go through. Continue?") % name |
81 | 47988778 | Iustin Pop | if not AskUser(usertext): |
82 | 07bd8a51 | Iustin Pop | return 1 |
83 | 07bd8a51 | Iustin Pop | |
84 | 07bd8a51 | Iustin Pop | op = opcodes.OpRenameCluster(name=name) |
85 | 07bd8a51 | Iustin Pop | SubmitOpCode(op) |
86 | 07bd8a51 | Iustin Pop | return 0 |
87 | 07bd8a51 | Iustin Pop | |
88 | 07bd8a51 | Iustin Pop | |
89 | a8083063 | Iustin Pop | def ShowClusterVersion(opts, args): |
90 | a8083063 | Iustin Pop | """Write version of ganeti software to the standard output. |
91 | a8083063 | Iustin Pop | |
92 | a8083063 | Iustin Pop | Args: |
93 | a8083063 | Iustin Pop | opts - class with options as members |
94 | a8083063 | Iustin Pop | |
95 | a8083063 | Iustin Pop | """ |
96 | a8083063 | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
97 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
98 | a8083063 | Iustin Pop | print ("Software version: %s" % result["software_version"]) |
99 | a8083063 | Iustin Pop | print ("Internode protocol: %s" % result["protocol_version"]) |
100 | a8083063 | Iustin Pop | print ("Configuration format: %s" % result["config_version"]) |
101 | a8083063 | Iustin Pop | print ("OS api version: %s" % result["os_api_version"]) |
102 | a8083063 | Iustin Pop | print ("Export interface: %s" % result["export_version"]) |
103 | a8083063 | Iustin Pop | return 0 |
104 | a8083063 | Iustin Pop | |
105 | a8083063 | Iustin Pop | |
106 | a8083063 | Iustin Pop | def ShowClusterMaster(opts, args): |
107 | a8083063 | Iustin Pop | """Write name of master node to the standard output. |
108 | a8083063 | Iustin Pop | |
109 | a8083063 | Iustin Pop | Args: |
110 | a8083063 | Iustin Pop | opts - class with options as members |
111 | a8083063 | Iustin Pop | |
112 | a8083063 | Iustin Pop | """ |
113 | a8083063 | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
114 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
115 | a8083063 | Iustin Pop | print (result["master"]) |
116 | a8083063 | Iustin Pop | return 0 |
117 | a8083063 | Iustin Pop | |
118 | a8083063 | Iustin Pop | |
119 | a8083063 | Iustin Pop | def ShowClusterConfig(opts, args): |
120 | a8083063 | Iustin Pop | """Shows cluster information. |
121 | a8083063 | Iustin Pop | |
122 | a8083063 | Iustin Pop | """ |
123 | a8083063 | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
124 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
125 | a8083063 | Iustin Pop | |
126 | a8083063 | Iustin Pop | print ("Cluster name: %s" % result["name"]) |
127 | a8083063 | Iustin Pop | |
128 | a8083063 | Iustin Pop | print ("Master node: %s" % result["master"]) |
129 | a8083063 | Iustin Pop | |
130 | 59322403 | Iustin Pop | print ("Architecture (this node): %s (%s)" % |
131 | 59322403 | Iustin Pop | (result["architecture"][0], result["architecture"][1])) |
132 | a8083063 | Iustin Pop | |
133 | a8083063 | Iustin Pop | return 0 |
134 | a8083063 | Iustin Pop | |
135 | a8083063 | Iustin Pop | |
136 | a8083063 | Iustin Pop | def ClusterCopyFile(opts, args): |
137 | a8083063 | Iustin Pop | """Copy a file from master to some nodes. |
138 | a8083063 | Iustin Pop | |
139 | a8083063 | Iustin Pop | Args: |
140 | a8083063 | Iustin Pop | opts - class with options as members |
141 | a8083063 | Iustin Pop | args - list containing a single element, the file name |
142 | a8083063 | Iustin Pop | Opts used: |
143 | a8083063 | Iustin Pop | nodes - list containing the name of target nodes; if empty, all nodes |
144 | a8083063 | Iustin Pop | |
145 | a8083063 | Iustin Pop | """ |
146 | a8083063 | Iustin Pop | op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes) |
147 | a8083063 | Iustin Pop | SubmitOpCode(op) |
148 | a8083063 | Iustin Pop | return 0 |
149 | a8083063 | Iustin Pop | |
150 | a8083063 | Iustin Pop | |
151 | a8083063 | Iustin Pop | def RunClusterCommand(opts, args): |
152 | a8083063 | Iustin Pop | """Run a command on some nodes. |
153 | a8083063 | Iustin Pop | |
154 | a8083063 | Iustin Pop | Args: |
155 | a8083063 | Iustin Pop | opts - class with options as members |
156 | a8083063 | Iustin Pop | args - the command list as a list |
157 | a8083063 | Iustin Pop | Opts used: |
158 | a8083063 | Iustin Pop | nodes: list containing the name of target nodes; if empty, all nodes |
159 | a8083063 | Iustin Pop | |
160 | a8083063 | Iustin Pop | """ |
161 | a8083063 | Iustin Pop | command = " ".join(args) |
162 | a8083063 | Iustin Pop | nodes = opts.nodes |
163 | a8083063 | Iustin Pop | op = opcodes.OpRunClusterCommand(command=command, nodes=nodes) |
164 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
165 | 02715459 | Iustin Pop | for node, output, exit_code in result: |
166 | a8083063 | Iustin Pop | print ("------------------------------------------------") |
167 | a8083063 | Iustin Pop | print ("node: %s" % node) |
168 | a8083063 | Iustin Pop | print ("%s" % output) |
169 | a8083063 | Iustin Pop | print ("return code = %s" % exit_code) |
170 | a8083063 | Iustin Pop | |
171 | a8083063 | Iustin Pop | |
172 | a8083063 | Iustin Pop | def VerifyCluster(opts, args): |
173 | a8083063 | Iustin Pop | """Verify integrity of cluster, performing various test on nodes. |
174 | a8083063 | Iustin Pop | |
175 | a8083063 | Iustin Pop | Args: |
176 | a8083063 | Iustin Pop | opts - class with options as members |
177 | a8083063 | Iustin Pop | |
178 | a8083063 | Iustin Pop | """ |
179 | a8083063 | Iustin Pop | op = opcodes.OpVerifyCluster() |
180 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
181 | a8083063 | Iustin Pop | return result |
182 | a8083063 | Iustin Pop | |
183 | a8083063 | Iustin Pop | |
184 | a8083063 | Iustin Pop | def MasterFailover(opts, args): |
185 | a8083063 | Iustin Pop | """Failover the master node. |
186 | a8083063 | Iustin Pop | |
187 | a8083063 | Iustin Pop | This command, when run on a non-master node, will cause the current |
188 | a8083063 | Iustin Pop | master to cease being master, and the non-master to become new |
189 | a8083063 | Iustin Pop | master. |
190 | a8083063 | Iustin Pop | |
191 | a8083063 | Iustin Pop | """ |
192 | a8083063 | Iustin Pop | op = opcodes.OpMasterFailover() |
193 | a8083063 | Iustin Pop | SubmitOpCode(op) |
194 | a8083063 | Iustin Pop | |
195 | a8083063 | Iustin Pop | |
196 | 73415719 | Iustin Pop | def SearchTags(opts, args): |
197 | 73415719 | Iustin Pop | """Searches the tags on all the cluster. |
198 | 73415719 | Iustin Pop | |
199 | 73415719 | Iustin Pop | """ |
200 | 73415719 | Iustin Pop | op = opcodes.OpSearchTags(pattern=args[0]) |
201 | 73415719 | Iustin Pop | result = SubmitOpCode(op) |
202 | 73415719 | Iustin Pop | if not result: |
203 | 73415719 | Iustin Pop | return 1 |
204 | 73415719 | Iustin Pop | result = list(result) |
205 | 73415719 | Iustin Pop | result.sort() |
206 | 73415719 | Iustin Pop | for path, tag in result: |
207 | 73415719 | Iustin Pop | print "%s %s" % (path, tag) |
208 | 73415719 | Iustin Pop | |
209 | 73415719 | Iustin Pop | |
210 | a8083063 | Iustin Pop | # this is an option common to more than one command, so we declare |
211 | a8083063 | Iustin Pop | # it here and reuse it |
212 | a8083063 | Iustin Pop | node_option = make_option("-n", "--node", action="append", dest="nodes", |
213 | a8083063 | Iustin Pop | help="Node to copy to (if not given, all nodes)" |
214 | a8083063 | Iustin Pop | ", can be given multiple times", metavar="<node>", |
215 | a8083063 | Iustin Pop | default=[]) |
216 | a8083063 | Iustin Pop | |
217 | a8083063 | Iustin Pop | commands = { |
218 | a8083063 | Iustin Pop | 'init': (InitCluster, ARGS_ONE, |
219 | a8083063 | Iustin Pop | [DEBUG_OPT, |
220 | a8083063 | Iustin Pop | make_option("-s", "--secondary-ip", dest="secondary_ip", |
221 | a8083063 | Iustin Pop | help="Specify the secondary ip for this node;" |
222 | a8083063 | Iustin Pop | " if given, the entire cluster must have secondary" |
223 | a8083063 | Iustin Pop | " addresses", |
224 | a8083063 | Iustin Pop | metavar="ADDRESS", default=None), |
225 | a8083063 | Iustin Pop | make_option("-t", "--hypervisor-type", dest="hypervisor_type", |
226 | a8083063 | Iustin Pop | help="Specify the hypervisor type (xen-3.0, fake)", |
227 | a8083063 | Iustin Pop | metavar="TYPE", choices=["xen-3.0", "fake"], |
228 | a8083063 | Iustin Pop | default="xen-3.0",), |
229 | a8083063 | Iustin Pop | make_option("-m", "--mac-prefix", dest="mac_prefix", |
230 | a8083063 | Iustin Pop | help="Specify the mac prefix for the instance IP" |
231 | a8083063 | Iustin Pop | " addresses, in the format XX:XX:XX", |
232 | a8083063 | Iustin Pop | metavar="PREFIX", |
233 | a8083063 | Iustin Pop | default="aa:00:00",), |
234 | a8083063 | Iustin Pop | make_option("-g", "--vg-name", dest="vg_name", |
235 | a8083063 | Iustin Pop | help="Specify the volume group name " |
236 | a8083063 | Iustin Pop | " (cluster-wide) for disk allocation [xenvg]", |
237 | a8083063 | Iustin Pop | metavar="VG", |
238 | a8083063 | Iustin Pop | default="xenvg",), |
239 | a8083063 | Iustin Pop | make_option("-b", "--bridge", dest="def_bridge", |
240 | a8083063 | Iustin Pop | help="Specify the default bridge name (cluster-wide)" |
241 | cf62a272 | Michael Hanselmann | " to connect the instances to [%s]" % |
242 | cf62a272 | Michael Hanselmann | constants.DEFAULT_BRIDGE, |
243 | a8083063 | Iustin Pop | metavar="BRIDGE", |
244 | cf62a272 | Michael Hanselmann | default=constants.DEFAULT_BRIDGE,), |
245 | 880478f8 | Iustin Pop | make_option("--master-netdev", dest="master_netdev", |
246 | 880478f8 | Iustin Pop | help="Specify the node interface (cluster-wide)" |
247 | cf62a272 | Michael Hanselmann | " on which the master IP address will be added " |
248 | cf62a272 | Michael Hanselmann | " [%s]" % constants.DEFAULT_BRIDGE, |
249 | 880478f8 | Iustin Pop | metavar="NETDEV", |
250 | cf62a272 | Michael Hanselmann | default=constants.DEFAULT_BRIDGE,), |
251 | a8083063 | Iustin Pop | ], |
252 | a8083063 | Iustin Pop | "[opts...] <cluster_name>", |
253 | a8083063 | Iustin Pop | "Initialises a new cluster configuration"), |
254 | a8083063 | Iustin Pop | 'destroy': (DestroyCluster, ARGS_NONE, |
255 | a8083063 | Iustin Pop | [DEBUG_OPT, |
256 | a8083063 | Iustin Pop | make_option("--yes-do-it", dest="yes_do_it", |
257 | a8083063 | Iustin Pop | help="Destroy cluster", |
258 | a8083063 | Iustin Pop | action="store_true"), |
259 | a8083063 | Iustin Pop | ], |
260 | a8083063 | Iustin Pop | "", "Destroy cluster"), |
261 | 07bd8a51 | Iustin Pop | 'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT], |
262 | 07bd8a51 | Iustin Pop | "<new_name>", |
263 | 07bd8a51 | Iustin Pop | "Renames the cluster"), |
264 | a8083063 | Iustin Pop | 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT], |
265 | a8083063 | Iustin Pop | "", "Does a check on the cluster configuration"), |
266 | a8083063 | Iustin Pop | 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT], |
267 | a8083063 | Iustin Pop | "", "Makes the current node the master"), |
268 | a8083063 | Iustin Pop | 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT], |
269 | a8083063 | Iustin Pop | "", "Shows the cluster version"), |
270 | a8083063 | Iustin Pop | 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT], |
271 | a8083063 | Iustin Pop | "", "Shows the cluster master"), |
272 | a8083063 | Iustin Pop | 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option], |
273 | a8083063 | Iustin Pop | "[-n node...] <filename>", |
274 | a8083063 | Iustin Pop | "Copies a file to all (or only some) nodes"), |
275 | a8083063 | Iustin Pop | 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option], |
276 | a8083063 | Iustin Pop | "[-n node...] <command>", |
277 | a8083063 | Iustin Pop | "Runs a command on all (or only some) nodes"), |
278 | a8083063 | Iustin Pop | 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT], |
279 | a8083063 | Iustin Pop | "", "Show cluster configuration"), |
280 | 846baef9 | Iustin Pop | 'list-tags': (ListTags, ARGS_NONE, |
281 | 846baef9 | Iustin Pop | [DEBUG_OPT], "", "List the tags of the cluster"), |
282 | 810c50b7 | Iustin Pop | 'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT], |
283 | 846baef9 | Iustin Pop | "tag...", "Add tags to the cluster"), |
284 | 810c50b7 | Iustin Pop | 'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT, TAG_SRC_OPT], |
285 | 846baef9 | Iustin Pop | "tag...", "Remove tags from the cluster"), |
286 | 73415719 | Iustin Pop | 'search-tags': (SearchTags, ARGS_ONE, |
287 | 73415719 | Iustin Pop | [DEBUG_OPT], "", "Searches the tags on all objects on" |
288 | 73415719 | Iustin Pop | " the cluster for a given pattern (regex)"), |
289 | a8083063 | Iustin Pop | } |
290 | a8083063 | Iustin Pop | |
291 | a8083063 | Iustin Pop | if __name__ == '__main__': |
292 | 846baef9 | Iustin Pop | sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER})) |