Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-cluster @ cf62a272

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 a8083063 Iustin Pop
29 a8083063 Iustin Pop
30 a8083063 Iustin Pop
def InitCluster(opts, args):
31 a8083063 Iustin Pop
  """Initialize the cluster.
32 a8083063 Iustin Pop
33 a8083063 Iustin Pop
  Args:
34 a8083063 Iustin Pop
    opts - class with options as members
35 a8083063 Iustin Pop
    args - list of arguments, expected to be [clustername]
36 a8083063 Iustin Pop
37 a8083063 Iustin Pop
  """
38 a8083063 Iustin Pop
  op = opcodes.OpInitCluster(cluster_name=args[0],
39 a8083063 Iustin Pop
                             secondary_ip=opts.secondary_ip,
40 a8083063 Iustin Pop
                             hypervisor_type=opts.hypervisor_type,
41 a8083063 Iustin Pop
                             vg_name=opts.vg_name,
42 a8083063 Iustin Pop
                             mac_prefix=opts.mac_prefix,
43 880478f8 Iustin Pop
                             def_bridge=opts.def_bridge,
44 880478f8 Iustin Pop
                             master_netdev=opts.master_netdev)
45 a8083063 Iustin Pop
  SubmitOpCode(op)
46 a8083063 Iustin Pop
  return 0
47 a8083063 Iustin Pop
48 a8083063 Iustin Pop
49 a8083063 Iustin Pop
def DestroyCluster(opts, args):
50 a8083063 Iustin Pop
  """Destroy the cluster.
51 a8083063 Iustin Pop
52 a8083063 Iustin Pop
  Args:
53 a8083063 Iustin Pop
    opts - class with options as members
54 098c0958 Michael Hanselmann
55 a8083063 Iustin Pop
  """
56 a8083063 Iustin Pop
  if not opts.yes_do_it:
57 a8083063 Iustin Pop
    print ("Destroying a cluster is irreversibly. If you really want destroy"
58 79f7be7b Iustin Pop
           " this cluster, supply the --yes-do-it option.")
59 a8083063 Iustin Pop
    return 1
60 a8083063 Iustin Pop
61 a8083063 Iustin Pop
  op = opcodes.OpDestroyCluster()
62 a8083063 Iustin Pop
  SubmitOpCode(op)
63 a8083063 Iustin Pop
  return 0
64 a8083063 Iustin Pop
65 a8083063 Iustin Pop
66 a8083063 Iustin Pop
def ShowClusterVersion(opts, args):
67 a8083063 Iustin Pop
  """Write version of ganeti software to the standard output.
68 a8083063 Iustin Pop
69 a8083063 Iustin Pop
  Args:
70 a8083063 Iustin Pop
    opts - class with options as members
71 a8083063 Iustin Pop
72 a8083063 Iustin Pop
  """
73 a8083063 Iustin Pop
  op = opcodes.OpQueryClusterInfo()
74 a8083063 Iustin Pop
  result = SubmitOpCode(op)
75 a8083063 Iustin Pop
  print ("Software version: %s" % result["software_version"])
76 a8083063 Iustin Pop
  print ("Internode protocol: %s" % result["protocol_version"])
77 a8083063 Iustin Pop
  print ("Configuration format: %s" % result["config_version"])
78 a8083063 Iustin Pop
  print ("OS api version: %s" % result["os_api_version"])
79 a8083063 Iustin Pop
  print ("Export interface: %s" % result["export_version"])
80 a8083063 Iustin Pop
  return 0
81 a8083063 Iustin Pop
82 a8083063 Iustin Pop
83 a8083063 Iustin Pop
def ShowClusterMaster(opts, args):
84 a8083063 Iustin Pop
  """Write name of master node to the standard output.
85 a8083063 Iustin Pop
86 a8083063 Iustin Pop
  Args:
87 a8083063 Iustin Pop
    opts - class with options as members
88 a8083063 Iustin Pop
89 a8083063 Iustin Pop
  """
90 a8083063 Iustin Pop
  op = opcodes.OpQueryClusterInfo()
91 a8083063 Iustin Pop
  result = SubmitOpCode(op)
92 a8083063 Iustin Pop
  print (result["master"])
93 a8083063 Iustin Pop
  return 0
94 a8083063 Iustin Pop
95 a8083063 Iustin Pop
96 a8083063 Iustin Pop
def ShowClusterConfig(opts, args):
97 a8083063 Iustin Pop
  """Shows cluster information.
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
  """
100 a8083063 Iustin Pop
  op = opcodes.OpQueryClusterInfo()
101 a8083063 Iustin Pop
  result = SubmitOpCode(op)
102 a8083063 Iustin Pop
103 a8083063 Iustin Pop
  print ("Cluster name: %s" % result["name"])
104 a8083063 Iustin Pop
105 a8083063 Iustin Pop
  print ("Master node: %s" % result["master"])
106 a8083063 Iustin Pop
107 59322403 Iustin Pop
  print ("Architecture (this node): %s (%s)" %
108 59322403 Iustin Pop
         (result["architecture"][0], result["architecture"][1]))
109 a8083063 Iustin Pop
110 a8083063 Iustin Pop
  return 0
111 a8083063 Iustin Pop
112 a8083063 Iustin Pop
113 a8083063 Iustin Pop
def ClusterCopyFile(opts, args):
114 a8083063 Iustin Pop
  """Copy a file from master to some nodes.
115 a8083063 Iustin Pop
116 a8083063 Iustin Pop
  Args:
117 a8083063 Iustin Pop
    opts - class with options as members
118 a8083063 Iustin Pop
    args - list containing a single element, the file name
119 a8083063 Iustin Pop
  Opts used:
120 a8083063 Iustin Pop
    nodes - list containing the name of target nodes; if empty, all nodes
121 a8083063 Iustin Pop
122 a8083063 Iustin Pop
  """
123 a8083063 Iustin Pop
  op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
124 a8083063 Iustin Pop
  SubmitOpCode(op)
125 a8083063 Iustin Pop
  return 0
126 a8083063 Iustin Pop
127 a8083063 Iustin Pop
128 a8083063 Iustin Pop
def RunClusterCommand(opts, args):
129 a8083063 Iustin Pop
  """Run a command on some nodes.
130 a8083063 Iustin Pop
131 a8083063 Iustin Pop
  Args:
132 a8083063 Iustin Pop
    opts - class with options as members
133 a8083063 Iustin Pop
    args - the command list as a list
134 a8083063 Iustin Pop
  Opts used:
135 a8083063 Iustin Pop
    nodes: list containing the name of target nodes; if empty, all nodes
136 a8083063 Iustin Pop
137 a8083063 Iustin Pop
  """
138 a8083063 Iustin Pop
  command = " ".join(args)
139 a8083063 Iustin Pop
  nodes = opts.nodes
140 a8083063 Iustin Pop
  op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
141 a8083063 Iustin Pop
  result = SubmitOpCode(op)
142 a8083063 Iustin Pop
  for node, sshcommand, output, exit_code in result:
143 a8083063 Iustin Pop
    print ("------------------------------------------------")
144 a8083063 Iustin Pop
    print ("node: %s" % node)
145 a8083063 Iustin Pop
    print ("command: %s" % sshcommand)
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 a8083063 Iustin Pop
  retcode = GenericMain(commands)
245 a8083063 Iustin Pop
  sys.exit(retcode)