root / scripts / gnt-cluster @ 2ec08468
History | View | Annotate | Download (8 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 | a8083063 | Iustin Pop | def ShowClusterVersion(opts, args): |
68 | a8083063 | Iustin Pop | """Write version of ganeti software to the standard output. |
69 | a8083063 | Iustin Pop | |
70 | a8083063 | Iustin Pop | Args: |
71 | a8083063 | Iustin Pop | opts - class with options as members |
72 | a8083063 | Iustin Pop | |
73 | a8083063 | Iustin Pop | """ |
74 | a8083063 | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
75 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
76 | a8083063 | Iustin Pop | print ("Software version: %s" % result["software_version"]) |
77 | a8083063 | Iustin Pop | print ("Internode protocol: %s" % result["protocol_version"]) |
78 | a8083063 | Iustin Pop | print ("Configuration format: %s" % result["config_version"]) |
79 | a8083063 | Iustin Pop | print ("OS api version: %s" % result["os_api_version"]) |
80 | a8083063 | Iustin Pop | print ("Export interface: %s" % result["export_version"]) |
81 | a8083063 | Iustin Pop | return 0 |
82 | a8083063 | Iustin Pop | |
83 | a8083063 | Iustin Pop | |
84 | a8083063 | Iustin Pop | def ShowClusterMaster(opts, args): |
85 | a8083063 | Iustin Pop | """Write name of master node to the standard output. |
86 | a8083063 | Iustin Pop | |
87 | a8083063 | Iustin Pop | Args: |
88 | a8083063 | Iustin Pop | opts - class with options as members |
89 | a8083063 | Iustin Pop | |
90 | a8083063 | Iustin Pop | """ |
91 | a8083063 | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
92 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
93 | a8083063 | Iustin Pop | print (result["master"]) |
94 | a8083063 | Iustin Pop | return 0 |
95 | a8083063 | Iustin Pop | |
96 | a8083063 | Iustin Pop | |
97 | a8083063 | Iustin Pop | def ShowClusterConfig(opts, args): |
98 | a8083063 | Iustin Pop | """Shows cluster information. |
99 | a8083063 | Iustin Pop | |
100 | a8083063 | Iustin Pop | """ |
101 | a8083063 | Iustin Pop | op = opcodes.OpQueryClusterInfo() |
102 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
103 | a8083063 | Iustin Pop | |
104 | a8083063 | Iustin Pop | print ("Cluster name: %s" % result["name"]) |
105 | a8083063 | Iustin Pop | |
106 | a8083063 | Iustin Pop | print ("Master node: %s" % result["master"]) |
107 | a8083063 | Iustin Pop | |
108 | 59322403 | Iustin Pop | print ("Architecture (this node): %s (%s)" % |
109 | 59322403 | Iustin Pop | (result["architecture"][0], result["architecture"][1])) |
110 | a8083063 | Iustin Pop | |
111 | a8083063 | Iustin Pop | return 0 |
112 | a8083063 | Iustin Pop | |
113 | a8083063 | Iustin Pop | |
114 | a8083063 | Iustin Pop | def ClusterCopyFile(opts, args): |
115 | a8083063 | Iustin Pop | """Copy a file from master to some nodes. |
116 | a8083063 | Iustin Pop | |
117 | a8083063 | Iustin Pop | Args: |
118 | a8083063 | Iustin Pop | opts - class with options as members |
119 | a8083063 | Iustin Pop | args - list containing a single element, the file name |
120 | a8083063 | Iustin Pop | Opts used: |
121 | a8083063 | Iustin Pop | nodes - list containing the name of target nodes; if empty, all nodes |
122 | a8083063 | Iustin Pop | |
123 | a8083063 | Iustin Pop | """ |
124 | a8083063 | Iustin Pop | op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes) |
125 | a8083063 | Iustin Pop | SubmitOpCode(op) |
126 | a8083063 | Iustin Pop | return 0 |
127 | a8083063 | Iustin Pop | |
128 | a8083063 | Iustin Pop | |
129 | a8083063 | Iustin Pop | def RunClusterCommand(opts, args): |
130 | a8083063 | Iustin Pop | """Run a command on some nodes. |
131 | a8083063 | Iustin Pop | |
132 | a8083063 | Iustin Pop | Args: |
133 | a8083063 | Iustin Pop | opts - class with options as members |
134 | a8083063 | Iustin Pop | args - the command list as a list |
135 | a8083063 | Iustin Pop | Opts used: |
136 | a8083063 | Iustin Pop | nodes: list containing the name of target nodes; if empty, all nodes |
137 | a8083063 | Iustin Pop | |
138 | a8083063 | Iustin Pop | """ |
139 | a8083063 | Iustin Pop | command = " ".join(args) |
140 | a8083063 | Iustin Pop | nodes = opts.nodes |
141 | a8083063 | Iustin Pop | op = opcodes.OpRunClusterCommand(command=command, nodes=nodes) |
142 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
143 | 02715459 | Iustin Pop | for node, output, exit_code in result: |
144 | a8083063 | Iustin Pop | print ("------------------------------------------------") |
145 | a8083063 | Iustin Pop | print ("node: %s" % node) |
146 | a8083063 | Iustin Pop | print ("%s" % output) |
147 | a8083063 | Iustin Pop | print ("return code = %s" % exit_code) |
148 | a8083063 | Iustin Pop | |
149 | a8083063 | Iustin Pop | |
150 | a8083063 | Iustin Pop | def VerifyCluster(opts, args): |
151 | a8083063 | Iustin Pop | """Verify integrity of cluster, performing various test on nodes. |
152 | a8083063 | Iustin Pop | |
153 | a8083063 | Iustin Pop | Args: |
154 | a8083063 | Iustin Pop | opts - class with options as members |
155 | a8083063 | Iustin Pop | |
156 | a8083063 | Iustin Pop | """ |
157 | a8083063 | Iustin Pop | op = opcodes.OpVerifyCluster() |
158 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
159 | a8083063 | Iustin Pop | return result |
160 | a8083063 | Iustin Pop | |
161 | a8083063 | Iustin Pop | |
162 | a8083063 | Iustin Pop | def MasterFailover(opts, args): |
163 | a8083063 | Iustin Pop | """Failover the master node. |
164 | a8083063 | Iustin Pop | |
165 | a8083063 | Iustin Pop | This command, when run on a non-master node, will cause the current |
166 | a8083063 | Iustin Pop | master to cease being master, and the non-master to become new |
167 | a8083063 | Iustin Pop | master. |
168 | a8083063 | Iustin Pop | |
169 | a8083063 | Iustin Pop | """ |
170 | a8083063 | Iustin Pop | op = opcodes.OpMasterFailover() |
171 | a8083063 | Iustin Pop | SubmitOpCode(op) |
172 | a8083063 | Iustin Pop | |
173 | a8083063 | Iustin Pop | |
174 | a8083063 | Iustin Pop | # this is an option common to more than one command, so we declare |
175 | a8083063 | Iustin Pop | # it here and reuse it |
176 | a8083063 | Iustin Pop | node_option = make_option("-n", "--node", action="append", dest="nodes", |
177 | a8083063 | Iustin Pop | help="Node to copy to (if not given, all nodes)" |
178 | a8083063 | Iustin Pop | ", can be given multiple times", metavar="<node>", |
179 | a8083063 | Iustin Pop | default=[]) |
180 | a8083063 | Iustin Pop | |
181 | a8083063 | Iustin Pop | commands = { |
182 | a8083063 | Iustin Pop | 'init': (InitCluster, ARGS_ONE, |
183 | a8083063 | Iustin Pop | [DEBUG_OPT, |
184 | a8083063 | Iustin Pop | make_option("-s", "--secondary-ip", dest="secondary_ip", |
185 | a8083063 | Iustin Pop | help="Specify the secondary ip for this node;" |
186 | a8083063 | Iustin Pop | " if given, the entire cluster must have secondary" |
187 | a8083063 | Iustin Pop | " addresses", |
188 | a8083063 | Iustin Pop | metavar="ADDRESS", default=None), |
189 | a8083063 | Iustin Pop | make_option("-t", "--hypervisor-type", dest="hypervisor_type", |
190 | a8083063 | Iustin Pop | help="Specify the hypervisor type (xen-3.0, fake)", |
191 | a8083063 | Iustin Pop | metavar="TYPE", choices=["xen-3.0", "fake"], |
192 | a8083063 | Iustin Pop | default="xen-3.0",), |
193 | a8083063 | Iustin Pop | make_option("-m", "--mac-prefix", dest="mac_prefix", |
194 | a8083063 | Iustin Pop | help="Specify the mac prefix for the instance IP" |
195 | a8083063 | Iustin Pop | " addresses, in the format XX:XX:XX", |
196 | a8083063 | Iustin Pop | metavar="PREFIX", |
197 | a8083063 | Iustin Pop | default="aa:00:00",), |
198 | a8083063 | Iustin Pop | make_option("-g", "--vg-name", dest="vg_name", |
199 | a8083063 | Iustin Pop | help="Specify the volume group name " |
200 | a8083063 | Iustin Pop | " (cluster-wide) for disk allocation [xenvg]", |
201 | a8083063 | Iustin Pop | metavar="VG", |
202 | a8083063 | Iustin Pop | default="xenvg",), |
203 | a8083063 | Iustin Pop | make_option("-b", "--bridge", dest="def_bridge", |
204 | a8083063 | Iustin Pop | help="Specify the default bridge name (cluster-wide)" |
205 | cf62a272 | Michael Hanselmann | " to connect the instances to [%s]" % |
206 | cf62a272 | Michael Hanselmann | constants.DEFAULT_BRIDGE, |
207 | a8083063 | Iustin Pop | metavar="BRIDGE", |
208 | cf62a272 | Michael Hanselmann | default=constants.DEFAULT_BRIDGE,), |
209 | 880478f8 | Iustin Pop | make_option("--master-netdev", dest="master_netdev", |
210 | 880478f8 | Iustin Pop | help="Specify the node interface (cluster-wide)" |
211 | cf62a272 | Michael Hanselmann | " on which the master IP address will be added " |
212 | cf62a272 | Michael Hanselmann | " [%s]" % constants.DEFAULT_BRIDGE, |
213 | 880478f8 | Iustin Pop | metavar="NETDEV", |
214 | cf62a272 | Michael Hanselmann | default=constants.DEFAULT_BRIDGE,), |
215 | a8083063 | Iustin Pop | ], |
216 | a8083063 | Iustin Pop | "[opts...] <cluster_name>", |
217 | a8083063 | Iustin Pop | "Initialises a new cluster configuration"), |
218 | a8083063 | Iustin Pop | 'destroy': (DestroyCluster, ARGS_NONE, |
219 | a8083063 | Iustin Pop | [DEBUG_OPT, |
220 | a8083063 | Iustin Pop | make_option("--yes-do-it", dest="yes_do_it", |
221 | a8083063 | Iustin Pop | help="Destroy cluster", |
222 | a8083063 | Iustin Pop | action="store_true"), |
223 | a8083063 | Iustin Pop | ], |
224 | a8083063 | Iustin Pop | "", "Destroy cluster"), |
225 | a8083063 | Iustin Pop | 'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT], |
226 | a8083063 | Iustin Pop | "", "Does a check on the cluster configuration"), |
227 | a8083063 | Iustin Pop | 'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT], |
228 | a8083063 | Iustin Pop | "", "Makes the current node the master"), |
229 | a8083063 | Iustin Pop | 'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT], |
230 | a8083063 | Iustin Pop | "", "Shows the cluster version"), |
231 | a8083063 | Iustin Pop | 'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT], |
232 | a8083063 | Iustin Pop | "", "Shows the cluster master"), |
233 | a8083063 | Iustin Pop | 'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option], |
234 | a8083063 | Iustin Pop | "[-n node...] <filename>", |
235 | a8083063 | Iustin Pop | "Copies a file to all (or only some) nodes"), |
236 | a8083063 | Iustin Pop | 'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option], |
237 | a8083063 | Iustin Pop | "[-n node...] <command>", |
238 | a8083063 | Iustin Pop | "Runs a command on all (or only some) nodes"), |
239 | a8083063 | Iustin Pop | 'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT], |
240 | a8083063 | Iustin Pop | "", "Show cluster configuration"), |
241 | a8083063 | Iustin Pop | } |
242 | a8083063 | Iustin Pop | |
243 | a8083063 | Iustin Pop | if __name__ == '__main__': |
244 | 3ecf6786 | Iustin Pop | sys.exit(GenericMain(commands)) |