Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-cluster @ 846baef9

History | View | Annotate | Download (9.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
from ganeti import constants
29

    
30

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

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

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

    
49

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

    
53
  Args:
54
    opts - class with options as members
55

    
56
  """
57
  if not opts.yes_do_it:
58
    print ("Destroying a cluster is irreversibly. If you really want destroy"
59
           " this cluster, supply the --yes-do-it option.")
60
    return 1
61

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

    
66

    
67
def RenameCluster(opts, args):
68
  """Rename the cluster.
69

    
70
  Args:
71
    opts - class with options as members, we use force only
72
    args - list of arguments, expected to be [new_name]
73

    
74
  """
75
  name = args[0]
76
  if not opts.force:
77
    usertext = ("This will rename the cluster to '%s'. If you are connected"
78
                " over the network to the cluster name, the operation is very"
79
                " dangerous as the IP address will be removed from the node"
80
                " and the change may not go through. Continue?") % name
81
    if not AskUser(usertext):
82
      return 1
83

    
84
  op = opcodes.OpRenameCluster(name=name)
85
  SubmitOpCode(op)
86
  return 0
87

    
88

    
89
def ShowClusterVersion(opts, args):
90
  """Write version of ganeti software to the standard output.
91

    
92
  Args:
93
    opts - class with options as members
94

    
95
  """
96
  op = opcodes.OpQueryClusterInfo()
97
  result = SubmitOpCode(op)
98
  print ("Software version: %s" % result["software_version"])
99
  print ("Internode protocol: %s" % result["protocol_version"])
100
  print ("Configuration format: %s" % result["config_version"])
101
  print ("OS api version: %s" % result["os_api_version"])
102
  print ("Export interface: %s" % result["export_version"])
103
  return 0
104

    
105

    
106
def ShowClusterMaster(opts, args):
107
  """Write name of master node to the standard output.
108

    
109
  Args:
110
    opts - class with options as members
111

    
112
  """
113
  op = opcodes.OpQueryClusterInfo()
114
  result = SubmitOpCode(op)
115
  print (result["master"])
116
  return 0
117

    
118

    
119
def ShowClusterConfig(opts, args):
120
  """Shows cluster information.
121

    
122
  """
123
  op = opcodes.OpQueryClusterInfo()
124
  result = SubmitOpCode(op)
125

    
126
  print ("Cluster name: %s" % result["name"])
127

    
128
  print ("Master node: %s" % result["master"])
129

    
130
  print ("Architecture (this node): %s (%s)" %
131
         (result["architecture"][0], result["architecture"][1]))
132

    
133
  return 0
134

    
135

    
136
def ClusterCopyFile(opts, args):
137
  """Copy a file from master to some nodes.
138

    
139
  Args:
140
    opts - class with options as members
141
    args - list containing a single element, the file name
142
  Opts used:
143
    nodes - list containing the name of target nodes; if empty, all nodes
144

    
145
  """
146
  op = opcodes.OpClusterCopyFile(filename=args[0], nodes=opts.nodes)
147
  SubmitOpCode(op)
148
  return 0
149

    
150

    
151
def RunClusterCommand(opts, args):
152
  """Run a command on some nodes.
153

    
154
  Args:
155
    opts - class with options as members
156
    args - the command list as a list
157
  Opts used:
158
    nodes: list containing the name of target nodes; if empty, all nodes
159

    
160
  """
161
  command = " ".join(args)
162
  nodes = opts.nodes
163
  op = opcodes.OpRunClusterCommand(command=command, nodes=nodes)
164
  result = SubmitOpCode(op)
165
  for node, output, exit_code in result:
166
    print ("------------------------------------------------")
167
    print ("node: %s" % node)
168
    print ("%s" % output)
169
    print ("return code = %s" % exit_code)
170

    
171

    
172
def VerifyCluster(opts, args):
173
  """Verify integrity of cluster, performing various test on nodes.
174

    
175
  Args:
176
    opts - class with options as members
177

    
178
  """
179
  op = opcodes.OpVerifyCluster()
180
  result = SubmitOpCode(op)
181
  return result
182

    
183

    
184
def MasterFailover(opts, args):
185
  """Failover the master node.
186

    
187
  This command, when run on a non-master node, will cause the current
188
  master to cease being master, and the non-master to become new
189
  master.
190

    
191
  """
192
  op = opcodes.OpMasterFailover()
193
  SubmitOpCode(op)
194

    
195

    
196
# this is an option common to more than one command, so we declare
197
# it here and reuse it
198
node_option = make_option("-n", "--node", action="append", dest="nodes",
199
                          help="Node to copy to (if not given, all nodes)"
200
                          ", can be given multiple times", metavar="<node>",
201
                          default=[])
202

    
203
commands = {
204
  'init': (InitCluster, ARGS_ONE,
205
           [DEBUG_OPT,
206
            make_option("-s", "--secondary-ip", dest="secondary_ip",
207
                        help="Specify the secondary ip for this node;"
208
                        " if given, the entire cluster must have secondary"
209
                        " addresses",
210
                        metavar="ADDRESS", default=None),
211
            make_option("-t", "--hypervisor-type", dest="hypervisor_type",
212
                        help="Specify the hypervisor type (xen-3.0, fake)",
213
                        metavar="TYPE", choices=["xen-3.0", "fake"],
214
                        default="xen-3.0",),
215
            make_option("-m", "--mac-prefix", dest="mac_prefix",
216
                        help="Specify the mac prefix for the instance IP"
217
                        " addresses, in the format XX:XX:XX",
218
                        metavar="PREFIX",
219
                        default="aa:00:00",),
220
            make_option("-g", "--vg-name", dest="vg_name",
221
                        help="Specify the volume group name "
222
                        " (cluster-wide) for disk allocation [xenvg]",
223
                        metavar="VG",
224
                        default="xenvg",),
225
            make_option("-b", "--bridge", dest="def_bridge",
226
                        help="Specify the default bridge name (cluster-wide)"
227
                          " to connect the instances to [%s]" %
228
                          constants.DEFAULT_BRIDGE,
229
                        metavar="BRIDGE",
230
                        default=constants.DEFAULT_BRIDGE,),
231
            make_option("--master-netdev", dest="master_netdev",
232
                        help="Specify the node interface (cluster-wide)"
233
                          " on which the master IP address will be added "
234
                          " [%s]" % constants.DEFAULT_BRIDGE,
235
                        metavar="NETDEV",
236
                        default=constants.DEFAULT_BRIDGE,),
237
            ],
238
           "[opts...] <cluster_name>",
239
           "Initialises a new cluster configuration"),
240
  'destroy': (DestroyCluster, ARGS_NONE,
241
              [DEBUG_OPT,
242
               make_option("--yes-do-it", dest="yes_do_it",
243
                           help="Destroy cluster",
244
                           action="store_true"),
245
              ],
246
              "", "Destroy cluster"),
247
  'rename': (RenameCluster, ARGS_ONE, [DEBUG_OPT, FORCE_OPT],
248
               "<new_name>",
249
               "Renames the cluster"),
250
  'verify': (VerifyCluster, ARGS_NONE, [DEBUG_OPT],
251
             "", "Does a check on the cluster configuration"),
252
  'masterfailover': (MasterFailover, ARGS_NONE, [DEBUG_OPT],
253
                     "", "Makes the current node the master"),
254
  'version': (ShowClusterVersion, ARGS_NONE, [DEBUG_OPT],
255
              "", "Shows the cluster version"),
256
  'getmaster': (ShowClusterMaster, ARGS_NONE, [DEBUG_OPT],
257
                "", "Shows the cluster master"),
258
  'copyfile': (ClusterCopyFile, ARGS_ONE, [DEBUG_OPT, node_option],
259
               "[-n node...] <filename>",
260
               "Copies a file to all (or only some) nodes"),
261
  'command': (RunClusterCommand, ARGS_ATLEAST(1), [DEBUG_OPT, node_option],
262
              "[-n node...] <command>",
263
              "Runs a command on all (or only some) nodes"),
264
  'info': (ShowClusterConfig, ARGS_NONE, [DEBUG_OPT],
265
                 "", "Show cluster configuration"),
266
  'list-tags': (ListTags, ARGS_NONE,
267
                [DEBUG_OPT], "", "List the tags of the cluster"),
268
  'add-tags': (AddTags, ARGS_ANY, [DEBUG_OPT],
269
               "tag...", "Add tags to the cluster"),
270
  'remove-tags': (RemoveTags, ARGS_ANY, [DEBUG_OPT],
271
                  "tag...", "Remove tags from the cluster"),
272
  }
273

    
274
if __name__ == '__main__':
275
  sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_CLUSTER}))