Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSH Keyfile location support for systemd launcher/cluster #863

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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