Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-cluster @ cf62a272

History | View | Annotate | Download (8 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
  """
56
  if not opts.yes_do_it:
57
    print ("Destroying a cluster is irreversibly. If you really want destroy"
58
           " this cluster, supply the --yes-do-it option.")
59
    return 1
60

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

    
65

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

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

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

    
82

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

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

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

    
95

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

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

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

    
105
  print ("Master node: %s" % result["master"])
106

    
107
  print ("Architecture (this node): %s (%s)" %
108
         (result["architecture"][0], result["architecture"][1]))
109

    
110
  return 0
111

    
112

    
113
def ClusterCopyFile(opts, args):
114
  """Copy a file from master to some nodes.
115

    
116
  Args:
117
    opts - class with options as members
118
    args - list containing a single element, the file name
119
  Opts used:
120
    nodes - list containing the name of target nodes; if empty, all nodes
121

    
122
  """
123
  op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
124
  SubmitOpCode(op)
125
  return 0
126

    
127

    
128
def RunClusterCommand(opts, args):
129
  """Run a command on some nodes.
130

    
131
  Args:
132
    opts - class with options as members
133
    args - the command list as a list
134
  Opts used:
135
    nodes: list containing the name of target nodes; if empty, all nodes
136

    
137
  """
138
  command = " ".join(args)
139
  nodes = opts.nodes
140
  op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
141
  result = SubmitOpCode(op)
142
  for node, sshcommand, output, exit_code in result:
143
    print ("------------------------------------------------")
144
    print ("node: %s" % node)
145
    print ("command: %s" % sshcommand)
146
    print ("%s" % output)
147
    print ("return code = %s" % exit_code)
148

    
149

    
150
def VerifyCluster(opts, args):
151
  """Verify integrity of cluster, performing various test on nodes.
152

    
153
  Args:
154
    opts - class with options as members
155

    
156
  """
157
  op = opcodes.OpVerifyCluster()
158
  result = SubmitOpCode(op)
159
  return result
160

    
161

    
162
def MasterFailover(opts, args):
163
  """Failover the master node.
164

    
165
  This command, when run on a non-master node, will cause the current
166
  master to cease being master, and the non-master to become new
167
  master.
168

    
169
  """
170
  op = opcodes.OpMasterFailover()
171
  SubmitOpCode(op)
172

    
173

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

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

    
243
if __name__ == '__main__':
244
  retcode = GenericMain(commands)
245
  sys.exit(retcode)