Skip to content

Commit

Permalink
SSH Keyfile location support for systemd launcher/cluster (#863)
Browse files Browse the repository at this point in the history
Add support for an SSH Keyfile location for the systemd adapter.
  • Loading branch information
adamboutcher authored Feb 6, 2025
1 parent bd519e1 commit db13933
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
3 changes: 3 additions & 0 deletions lib/ood_core/job/adapters/systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class Factory
# @option config [Object] :ssh_hosts (nil) The list of permissable hosts, defaults to :submit_host
# @option config [Object] :strict_host_checking (true) Set to false to disable strict host checking and updating the known_hosts file
# @option config [Object] :submit_host The SSH target to connect to, may be the head of a round-robin
# @option config [Object] :ssh_keyfile The SSH Key file to use as identity.
def self.build_systemd(config)
c = config.to_h.symbolize_keys
debug = c.fetch(:debug, false)
max_timeout = c.fetch(:max_timeout, nil)
ssh_hosts = c.fetch(:ssh_hosts, [c[:submit_host]])
strict_host_checking = c.fetch(:strict_host_checking, true)
submit_host = c[:submit_host]
ssh_keyfile = c.fetch(:ssh_keyfile, "")

Adapters::LinuxSystemd.new(
ssh_hosts: ssh_hosts,
Expand All @@ -31,6 +33,7 @@ def self.build_systemd(config)
ssh_hosts: ssh_hosts,
strict_host_checking: strict_host_checking,
submit_host: submit_host,
ssh_keyfile: ssh_keyfile,
)
)
end
Expand Down
45 changes: 28 additions & 17 deletions lib/ood_core/job/adapters/systemd/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# @api private
class OodCore::Job::Adapters::LinuxSystemd::Launcher
attr_reader :debug, :site_timeout, :session_name_label, :ssh_hosts,
:strict_host_checking, :username
:strict_host_checking, :username, :ssh_keyfile
# The root exception class that all LinuxSystemd adapter-specific exceptions inherit
# from
class Error < StandardError; end
Expand All @@ -26,6 +26,7 @@ def initialize(
ssh_hosts:,
strict_host_checking: false,
submit_host:,
ssh_keyfile: "",
**_
)
@debug = !! debug
Expand All @@ -35,6 +36,7 @@ def initialize(
@strict_host_checking = strict_host_checking
@submit_host = submit_host
@username = Etc.getlogin
@ssh_keyfile = ssh_keyfile
end

# @param hostname [#to_s] The hostname to submit the work to
Expand Down Expand Up @@ -97,27 +99,36 @@ def call(cmd, *args, env: {}, stdin: "")
# if ! strict_host_checking
# -o UserKnownHostsFile=/dev/null (do not update the user's known hosts file)
# -o StrictHostKeyChecking=no (do no check the user's known hosts file)
# if ssh_keyfile
# -i ssh_keyfile (Use this keyfile location)
#
# @param destination_host [#to_s] the destination host you wish to ssh into
# @param cmd [Array<#to_s>] the command to be executed on the destination host
def ssh_cmd(destination_host, cmd)
if strict_host_checking
[
'ssh', '-t',
'-p', OodCore::Job::Adapters::Helper.ssh_port,
'-o', 'BatchMode=yes',
"#{username}@#{destination_host}"
].concat(cmd)
else
[
'ssh', '-t',
'-p', OodCore::Job::Adapters::Helper.ssh_port,
'-o', 'BatchMode=yes',
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'StrictHostKeyChecking=no',
"#{username}@#{destination_host}"
].concat(cmd)

sshcmd=[
'ssh', '-t',
'-p', OodCore::Job::Adapters::Helper.ssh_port,
'-o', 'Batchmode=yes',
]

if !strict_host_checking
sshcmd.concat([
'-o','StrictHostKeyChecking=no',
'-o','UserKnownHostsFile=/dev/null',
])
end

if (!ssh_keyfile.to_s.empty?)
sshcmd.concat([
'-i',ssh_keyfile.to_s,
])
end

sshcmd.concat([
"#{username}@#{destination_host}"
]).concat(cmd)

end

def shell
Expand Down

0 comments on commit db13933

Please sign in to comment.