Skip to content

Commit

Permalink
Optionally use ssh settings only with vagrant ssh command
Browse files Browse the repository at this point in the history
This allows bootstrapping the user on a base image, by using
default ssh settings during provisioning and user defined
ssh settings when doing `vagrant ssh`.
  • Loading branch information
wbclark committed Sep 8, 2022
1 parent 2a9f80c commit 485d9b1
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions vagrant/lib/forklift/box_distributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,24 @@ def distribute!
end
end

def fetch_from_box_or_settings(box, key)
box.fetch(key) { @settings.fetch(key, nil) }
def fetch_setting(box, key, default = nil)
box.fetch(key) { @settings.fetch(key, default) }
end

def argv_include_any?(tokens)
tokens.any? { |token| ARGV.include?(token) }
end

def add_ssh_setting(box, machine, key)
return if (value = fetch_setting(box, "ssh_#{key}")).nil?
machine.ssh.send("#{key}=", value)
end

def add_ssh_settings(box, machine, keys)
return if fetch_setting(box, 'use_ssh_settings_vagrant_ssh_only', false) && !argv_include_any?(%w[ssh ssh-config])
keys.each do |key|
add_ssh_setting(box, machine, key)
end
end

def define_vm(config, box = {})
Expand All @@ -71,11 +87,7 @@ def define_vm(config, box = {})
machine.vm.box = box.fetch('box_name', nil)
machine.vm.box_version = box.fetch('box_version', nil)

%w[username forward_agent keys_only].each do |key|
unless (value = fetch_from_box_or_settings(box, "ssh_#{key}")).nil?
machine.ssh.send("#{key}=", value)
end
end
add_ssh_settings(box, machine, %w[username forward_agent keys_only])

machine.vm.box_check_update = box.fetch('box_check_update', true)

Expand Down

0 comments on commit 485d9b1

Please sign in to comment.