Revision 4b62db14 qa/qa_utils.py
b/qa/qa_utils.py | ||
---|---|---|
100 | 100 |
"""Starts SSH. |
101 | 101 |
|
102 | 102 |
""" |
103 |
args = GetSSHCommand(node, cmd, strict=strict) |
|
104 |
return subprocess.Popen(args, shell=False) |
|
103 |
return subprocess.Popen(GetSSHCommand(node, cmd, strict=strict), |
|
104 |
shell=False) |
|
105 |
|
|
106 |
|
|
107 |
def GetCommandOutput(node, cmd): |
|
108 |
"""Returns the output of a command executed on the given node. |
|
109 |
|
|
110 |
""" |
|
111 |
p = subprocess.Popen(GetSSHCommand(node, cmd), |
|
112 |
shell=False, stdout=subprocess.PIPE) |
|
113 |
AssertEqual(p.wait(), 0) |
|
114 |
return p.stdout.read() |
|
105 | 115 |
|
106 | 116 |
|
107 | 117 |
def UploadFile(node, src): |
... | ... | |
130 | 140 |
f.close() |
131 | 141 |
|
132 | 142 |
|
143 |
def _ResolveName(cmd, key): |
|
144 |
"""Helper function. |
|
145 |
|
|
146 |
""" |
|
147 |
master = qa_config.GetMasterNode() |
|
148 |
|
|
149 |
output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd)) |
|
150 |
for line in output.splitlines(): |
|
151 |
(lkey, lvalue) = line.split(':', 1) |
|
152 |
if lkey == key: |
|
153 |
return lvalue.lstrip() |
|
154 |
raise KeyError("Key not found") |
|
155 |
|
|
156 |
|
|
133 | 157 |
def ResolveInstanceName(instance): |
134 | 158 |
"""Gets the full name of an instance. |
135 | 159 |
|
136 | 160 |
""" |
161 |
return _ResolveName(['gnt-instance', 'info', instance['info']], |
|
162 |
'Instance name') |
|
163 |
|
|
164 |
|
|
165 |
def ResolveNodeName(node): |
|
166 |
"""Gets the full name of a node. |
|
167 |
|
|
168 |
""" |
|
169 |
return _ResolveName(['gnt-node', 'info', node['primary']], |
|
170 |
'Node name') |
|
171 |
|
|
172 |
|
|
173 |
def GetNodeInstances(node, secondaries=False): |
|
174 |
"""Gets a list of instances on a node. |
|
175 |
|
|
176 |
""" |
|
137 | 177 |
master = qa_config.GetMasterNode() |
138 | 178 |
|
139 |
info_cmd = utils.ShellQuoteArgs(['gnt-instance', 'info', instance['name']]) |
|
140 |
sed_cmd = utils.ShellQuoteArgs(['sed', '-n', '-e', 's/^Instance name: *//p']) |
|
179 |
node_name = ResolveNodeName(node) |
|
141 | 180 |
|
142 |
cmd = '%s | %s' % (info_cmd, sed_cmd) |
|
143 |
ssh_cmd = GetSSHCommand(master['primary'], cmd) |
|
144 |
p = subprocess.Popen(ssh_cmd, shell=False, stdout=subprocess.PIPE) |
|
145 |
AssertEqual(p.wait(), 0) |
|
181 |
# Get list of all instances |
|
182 |
cmd = ['gnt-instance', 'list', '--separator=:', '--no-headers', |
|
183 |
'--output=name,pnode,snodes'] |
|
184 |
output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd)) |
|
185 |
|
|
186 |
instances = [] |
|
187 |
for line in output.splitlines(): |
|
188 |
(name, pnode, snodes) = line.split(':', 2) |
|
189 |
if ((not secondaries and pnode == node_name) or |
|
190 |
(secondaries and node_name in snodes.split(','))): |
|
191 |
instances.append(name) |
|
146 | 192 |
|
147 |
return p.stdout.read().strip()
|
|
193 |
return instances
|
|
148 | 194 |
|
149 | 195 |
|
150 | 196 |
def _PrintWithColor(text, seq): |
Also available in: Unified diff