Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ 246e180a

History | View | Annotate | Download (5.5 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
import sys
23 a8083063 Iustin Pop
from optparse import make_option
24 a8083063 Iustin Pop
25 a8083063 Iustin Pop
from ganeti.cli import *
26 a8083063 Iustin Pop
from ganeti import opcodes
27 a8083063 Iustin Pop
from ganeti import logger
28 a8083063 Iustin Pop
from ganeti import utils
29 a8083063 Iustin Pop
30 a8083063 Iustin Pop
31 a8083063 Iustin Pop
def AddNode(opts, args):
32 a8083063 Iustin Pop
  """Add node cli-to-processor bridge."""
33 a8083063 Iustin Pop
  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
34 a8083063 Iustin Pop
  SubmitOpCode(op)
35 a8083063 Iustin Pop
36 a8083063 Iustin Pop
37 a8083063 Iustin Pop
def ListNodes(opts, args):
38 a8083063 Iustin Pop
  """List nodes and their properties.
39 a8083063 Iustin Pop
40 a8083063 Iustin Pop
  """
41 a8083063 Iustin Pop
  if opts.output is None:
42 a8083063 Iustin Pop
    selected_fields = ["name", "dtotal", "dfree",
43 a8083063 Iustin Pop
                       "mtotal", "mnode", "mfree",
44 ec223efb Iustin Pop
                       "pinst_cnt", "sinst_cnt"]
45 a8083063 Iustin Pop
  else:
46 a8083063 Iustin Pop
    selected_fields = opts.output.split(",")
47 a8083063 Iustin Pop
48 246e180a Iustin Pop
  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
49 a8083063 Iustin Pop
  output = SubmitOpCode(op)
50 a8083063 Iustin Pop
51 a8083063 Iustin Pop
  if not opts.no_headers:
52 ec223efb Iustin Pop
    headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
53 ec223efb Iustin Pop
               "pinst_list": "PriInstances", "sinst_list": "SecInstances",
54 137161c9 Michael Hanselmann
               "pip": "PrimaryIP", "sip": "SecondaryIP",
55 137161c9 Michael Hanselmann
               "dtotal": "DTotal", "dfree": "DFree",
56 137161c9 Michael Hanselmann
               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
57 137161c9 Michael Hanselmann
  else:
58 137161c9 Michael Hanselmann
    headers = None
59 137161c9 Michael Hanselmann
60 137161c9 Michael Hanselmann
  if opts.human_readable:
61 137161c9 Michael Hanselmann
    unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
62 137161c9 Michael Hanselmann
  else:
63 137161c9 Michael Hanselmann
    unitfields = None
64 137161c9 Michael Hanselmann
65 ec223efb Iustin Pop
  numfields = ["dtotal", "dfree",
66 ec223efb Iustin Pop
               "mtotal", "mnode", "mfree",
67 ec223efb Iustin Pop
               "pinst_cnt", "sinst_cnt"]
68 ec223efb Iustin Pop
69 ec223efb Iustin Pop
  # change raw values to nicer strings
70 ec223efb Iustin Pop
  for row in output:
71 ec223efb Iustin Pop
    for idx, field in enumerate(selected_fields):
72 ec223efb Iustin Pop
      val = row[idx]
73 ec223efb Iustin Pop
      if field == "pinst_list":
74 ec223efb Iustin Pop
        val = ",".join(val)
75 ec223efb Iustin Pop
      elif field == "sinst_list":
76 ec223efb Iustin Pop
        val = ",".join(val)
77 ec223efb Iustin Pop
      elif val is None:
78 ec223efb Iustin Pop
        val = "?"
79 ec223efb Iustin Pop
      row[idx] = str(val)
80 137161c9 Michael Hanselmann
81 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
82 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
83 16be8703 Iustin Pop
                       numfields=numfields, data=output)
84 16be8703 Iustin Pop
  for line in data:
85 16be8703 Iustin Pop
    logger.ToStdout(line)
86 a8083063 Iustin Pop
87 a8083063 Iustin Pop
  return 0
88 a8083063 Iustin Pop
89 a8083063 Iustin Pop
90 a8083063 Iustin Pop
def ShowNodeConfig(opts, args):
91 a8083063 Iustin Pop
  """Show node information.
92 a8083063 Iustin Pop
93 a8083063 Iustin Pop
  """
94 4a72cc75 Iustin Pop
  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
95 4a72cc75 Iustin Pop
                                           "pinst_list", "sinst_list"],
96 246e180a Iustin Pop
                            names=args)
97 a8083063 Iustin Pop
  result = SubmitOpCode(op)
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
  for name, primary_ip, secondary_ip, pinst, sinst in result:
100 a8083063 Iustin Pop
    logger.ToStdout("Node name: %s" % name)
101 a8083063 Iustin Pop
    logger.ToStdout("  primary ip: %s" % primary_ip)
102 a8083063 Iustin Pop
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
103 a8083063 Iustin Pop
    if pinst:
104 a8083063 Iustin Pop
      logger.ToStdout("  primary for instances:")
