Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-cluster @ 880478f8

History | View | Annotate | Download (8.1 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
import sys
23
from optparse import make_option
24
import pprint
25

    
26
from ganeti.cli import *
27
from ganeti import opcodes
28

    
29

    
30
def InitCluster(opts, args):
31
  """Initialize the cluster.
32

    
33
  Args:
34
    opts - class with options as members
35
    args - list of arguments, expected to be [clustername]
36

    
37
  """
38
  op = opcodes.OpInitCluster(cluster_name=args[0],
39
                             secondary_ip=opts.secondary_ip,
40
                             hypervisor_type=opts.hypervisor_type,
41
                             vg_name=opts.vg_name,
42
                             mac_prefix=opts.mac_prefix,
43
                             def_bridge=opts.def_bridge,
44
                             master_netdev=opts.master_netdev)
45
  SubmitOpCode(op)
46
  return 0
47

    
48

    
49
def DestroyCluster(opts, args):
50
  """Destroy the cluster.
51

    
52
  Args:
53
    opts - class with options as members
54
  """
55
  if not opts.yes_do_it:
56
    print ("Destroying a cluster is irreversibly. If you really want destroy"
57
           " this cluster, supply the --yes-do-it option.")
58
    return 1
59

    
60
  op = opcodes.OpDestroyCluster()
61
  SubmitOpCode(op)
62
  return 0
63

    
64

    
65
def ShowClusterVersion(opts, args):
66
  """Write version of ganeti software to the standard output.
67

    
68
  Args:
69
    opts - class with options as members
70

    
71
  """
72
  op = opcodes.OpQueryClusterInfo()
73
  result = SubmitOpCode(op)
74
  print ("Software version: %s" % result["software_version"])
75
  print ("Internode protocol: %s" % result["protocol_version"])
76
  print ("Configuration format: %s" % result["config_version"])
77
  print ("OS api version: %s" % result["os_api_version"])
78
  print ("Export interface: %s" % result["export_version"])
79
  return 0
80

    
81

    
82
def ShowClusterMaster(opts, args):
83
  """Write name of master node to the standard output.
84

    
85
  Args:
86
    opts - class with options as members
87

    
88
  """
89
  op = opcodes.OpQueryClusterInfo()
90
  result = SubmitOpCode(op)
91
  print (result["master"])
92
  return 0
93

    
94

    
95
def ShowClusterConfig(opts, args):
96
  """Shows cluster information.
97

    
98
  """
99
  op = opcodes.OpQueryClusterInfo()
100
  result = SubmitOpCode(op)
101

    
102
  print ("Cluster name: %s" % result["name"])
103

    
104
  print ("Architecture: %s (%s)" %
105
            (result["architecture"][0], result["architecture"][1]))
106

    
107
  print ("Master node: %s" % result["master"])
108

    
109
  print ("Instances:")
110
  for name, node in result["instances"]:
111
    print ("  - %s (on %s)" % (name, node))
112
  print ("Nodes:")
113
  for name in result["nodes"]:
114
    print ("  - %s" % name)
115

    
116
  return 0
117

    
118

    
119
def ClusterCopyFile(opts, args):
120
  """Copy a file from master to some nodes.
121

    
122
  Args:
123
    opts - class with options as members
124
    args - list containing a single element, the file name
125
  Opts used:
126
    nodes - list containing the name of target nodes; if empty, all nodes
127

    
128
  """
129
  op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
130
  SubmitOpCode(op)
131
  return 0
132

    
133

    
134
def RunClusterCommand(opts, args):
135
  """Run a command on some nodes.
136

    
137
  Args:
138
    opts - class with options as members
139
    args - the command list as a list
140
  Opts used:
141
    nodes: list containing the name of target nodes; if empty, all nodes
142

    
143
  """
144
  command = " ".join(args)
145
  nodes = opts.nodes
146
  op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
147
  result = SubmitOpCode(op)
148
  for node, sshcommand, output, exit_code in result:
149
    print ("------------------------------------------------")
150
    print ("node: %s" % node)
151
    print ("command: %s" % sshcommand)
152
    print ("%s" % output)
153
    print ("return code = %s" % exit_code)
154

    
155

    
156
def VerifyCluster(opts, args):
157
  """Verify integrity of cluster, performing various test on nodes.
158

    
159
  Args:
160
    opts - class with options as members
161

    
162
  """
163
  op = opcodes.OpVerifyCluster()
164
  result = SubmitOpCode(op)
165
  return result
166

    
167

    
168
def MasterFailover(opts, args):
169
  """Failover the master node.
170

    
171
  This command, when run on a non-master node, will cause the current
172
  master to cease being master, and the non-master to become new
173
  master.
174

    
175
  """
176
  op = opcodes.OpMasterFailover()
177
  SubmitOpCode(op)
178

    
179

    
180
# this is an option common to more than one command, so we declare
181
# it here and reuse it
182
node_option = make_option("-n", "--node", action="append", dest="nodes",
183
                          help="Node to copy to (if not given, all nodes)"
184
                          ", can be given multiple times", metavar="<node>",
185
                          default=[])
186

    
187
commands = {
188
  'init': (InitCluster, ARGS_ONE,
189
           [DEBUG_OPT,
190
            make_option("-s", "--secondary-ip", dest="secondary_ip",
191
                        help="Specify the secondary ip for this node;"
192
                        " if given, the entire cluster must have secondary"
193
                        " addresses",
194
                        metavar="ADDRESS", default=None),
195
            make_option("-t", "--hypervisor-type", dest="hypervisor_type",
196
                        help="Specify the hypervisor type (xen-3.0, fake)",
197
                        metavar="TYPE", choices=["xen-3.0", "fake"],
198
                        default="xen-3.0",),
199
            make_option("-m", "--mac-prefix", dest="mac_prefix",
200
                        help="Specify the mac prefix for the instance IP"
201
                        " addresses, in the format XX:XX:XX",
202
                        metavar="PREFIX",
203
                        default="aa:00:00",),
204
            make_option("-g", "--vg-name", dest="vg_name",
205
                        help="Specify the volume group name "
206
                        " (cluster-wide) for disk allocation [xenvg]",
207
                        metavar="VG",
208
                        default="xenvg",),
209
            make_option("-b", "--bridge", dest="def_bridge",
210
                        help="Specify the default bridge name (cluster-wide)"
211
                        " to connect the instances to [xen-br0]",
212
                        metavar="BRIDGE",
213
                        default="xen-br0",),
214
            make_option("--master-netdev", dest="master_netdev",
215
                        help="Specify the node interface (cluster-wide)"
216
                        " on which the master IP address will be added "
217
                        " [xen-br0]",
218
                        metavar="NETDEV",
219
                        default="xen-br0",),
220
            ],
221
           "[opts...] <cluster_name>",
222
           "Initialises a new cluster configuration"),
223
  'destroy': (DestroyCluster, ARGS_NONE,
224
              [DEBUG_OPT,
225
               make_option("--yes-do-it", dest="yes_do_it",
226
                           help="Destroy cluster",
227
                           action="store_true"),
228
              ],
229
              "", "Destroy cluster"),
230
  'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
231
             "", "Does a check on the cluster configuration"),
232
  'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
233
                     "", "Makes the current node the master"),
234
  'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
235
              "", "Shows the cluster version"),
236
  'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
237
                "", "Shows the cluster master"),
238
  'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
239
               "[-n node...] <filename>",
240
               "Copies a file to all (or only some) nodes"),
241
  'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
242
              "[-n node...] <command>",
243
              "Runs a command on all (or only some) nodes"),
244
  'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
245
                 "", "Show cluster configuration"),
246
  }
247

    
248
if __name__ == '__main__':
249
  retcode = GenericMain(commands)
250
  sys.exit(retcode)