Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ 4a72cc75

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

    
30

    
31
def AddNode(opts, args):
32
  """Add node cli-to-processor bridge."""
33
  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
34
  SubmitOpCode(op)
35

    
36

    
37
def ListNodes(opts, args):
38
  """List nodes and their properties.
39

    
40
  """
41
  if opts.output is None:
42
    selected_fields = ["name", "dtotal", "dfree",
43
                       "mtotal", "mnode", "mfree",
44
                       "pinst_cnt", "sinst_cnt"]
45
  else:
46
    selected_fields = opts.output.split(",")
47

    
48
  op = opcodes.OpQueryNodes(output_fields=selected_fields, nodes=[])
49
  output = SubmitOpCode(op)
50

    
51
  if not opts.no_headers:
52
    headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
53
               "pinst_list": "PriInstances", "sinst_list": "SecInstances",
54
               "pip": "PrimaryIP", "sip": "SecondaryIP",
55
               "dtotal": "DTotal", "dfree": "DFree",
56
               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
57
  else:
58
    headers = None
59

    
60
  if opts.human_readable:
61
    unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
62
  else:
63
    unitfields = None
64

    
65
  numfields = ["dtotal", "dfree",
66
               "mtotal", "mnode", "mfree",
67
               "pinst_cnt", "sinst_cnt"]
68

    
69
  # change raw values to nicer strings
70
  for row in output:
71
    for idx, field in enumerate(selected_fields):
72
      val = row[idx]
73
      if field == "pinst_list":
74
        val = ",".join(val)
75
      elif field == "sinst_list":
76
        val = ",".join(val)
77
      elif val is None:
78
        val = "?"
79
      row[idx] = str(val)
80

    
81
  data = GenerateTable(separator=opts.separator, headers=headers,
82
                       fields=selected_fields, unitfields=unitfields,
83
                       numfields=numfields, data=output)
84
  for line in data:
85
    logger.ToStdout(line)
86

    
87
  return 0
88

    
89

    
90
def ShowNodeConfig(opts, args):
91
  """Show node information.
92

    
93
  """
94
  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
95
                                           "pinst_list", "sinst_list"],
96
                            nodes=args)
97
  result = SubmitOpCode(op)
98

    
99
  for name, primary_ip, secondary_ip, pinst, sinst in result:
100
    logger.ToStdout("Node name: %s" % name)
101
    logger.ToStdout("  primary ip: %s" % primary_ip)
102
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
103
    if pinst:
104
      logger.ToStdout("  primary for instances:")
105
      for iname in pinst:
106
        logger.ToStdout("    - %s" % iname)
107
    else:
108
      logger.ToStdout("  primary for no instances")
109
    if sinst:
110
      logger.ToStdout("  secondary for instances:")
111
      for iname in sinst:
112
        logger.ToStdout("    - %s" % iname)
113
    else:
114
      logger.ToStdout("  secondary for no instances")
115

    
116
  return 0
117

    
118

    
119
def RemoveNode(opts, args):
120
  """Remove node cli-to-processor bridge."""
121
  op = opcodes.OpRemoveNode(node_name=args[0])
122
  SubmitOpCode(op)
123

    
124

    
125
def ListVolumes(opts, args):
126
  """List logical volumes on node(s).
127

    
128
  """
129
  if opts.output is None:
130
    selected_fields = ["node", "phys", "vg",
131
                       "name", "size", "instance"]
132
  else:
133
    selected_fields = opts.output.split(",")
134

    
135
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
136
  output = SubmitOpCode(op)
137

    
138
  if not opts.no_headers:
139
    headers = {"node": "Node", "phys": "PhysDev",
140
               "vg": "VG", "name": "Name",
141
               "size": "Size", "instance": "Instance"}
142
  else:
143
    headers = None
144

    
145
  if opts.human_readable:
146
    unitfields = ["size"]
147
  else:
148
    unitfields = None
149

    
150
  numfields = ["size"]
151

    
152
  data = GenerateTable(separator=opts.separator, headers=headers,
153
                       fields=selected_fields, unitfields=unitfields,
154
                       numfields=numfields, data=output)
155

    
156
  for line in data:
157
    logger.ToStdout(line)
158

    
159
  return 0
160

    
161

    
162
commands = {
163
  'add': (AddNode, ARGS_ONE,
164
          [DEBUG_OPT,
165
           make_option("-s", "--secondary-ip", dest="secondary_ip",
166
                       help="Specify the secondary ip for the node",
167
                       metavar="ADDRESS", default=None),],
168
          "<node_name>", "Add a node to the cluster"),
169
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
170
           "[<node_name>...]", "Show information about the node(s)"),
171
  'list': (ListNodes, ARGS_NONE,
172
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
173
           "", "Lists the nodes in the cluster"),
174
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
175
             "<node_name>", "Removes a node from the cluster"),
176
  'volumes': (ListVolumes, ARGS_ANY,
177
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
178
              "[<node_name>...]", "List logical volumes on node(s)"),
179
  }
180

    
181

    
182
if __name__ == '__main__':
183
  sys.exit(GenericMain(commands))