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 6, 2022
1 parent d381ab8 commit 992c9e3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions vagrant/lib/forklift/box_distributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,17 @@ 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 define_vm(config, box = {})
Expand All @@ -71,9 +80,9 @@ 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)
unless fetch_setting(box, 'use_ssh_settings_vagrant_ssh_only', false) && !argv_include_any?(%w[ssh ssh-config])
%w[username forward_agent keys_only].each do |key|
add_ssh_setting(box, machine, key)
end
end

Expand Down

0 comments on commit 992c9e3

Please sign in to comment.