Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ ec223efb

History | View | Annotate | Download (5.3 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 ec223efb Iustin Pop
  op = opcodes.OpQueryNodes(output_fields=selected_fields, nodes=[])
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 a8083063 Iustin Pop
  op = opcodes.OpQueryNodeData(nodes=args)
95 a8083063 Iustin Pop
  result = SubmitOpCode(op)
96 a8083063 Iustin Pop
97 a8083063 Iustin Pop
  for name, primary_ip, secondary_ip, pinst, sinst in result:
98 a8083063 Iustin Pop
    logger.ToStdout("Node name: %s" % name)
99 a8083063 Iustin Pop
    logger.ToStdout("  primary ip: %s" % primary_ip)
100 a8083063 Iustin Pop
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
101 a8083063 Iustin Pop
    if pinst:
102 a8083063 Iustin Pop
      logger.ToStdout("  primary for instances:")
103 a8083063 Iustin Pop
      for iname in pinst:
104 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
105 a8083063 Iustin Pop
    else:
106 a8083063 Iustin Pop
      logger.ToStdout("  primary for no instances")
107 a8083063 Iustin Pop
    if sinst:
108 a8083063 Iustin Pop
      logger.ToStdout("  secondary for instances:")
109 a8083063 Iustin Pop
      for iname in sinst:
110 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
111 a8083063 Iustin Pop
    else:
112 a8083063 Iustin Pop
      logger.ToStdout("  secondary for no instances")
113 a8083063 Iustin Pop
114 a8083063 Iustin Pop
  return 0
115 a8083063 Iustin Pop
116 a8083063 Iustin Pop
117 a8083063 Iustin Pop
def RemoveNode(opts, args):
118 a8083063 Iustin Pop
  """Remove node cli-to-processor bridge."""
119 a8083063 Iustin Pop
  op = opcodes.OpRemoveNode(node_name=args[0])
120 a8083063 Iustin Pop
  SubmitOpCode(op)
121 a8083063 Iustin Pop
122 a8083063 Iustin Pop
123 dcb93971 Michael Hanselmann
def ListVolumes(opts, args):
124 dcb93971 Michael Hanselmann
  """List logical volumes on node(s).
125 dcb93971 Michael Hanselmann
126 dcb93971 Michael Hanselmann
  """
127 dcb93971 Michael Hanselmann
  if opts.output is None:
128 dcb93971 Michael Hanselmann
    selected_fields = ["node", "phys", "vg",
129 dcb93971 Michael Hanselmann
                       "name", "size", "instance"]
130 dcb93971 Michael Hanselmann
  else:
131 dcb93971 Michael Hanselmann
    selected_fields = opts.output.split(",")
132 dcb93971 Michael Hanselmann
133 dcb93971 Michael Hanselmann
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
134 dcb93971 Michael Hanselmann
  output = SubmitOpCode(op)
135 dcb93971 Michael Hanselmann
136 dcb93971 Michael Hanselmann
  if not opts.no_headers:
137 137161c9 Michael Hanselmann
    headers = {"node": "Node", "phys": "PhysDev",
138 137161c9 Michael Hanselmann
               "vg": "VG", "name": "Name",
139 137161c9 Michael Hanselmann
               "size": "Size", "instance": "Instance"}
140 137161c9 Michael Hanselmann
  else:
141 137161c9 Michael Hanselmann
    headers = None
142 137161c9 Michael Hanselmann
143 137161c9 Michael Hanselmann
  if opts.human_readable:
144 137161c9 Michael Hanselmann
    unitfields = ["size"]
145 137161c9 Michael Hanselmann
  else:
146 137161c9 Michael Hanselmann
    unitfields = None
147 137161c9 Michael Hanselmann
148 137161c9 Michael Hanselmann
  numfields = ["size"]
149 137161c9 Michael Hanselmann
150 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
151 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
152 16be8703 Iustin Pop
                       numfields=numfields, data=output)
153 16be8703 Iustin Pop
154 16be8703 Iustin Pop
  for line in data:
155 16be8703 Iustin Pop
    logger.ToStdout(line)
156 dcb93971 Michael Hanselmann
157 dcb93971 Michael Hanselmann
  return 0
158 dcb93971 Michael Hanselmann
159 dcb93971 Michael Hanselmann
160 a8083063 Iustin Pop
commands = {
161 a8083063 Iustin Pop
  'add': (AddNode, ARGS_ONE,
162 a8083063 Iustin Pop
          [DEBUG_OPT,
163 a8083063 Iustin Pop
           make_option("-s", "--secondary-ip", dest="secondary_ip",
164 a8083063 Iustin Pop
                       help="Specify the secondary ip for the node",
165 a8083063 Iustin Pop
                       metavar="ADDRESS", default=None),],
166 a8083063 Iustin Pop
          "<node_name>", "Add a node to the cluster"),
167 a8083063 Iustin Pop
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
168 a8083063 Iustin Pop
           "[<node_name>...]", "Show information about the node(s)"),
169 a8083063 Iustin Pop
  'list': (ListNodes, ARGS_NONE,
170 dcb93971 Michael Hanselmann
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
171 a8083063 Iustin Pop
           "", "Lists the nodes in the cluster"),
172 a8083063 Iustin Pop
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
173 a8083063 Iustin Pop
             "<node_name>", "Removes a node from the cluster"),
174 dcb93971 Michael Hanselmann
  'volumes': (ListVolumes, ARGS_ANY,
175 dcb93971 Michael Hanselmann
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
176 dcb93971 Michael Hanselmann
              "[<node_name>...]", "List logical volumes on node(s)"),
177 a8083063 Iustin Pop
  }
178 a8083063 Iustin Pop
179 a8083063 Iustin Pop
180 a8083063 Iustin Pop
if __name__ == '__main__':
181 3ecf6786 Iustin Pop
  sys.exit(GenericMain(commands))