Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ c450e9b0

History | View | Annotate | Download (8.2 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

    
25
from ganeti.cli import *
26
from ganeti import opcodes
27
from ganeti import logger
28
from ganeti import utils
29
from ganeti import constants
30
from ganeti import errors
31

    
32

    
33
def AddNode(opts, args):
34
  """Add node cli-to-processor bridge."""
35
  logger.ToStderr("-- WARNING -- \n"
36
    "Performing this operation is going to replace the ssh daemon keypair\n"
37
    "on the target machine (%s) with the ones of the current one\n"
38
    "and grant full intra-cluster ssh root access to/from it\n" % args[0])
39
  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
40
  SubmitOpCode(op)
41

    
42

    
43
def ListNodes(opts, args):
44
  """List nodes and their properties.
45

    
46
  """
47
  if opts.output is None:
48
    selected_fields = ["name", "dtotal", "dfree",
49
                       "mtotal", "mnode", "mfree",
50
                       "pinst_cnt", "sinst_cnt"]
51
  else:
52
    selected_fields = opts.output.split(",")
53

    
54
  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
55
  output = SubmitOpCode(op)
56

    
57
  if not opts.no_headers:
58
    headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
59
               "pinst_list": "PriInstances", "sinst_list": "SecInstances",
60
               "pip": "PrimaryIP", "sip": "SecondaryIP",
61
               "dtotal": "DTotal", "dfree": "DFree",
62
               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
63
               "bootid": "BootID"}
64
  else:
65
    headers = None
66

    
67
  if opts.human_readable:
68
    unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
69
  else:
70
    unitfields = None
71

    
72
  numfields = ["dtotal", "dfree",
73
               "mtotal", "mnode", "mfree",
74
               "pinst_cnt", "sinst_cnt"]
75

    
76
  # change raw values to nicer strings
77
  for row in output:
78
    for idx, field in enumerate(selected_fields):
79
      val = row[idx]
80
      if field == "pinst_list":
81
        val = ",".join(val)
82
      elif field == "sinst_list":
83
        val = ",".join(val)
84
      elif val is None:
85
        val = "?"
86
      row[idx] = str(val)
87

    
88
  data = GenerateTable(separator=opts.separator, headers=headers,
89
                       fields=selected_fields, unitfields=unitfields,
90
                       numfields=numfields, data=output)
91
  for line in data:
92
    logger.ToStdout(line)
93

    
94
  return 0
95

    
96

    
97
def FailoverNode(opts, args):
98
  """Failover all primary instance on a node.
99

    
100
  """
101
  force = opts.force
102
  selected_fields = ["name", "pinst_list"]
103

    
104
  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=args)
105
  result = SubmitOpCode(op)
106
  node, pinst = result[0]
107

    
108
  if not pinst:
109
    logger.ToStderr("No primary instances on node %s, exiting." % node)
110
    return 0
111

    
112
  pinst = utils.NiceSort(pinst)
113

    
114
  retcode = 0
115

    
116
  if not force and not AskUser("Fail over instance(s) %s?" %
117
                               (",".join("'%s'" % name for name in pinst))):
118
    return 2
119

    
120
  good_cnt = bad_cnt = 0
121
  for iname in pinst:
122
    op = opcodes.OpFailoverInstance(instance_name=iname,
123
                                    ignore_consistency=opts.ignore_consistency)
124
    try:
125
      logger.ToStdout("Failing over instance %s" % iname)
126
      SubmitOpCode(op)
127
      logger.ToStdout("Instance %s has been failed over" % iname)
128
      good_cnt += 1
129
    except errors.GenericError, err:
130
      nret, msg = FormatError(err)
131
      retcode |= nret
132
      logger.ToStderr("Error failing over instance %s: %s" % (iname, msg))
133
      bad_cnt += 1
134

    
135
  if retcode == 0:
136
    logger.ToStdout("All %d instance(s) failed over successfully." % good_cnt)
137
  else:
