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

#169 easily override instance name #170

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions lib/kitchen/driver/vagrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Vagrant < Kitchen::Driver::Base
driver.windows_os? ? nil : driver.instance.name
end

default_config :vm_name, nil

no_parallel_for :create, :destroy

# Creates a Vagrant VM instance.
Expand Down
53 changes: 34 additions & 19 deletions templates/Vagrantfile.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,84 @@
require "<%= vagrantfile %>"
<% end %>

<% set_hostname = (config[:vm_hostname] != false) %>
<% if set_hostname %>
Vagrant.configure("2") do |c|
c.berkshelf.enabled = false if Vagrant.has_plugin?("vagrant-berkshelf")
c.vm.box = "<%= config[:box] %>"
<% else %>
Vagrant.configure("2") do |vm_instance|
vm_instance.berkshelf.enabled = false if Vagrant.has_plugin?("vagrant-berkshelf")
<% end %>
<% if (set_hostname and config[:vm_name]) %>
c.vm.define "<%= config[:vm_name] %>", primary: true do |vm_instance|
<% elsif set_hostname %>
c.vm.define "kt-<%= File.basename(config[:kitchen_root]) %>-<%= instance.name%>", primary: true do |vm_instance|
<% else # nothing %>
<% end %>
vm_instance.vm.box = "<%= config[:box] %>"

<% if config[:box_url] %>
c.vm.box_url = "<%= config[:box_url] %>"
vm_instance.vm.box_url = "<%= config[:box_url] %>"
<% end %>

<% if config[:box_version] %>
c.vm.box_version = "<%= config[:box_version] %>"
vm_instance.vm.box_version = "<%= config[:box_version] %>"
<% end %>

<% if config[:box_check_update] %>
c.vm.box_check_update = "<%= config[:box_check_update] %>"
vm_instance.vm.box_check_update = "<%= config[:box_check_update] %>"
<% end %>

<% if config[:vm_hostname] %>
c.vm.hostname = "<%= config[:vm_hostname] %>"
vm_instance.vm.hostname = "<%= config[:vm_hostname] %>"
<% end %>

<% if config[:communicator] %>
c.vm.communicator = "<%= config[:communicator] %>"
vm_instance.vm.communicator = "<%= config[:communicator] %>"
<% end %>

<% if config[:guest] %>
c.vm.guest = "<%= config[:guest] %>"
vm_instance.vm.guest = "<%= config[:guest] %>"
<% end %>

<% if config[:communicator] %>
<% if config[:username] %>
c.<%= config[:communicator] %>.username = "<%= config[:username] %>"
vm_instance.<%= config[:communicator] %>.username = "<%= config[:username] %>"
<% end %>
<% if config[:password] %>
c.<%= config[:communicator] %>.password = "<%= config[:password] %>"
vm_instance.<%= config[:communicator] %>.password = "<%= config[:password] %>"
<% end %>
<% else %>
<% if config[:username] %>
c.ssh.username = "<%= config[:username] %>"
vm_instance.ssh.username = "<%= config[:username] %>"
<% end %>
<% if config[:password] %>
c.ssh.password = "<%= config[:password] %>"
vm_instance.ssh.password = "<%= config[:password] %>"
<% end %>
<% end %>

<% if config[:ssh_key] %>
c.ssh.private_key_path = "<%= config[:ssh_key] %>"
vm_instance.ssh.private_key_path = "<%= config[:ssh_key] %>"
<% end %>
<% config[:ssh].each do |key, value| %>
c.ssh.<%= key %> = <%= value %>
vm_intance.ssh.<%= key %> = <%= value %>
<% end %>
<% if config[:winrm] %>
<% config[:winrm].each do |key, value| %>
c.winrm.<%= key %> = <%= value %>
vm_instance.winrm.<%= key %> = <%= value %>
<% end %>
<% end %>

<% Array(config[:network]).each do |opts| %>
c.vm.network(:<%= opts[0] %>, <%= opts[1..-1].join(", ") %>)
vm_instance.vm.network(:<%= opts[0] %>, <%= opts[1..-1].join(", ") %>)
<% end %>

c.vm.synced_folder ".", "/vagrant", disabled: true
vm_instance.vm.synced_folder ".", "/vagrant", disabled: true
<% config[:synced_folders].each do |source, destination, options| %>
c.vm.synced_folder "<%= source %>", "<%= destination %>", <%= options %>
vm_instance.vm.synced_folder "<%= source %>", "<%= destination %>", <%= options %>
<% end %>

c.vm.provider :<%= config[:provider] %> do |p|
vm_instance.vm.provider :<%= config[:provider] %> do |p|
<% case config[:provider]
when "virtualbox", /^vmware_/
if config[:gui] == true || config[:gui] == false %>
Expand Down Expand Up @@ -120,6 +132,9 @@ Vagrant.configure("2") do |c|
<% end %>
<% end %>
<% end %>
end
end

<% if set_hostname %>
end
<% end %>
end