Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-node @ 810c50b7

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

    
31

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

    
41

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

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

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

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

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

    
70
  numfields = ["dtotal", "dfree",
71
               "mtotal", "mnode", "mfree",
72
               "pinst_cnt", "sinst_cnt"]
73

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

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

    
92
  return 0
93

    
94

    
95
def ShowNodeConfig(opts, args):
96
  """Show node information.
97

    
98
  """
99
  op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip",
100
                                           "pinst_list", "sinst_list"],
101
                            names=args)
102
  result = SubmitOpCode(op)
103

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

    
121
  return 0
122

    
123

    
124
def RemoveNode(opts, args):
125
  """Remove node cli-to-processor bridge."""
126
  op = opcodes.OpRemoveNode(node_name=args[0])
127
  SubmitOpCode(op)
128

    
129

    
130
def ListVolumes(opts, args):
131
  """List logical volumes on node(s).
132

    
133
  """
134
  if opts.output is None:
135
    selected_fields = ["node", "phys", "vg",
136
                       "name", "size", "instance"]
137
  else:
138
    selected_fields = opts.output.split(",")
139

    
140
  op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
141
  output = SubmitOpCode(op)
142

    
143
  if not opts.no_headers:
144
    headers = {"node": "Node", "phys": "PhysDev",
145
               "vg": "VG", "name": "Name",
146
               "size": "Size", "instance": "Instance"}
147
  else:
148
    headers = None
149

    
150
  if opts.human_readable:
151
    unitfields = ["size"]
152
  else:
153
    unitfields = None
154

    
155
  numfields = ["size"]
156

    
157
  data = GenerateTable(separator=opts.separator, headers=headers,
158
                       fields=selected_fields, unitfields=unitfields,
159
                       numfields=numfields, data=output)
160

    
161
  for line in data:
162
    logger.ToStdout(line)
163

    
164
  return 0
165

    
166

    
167
commands = {
168
  'add': (AddNode, ARGS_ONE,
169
          [DEBUG_OPT,
170
           make_option("-s", "--secondary-ip", dest="secondary_ip",
171
                       help="Specify the secondary ip for the node",
172
                       metavar="ADDRESS", default=None),],
173
          "<node_name>", "Add a node to the cluster"),
174
  'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
175
           "[<node_name>...]", "Show information about the node(s)"),
176
  'list': (ListNodes, ARGS_NONE,
177
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
178
           "", "Lists the nodes in the cluster"),
179
  'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
180
             "<node_name>", "Removes a node from the cluster"),
181
  'volumes': (ListVolumes, ARGS_ANY,
182
              [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
183
              "[<node_name>...]", "List logical volumes on node(s)"),
184
  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
185
                "<node_name>", "List the tags of the given node"),
186
  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
187
               "<node_name> tag...", "Add tags to the given node"),
188
  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
189
                  "<node_name> tag...", "Remove tags from the given node"),
190
  }
191

    
192

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