Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ 810c50b7

History | View | Annotate | Download (6.2 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 846baef9 Iustin Pop
from ganeti import constants
30 a8083063 Iustin Pop
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
def AddNode(opts, args):
33 a8083063 Iustin Pop
  """Add node cli-to-processor bridge."""
34 99e8bbde Guido Trotter
  logger.ToStderr("-- WARNING -- \n"
35 99e8bbde Guido Trotter
    "Performing this operation is going to replace the ssh daemon keypair\n"
36 99e8bbde Guido Trotter
    "on the target machine (%s) with the ones of the current one\n"
37 99e8bbde Guido Trotter
    "and grant full intra-cluster ssh root access to/from it\n" % args[0])
38 a8083063 Iustin Pop
  op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip)
39 a8083063 Iustin Pop
  SubmitOpCode(op)
40 a8083063 Iustin Pop
41 a8083063 Iustin Pop
42 a8083063 Iustin Pop
def ListNodes(opts, args):
43 a8083063 Iustin Pop
  """List nodes and their properties.
44 a8083063 Iustin Pop
45 a8083063 Iustin Pop
  """
46 a8083063 Iustin Pop
  if opts.output is None:
47 a8083063 Iustin Pop
    selected_fields = ["name", "dtotal", "dfree",
48 a8083063 Iustin Pop
                       "mtotal", "mnode", "mfree",
49 ec223efb Iustin Pop
                       "pinst_cnt", "sinst_cnt"]
50 a8083063 Iustin Pop
  else:
51 a8083063 Iustin Pop
    selected_fields = opts.output.split(",")
52 a8083063 Iustin Pop
53 246e180a Iustin Pop
  op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[])
54 a8083063 Iustin Pop
  output = SubmitOpCode(op)
55 a8083063 Iustin Pop
56 a8083063 Iustin Pop
  if not opts.no_headers:
57 ec223efb Iustin Pop
    headers = {"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
58 ec223efb Iustin Pop
               "pinst_list": "PriInstances", "sinst_list": "SecInstances",
59 137161c9 Michael Hanselmann
               "pip": "PrimaryIP", "sip": "SecondaryIP",
60 137161c9 Michael Hanselmann
               "dtotal": "DTotal", "dfree": "DFree",
61 137161c9 Michael Hanselmann
               "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree"}
62 137161c9 Michael Hanselmann
  else:
63 137161c9 Michael Hanselmann
    headers = None
64 137161c9 Michael Hanselmann
65 137161c9 Michael Hanselmann
  if opts.human_readable:
66 137161c9 Michael Hanselmann
    unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
67 137161c9 Michael Hanselmann
  else:
68 137161c9 Michael Hanselmann
    unitfields = None
69 137161c9 Michael Hanselmann
70 ec223efb Iustin Pop
  numfields = ["dtotal", "dfree",
71 ec223efb Iustin Pop
               "mtotal", "mnode", "mfree",
72 ec223efb Iustin Pop
               "pinst_cnt", "sinst_cnt"]
73 ec223efb Iustin Pop
74 ec223efb Iustin Pop
  # change raw values to nicer strings
75 ec223efb Iustin Pop
  for row in output:
76 ec223efb Iustin Pop
    for idx, field in enumerate(selected_fields):
77 ec223efb Iustin Pop
      val = row[idx]
78 ec223efb Iustin Pop
      if field == "pinst_list":
79 ec223efb Iustin Pop
        val = ",".join(val)
80 ec223efb Iustin Pop
      elif field == "sinst_list":
81 ec223efb Iustin Pop
        val = ",".join(val)
82 ec223efb Iustin Pop
      elif val is None:
83 ec223efb Iustin Pop
        val = "?"
84 ec223efb Iustin Pop
      row[idx] = str(val)
85 137161c9 Michael Hanselmann
86 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
87 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
88 16be8703 Iustin Pop
                       numfields=numfields, data=output)
89 16be8703 Iustin Pop
  for line in data:
90 16be8703 Iustin Pop
    logger.ToStdout(line)
91 a8083063 Iustin Pop
92 a8083063 Iustin Pop
  return 0
93 a8083063 Iustin Pop
94 a8083063 Iustin Pop
95 a8083063 Iustin Pop
def ShowNodeConfig(opts, args):
96 a8083063 Iustin Pop
  """Show node information.
97 a8083063 Iustin Pop
98 a8083063 Iustin Pop
  """
99 4a72cc75 Iustin Pop
  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
100 4a72cc75 Iustin Pop
                                           "pinst_list", "sinst_list"],
101 246e180a Iustin Pop
                            names=args)
102 a8083063 Iustin Pop
  result = SubmitOpCode(op)
103 a8083063 Iustin Pop
104 a8083063 Iustin Pop
  for name, primary_ip, secondary_ip, pinst, sinst in result:
105 a8083063 Iustin Pop
    logger.ToStdout("Node name: %s" % name)
106 a8083063 Iustin Pop
    logger.ToStdout("  primary ip: %s" % primary_ip)
107 a8083063 Iustin Pop
    logger.ToStdout("  secondary ip: %s" % secondary_ip)
108 a8083063 Iustin Pop
    if pinst:
109 a8083063 Iustin Pop
      logger.ToStdout("  primary for instances:")
110 a8083063 Iustin Pop
      for iname in pinst:
111 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
112 a8083063 Iustin Pop
    else:
113 a8083063 Iustin Pop
      logger.ToStdout("  primary for no instances")
114 a8083063 Iustin Pop
    if sinst:
115 a8083063 Iustin Pop
      logger.ToStdout("  secondary for instances:")
116 a8083063 Iustin Pop
      for iname in sinst:
117 a8083063 Iustin Pop
        logger.ToStdout("    - %s" % iname)
118 a8083063 Iustin Pop
    else:
119 a8083063 Iustin Pop
      logger.ToStdout("  secondary for no instances")
120 a8083063 Iustin Pop
121 a8083063 Iustin Pop
  return 0
122 a8083063 Iustin Pop
123 a8083063 Iustin Pop
124 a8083063 Iustin Pop
def RemoveNode(opts, args):
125 a8083063 Iustin Pop
  """Remove node cli-to-processor bridge."""
126 a8083063 Iustin Pop
  op = opcodes.OpRemoveNode(node_name=args[0])
127 a8083063 Iustin Pop
  SubmitOpCode(op)
128 a8083063 Iustin Pop
129 a8083063 Iustin Pop
130 dcb93971 Michael Hanselmann
def ListVolumes(opts, args):
131 dcb93971 Michael Hanselmann
  """List logical volumes on node(s).
132 dcb93971 Michael Hanselmann
133 dcb93971 Michael Hanselmann
  """
134 dcb93971 Michael Hanselmann
  if opts.output is None:
135 dcb93971 Michael Hanselmann
    selected_fields = ["node", "phys", "vg",
136 dcb93971 Michael Hanselmann
                       "name", "size", "instance"]
137 dcb93971 Michael Hanselmann
  else:
138 dcb93971 Michael Hanselmann
    selected_fields = opts.output.split(",")
139 dcb93971 Michael Hanselmann
140 dcb93971 Michael Hanselmann
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
141 dcb93971 Michael Hanselmann
  output = SubmitOpCode(op)
142 dcb93971 Michael Hanselmann
143 dcb93971 Michael Hanselmann
  if not opts.no_headers:
144 137161c9 Michael Hanselmann
    headers = {"node": "Node", "phys": "PhysDev",
145 137161c9 Michael Hanselmann
               "vg": "VG", "name": "Name",
146 137161c9 Michael Hanselmann
               "size": "Size", "instance": "Instance"}
147 137161c9 Michael Hanselmann
  else:
148 137161c9 Michael Hanselmann
    headers = None
149 137161c9 Michael Hanselmann
150 137161c9 Michael Hanselmann
  if opts.human_readable:
151 137161c9 Michael Hanselmann
    unitfields = ["size"]
152 137161c9 Michael Hanselmann
  else:
153 137161c9 Michael Hanselmann
    unitfields = None
154 137161c9 Michael Hanselmann
155 137161c9 Michael Hanselmann
  numfields = ["size"]
156 137161c9 Michael Hanselmann
157 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
158 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
159 16be8703 Iustin Pop
                       numfields=numfields, data=output)
160 16be8703 Iustin Pop
161 16be8703 Iustin Pop
  for line in data:
162 16be8703 Iustin Pop
    logger.ToStdout(line)
163 dcb93971 Michael Hanselmann
164 dcb93971 Michael Hanselmann
  return 0
165 dcb93971 Michael Hanselmann
166 dcb93971 Michael Hanselmann
167 a8083063 Iustin Pop
commands = {
168 a8083063 Iustin Pop
  'add': (AddNode, ARGS_ONE,
169 a8083063 Iustin Pop
          [DEBUG_OPT,
170 a8083063 Iustin Pop
           make_option("-s", "--secondary-ip", dest="secondary_ip",
171 a8083063 Iustin Pop
                       help="Specify the secondary ip for the node",
172 a8083063 Iustin Pop
                       metavar="ADDRESS", default=None),],
173 a8083063 Iustin Pop
          "<node_name>", "Add a node to the cluster"),
174 a8083063 Iustin Pop
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
175 a8083063 Iustin Pop
           "[<node_name>...]", "Show information about the node(s)"),
176 a8083063 Iustin Pop
  'list': (ListNodes, ARGS_NONE,
177 dcb93971 Michael Hanselmann
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
178 a8083063 Iustin Pop
           "", "Lists the nodes in the cluster"),
179 a8083063 Iustin Pop
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
180 a8083063 Iustin Pop
             "<node_name>", "Removes a node from the cluster"),
181 dcb93971 Michael Hanselmann
  'volumes': (ListVolumes, ARGS_ANY,
182 dcb93971 Michael Hanselmann
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
183 dcb93971 Michael Hanselmann
              "[<node_name>...]", "List logical volumes on node(s)"),
184 846baef9 Iustin Pop
  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
185 846baef9 Iustin Pop
                "<node_name>", "List the tags of the given node"),
186 810c50b7 Iustin Pop
  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
187 846baef9 Iustin Pop
               "<node_name> tag...", "Add tags to the given node"),
188 810c50b7 Iustin Pop
  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
189 846baef9 Iustin Pop
                  "<node_name> tag...", "Remove tags from the given node"),
190 a8083063 Iustin Pop
  }
191 a8083063 Iustin Pop
192 a8083063 Iustin Pop
193 a8083063 Iustin Pop
if __name__ == '__main__':
194 846baef9 Iustin Pop
  sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))