105 a8083063 Iustin Pop
      for iname in pinst:
106 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
107 a8083063 Iustin Pop
    else:
108 a8083063 Iustin Pop
      logger.ToStdout("  primary for no instances")
109 a8083063 Iustin Pop
    if sinst:
110 a8083063 Iustin Pop
      logger.ToStdout("  secondary for instances:")
111 a8083063 Iustin Pop
      for iname in sinst:
112 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
113 a8083063 Iustin Pop
    else:
114 a8083063 Iustin Pop
      logger.ToStdout("  secondary for no instances")
115 a8083063 Iustin Pop
116 a8083063 Iustin Pop
  return 0
117 a8083063 Iustin Pop
118 a8083063 Iustin Pop
119 a8083063 Iustin Pop
def RemoveNode(opts, args):
120 a8083063 Iustin Pop
  """Remove node cli-to-processor bridge."""
121 a8083063 Iustin Pop
  op = opcodes.OpRemoveNode(node_name=args[0])
122 a8083063 Iustin Pop
  SubmitOpCode(op)
123 a8083063 Iustin Pop
124 a8083063 Iustin Pop
125 dcb93971 Michael Hanselmann
def ListVolumes(opts, args):
126 dcb93971 Michael Hanselmann
  """List logical volumes on node(s).
127 dcb93971 Michael Hanselmann
128 dcb93971 Michael Hanselmann
  """
129 dcb93971 Michael Hanselmann
  if opts.output is None:
130 dcb93971 Michael Hanselmann
    selected_fields = ["node", "phys", "vg",
131 dcb93971 Michael Hanselmann
                       "name", "size", "instance"]
132 dcb93971 Michael Hanselmann
  else:
133 dcb93971 Michael Hanselmann
    selected_fields = opts.output.split(",")
134 dcb93971 Michael Hanselmann
135 dcb93971 Michael Hanselmann
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
136 dcb93971 Michael Hanselmann
  output = SubmitOpCode(op)
137 dcb93971 Michael Hanselmann
138 dcb93971 Michael Hanselmann
  if not opts.no_headers:
139 137161c9 Michael Hanselmann
    headers = {"node": "Node", "phys": "PhysDev",
140 137161c9 Michael Hanselmann
               "vg": "VG", "name": "Name",
141 137161c9 Michael Hanselmann
               "size": "Size", "instance": "Instance"}
142 137161c9 Michael Hanselmann
  else:
143 137161c9 Michael Hanselmann
    headers = None
144 137161c9 Michael Hanselmann
145 137161c9 Michael Hanselmann
  if opts.human_readable:
146 137161c9 Michael Hanselmann
    unitfields = ["size"]
147 137161c9 Michael Hanselmann
  else:
148 137161c9 Michael Hanselmann
    unitfields = None
149 137161c9 Michael Hanselmann
150 137161c9 Michael Hanselmann
  numfields = ["size"]
151 137161c9 Michael Hanselmann
152 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
153 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
154 16be8703 Iustin Pop
                       numfields=numfields, data=output)
155 16be8703 Iustin Pop
156 16be8703 Iustin Pop
  for line in data:
157 16be8703 Iustin Pop
    logger.ToStdout(line)
158 dcb93971 Michael Hanselmann
159 dcb93971 Michael Hanselmann
  return 0
160 dcb93971 Michael Hanselmann
161 dcb93971 Michael Hanselmann
162 a8083063 Iustin Pop
commands = {
163 a8083063 Iustin Pop
  'add': (AddNode, ARGS_ONE,
164 a8083063 Iustin Pop
          [DEBUG_OPT,
165 a8083063 Iustin Pop
           make_option("-s", "--secondary-ip", dest="secondary_ip",
166 a8083063 Iustin Pop
                       help="Specify the secondary ip for the node",
167 a8083063 Iustin Pop
                       metavar="ADDRESS", default=None),],
168 a8083063 Iustin Pop
          "<node_name>", "Add a node to the cluster"),
169 a8083063 Iustin Pop
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
170 a8083063 Iustin Pop
           "[<node_name>...]", "Show information about the node(s)"),
171 a8083063 Iustin Pop
  'list': (ListNodes, ARGS_NONE,
172 dcb93971 Michael Hanselmann
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
173 a8083063 Iustin Pop
           "", "Lists the nodes in the cluster"),
174 a8083063 Iustin Pop
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
175 a8083063 Iustin Pop
             "<node_name>", "Removes a node from the cluster"),
176 dcb93971 Michael Hanselmann
  'volumes': (ListVolumes, ARGS_ANY,
177 dcb93971 Michael Hanselmann
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
178 dcb93971 Michael Hanselmann
              "[<node_name>...]", "List logical volumes on node(s)"),
179 a8083063 Iustin Pop
  }
180 a8083063 Iustin Pop
181 a8083063 Iustin Pop
182 a8083063 Iustin Pop
if __name__ == '__main__':
183 3ecf6786 Iustin Pop
  sys.exit(GenericMain(commands))