138
    logger.ToStdout("There were errors during the failover:\n"
139
                    "%d error(s) out of %d instance(s)." %
140
                    (bad_cnt, good_cnt + bad_cnt))
141
  return retcode
142

    
143

    
144
def ShowNodeConfig(opts, args):
145
  """Show node information.
146

    
147
  """
148
  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
149
                                           "pinst_list", "sinst_list"],
150
                            names=args)
151
  result = SubmitOpCode(op)
152

    
153
  for name, primary_ip, secondary_ip, pinst, sinst in result:
154
    logger.ToStdout("Node name: %s" % name)
155
    logger.ToStdout("  primary ip: %s" % primary_ip)
156
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
157
    if pinst:
158
      logger.ToStdout("  primary for instances:")
159
      for iname in pinst:
160
        logger.ToStdout("    - %s" % iname)
161
    else:
162
      logger.ToStdout("  primary for no instances")
163
    if sinst:
164
      logger.ToStdout("  secondary for instances:")
165
      for iname in sinst:
166
        logger.ToStdout("    - %s" % iname)
167
    else:
168
      logger.ToStdout("  secondary for no instances")
169

    
170
  return 0
171

    
172

    
173
def RemoveNode(opts, args):
174
  """Remove node cli-to-processor bridge."""
175
  op = opcodes.OpRemoveNode(node_name=args[0])
176
  SubmitOpCode(op)
177

    
178

    
179
def ListVolumes(opts, args):
180
  """List logical volumes on node(s).
181

    
182
  """
183
  if opts.output is None:
184
    selected_fields = ["node", "phys", "vg",
185
                       "name", "size", "instance"]
186
  else:
187
    selected_fields = opts.output.split(",")
188

    
189
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
190
  output = SubmitOpCode(op)
191

    
192
  if not opts.no_headers:
193
    headers = {"node": "Node", "phys": "PhysDev",
194
               "vg": "VG", "name": "Name",
195
               "size": "Size", "instance": "Instance"}
196
  else:
197
    headers = None
198

    
199
  if opts.human_readable:
200
    unitfields = ["size"]
201
  else:
202
    unitfields = None
203

    
204
  numfields = ["size"]
205

    
206
  data = GenerateTable(separator=opts.separator, headers=headers,
207
                       fields=selected_fields, unitfields=unitfields,
208
                       numfields=numfields, data=output)
209

    
210
  for line in data:
211
    logger.ToStdout(line)
212

    
213
  return 0
214

    
215

    
216
commands = {
217
  'add': (AddNode, ARGS_ONE,
218
          [DEBUG_OPT,
219
           make_option("-s", "--secondary-ip", dest="secondary_ip",
220
                       help="Specify the secondary ip for the node",
221
                       metavar="ADDRESS", default=None),],
222
          "<node_name>", "Add a node to the cluster"),
223
  'failover': (FailoverNode, ARGS_ONE,
224
               [DEBUG_OPT, FORCE_OPT,
225
                make_option("--ignore-consistency", dest="ignore_consistency",
226
                            action="store_true", default=False,
227
                            help="Ignore the consistency of the disks on"
228
                            " the secondary"),
229
                ],
230
               "[-f] <node>",
231
               "Stops the primary instances on a node and start them on their"
232
               " secondary node (only for instances of type remote_raid1)"),
233
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
234
           "[<node_name>...]", "Show information about the node(s)"),
235
  'list': (ListNodes, ARGS_NONE,
236
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
237
           "", "Lists the nodes in the cluster"),
238
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
239
             "<node_name>", "Removes a node from the cluster"),
240
  'volumes': (ListVolumes, ARGS_ANY,
241
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
242
              "[<node_name>...]", "List logical volumes on node(s)"),
243
  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
244
                "<node_name>", "List the tags of the given node"),
245
  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
246
               "<node_name> tag...", "Add tags to the given node"),
247
  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
248
                  "<node_name> tag...", "Remove tags from the given node"),
249
  }
250

    
251

    
252
if __name__ == '__main__':
253
  sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))