root / scripts / gnt-node @ 3ef10550
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 | 3ef10550 | Michael Hanselmann | "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree", |
62 | 3ef10550 | Michael Hanselmann | "bootid": "BootID"} |
63 | 137161c9 | Michael Hanselmann | else: |
64 | 137161c9 | Michael Hanselmann | headers = None |
65 | 137161c9 | Michael Hanselmann | |
66 | 137161c9 | Michael Hanselmann | if opts.human_readable: |
67 | 137161c9 | Michael Hanselmann | unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"] |
68 | 137161c9 | Michael Hanselmann | else: |
69 | 137161c9 | Michael Hanselmann | unitfields = None |
70 | 137161c9 | Michael Hanselmann | |
71 | ec223efb | Iustin Pop | numfields = ["dtotal", "dfree", |
72 | ec223efb | Iustin Pop | "mtotal", "mnode", "mfree", |
73 | ec223efb | Iustin Pop | "pinst_cnt", "sinst_cnt"] |
74 | ec223efb | Iustin Pop | |
75 | ec223efb | Iustin Pop | # change raw values to nicer strings |
76 | ec223efb | Iustin Pop | for row in output: |
77 | ec223efb | Iustin Pop | for idx, field in enumerate(selected_fields): |
78 | ec223efb | Iustin Pop | val = row[idx] |
79 | ec223efb | Iustin Pop | if field == "pinst_list": |
80 | ec223efb | Iustin Pop | val = ",".join(val) |
81 | ec223efb | Iustin Pop | elif field == "sinst_list": |
82 | ec223efb | Iustin Pop | val = ",".join(val) |
83 | ec223efb | Iustin Pop | elif val is None: |
84 | ec223efb | Iustin Pop | val = "?" |
85 | ec223efb | Iustin Pop | row[idx] = str(val) |
86 | 137161c9 | Michael Hanselmann | |
87 | 16be8703 | Iustin Pop | data = GenerateTable(separator=opts.separator, headers=headers, |
88 | 16be8703 | Iustin Pop | fields=selected_fields, unitfields=unitfields, |
89 | 16be8703 | Iustin Pop | numfields=numfields, data=output) |
90 | 16be8703 | Iustin Pop | for line in data: |
91 | 16be8703 | Iustin Pop | logger.ToStdout(line) |
92 | a8083063 | Iustin Pop | |
93 | a8083063 | Iustin Pop | return 0 |
94 | a8083063 | Iustin Pop | |
95 | a8083063 | Iustin Pop | |
96 | a8083063 | Iustin Pop | def ShowNodeConfig(opts, args): |
97 | a8083063 | Iustin Pop | """Show node information. |
98 | a8083063 | Iustin Pop | |
99 | a8083063 | Iustin Pop | """ |
100 | 4a72cc75 | Iustin Pop | op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip", |
101 | 4a72cc75 | Iustin Pop | "pinst_list", "sinst_list"], |
102 | 246e180a | Iustin Pop | names=args) |
103 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
104 | a8083063 | Iustin Pop | |
105 | a8083063 | Iustin Pop | for name, primary_ip, secondary_ip, pinst, sinst in result: |
106 | a8083063 | Iustin Pop | logger.ToStdout("Node name: %s" % name) |
107 | a8083063 | Iustin Pop | logger.ToStdout(" primary ip: %s" % primary_ip) |
108 | a8083063 | Iustin Pop | logger.ToStdout(" secondary ip: %s" % secondary_ip) |
109 | a8083063 | Iustin Pop | if pinst: |
110 | a8083063 | Iustin Pop | logger.ToStdout(" primary for instances:") |
111 | a8083063 | Iustin Pop | for iname in pinst: |
112 | a8083063 | Iustin Pop | logger.ToStdout(" - %s" % iname) |
113 | a8083063 | Iustin Pop | else: |
114 | a8083063 | Iustin Pop | logger.ToStdout(" primary for no instances") |
115 | a8083063 | Iustin Pop | if sinst: |
116 | a8083063 | Iustin Pop | logger.ToStdout(" secondary for instances:") |
117 | a8083063 | Iustin Pop | for iname in sinst: |
118 | a8083063 | Iustin Pop | logger.ToStdout(" - %s" % iname) |
119 | a8083063 | Iustin Pop | else: |
120 | a8083063 | Iustin Pop | logger.ToStdout(" secondary for no instances") |
121 | a8083063 | Iustin Pop | |
122 | a8083063 | Iustin Pop | return 0 |
123 | a8083063 | Iustin Pop | |
124 | a8083063 | Iustin Pop | |
125 | a8083063 | Iustin Pop | def RemoveNode(opts, args): |
126 | a8083063 | Iustin Pop | """Remove node cli-to-processor bridge.""" |
127 | a8083063 | Iustin Pop | op = opcodes.OpRemoveNode(node_name=args[0]) |
128 | a8083063 | Iustin Pop | SubmitOpCode(op) |
129 | a8083063 | Iustin Pop | |
130 | a8083063 | Iustin Pop | |
131 | dcb93971 | Michael Hanselmann | def ListVolumes(opts, args): |
132 | dcb93971 | Michael Hanselmann | """List logical volumes on node(s). |
133 | dcb93971 | Michael Hanselmann | |
134 | dcb93971 | Michael Hanselmann | """ |
135 | dcb93971 | Michael Hanselmann | if opts.output is None: |
136 | dcb93971 | Michael Hanselmann | selected_fields = ["node", "phys", "vg", |
137 | dcb93971 | Michael Hanselmann | "name", "size", "instance"] |
138 | dcb93971 | Michael Hanselmann | else: |
139 | dcb93971 | Michael Hanselmann | selected_fields = opts.output.split(",") |
140 | dcb93971 | Michael Hanselmann | |
141 | dcb93971 | Michael Hanselmann | op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields) |
142 | dcb93971 | Michael Hanselmann | output = SubmitOpCode(op) |
143 | dcb93971 | Michael Hanselmann | |
144 | dcb93971 | Michael Hanselmann | if not opts.no_headers: |
145 | 137161c9 | Michael Hanselmann | headers = {"node": "Node", "phys": "PhysDev", |
146 | 137161c9 | Michael Hanselmann | "vg": "VG", "name": "Name", |
147 | 137161c9 | Michael Hanselmann | "size": "Size", "instance": "Instance"} |
148 | 137161c9 | Michael Hanselmann | else: |
149 | 137161c9 | Michael Hanselmann | headers = None |
150 | 137161c9 | Michael Hanselmann | |
151 | 137161c9 | Michael Hanselmann | if opts.human_readable: |
152 | 137161c9 | Michael Hanselmann | unitfields = ["size"] |
153 | 137161c9 | Michael Hanselmann | else: |
154 | 137161c9 | Michael Hanselmann | unitfields = None |
155 | 137161c9 | Michael Hanselmann | |
156 | 137161c9 | Michael Hanselmann | numfields = ["size"] |
157 | 137161c9 | Michael Hanselmann | |
158 | 16be8703 | Iustin Pop | data = GenerateTable(separator=opts.separator, headers=headers, |
159 | 16be8703 | Iustin Pop | fields=selected_fields, unitfields=unitfields, |
160 | 16be8703 | Iustin Pop | numfields=numfields, data=output) |
161 | 16be8703 | Iustin Pop | |
162 | 16be8703 | Iustin Pop | for line in data: |
163 | 16be8703 | Iustin Pop | logger.ToStdout(line) |
164 | dcb93971 | Michael Hanselmann | |
165 | dcb93971 | Michael Hanselmann | return 0 |
166 | dcb93971 | Michael Hanselmann | |
167 | dcb93971 | Michael Hanselmann | |
168 | a8083063 | Iustin Pop | commands = { |
169 | a8083063 | Iustin Pop | 'add': (AddNode, ARGS_ONE, |
170 | a8083063 | Iustin Pop | [DEBUG_OPT, |
171 | a8083063 | Iustin Pop | make_option("-s", "--secondary-ip", dest="secondary_ip", |
172 | a8083063 | Iustin Pop | help="Specify the secondary ip for the node", |
173 | a8083063 | Iustin Pop | metavar="ADDRESS", default=None),], |
174 | a8083063 | Iustin Pop | "<node_name>", "Add a node to the cluster"), |
175 | a8083063 | Iustin Pop | 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT], |
176 | a8083063 | Iustin Pop | "[<node_name>...]", "Show information about the node(s)"), |
177 | a8083063 | Iustin Pop | 'list': (ListNodes, ARGS_NONE, |
178 | dcb93971 | Michael Hanselmann | [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
179 | a8083063 | Iustin Pop | "", "Lists the nodes in the cluster"), |
180 | a8083063 | Iustin Pop | 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT], |
181 | a8083063 | Iustin Pop | "<node_name>", "Removes a node from the cluster"), |
182 | dcb93971 | Michael Hanselmann | 'volumes': (ListVolumes, ARGS_ANY, |
183 | dcb93971 | Michael Hanselmann | [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
184 | dcb93971 | Michael Hanselmann | "[<node_name>...]", "List logical volumes on node(s)"), |
185 | 846baef9 | Iustin Pop | 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT], |
186 | 846baef9 | Iustin Pop | "<node_name>", "List the tags of the given node"), |
187 | 810c50b7 | Iustin Pop | 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
188 | 846baef9 | Iustin Pop | "<node_name> tag...", "Add tags to the given node"), |
189 | 810c50b7 | Iustin Pop | 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
190 | 846baef9 | Iustin Pop | "<node_name> tag...", "Remove tags from the given node"), |
191 | a8083063 | Iustin Pop | } |
192 | a8083063 | Iustin Pop | |
193 | a8083063 | Iustin Pop | |
194 | a8083063 | Iustin Pop | if __name__ == '__main__': |
195 | 846baef9 | Iustin Pop | sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE})) |