Skip to content

Commit

Permalink
Merge branch 'pr/sw3630-sshconfig'
Browse files Browse the repository at this point in the history
* pr/sw3630-sshconfig:
  Remove dead code
  Own a section of the SSH config and always replace what's in that section (SOFTWARE-3630)
  • Loading branch information
matyasselmeci committed Jul 10, 2019
2 parents df6e2f0 + 99ec2d2 commit 7e713e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
3 changes: 3 additions & 0 deletions config/20-bosco.ini
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ ssh_key = UNAVAILABLE

; (Optional) The maximum number of jobs to submit to the remote cluster, idle + running.
max_jobs = 1000

; Set this to False to leave the remote .ssh/config file alone
;edit_ssh_config = True
72 changes: 45 additions & 27 deletions osg_configure/configure_modules/bosco.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

class BoscoConfiguration(JobManagerConfiguration):
"""Class to handle attributes related to Bosco job manager configuration"""

SSH_CONFIG_SECTION_BEGIN = "### THIS SECTION MANAGED BY OSG-CONFIGURE\n"
SSH_CONFIG_SECTION_END = "### END OF SECTION MANAGED BY OSG-CONFIGURE\n"

def __init__(self, *args, **kwargs):
# pylint: disable-msg=W0142
Expand Down Expand Up @@ -49,7 +50,12 @@ def __init__(self, *args, **kwargs):
'max_jobs':
configfile.Option(name='max_jobs',
requred=configfile.Option.OPTIONAL,
default_value=1000)}
default_value=1000),
'edit_ssh_config':
configfile.Option(name='edit_ssh_config',
required=configfile.Option.OPTIONAL,
opt_type=bool,
default_value=True)}


self.config_section = "BOSCO"
Expand Down Expand Up @@ -96,14 +102,6 @@ def check_attributes(self, attributes):
self.log('BoscoConfiguration.check_attributes completed')
return attributes_ok

#if not validation.valid_domain(self.options['endpoint'].value):
# attributes_ok = False
# self.log("Endpoint is not a valid hostname: %s" %
# (self.options['endpoint'].value),
# option='endpoint',
# section=self.config_section,
# level=logging.ERROR)

if self.options['batch'].value not in ['pbs', 'lsf', 'sge', 'condor', 'slurm']:
attributes_ok = False
self.log("Batch attribute is not valid: %s" %
Expand Down Expand Up @@ -231,24 +229,9 @@ def _installBosco(self, username):

os.chmod(ssh_key_loc, stat.S_IRUSR | stat.S_IWUSR)

# Add a section to .ssh/config for this host
config_path = os.path.join(user_home, ".ssh", "config")
# Split the entry point by the "@"
(username, host) = self.options["endpoint"].value.split('@')
host_config = """
Host %(host)s
HostName %(host)s
User %(username)s
IdentityFile %(key_loc)s
""" % {'host': host, 'username': user_name, 'key_loc': ssh_key_loc}
if self.opt_val("edit_ssh_config"):
self.edit_ssh_config(ssh_key_loc, user_home, user_name)

# Search the config for the above host
if not self._search_config(host, config_path):

with open(config_path, 'a') as f:
f.write(host_config)
self.log("Wrote %s", config_path, level=logging.DEBUG)

# Change the ownership of everything to the user
# https://stackoverflow.com/questions/2853723/whats-the-python-way-for-recursively-setting-file-permissions
path = os.path.join(user_home, ".ssh")
Expand Down Expand Up @@ -331,6 +314,41 @@ def result():
return False
return True

def edit_ssh_config(self, ssh_key_loc, user_home, user_name):
# Add a section to .ssh/config for this host
config_path = os.path.join(user_home, ".ssh", "config")
# Split the entry point by the "@"
username, host = self.options["endpoint"].value.split('@')
host_config = """
Host %(host)s
HostName %(host)s
User %(user_name)s
IdentityFile %(ssh_key_loc)s
""" % locals()
text_to_add = "%s%s%s" % (self.SSH_CONFIG_SECTION_BEGIN, host_config, self.SSH_CONFIG_SECTION_END)
if not os.path.exists(config_path):
utilities.atomic_write(config_path, text_to_add)
return

config_contents = ""
with open(config_path) as f:
config_contents = f.read()

section_re = re.compile(r"%s.+?%s" % (re.escape(self.SSH_CONFIG_SECTION_BEGIN), re.escape(self.SSH_CONFIG_SECTION_END)),
re.MULTILINE | re.DOTALL)
host_re = re.compile(r"^\s*Host\s+%s\s*$" % re.escape(host), re.MULTILINE)

if section_re.search(config_contents):
config_contents = section_re.sub(text_to_add, config_contents)
self.logger.debug("osg-configure section found in %s", config_path)
elif host_re.search(config_contents):
self.logger.info("Host %s already found in %s but not in an osg-configure section. Not modifying it.", host, config_path)
return
else:
config_contents += "\n" + text_to_add

utilities.atomic_write(config_path, config_contents)

def _write_route_config_vars(self):
"""
Write condor-ce config attributes for the bosco job route. Sets values for:
Expand Down

0 comments on commit 7e713e3

Please sign in to comment.