11 from __future__
import print_function
14 from BuildMTTTool
import *
30 BuildMTTTool.__init__(self)
33 self.
options[
'parent'] = (
None,
"Section that precedes this one in the dependency tree")
34 self.
options[
'nodelist'] = (
None,
"List of nodes to create hostfile from")
35 self.
options[
'nodeuse_ratio'] = (
None,
"Fraction of nodes to use")
36 self.
options[
'hostfile'] = (
None,
"Name of hostfile to generate")
37 self.
options[
'nodestatus_cmd'] = (
"sinfo -N -h",
"Command to print status of nodes")
38 self.
options[
'nodename_column'] = (
"0",
"Column index from nodestatus_cmd where nodename resides")
39 self.
options[
'nodestatus_column'] = (
"3",
"Column index from nodestatus_cmd where status of node resides")
40 self.
options[
'idle_status_string'] = (
"idle",
"String to check for when checking node status")
46 IPlugin.activate(self)
52 IPlugin.deactivate(self)
60 lines = testDef.printOptions(self.
options)
66 testDef.logger.verbose_print(
"Hostfile Execute")
69 testDef.parseOptions(log, self.
options, keyvals, cmds)
74 wrong_input_stderr =
""
75 if cmds[
'hostfile']
is None:
76 wrong_input_stderr +=
"No hostfile specified. "
77 if cmds[
'nodelist']
is None and cmds[
'nodeuse_ratio']
is None:
78 wrong_input_stderr +=
"No nodelist specified, or nodeuse_ratio specified. "
79 if wrong_input_stderr:
81 log[
'stderr'] = wrong_input_stderr
85 log[
'stderr'] =
"Required input not in cmds"
91 if cmds[
'parent']
is not None:
96 parentlog = testDef.logger.getLog(cmds[
'parent'])
99 log[
'stderr'] =
"Parent",cmds[
'parent'],
"log not found"
103 log[
'stderr'] =
"Parent not specified"
106 parentloc = os.path.join(os.getcwd(),log[
'options'][
'scratch'])
110 log[
'stderr'] =
"No scratch directory in log"
112 if parentlog
is not None:
114 parentloc = parentlog[
'location']
118 log[
'stderr'] =
"Location of package to build was not specified in parent stage"
122 if log[
'section'].startswith(
"TestGet:")
or log[
'section'].startswith(
"MiddlewareGet:"):
123 location = os.path.join(parentloc,log[
'section'].replace(
":",
"_"))
126 log[
'stderr'] =
"No section in log"
130 if testDef.options[
'dryrun']:
138 if os.path.exists(os.path.join(location,cmds[
'hostfile'])):
139 testDef.logger.verbose_print(
"hostfile " + os.path.join(location,cmds[
'hostfile']) +
" exists. Skipping...")
140 log[
'location'] = location
144 testDef.logger.verbose_print(
"hostfile " + os.path.join(location,cmds[
'hostfile']) +
" does not exist. Continuing...")
151 if not os.path.exists(location):
152 os.makedirs(location)
158 status, stdout, stderr, time = testDef.execmd.execute(cmds, shlex.split(cmds[
'nodestatus_cmd']), testDef)
161 log[
'stderr'] =
"Command %s failed: %s" % (cmds[
'nodestatus_cmd'],
" ".join(stderr))
164 stdout_split = [l.split()
for l
in stdout]
166 nodename_column = int(cmds[
'nodename_column'])
169 log[
'stderr'] =
"Invalid nodename_column %s -- must be an integer" % cmds[
'nodename_column']
173 nodestatus_column = int(cmds[
'nodestatus_column'])
176 log[
'stderr'] =
"Invalid nodestatus_column %s -- must be an integer" % cmds[
'nodestatus_column']
178 node_status = {l[nodename_column]: l[nodestatus_column]
for l
in stdout_split}
182 if nodename_column < 0
or nodename_column >= len(stdout_split[0]):
183 log[
'stderr'] +=
"nodename_column is out of bounds: %s " % str(nodename_column)
184 if nodestatus_column < 0
or nodestatus_column >= len(stdout_split[0]):
185 log[
'stderr'] +=
"nodestatus_column is out of bounds: %s " % str(nodestatus_column)
189 if cmds[
'nodelist']
is not None:
192 hosts = hostlist.expand_hostlist(cmds[
'nodelist'])
193 except hostlist.BadHostlist:
195 log[
'stderr'] =
"Bad nodelist format: %s" % cmds[
'nodelist']
200 hosts = [l[nodename_column]
for l
in stdout_split]
203 if cmds[
'nodeuse_ratio']
is not None:
205 ratio = float(cmds[
'nodeuse_ratio'])
208 ratio = float(cmds[
'nodeuse_ratio'].split(
"/")[0])/float(cmds[
'nodeuse_ratio'].split(
"/")[1])
211 log[
'stderr'] =
"Bad nodeuse_ratio format: %s" % cmds[
'nodeuse_ratio']
214 idle_hosts = [h
for h
in hosts
if node_status[h] == cmds[
'idle_status_string']]
215 nonidle_hosts = [h
for h
in hosts
if node_status[h] != cmds[
'idle_status_string']]
216 num_nodes = int(math.ceil(float(len(hosts))*ratio))
217 if num_nodes <= len(idle_hosts):
218 hosts = idle_hosts[:num_nodes]
220 hosts = idle_hosts + nonidle_hosts[:num_nodes - len(idle_hosts)]
224 with open(cmds[
'hostfile'],
"w")
as f:
225 f.write(
"\n".join(hosts))
229 log[
'stderr'] =
"File exception when writing hostfile %s" % cmds[
'hostfile']
234 log[
'stdout'] =
"Creation of hostfile %s from nodelist %s success" % (cmds[
'hostfile'],cmds[
'nodelist'])
235 log[
'location'] = location