Skip to content

Commit

Permalink
Add support for setting guestinfo variables
Browse files Browse the repository at this point in the history
This patch adds an `extra_config` section to the provider configuration which
can be set to a hash. This hash is used to populate the `extraConfig` section
of the `VirtualMachineConfigSpec` used to clone new VMs. The keys of the hash
must start with `guestinfo.` and the values are available to guest VMs with
VMware tools installed via the `vmtoolsd` command.
  • Loading branch information
Sharpie committed Feb 18, 2016
1 parent fe74596 commit bf84188
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ This provider has the following settings, all are required unless noted:
to the VM upon creation. This method takes a key/value pair,
e.g. `vsphere.custom_attribute('timestamp', Time.now.to_s)`, and may be called
multiple times to set different attributes.
* `extra_config` - _Optional_ A hash of extra configuration values to add to
the VM during creation. These are of the form `{'guestinfo.some.variable' => 'somevalue'}`,
where the key must start with `guestinfo.`. VMs with VWware Tools installed can
retrieve the value of these variables using the `vmtoolsd` command: `vmtoolsd --cmd 'info-get guestinfo.some.variable'`.

### Cloning from a VM rather than a template

Expand Down
9 changes: 9 additions & 0 deletions lib/vSphere/action/clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def call(env)
add_custom_cpu(spec, config.cpu_count) unless config.cpu_count.nil?
add_custom_cpu_reservation(spec, config.cpu_reservation) unless config.cpu_reservation.nil?
add_custom_mem_reservation(spec, config.mem_reservation) unless config.mem_reservation.nil?
add_custom_extra_config(spec, config.extra_config)

if !config.clone_from_vm && ds.is_a?(RbVmomi::VIM::StoragePod)

Expand Down Expand Up @@ -233,6 +234,14 @@ def add_custom_cpu_reservation(spec, cpu_reservation)
def add_custom_mem_reservation(spec, mem_reservation)
spec[:config][:memoryAllocation] = RbVmomi::VIM.ResourceAllocationInfo(reservation: mem_reservation)
end

def add_custom_extra_config(spec, extra_config = {})
return if extra_config.empty?

# extraConfig must be an array of hashes with `key` and `value`
# entries.
spec[:config][:extraConfig] = extra_config.map { |k, v| { 'key' => k, 'value' => v } }
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/vSphere/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class Config < Vagrant.plugin('2', :config)
attr_accessor :cpu_count
attr_accessor :cpu_reservation
attr_accessor :mem_reservation
attr_accessor :extra_config

attr_reader :custom_attributes

def initialize
@custom_attributes = {}
@extra_config = {}
end

def custom_attribute(key, value)
Expand Down

0 comments on commit bf84188

Please sign in to comment.