Commit 944cc1cc authored by Dmitry Duplyakin's avatar Dmitry Duplyakin

SLURM: add methods for listing nodes and updating /etc/hosts

parent bbaaea95
......@@ -4,6 +4,8 @@ from elasticslice.managers.core import SimpleElasticSliceHelper, \
SimpleElasticSliceManager
from elasticslice.util.util import ShellCommand
from elasticslice.util.log import configure_file_logging
from shutil import copyfile
import time
SECOND = 1
MINUTE = SECOND * 60
......@@ -163,7 +165,76 @@ class SlurmDynamicManager(SimpleElasticSliceHelper,SimpleElasticSliceManager):
self.file_log.debug("Handling deleted node: %s" % node)
pass
# Helper functions redefined:
def get_nodenames(self, include_IPs=False, include_fullnames=False):
"""
This method prints short (virtual) nodenames for all nodes in the experiment.
It can optionally also print external (physical) nodenames,
as well as internal (experiment) IP addresses.
"""
wrapped_pgmanifest = self.server.get_wrapped_manifest()
result = []
for node in wrapped_pgmanifest.nodes:
name = node.name
line = ""
if include_IPs:
line += wrapped_pgmanifest.nodenames[name].interfaces[0].address_info[0] + " "
if include_fullnames:
line += wrapped_pgmanifest.getHostnameAndPort(name)[0] + " "
line += name
result.append(line)
return result
def get_etc_hosts(self, cleanup=True):
"""
This method returns the contents of the local /etc/hosts file.
Optionally, it cleans up the contents by removing EOL chars and replacing tabs with spaces,
as well removes leading, trailing, and duplicate spaces. It will also remove empty lines.
"""
with open('/etc/hosts', "r") as f:
contents = f.readlines()
if cleanup:
return filter(None, [' '.join(l.rstrip('\n').replace('\t',' ').strip().split()) for l in contents])
else:
return contents
def update_etc_hosts(self):
"""
This method updates /etc/hosts file. It adds a block of entries for all experiment nodes
(obtained using the get_nodenames() funcion) at the end of the file, unless it is already there.
A special marker is used to determine whether such block is present or not.
This method preserves all manually entered entries before the auto-generated block.
"""
MARKER = "# AUTO-GENERATED BLOCK - DO NOT EDIT BY HAND"
END_LINE = "# END OF AUTO-GENERATED BLOCK - DO NOT ADD LINES BELOW"
TMP_FILE = "/etc/.new_hosts"
contents = self.get_etc_hosts(cleanup=True)
new_block = [MARKER] + self.get_nodenames(include_IPs=True, include_fullnames=True) + [END_LINE]
detected_change = False
if MARKER in contents:
pos = contents.index(MARKER)
old_block = contents[pos:]
if set(old_block) != set(new_block):
# Old block is really different than the new one
detected_change = True
new_contents = contents[:pos] + new_block
else:
detected_change = True
new_contents = contents + new_block
if detected_change:
with open(TMP_FILE, "w") as f:
f.write("\n".join(new_contents))
# Make a backup copy of the old /etc/hosts
time_suff = time.strftime("-%Y%m%d-%H%M%S")
copyfile('/etc/hosts', '/etc/hosts' + time_suff)
# Replace the old file with the new one
copyfile(TMP_FILE, '/etc/hosts')
class SlurmScheduler(object):
"""Define operations for interacting with the SLURM resource manager and scheduler"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment